// ****************************************************************
// *   										*
// *   SEARCH.JS written by Nicholas Sterosky on 5/12/2005. This	*
// *	 file may be viewed for informational purposes, but may not	*
// *	 be altered or distributed for sale in any way without 	*
// *	 written consent. It is approved for use on the website	*
// *   "http://www.aapl.com" and other Aldrich-APL owned domains	*
// *	 only.									*
// * 											*
// ****************************************************************


function beginSearch() {
	
	if (document.form1.criteria.value.length < 2) alert('Please enter a phrase larger than one character.');  
	else oktoSearch(0,0,0);
	return(false);

	}

function generateHTML(i, productDatabase) {
	
	var newCode="";
	newCode="<table id='tableFormat' width=540><tr><td width=70>Product:</td><td width=200><u>" + productDatabase[i].name + "</u></td><td width=70>CAS #:</td><td width=200>" + productDatabase[i].cas + "</td></tr><tr><td width=70>Purity:</td><td width=200> " + productDatabase[i].purity + "</td><td width=70>Form:</td><td width=200>" + productDatabase[i].form + "</td></tr><tr><td width=70>AAPL #:</td><td width=200>" + productDatabase[i].aapl + "</td><td width=70>Formula:</td><td width=200>" + productDatabase[i].formula +  "</td></tr></table><hr>";
	return(newCode);
	
	}

function oktoSearch(fromHome,c,d) {

	var resultswindow=null;
	var productDatabase = initialize();
	var i=0;
	var z=0;
	var results=false;
	var finalresults= "";
	var a="";
	var b="";
	var aLen=0;
	var bLen=0;
	var count=0;
	var searchFor;
	var searchBy;
	
	//SEE IF SEARCH ORIGINATED FROM HOME PAGE OR SEARCH PAGE
	
	if (fromHome == 1) {
		searchFor= c;
		if (d=='n') searchBy='name';	
		if (d=='f') searchBy='formula';	
		if (d=='c') searchBy='cas';	
		if (d=='a') searchBy='aapl';	
	}
	else  {
		searchFor = document.form1.criteria.value;
		if (document.form1.type[0].checked == true) searchBy="name";
		if (document.form1.type[1].checked == true) searchBy="formula";
		if (document.form1.type[2].checked == true) searchBy="aapl";
		if (document.form1.type[3].checked == true) searchBy="cas";
	}

	//OPEN A NEW WINDOW   
	resultswindow= window.open("", "results", "top=200,left=200,height=400,width=650,scrollbars=1,resizable=0,");  

	//   **** SEARCH BY FORMULA ****
	if (searchBy == "formula") {  
		//CHECK THROUGH ARRAY FOR NAMES THAT MATCH
	   	while (i != productDatabase.length) {
 	      	if (productDatabase[i].formula == searchFor )  {  
      			finalresults += generateHTML(i, productDatabase);
		      	//SIGNIFY THAT RESULTS WERE FOUND	
		      	results=true; 
				count++;
        			}
    			i++;
    		}
  	}

	//   **** SEARCH BY NAME ****
	if (searchBy == "name") {  
	   	//CHECK THROUGH ARRAY FOR NAMES THAT MATCH
   		while (i != productDatabase.length) {
    			stop=0;
    			z=0;
    			a=searchFor.toLowerCase();
    			aLen= a.length;    
			b= productDatabase[i].name.toLowerCase();
			bLen = b.length;
      	      while (z != bLen) {
            		if ( b.substring(z,z+aLen) == a )  {
                   		if (stop!=1) finalresults += generateHTML(i, productDatabase);
           	   			results=true;
					stop=1; 
					count++;
                  	}
          			z++;
        		}
	    		i++ 
    		}
 	}

	//   **** SEARCH BY APL# ****
	if (searchBy == "aapl") {  
		//CHECK THROUGH ARRAY FOR NAMES THAT MATCH
		while (i != productDatabase.length) {
       		if (productDatabase[i].aapl == searchFor.toUpperCase() )  {  
        			finalresults += generateHTML(i, productDatabase);
  			      results=true; 
				count++;
        		}
    			i++;
    		}
	}

	//   **** SEARCH BY CAS# ****
	if (searchBy == "cas") {  
		//CHECK THROUGH ARRAY FOR NAMES THAT MATCH
		while (i != productDatabase.length) {
 			if (productDatabase[i].cas == searchFor )  {  
			      finalresults += generateHTML(i, productDatabase);
			      results=true; 
				count++;
        		}
    			i++;
    		}
  	}

	//IF NO RESULTS WERE FOUND GIVE A DEFAULT MESSAGE:
	if (results==false) finalresults=  "<b>Your search did not return any results.<hr></b>";

	//ADD A CLOSE BUTTON
	finalresults += "<br><input type='button' value='Close This Window' onClick='window.close();'>";

	//DISPLAY THE FINAL RESULTS
	resultswindow.document.write("<html><title>AAPL Search Results</title><head><style type='text/css'>#tableFormat { font-family: sans-serif, sans-serif; font-size:9pt;}</style></head><BODY BACKGROUND='pics/searchback.JPG'><center><font color='purple' size=5><u>SEARCH RESULTS</u></font></center><b><h3>" + count + " results returned.</h3><hr>" + finalresults + "</body></html>");

	return(false);

}











