if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=whichElement;

var last_element="";

function whichElement(e)
{
	var target = (e && e.target) || (event && event.srcElement);
	var obj = document.getElementById('div_product_suggest');
	if(target!=obj && target.id!="search_box" && !target.id.match('td_search') && !target.id.match('a_search')){obj.style.display='none'}
}

function ajax_get_suggestions(searchstr,divid)
{
   if (!searchstr.search(/^[\s]*$/gi))
   {
      hide_suggestions_popup(divid);
      return;
   }

   t=new Date().getTime();

   var link_code = "ajax_search_suggestions.php?searchstr="+searchstr+"&bustcache="+t;

   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)
      {
         clearTimeout(xmlHttpTimeout);
	     document.getElementById(divid).innerHTML=xmlHttp.responseText;
    	 document.getElementById(divid).style.display='';
      }
    }
    link_code=location.protocol + "//" +location.hostname+xcart_web_dir+"/"+link_code;

    xmlHttp.open('GET', link_code, true);
    xmlHttp.send(null);

	var xmlHttpTimeout=setTimeout("ajaxTimeout();",30000);
}

function ajaxTimeout(calltype){

   if (xmlHttp)
	   xmlHttp.abort();
}
function hide_suggestions_popup(divid)
{
      document.getElementById(divid).innerHTML="";
      document.getElementById(divid).style.display='none';
}
function show_suggestions_popup(divid)
{
      if (document.getElementById(divid).innerHTML!="")
	      document.getElementById(divid).style.display='';
}
function checkInput(input,kcode,divid) {
  switch (kcode) {
    case 16: // shift
    case 17: // ctrl
    case 18: // alt
    case 20: // caps lock
    case 33: // page up
    case 34: // page down
    case 35: // end
    case 36: // home
    case 37: // left arrow
    case 38: // up arrow
    case 39: // right arrow
    case 40: // down arrow
     if (document.getElementById('td_search_1'))
     {
	      document.getElementById('a_search_1').focus();
    	  document.getElementById('td_search_1').className="onstyle";
     }
     return true;

    case 9:  // tab
    case 13: // enter
    case 27: // esc
      hide_suggestions_popup(divid);
      return true;

    default: // all other keys
      if (input.length > 0)
        ajax_get_suggestions(input,divid);
      else
        hide_suggestions_popup(divid);
      return true;
  }
}
function showHideDiv(divid)
{
//      if (last_element!="div_product_suggest")
	      document.getElementById(divid).style.display='none';
}
function changeLocation(pid)
{
	window.location.href='product.php?productid='+pid;
}
function scrollDiv(id,kcode) {
  switch (kcode) {
    case 38: // up arrow
      var previndex=parseInt(id)-1;
      var obj=document.getElementById('a_search_'+previndex);
      if (obj)
      {
	     document.getElementById('td_search_'+id).className="offstyle";
         document.getElementById('td_search_'+previndex).className="onstyle";
         obj.focus();
      }
      return true;
    case 40: // down arrow
      var nextindex=parseInt(id)+1;
      var obj=document.getElementById('a_search_'+nextindex);
      if (obj)
      {
         document.getElementById('td_search_'+id).className="offstyle";
         document.getElementById('td_search_'+nextindex).className="onstyle";
         obj.focus();
      }
      return true;

    case 13: // enter
    default: // all other keys
  }
}