var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var isIE = false;
var beach = null;
if (window.XMLHttpRequest) {
	liveSearchReq = new XMLHttpRequest();
}
function liveSearchInit(beach) {
    this.beach = beach;
	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",liveSearchHideDelayed,false);
		isIE = true;
	}
	document.getElementById('livesearch').setAttribute("autocomplete","off");
}
function liveSearchHideDelayed() {
	window.setTimeout("liveSearchHide()",300);
}
function liveSearchHide() {
	document.getElementById("LSResult").style.display = "none";
	var f = document.getElementById("hideframe");
	if (f) f.style.visibility = "hidden";
	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 = highlight.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
		} 
		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 = highlight.previousSibling;
		}
		if (highlight) {
				highlight.setAttribute("id","LSHighlight");
		}
		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";
	} 
	//BACKSPACE - required for IE
	else if (event.keyCode == 8 && isIE) {
		liveSearchStart(this.beach);
	}
}
function liveSearchStart(beach) {
	if (t) {
		window.clearTimeout(t);
	}
	if(beach=="beach"){
        t = window.setTimeout("liveSearchDoSearch('beach')",200);
    }
    else{
        t = window.setTimeout("liveSearchDoSearch()",200);
    }
    
}
function liveSearchDoSearch(beach) {
	if (typeof liveSearchRoot == "undefined") {
		liveSearchRoot = "";
	}
	if (typeof liveSearchRootSubDir == "undefined") {
		liveSearchRootSubDir = "";
	}
	if (typeof liveSearchParams == "undefined") {
		liveSearchParams2 = "";
	} else {
		liveSearchParams2 = "&" + liveSearchParams;
	}
	if (liveSearchLast != document.forms.searchform.city.value || document.getElementById("LSResult").style.display == "none") {
		if (liveSearchReq && liveSearchReq.readyState < 4) {
			liveSearchReq.abort();
		}
		if (document.forms.searchform.city.value == "") {
			liveSearchHide();
			liveSearchLast = "";
			return false;
		}
		if (window.ActiveXObject) {
			liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
		} else if (window.XMLHttpRequest) {
			// branch for IE/Windows ActiveX version
		}
		liveSearchReq.onreadystatechange = liveSearchProcessReqChange;
		var qt = document.forms.searchform.city.value.split(',');
        if(beach){
            var beach_status = "&beach=1";
        }
        else{
            var beach_status = "&beach=0";
        }
		liveSearchReq.open("GET", liveSearchRoot + "livesearch.php?q=" + qt[0] + beach_status + liveSearchParams2);
		liveSearchLast = document.forms.searchform.city.value;
		liveSearchReq.send(null);
	}
}
function liveSearchProcessReqChange() {
	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("LSResult");
		res.style.display = "block";
		var  sh = document.getElementById("LSShadow");
		sh.innerHTML = liveSearchReq.responseText;
		hideElements();
	}
}
function liveSearchSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight) {
		document.forms.searchform.city.value = highlight.innerHTML;
		liveSearchHideDelayed();
		return false;
	} else {
		return true;
	}
}
function liveSearchHover(el) {
	highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
	el.setAttribute("id","LSHighlight");
}
function liveSearchClicked(el) {
	highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
	el.setAttribute("id","LSHighlight");
	document.forms.searchform.city.value = el.innerHTML;
	return liveSearchSubmit();
}
function hideElements() {
	var res = document.getElementById("LSResult");
	var f = document.getElementById("hideframe");
	if (f) {
		var fs = f.style;
		fs.top = res.offsetTop - 2+"px";
		fs.left = res.offsetLeft - 2+"px";
		fs.width = res.offsetWidth + 1+"px";
		fs.height = res.offsetHeight + 1+"px";
		fs.visibility = "";

	} else {
		f = document.createElement('iframe');
		f.src = '';
		f.setAttribute("id", "hideframe");
		var fs = f.style;
		fs.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity = 0)";
		fs.position = 'absolute';
        fs.border = "none";
		fs.margin = fs.padding = '0px';
		fs.top = res.offsetTop - 2+"px";
		fs.left = res.offsetLeft - 2+"px";
		fs.width = res.offsetWidth + 1+"px";
		fs.height = res.offsetHeight + 1+"px";
		fs.zIndex = 1;
		res.style.zIndex = 2;
		document.body.appendChild(f);
	}
}