
var init = 1;
var simi = 0;
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var boxResult="LSResult";
var boxShaddow="LSShadow";
var boxHighlight="LSHighlight";

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(boxResult).style.display = "none";
	var highlight = document.getElementById(boxHighlight);
	if (highlight) {
		highlight.removeAttribute("id");
	}
}

function liveSearchKeyPress(event) {

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

	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		 highlight = document.getElementById(boxHighlight);
		if (!highlight) {
			highlight = document.getElementById(boxResult).firstChild.firstChild.lastChild;
		} 
		else {
			highlight.removeAttribute("id");
            highlight = isIE? highlight.previousSibling: highlight.previousSibling.previousSibling;
		}
		if (highlight) {
			highlight.setAttribute("id",boxHighlight);
			var queryName = document.getElementById(boxHighlight).getAttribute("name");
            liveSearchLast = queryName;
			document.forms.searchfrm.query.value = queryName;
		}
		if (!isIE) { event.preventDefault(); }
	} 
	//ESC
	else if (event.keyCode == 27) {
		 highlight = document.getElementById(boxHighlight);
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById(boxResult).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(document.forms.searchfrm.country.value !=""){
//		liveSearchParams="&country="+document.forms.searchfrm.country.value;
//	}
	if (simi == 0 ||((simi != 38) && (simi != 40) && (simi != 37) && (simi != 39) )) {
		if (liveSearchReq && liveSearchReq.readyState < 4) {
			liveSearchReq.abort();
		}
		var query = document.forms.searchfrm.searchbox.value;
		

		if ( query == "" ) {
			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;
		//var url = "http://www.travelindex.com/searchkeyword.cgi?query=" + escape(document.forms.searchfrm.query.value) +"&sdi="+num+liveSearchParams;
		var url = "searchkeyword.php?city=" + escape(query) +"&sdi="+num+liveSearchParams;
		
		

		liveSearchReq.open("GET", url);

		liveSearchLast = query;
	    liveSearchReq.send(null);
	
	}
}

function liveSearchProcessReqChange() {
	
	if (liveSearchReq.readyState == 4) {
		 var  sha = liveSearchReq.responseText;
		 //alert('result:'+sha);
		if (sha == 0) {
        var  res = document.getElementById(boxResult);
		res.style.display = "none";
		var  sh = document.getElementById(boxShaddow);
		
		sh.innerHTML = '';
	
	    }else{

			var xmlDoc=liveSearchReq.responseXML                                                                    // "xmlDoc" the returned xml object
			
            var Xml2String                                                                                             // Convert the xml to string just to display it 

            if (xmlDoc.xml) {
                Xml2String=xmlDoc.xml                                                                            // Converts the xml object to string  (  For IE)
            }else{
                Xml2String= liveSearchReq.responseText;///new XMLSerializer().serializeToString(xmlDoc);                        // Converts the xml object to string (For rest browsers, mozilla, etc)
            }

	
			var  res = document.getElementById(boxResult);
			res.style.display = "block";

			var  sh = document.getElementById(boxShaddow);
			//sh.innerHTML = liveSearchReq.responseText;
			sh.innerHTML = Xml2String;
	   }
	
	}
}

function liveSearchSubmit(key,country,state,cname) {
var sbox = document.getElementById("searchbox");
sbox.value = key;
if(sbox.value != "") sbox.value+=", ";
if(state != ""){
	sbox.value+=state;
	sbox.value+=", ";
}
if(cname != "") sbox.value+=cname;

document.getElementById("country").value = country;
document.getElementById("state").value = state;
document.getElementById("city").value = key;
if(state)
	document.getElementById("state2").value = checkstate(state);
liveSearchHide();
return true;



}
function liveSearchHover(el) {
      highlight = document.getElementById(boxHighlight);
      if (highlight) {
                        highlight.removeAttribute("id");
                }
       el.setAttribute("id",boxHighlight);
		document.searchfrm.searchbox.focus();
}

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

      return liveSearchSubmit(el.innerHTML);
}


