// This function trims a given string of leading and trailing whitespace.
function trim(str) 
    {
    str = str.toString();
    var start = 0;
    while ((start < str.length) && ( (str.charAt(start) == ' ') || (str.charAt(start) == '\n')|| (str.charAt(start) == '\r') || (escape(str.charAt(start))=='%A0') || ( str.charAt(start) == escape('&nbsp;') ) ) ) ++start;
    var end = str.length;
    while ((end > 0) && ( (str.charAt(end-1) == ' ') || (str.charAt(end-1) == '\n') || (str.charAt(end-1) == '\r') || (escape(str.charAt(end-1))=='%A0') || (str.charAt(start) == escape('&nbsp;')) ) ) --end;
    if (start>end) {return "";}
    else {return str.substring(start, end);}
}

//Search Results page should call this function
function doSearch() 
    {
    doHeader(); 
    doHeader3(); 
    
    var fullEntry = getSearchTypeValueFromQueryString();
    generateSearchResults(fullEntry[0] , fullEntry[1]);
    }

//Function to return the value of a QueryString in the URL
// Hard coded for Meanwell
function getSearchTypeValueFromQueryString(Val) 
    {
    thisURLparamStr = document.location.search;
//alert("thisURLparamStr " + thisURLparamStr);
    //chop "?" off thisURLparamStr
    if (thisURLparamStr.charAt(0) == "?") thisURLparamStr = thisURLparamStr.substring(1, thisURLparamStr.length);
    returnStr = "";
    returnType = "EX";   //default to EXACT search
    if (thisURLparamStr != "") 
        {
        //Build array out of thisURLparamStr using "&" as delimiter
        divide1 = (thisURLparamStr.split("&"))
        for (i=0; i < divide1.length; i++)
			{
            divide2 = divide1[i].split("=")
            returnStr = unescape(divide2[1]);
//alert(divide2[0] + " = " + returnStr);
            if (unescape(divide2[0]) == "FastFind1")
				{
				for (i = 0; i < document.FastFindForm1.FastFind1.length; i++)
					{
					if (document.FastFindForm1.FastFind1.options[i].value == returnStr)
						document.FastFindForm1.FastFind1.selectedIndex = i;
					}
				returnType = "CA";					// catagory type
				}
            if (unescape(divide2[0]) == "FastFind2")
				{
				for (i = 0; i < document.FastFindForm2.FastFind2.length; i++)
					{
					if (document.FastFindForm2.FastFind2.options[i].value == returnStr)
						document.FastFindForm2.FastFind2.selectedIndex = i;
					}
				returnType = "CA";					// catagory type
				}
            if (unescape(divide2[0]) == "FreeFind")
				{
				document.FreeFindForm.FreeFind.value = returnStr;
				returnType = "FF";					// free form text search
				}
            }
        }
    return  (new Array(returnType , returnStr +"") );
    }

function generateSearchResults(type, str)
    {
    var sxvar = str.toLowerCase();
    var total=0;
    var lastType = 0;
    var certname = ""
    
        for (i = 0; i < item.length; i++) 
            {
            if (searchLogic( type, sxvar, item[i]) )
                    {
                    if(total == 0) 
                        {
                        lastType = item[i][2];
                        document.write(matchHead);
                        document.write(sepLine2 + catagory[lastType]);
                        document.write(sepLine3);                        
                        document.write(tableHead); 
                //      document.write(r1a + "ID" + r2a + "Description" + r3a + "Wattage" +r4a + "Form" + r5a + "Certificate" + r6a + "Test data" + r7a + "Report data" + r8a); 
                        }
                    if (lastType != item[i][2])
                        {
                        lastType =  item[i][2];
                        document.write(sepLine1);
                        document.write(sepLine2 + catagory[lastType]);
                        document.write(sepLine3);
                        document.write(tableHead); 
                    } 
                    
                    certname = "";      
                    for (j = 0; j < certs.length; j++)  
                        {    
                            if(item[i][6+j] == 1)
                                {
                                if(certname == "")    certname = certs[j][1] + item[i][0] + certs[j][2] ;
                                else                  certname = certname + ", " + certs[j][1] + item[i][0] + certs[j][2] ;           
                                }  
                           if(item[i][6+j] == 2)
                                {
                                if(certname == "")    certname = certsDefault[j][1]  ;
                                else                  certname = certname + ", " + certsDefault[j][1] ;           
                                }                                  
                                 
                         }      
                         
                    if(certname == "")  certname = "&nbsp;"                             
                       
                       
                    if ( (total % 2) == 0)          document.write(r1a + "c_" + item[i][23] + ".pdf\">" + item[i][0] + r2a + item[i][4] + r3a + item[i][3] + r4a + item[i][25] + r5a + item[i][5] + r6a + certname + r7a + item[i][23] + ".html\">" + r8a + item[i][23] + ".pdf\">" + r9a);
                    else                            document.write(r1b + "c_" + item[i][23] + ".pdf\">" + item[i][0] + r2b + item[i][4] + r3b + item[i][3] + r4b + item[i][25] + r5b + item[i][5] + r6b + certname + r7b + item[i][23] + ".html\">" + r8b + item[i][23] + ".pdf\">" + r9b);
                    total++;
                    }                   
            }   
        if ( total == 0)
            {
            document.write(nomatch);
            }
    document.write(endHTML);
    document.close();           
}   


//Normalize functions
function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}


function normalizeToAlphaNumeric (s)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (isLetterOrDigit(c)) returnString += c;
    }
    return returnString;
}




function searchLogic( type, search, index)
    {
    if (type == "FF")           // free form
        {
        if (search == "all" || index[1].toLowerCase().indexOf(normalizeToAlphaNumeric(search))> -1 || index[24].toLowerCase().indexOf(normalizeToAlphaNumeric(search)) > -1 && search != "") return true;
       }
    else if (type == "CA")      // catagory search
       {
       if (search == "0" || (index[2] == search  && search != "")) return true;
       }
    else                        // assume exact
       {
        if (search == "all" || (index[0].toLowerCase() == search  && search != "")) return true;
       }
     return false;  
    
    }

       




