var init = 1;
var simi = 0;
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
	
var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveSearchReq = new XMLHttpRequest();
}
function liveSearchInit() {
	if (navigator.userAgent.indexOf("Safari") > 0) {
        document.getElementById('searchbox').addEventListener("keydown",liveSearchKeyPress,false);
	} else if (navigator.product == "Gecko") {
		document.getElementById('searchbox').addEventListener("keypress",liveSearchKeyPress,false);
		document.getElementById('searchbox').addEventListener("blur",liveSearchHideDelayed,false);
		
	} else {
	  document.getElementById('searchbox').attachEvent('onkeydown',liveSearchKeyPress);
       isIE = true;
	}

}

function liveSearchHideDelayed() {
	window.setTimeout("liveSearchHide()",400);
}
	
function liveSearchHide() {
	document.getElementById("LSResult").style.display = "none";
	var highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
}

function liveSearchKeyPress(event) {
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			highlight = document.getElementById("LSShadow").firstChild.firstChild;
		} else {
			highlight.removeAttribute("id");
            highlight = isIE? highlight.nextSibling: highlight.nextSibling.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
			var queryName = document.getElementById("LSHighlight").getAttribute("name");
			liveSearchLast = queryName;
			document.forms.searchform.query.value = queryName;
		 } 
		if (!isIE) {
			event.preventDefault(); 
			}

	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		 highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			highlight = document.getElementById("LSResult").firstChild.firstChild.lastChild;
		} 
		else {
			highlight.removeAttribute("id");
            highlight = isIE? highlight.previousSibling: highlight.previousSibling.previousSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
			var queryName = document.getElementById("LSHighlight").getAttribute("name");
            liveSearchLast = queryName;
			document.forms.searchform.query.value = queryName;
		}
		if (!isIE) { event.preventDefault(); }
	} 
	//ESC
	else if (event.keyCode == 27) {
		 highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("LSResult").style.display = "none";
	} 
simi = event.keyCode;
}
function liveSearchStart() {
   if (init) {
    liveSearchInit();
	init=0;
	}
  if (t) {
	window.clearTimeout(t);
	}
  t = window.setTimeout("liveSearchDoSearch()",200);
}
function liveSearchDoSearch() {
	if (typeof liveSearchRoot == "undefined") {
      liveSearchRoot = "";
	}
	if (typeof liveSearchRootSubDir == "undefined") {
      liveSearchRootSubDir = "";
	}
	if (typeof liveSearchParams == "undefined") {
     liveSearchParams = "";
	}
	if (simi == 0) {
		if (liveSearchReq && liveSearchReq.readyState < 4) {
			liveSearchReq.abort();
		}
		if ( document.forms.searchform.query.value == "") {
			liveSearchHide();
			return false;
		}
		if (window.XMLHttpRequest) {
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
	    var num=Math.random();
		liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
		liveSearchReq.open("GET", "http://www.s2buy.com/searchkeyword.cgi?query=" + document.forms.searchform.query.value +"&sdi="+num);
		liveSearchLast = document.forms.searchform.query.value;
	    liveSearchReq.send(null);
	
	}else {
  
		if ((simi != 38) && (simi != 40) && (simi != 37) && (simi != 39) ) {
				if (liveSearchReq && liveSearchReq.readyState < 4) {
					liveSearchReq.abort();
				}
				if ( document.forms.searchform.query.value == "") {
					liveSearchHide();
					return false;
				}
				if (window.XMLHttpRequest) {
				// branch for IE/Windows ActiveX version
				} else if (window.ActiveXObject) {
					liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
				}
			    var num=Math.random();
				liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
				liveSearchReq.open("GET", "http://www.s2buy.com/searchkeyword.cgi?query=" + document.forms.searchform.query.value +"&sdi="+num);
				liveSearchLast = document.forms.searchform.query.value;
			    liveSearchReq.send(null);
		}

	}
}

function liveSearchProcessReqChange() {
	
	if (liveSearchReq.readyState == 4) {
		 var  sha = liveSearchReq.responseText;
		if (sha == 0) {
        var  res = document.getElementById("LSResult");
		res.style.display = "none";
		var  sh = document.getElementById("LSShadow");
		
		sh.innerHTML = '';
	
	    }else{
	
	    var  res = document.getElementById("LSResult");
		res.style.display = "block";

		var  sh = document.getElementById("LSShadow");
		sh.innerHTML = liveSearchReq.responseText;
	   }
	
	}
}

function liveSearchSubmit(key) {
document.getElementById("searchbox").value = key;
var highlight = document.getElementById("LSHighlight");
if (highlight && highlight.firstChild) {
 if (document.searchform.catid) {
   var ss;

  for(var i=0;i<document.searchform.catid.length;i++){

	 if (document.searchform.catid[i].checked==true)
	  {
	  ss=document.searchform.catid[i].value;
	  } 
  }
	if (ss) {
		higghta =	"http://www.s2buy.com/search.cgi?query="+key+"&catid="+ss;

	}else{
		higghta =	"http://www.s2buy.com/search.cgi?query="+key;

	}
	
}else{
	higghta =	"http://www.s2buy.com/search.cgi?query="+key;
  }
document.searchform.submit();

return false;
}else {
     return true;
	}


}
function liveSearchHover(el) {
      highlight = document.getElementById("LSHighlight");
      if (highlight) {
                        highlight.removeAttribute("id");
                }
       el.setAttribute("id","LSHighlight");
		document.searchform.query.focus();
}

function liveSearchClicked(el,key) {
	 highlight = document.getElementById("LSHighlight");
     if (highlight) {
                        highlight.removeAttribute("id");
                }
      el.setAttribute("id","LSHighlight");
      return liveSearchSubmit(key);
}

