 var t; 
 var found_results;
 var search_keyword;
 var flag = 0;
 var HTML = "";
 var key;
 var firstItem = "";


  function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
  }

   
 
  function OnClientItemSelected(event) {
            ShowIcon();
            flag = 1; //Fix for FireFox
            var auto_list_selected = $find("AutoComplete")._selectIndex;
            if (auto_list_selected != -1) {
                $find("AutoComplete").get_element().value = $find("AutoComplete").get_completionList().childNodes[auto_list_selected]._value;
                search_keyword = $find("postAutoComplete").set_contextKey($find("AutoComplete").get_element().value);
                showAutoComplete();
            }         
            found_results = 1;
   }

        function postPopulated() {
             clearTimeout(t);
            var autoComplete = $find('postAutoComplete');
            var autoList = autoComplete.get_completionList();
            
            var contextkey = $find('AutoComplete').get_element().value;
       
	    HTML = "<br/><table><th colspan='2'>Search Results for " + contextkey + "</th>";

            for (i = 0; i < autoList.childNodes.length; i++) {
                var product = autoList.childNodes[i]._value;
                var text = autoList.childNodes[i].firstChild.nodeValue;
                if (product != "")
                {
                   HTML += "<tr><td class='name'>" + product + "</td><td class='moreinfo'><a href='/public/products/product-list.aspx#"+ text +"'>More Information</a></td></tr>";
                }
            }
            
            document.getElementById('results').innerHTML = HTML+"</table>";
            HTML = "";
            flag = 0;
        }
 
        function OnClientPopulated(sender, eventArgs) {
            
           clearTimeout(t);
           search_keyword = $find('AutoComplete').get_element().value.toUpperCase();
           var autoList = $find("AutoComplete").get_completionList();
          
           firstItem = autoList.childNodes[0].firstChild.nodeValue;

           if(firstItem == "connectionerror")
           {
                 autoList.childNodes[0].innerHTML = "";
                 document.getElementById('results').innerHTML = "<br><br><b>An error has occurred with connecting to the database, please contact an AgroNational representative to obtain product information.</b>";
           }
           else
           {
            
            for (i = 0; i < autoList.childNodes.length; i++) {
              
                var imageUrl = autoList.childNodes[i]._value;
                var text = autoList.childNodes[i].firstChild.nodeValue;

                autoList.childNodes[i]._value = text;
            
             search_keyword = search_keyword.replace(", ",","); search_keyword = search_keyword.replace(" ",","); 
             
             Temptext = search_keyword.split(",");  
            
             for(k=0;k<Temptext.length;k++){
                if(Temptext[k] != "")
                 text = text.replace(trim(Temptext[k]),"<b>"+trim(Temptext[k])+"</b>","gi");
             }
            
               if (imageUrl != "")
                 autoList.childNodes[i].innerHTML = "<img src=/public/images/" + imageUrl + ".gif /> " + text;
               else
                 autoList.childNodes[i].innerHTML = text;
              
            }
          found_results = 1;
          

          var value = document.getElementById('results').innerHTML;
          
          if(value.toLowerCase().indexOf("img") != -1 || value.toLowerCase().indexOf("find") != -1)
	        document.getElementById('results').innerHTML = "<!--EMPTY-->";
         }
       }

       function ShowIcon() {
            var e = document.getElementById("results");
            e.innerHTML = '<img src="/public/images/ajax-loader.gif" alt="Progress_Icon"  style="margin-top: 50px; margin-left: 180px;"/>';
            t =  setTimeout("NoResults()",60000);
        }

        function ShowSearchIcon() {
            var e = document.getElementById("processing");
            e.setAttribute("src", "/images/search_icon.gif");
            e.style.enabled = true;
            e.style.height = "22px";
        }

	function NoResults() {
            
	      var value;
              value = document.getElementById('results').innerHTML;
              if(value.toLowerCase().indexOf("img") == 1)
		    document.getElementById('results').innerHTML = "<br/><br/>Couldn't find what you are looking for, try checking your spelling or changing your terms";
                  
        }

        function message()
        {
              document.getElementById('results').innerHTML = "<b>You have to enter the keywords to search the products</b>"; 
        }

        function showAutoComplete() {
          
           if($find("AutoComplete").get_element().value != "")
           {
            ShowIcon();
            clearTimeout(t);
            var autoComplete = $find('postAutoComplete');
            
            //document.getElementById('results').innerHTML = "";

            autoComplete._textBoxHasFocus = true;

           Sys.Net.WebServiceProxy.invoke(autoComplete.get_servicePath(),

           autoComplete.get_serviceMethod(),

           false,

           { prefixText: '', key: autoComplete._completionSetCount, contextKey: autoComplete.get_contextKey() },

           Function.createDelegate(autoComplete, autoComplete._onMethodComplete),

           Function.createDelegate(autoComplete, autoComplete._onMethodFailed),

           '',

           60000);
          }
         else
           message();
        }

       function productList(){

        if(firstItem == "connectionerror")
        {
                 document.getElementById('results').innerHTML = "<br><br><b>An error has occurred with connecting to the database, please contact an AgroNational representative to obtain product information.</b>";
        }
        else
        {

		if($find("AutoComplete").get_element().value != "")
        	{

        	ShowIcon();
        	clearTimeout(t);
		var autoComplete = $find('postAutoComplete');

        	autoComplete._textBoxHasFocus = true;

       		var contextkey = $find('AutoComplete').get_element().value;

        	//document.getElementById('results').innerHTML = "";

        	var webRequest = Sys.Net.WebServiceProxy.invoke(autoComplete.get_servicePath(),

           	autoComplete.get_serviceMethod(),

           	false,

           	{ prefixText: '', key: key, contextKey: contextkey },

           	Function.createDelegate(autoComplete, autoComplete._onMethodComplete),

           	Function.createDelegate(autoComplete, autoComplete._onMethodFailed),

           	'',

           	60000);
		
            	key = -1;
         	}
         	else
           	message();
         }
       }
     

       function processing_onclick() {
           key = 1;
           productList();
       }

       function do_click(eventArgs) {

          if (window.event)
              key = window.event.keyCode;
          else
              key = eventArgs.which;

          if (key == 13 && flag != 1) {
              productList();
          }
          else
            key = -1;

      }