// JavaScript Document

  var req;
  var latestServerQuery = "";
  var THROTTLE_PERIOD = 2000;

  function showHideDiv(divID,action)
  {
   var divs = document.getElementsByTagName('div');
   for(i=0;i<divs.length;i++)
   {
    if(divs[i].id.match(divID))
    {
     if ( (divs[i].style.display == 'block' || action == 'hide' || divs[i].style.display == '') && action != 'show' )
     {
      divs[i].style.display = 'none';
     } else {
      divs[i].style.display = 'block';
     }
    }
   }
  }

  function Initialize(){
    try {
      req=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e){
      try{
        req=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(oc){
        req=null;
      }
    }
    if(!req && typeof XMLHttpRequest!="undefined") {
      req = new XMLHttpRequest();
    }
  }
  
  function requestValidCategoriesLoop(url) {
	 var searchBox = document.getElementById("InstantSearch");
	 
	  if( searchBox.value.length < 3 )
	  {
	      showHideDiv("xmlSearchResults", "hide");
	      setTimeout('requestValidCategoriesLoop("' + url + '");', THROTTLE_PERIOD);
		  return;
	  }
	  
     if (searchBox.value != latestServerQuery &&
		 searchBox.value != "InstantSearch") {
       SendQuery(searchBox.value, url);				 
       latestServerQuery = searchBox.value;
     }
     setTimeout('requestValidCategoriesLoop("' + url + '");', THROTTLE_PERIOD);
  }

  function SendQuery(key, url){
      Initialize();
     var url = url+"?terms="+key;
  globalKey = key;
      if(req!=null){
        req.onreadystatechange = Process;
        req.open("GET", url, true);
        req.send(null);
      }
    }

  function Process(){
      if (globalKey.length >= 4)
      {
            showHideDiv("xmlSearchResults", "show");
            document.getElementById("xmlSearchResults").innerHTML = '<div class="xmlSearching"><img align="absmiddle" src="images/arrows_circle.gif"> Searching.....<br /></div>';
      }
    if (req.readyState == 4){
      if (req.status == 200){
        if(req.responseText==""){
          showHideDiv("xmlSearchResults", "hide");
        }else{
          showHideDiv("xmlSearchResults", "show");
          document.getElementById("xmlSearchResults").innerHTML = req.responseText;
        }
      }else{
        document.getElementById("xmlSearchResults").innerHTML = "There was a problem retrieving data:<br>"+req.statusText;
      }
    }
  }

  function xmlSearch(){
    showHideDiv("xmlSearchResults", "hide");
  }
  
  function wopen(url, name, w, h)
  {
  // Fudge factors for window decoration space.
   // In my tests these work well on all platforms & browsers.
  w += 32;
  h += 96;
   var win = window.open(url,
    name,
    'width=' + w + ', height=' + h + ', ' +
    'location=no, menubar=no, ' +
    'status=no, toolbar=no, scrollbars=no, resizable=no');
   win.resizeTo(w, h);
   win.focus();
  }