var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var liveSearchRoot = "/cgi-bin/"
var platform = 0;
var form_id = ""; 
var tr_index = 0;
var w = 0;
	
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('livesearch').addEventListener("keydown",liveSearchKeyPress,false);
		document.getElementById('livesearch').addEventListener("blur",liveSearchHide,false);
	} else if (navigator.product == "Gecko") {
		
		document.getElementById('livesearch').addEventListener("keypress",liveSearchKeyPress,false);
                //document.getElementById('livesearch').addEventListener("blur",liveSearchHideDelayed,false);
		
	} else {
		document.getElementById('livesearch').attachEvent('onkeydown',liveSearchKeyPress);
        //document.getElementById('livesearch').attachEvent("onblur",liveSearchHide,false);
		isIE = true;
	}
	
	document.getElementById('livesearch').setAttribute("autocomplete","off");

}

function liveSearchHideDelayed(t) {
	window.setTimeout("liveSearchHide()",10);
	if( t == 1 ) 
		document.getElementById("LSResult"+form_id).style.display = "none";
}

	
function liveSearchHide() {
	if ( jQuery.trim(document.getElementById("livesearch"+form_id).value).length < 3)// || (jQuery.trim(document.getElementById("livesearch"+form_id).value) != "" && jQuery.trim(document.getElementById("livesearch"+form_id).value).length < 3))
	{	
	    document.getElementById("LSResult"+form_id).style.display = "none";
	}
	var highlight = document.getElementById("LSHighlight"+form_id);
	if (highlight) {
		highlight.removeAttribute("id");
	}
	jQuery("#hideSelect").css("display", "");
	jQuery("#hideSelect2").css("display", "");
	jQuery("#LSShadow").css("height", "0");
}


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 = highlight.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
		} 
		if (!isIE) { event.preventDefault(); }
		if(document.getElementById("qn"+id))
	       document.getElementById("qn"+id).value = "";
	} 
	//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 = highlight.previousSibling;
		}
		if (highlight) {
				highlight.setAttribute("id","LSHighlight");
		}
		if (!isIE) { event.preventDefault(); }
		if(document.getElementById("qn"+id))
	       document.getElementById("qn"+id).value = "";
	} 
	//ESC
	else if (event.keyCode == 27) {
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("LSResult").style.display = "none";
		if(document.getElementById("qn"+id))
	       document.getElementById("qn"+id).value = "";
	} 
	//BACKSPACE - required for IE
	else if (event.keyCode == 8 && isIE) {
	    if(document.getElementById("qn"+id))
	       document.getElementById("qn"+id).value = "";
		liveSearchStart(0,'');
	}
	
	//RETURN
	else if (event.keyCode == 13 ) {
	    if(document.getElementById("qn"+id))
	       document.getElementById("qn"+id).value = "";
		return liveSearchSubmit();
	}
	
	
}
function liveSearchStart(all, id) {
	if (t) {
		window.clearTimeout(t);
	}
	
	if(document.getElementById("qn"+id))
	{   var title = document.getElementById("qn"+id).value;
	    jQuery("#hintbox").hide();
	}
    else
        var title = "";

	t = window.setTimeout("liveSearchDoSearch("+all+",\""+title+"\")",1000);
	//liveSearchDoSearch(all, '');
}

function liveSearchDoSearch(all, title) {

  var a = jQuery.trim(document.forms.searchform.s.value);
  if (liveSearchLast != jQuery.trim(document.forms.searchform.s.value) ) {
	if (liveSearchReq && liveSearchReq.readyState < 4) {
		liveSearchReq.abort();
	}
	
    if(title != "")
        document.forms.searchform.s.value = jQuery.trim(title);
    
	if (typeof liveSearchRoot == "undefined") {
		liveSearchRoot = "";
	}
	if (typeof liveSearchRootSubDir == "undefined") {
		liveSearchRootSubDir = "";
	}
	if (typeof liveSearchParams == "undefined") {
		liveSearchParams2 = "";
	} else {
		liveSearchParams2 = "&" + liveSearchParams;
	}
	if ((jQuery.trim(document.forms.searchform.s.value) == "")||(jQuery.trim(document.forms.searchform.s.value).length <=2)) {
		liveSearchHide();
		liveSearchLast = "";
		w = 0;
		return false;
	}
	
	if (a.length >=3)
	{
	    Images = new Image(220,19);
        Images.src = "http://www.rom-search.com/images/ajax-loader.gif";
	    var  res = document.getElementById("LSResult");
        res.style.display = "block";
	    document.getElementById("LSShadow").innerHTML = '<div align=center style="width:auto; height:40px;border:1px solid #A5ACB2;background-color:#FFFFFF; padding-top:10px"><img src="'+Images.src+'" width="220" height="19" border="0"></div>';
        jQuery("#hideSelect").css("display", "none");
		//jQuery("#hideSelect2").css("display", "none");	    
	    w++;
	}
	/*
	if(document.getElementById("year_from"))
	   var year_from = document.getElementById("year_from").value;
  else
      var year_from = 0;
	   
  if(document.getElementById("year_limit"))
	   var year_limit = document.getElementById("year_limit").value;
   else
      var year_limit = 0;
	   
   if(document.getElementById("is_rev"))
	     var is_rev = document.getElementById("is_rev").value;
   else
      var is_rev = 0;
	     
   if(document.getElementById("genre_id"))
	     var genre_id = document.getElementById("genre_id").value;
   else
      var genre_id = 0;
	*/	
	
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (isIE=="false"){
	liveSearchReq.onreadystatechange = liveSearchProcessReqChange;
	url=liveSearchRoot + "live_search.php?s=" + escape(document.forms.searchform.s.value) + liveSearchParams2 + "&pid1=" + platform + "&all=" + all;// + "&genre_id=" + genre_id + "&year_limit=" + year_limit + "&year_from=" + year_from + "&is_rev=" + is_rev ;
	liveSearchReq.open("GET",url,true);
	//liveSearchReq.setRequestHeader("If-Modified-Since", "Wed,26 Dec 2007 00:00:00 GMT");
	liveSearchLast = document.forms.searchform.s.value;}
	else{
	url=liveSearchRoot + "live_search.php?s=" + escape(document.forms.searchform.s.value) + liveSearchParams2 + "&pid1=" + platform  + "&all=" + all;// + "&genre_id=" + genre_id + "&year_limit=" + year_limit + "&year_from=" + year_from + "&is_rev=" + is_rev ;
	liveSearchReq.open("GET",url,true);
	liveSearchReq.onreadystatechange = liveSearchProcessReqChange;
	//liveSearchReq.setRequestHeader("If-Modified-Since", "Wed,26 Dec 2007 00:00:00 GMT");
	liveSearchLast = document.forms.searchform.s.value;	
	}
	if (isIE=="true"){
	liveSearchReq.send();}
	else{	
	liveSearchReq.send(null);}
	
	tr_index = 0;
	}
}

function liveSearchProcessReqChange() {
	
	if ((liveSearchReq.readyState == 4)||(liveSearchReq.readyState == "complited")) {
		var  res = document.getElementById("LSResult");
		res.style.display = "block";
		var  sh = document.getElementById("LSShadow");
		var response = liveSearchReq.responseText;
		sh.innerHTML = response;
		jQuery("#hideSelect").css("display", "none");
		//jQuery("#hideSelect2").css("display", "none");
		jQuery("#LSShadow").css("height", "100%");
	}
}

function liveSearchSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight && highlight.firstChild) {
		window.location = highlight.firstChild.nextSibling.getAttribute("href");
		return false;
	} 
	else {
		return true;
	}
}

function selectGamePlatform(pid)
{
    platform = pid;
    
    if(document.getElementById("qn"+id))
	       document.getElementById("qn"+id).value = "";

    return platform;   
}

function setNewQ(title)
{
    document.forms.searchform.s.value = title;    
}

