var matches = -1;
var start_index = 0;

function find_chem(chem_name)
{

// local matches array
	var matches = new Array();
	
	for (var i=1;i<chemicals.length;i++)
		if (chemicals[i][0].toUpperCase().indexOf(chem_name.toUpperCase())!=-1)
			matches[matches.length] = chemicals[i];

	if (matches.length==0)
		matches=-1;

// returns -1 for no matches or matches array
	return(matches);
}


function do_lookup()
{

// find matches
	start_index=0;
	document.chem_lookup.next_button.disabled=true;
	document.chem_lookup.prev_button.disabled=true;

	matches = find_chem(document.chem_lookup.chem_name.value);

// only do if matches found
	if (matches!=-1)
	{ 
		disp_matches();
		if (matches.length>document.chem_lookup.matched.options.length)
			document.chem_lookup.next_button.disabled=false;
	}

// if no matches
	else
	{
		document.chem_lookup.matched.options[0].text = "No Matches Found";
		for (var i=1;i<document.chem_lookup.matched.options.length;i++)
			document.chem_lookup.matched.options[i].text = "";
	}

	document.chem_lookup.matched.selectedIndex=-1;
	document.chem_lookup.mat_1.value="";
	document.chem_lookup.mat_2.value="";
	document.chem_lookup.mat_0.value="";
	alert('Found '+(matches!=-1?matches.length:'0')+' match'+(matches!=-1?(matches.length>1?'es':''):'es')+'.');

		
}

function disp_materials()
{
	var cur_match = start_index + document.chem_lookup.matched.selectedIndex;

	if (matches!=-1)
	{
		if (cur_match >= matches.length)
		{
			document.chem_lookup.matched.selectedIndex = -1;
			document.chem_lookup.mat_1.value="";
			document.chem_lookup.mat_2.value="";
			document.chem_lookup.mat_0.value="";
	
			return;
		}

		var mat1="";
		var mat2="";
		var mat0="";

		for (var i=1;i<matches[cur_match].length;i++)
			switch(matches[cur_match][i])
			{
			case "1":
				mat1=mat1+chemicals[0][i]+"\n";
				break;
			case "2":
				mat2=mat2+chemicals[0][i]+"\n";
				break;
			case "0":
				mat0=mat0+chemicals[0][i]+"\n";
				break;
			}
		document.chem_lookup.mat_1.value=mat1;
		document.chem_lookup.mat_2.value=mat2;
		document.chem_lookup.mat_0.value=mat0;

		return;
	}
	else
	{
		document.chem_lookup.matched.selectedIndex = -1;
		document.chem_lookup.mat_1.value="";
		document.chem_lookup.mat_2.value="";
		document.chem_lookup.mat_0.value="";
	
		return;
	}
}

function disp_matches()
{
	for (var i=start_index;i<(start_index+document.chem_lookup.matched.options.length);i++)
		if (i<matches.length)
			document.chem_lookup.matched.options[i-start_index].text = matches[i][0];
		else
			document.chem_lookup.matched.options[i-start_index].text = "";
}
	
function show_next()
{

	if (document.chem_lookup.next_button.disabled!=true)
	{
		start_index = start_index + document.chem_lookup.matched.options.length;
		document.chem_lookup.prev_button.disabled=false;
		if ((start_index + document.chem_lookup.matched.options.length)>matches.length)
			document.chem_lookup.next_button.disabled=true;
		disp_matches()
		document.chem_lookup.matched.selectedIndex=-1;
		document.chem_lookup.mat_1.value="";
		document.chem_lookup.mat_2.value="";
		document.chem_lookup.mat_0.value="";
	}
}

function show_prev()
{
	if (document.chem_lookup.prev_button.disabled!=true)
	{
		start_index = start_index - document.chem_lookup.matched.options.length;
		document.chem_lookup.next_button.disabled=false;
		if ((start_index - document.chem_lookup.matched.options.length)<0)
			document.chem_lookup.prev_button.disabled=true;
		disp_matches()
		document.chem_lookup.matched.selectedIndex=-1;
		document.chem_lookup.mat_1.value="";
		document.chem_lookup.mat_2.value="";
		document.chem_lookup.mat_0.value="";
	}
}
	

	





















































































