var iResultIndex = 0;
var oSearchDiv, oSearchQuery;
var sSearchString = "";
var sLastSearch = "";

function swapIntroText(oItem, sState) {
   if (sState == "focus") {
      //focus
      if (oItem.value == "Search the Site") {
         oItem.value = "";
      }
   }else{
      //blur
      if (oItem.value == "") {
         oItem.value = "Search the Site";
      }
   }
}

function toggleSearchDiv() {
   //Initialize.
   //return false;
   oSearchDiv = document.getElementById("ajaxSearchBox");
   oSearchQuery = document.getElementById("SearchQuery");
   
   oSearchDiv.style.width = (oSearchQuery.clientWidth * 1.5) + "px";
   //oSearchDiv.style.height = "18px";
   oSearchDiv.style.position = "fixed";
   oSearchDiv.style.top = (findObjPosY(oSearchQuery) + oSearchQuery.clientHeight + 0) + "px";
   oSearchDiv.style.left = findObjPosX(oSearchQuery) + "px";
   
   if (oSearchDiv.style.visibility == "visible") {
      oSearchDiv.style.visibility = "hidden";
   }else{
      oSearchDiv.style.visibility = "visible";
      oSearchQuery.onkeyup = navigateSearchDiv;
   }
}

function navigateSearchDiv() {
   var iKeyID = (window.event) ? event.keyCode : e.keyCode;
   
   if (iResultIndex > 0) {
      document.getElementById("ajaxSearchResult" + (iResultIndex)).style.background = "#FFF";
   }
   
   switch(iKeyID) {
      case 38:
         //alert("Up Key");
         if (iResultIndex > 0) {
            iResultIndex -= 1;
         }
         
         break;
      
      case 40:
         //alert("Down Key");
         if (!(iResultIndex >= parseInt(document.getElementById("ajaxNumResults").innerText))) {
            iResultIndex += 1;
         }
         
         break;
      
      default:
         sSearchString = trim(oSearchQuery.value)
         
         if (sSearchString != sLastSearch) {
            if (sSearchString.length >= 3) {
               oSearchDiv.innerHTML = "<div class=\"searchBoxGTxt\">Searching...</div>";
               executeAjaxSearch();
            }else {
               oSearchDiv.innerHTML = "";
            }
         }
         
         break;
   }
   
   if (document.getElementById("ajaxSearchResult" + iResultIndex)) {
      document.getElementById("ajaxSearchResult" + iResultIndex).style.background = "#CCC";
      oSearchQuery.value = document.getElementById("ajaxSearchResultSKU" + iResultIndex).innerText;
   }
}

function executeAjaxSearch() {
   if ((sSearchString.length >= 3) && (sSearchString != sLastSearch)) {
      populateSearchDiv(sSearchString);
      sLastSearch = sSearchString;
   }
}

function populateSearchDiv(sSearchQuery) {
   try {    
      // Firefox, Opera 8.0+, Safari    
      xmlHttp=new XMLHttpRequest();    
   } catch (e) {    // Internet Explorer    
      try {      
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
      } catch (e) {      
         try {        
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
         } catch (e) {        
            alert("Your browser does not support AJAX!");        
            return false;        
         }      
      }    
   }
   
   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
         sResponse = xmlHttp.responseText;
         
         if (sResponse == "") {
            oSearchDiv.innerHTML = "<div class=\"searchBoxGTxt\">Search query not found!</div>"
         }else{
            oSearchDiv.innerHTML = sResponse;
         }
      }
   }
   
   //Post data.
   xmlHttp.open("GET", "/_system/_advancedstore/@scripts/ajaxSearch.asp?searchQuery=" + escape(sSearchQuery), true);
   xmlHttp.send(null);
}