/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// +----------------------------------------------------------------------+
*/


var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var txt_obj_rec = "";
var type_rec = "";
var txt_obj_out = "";

var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveSearchReq = new XMLHttpRequest();
}

function liveSearchInit(obj_in) {
 	txt_obj_rec=obj_in;
	if (navigator.userAgent.indexOf("Safari") > 0) {

		document.getElementById(txt_obj_rec).addEventListener("keydown",liveSearchKeyPress,false);
		//document.getElementById('livesearch').addEventListener("blur",liveSearchHide,false);
	} else if (navigator.product == "Gecko") {

		document.getElementById(txt_obj_rec).addEventListener("keypress",liveSearchKeyPress,false);
		document.getElementById(txt_obj_rec).addEventListener("blur",liveSearchHideDelayed,false);
	} else if (liveSearchReq) {

		document.getElementById(txt_obj_rec).attachEvent('onKeyDown',liveSearchKeyPress);
		//document.getElementById('livesearch').attachEvent("onblur",liveSearchHide,false);
		isIE = true;
	}
	document.getElementById(txt_obj_rec).setAttribute("autocomplete","off");
}

function liveSearchKeyPress(event) {
	//window.document.getElementById("rec_desti").style.display = "none";
	window.document.forms['recherche'].rec_desti.style.display = "none";
//	document.getElementById("traduc_list1").style.display = "none";
	
		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(); }
	} 
	if (event.keyCode == 39 || event.keyCode == 13)
	//KEY RIGHT or ENTER
	{
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			document.getElementById(txt_obj_rec).value=highlight.firstChild.id;
			highlight.removeAttribute("id");
			liveSearchHideDelayed();
		}
		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";
		
		
	} 
	
}

function liveSearchHideDelayed() {
	
	window.setTimeout("liveSearchHide()",400);
}

function liveSearchHide() {
	//if(window.document.getElementById("livesearch").value==""){
		if(window.document.forms['recherche'].livesearch.value==""){
		window.document.forms['recherche'].rec_desti.style.display = "block";
		window.document.forms['recherche'].rec_heberg.style.display = "block";
		//window.document.getElementById("rec_desti").style.display = "block";
		//window.document.getElementById("rec_heberg").style.display = "none";
		//document.getElementById("traduc_list1").style.display = "block";
	}
	window.document.getElementById("LSResult").style.display = "none";
	var highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
}




function liveSearchStart(obj_in,type_rec_ls,obj_out) {
	
	txt_obj_rec = obj_in;
	txt_obj_out = obj_out;
	type_rec=type_rec_ls;

	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") {
		liveSearchParams2 = "";
	} else {
		liveSearchParams2 = "&" + liveSearchParams;
	}
	if (liveSearchLast != document.getElementById(txt_obj_rec).value) {
	if (liveSearchReq && liveSearchReq.readyState < 4) {
		liveSearchReq.abort();
	}
	if ( document.getElementById(txt_obj_rec).value == "") {
		liveSearchHide();
		return false;
	}
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
	liveSearchLast = document.getElementById(txt_obj_rec).value;

	if(type_rec=='airport'){
		liveSearchReq.open("GET", "../../../librairie/php/commun/requete_ajax/livesearch.php?rec="+ type_rec +"&obj_out="+ txt_obj_out +"&obj="+ txt_obj_rec +"&chaine="+ document.getElementById(txt_obj_rec).value + liveSearchParams2 );
	}
	if(type_rec=='station'){ 
		if(document.getElementById("volesp")){ 
			liveSearchReq.open("GET", "../../../librairie/php/commun/requete_ajax/livesearch.php?rec=station&langue=FR&dest="+document.getElementById("volesp").value+"&zone="+document.getElementById("list1").value+"&livesearch=" + document.forms.recherche.livesearch.value + liveSearchParams2);	
		}
		else{
			liveSearchReq.open("GET", "../../../librairie/php/commun/requete_ajax/livesearch.php?rec=station&langue=FR&zone="+document.getElementById("list1").value+"&livesearch=" + document.forms.recherche.livesearch.value + liveSearchParams2);
		}
	 }
	liveSearchLast = document.getElementById(txt_obj_rec).value;	
	liveSearchReq.send(null);

	}
}






function liveSearchProcessReqChange() {

		
	if (liveSearchReq.readyState == 4) {
		
		var  cible = document.getElementById(txt_obj_rec);
		var  res = document.getElementById("LSResult");
		
		hideSelect();
		res.style.display = "block";
		res.style.background="#FFFFFF";
		res.style.border = "1px solid #999999";
		res.style.width = cible.offsetWidth-2;
		
    y = 0;
    x=0;
    var obj = cible;
    while (obj.offsetParent != null) {
          y += obj.offsetTop;
          x += obj.offsetLeft;
          obj = obj.offsetParent;
          
    }
    y += obj.offsetTop;
    x += obj.offsetLeft;

		res.style.top = y + 20;
		res.style.left =  x;
		
		res.style.font = "11px Arial";
		res.style.height = "140px";
		res.style.overflow = "auto";

		var  sh = document.getElementById("LSShadow");
		document.getElementById("LSResult").style.display = "block";
		document.getElementById("LSResult").style.visibility = "visible";
		sh.innerHTML = liveSearchReq.responseText;

	}
}

function liveSearchSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight && highlight.firstChild) {
//		alert(highlight.firstChild);	// that's the table
//		alert(highlight.firstChild.firstChild.firstChild.firstChild.firstChild.nextSibling.getAttribute("href"));
//		window.location = liveSearchRoot + liveSearchRootSubDir + highlight.firstChild.nextSibling.getAttribute("href");
		window.location = highlight.firstChild.firstChild.firstChild.firstChild.firstChild.nextSibling.getAttribute("href");
		return false;
	} 
	highlight = document.getElementById("LSHighlightM");
	if (highlight && highlight.firstChild) {
//		alert(highlight.firstChild);	// that's the table
//		alert(highlight.firstChild.firstChild.firstChild.firstChild.firstChild.nextSibling.getAttribute("href"));
//		window.location = liveSearchRoot + liveSearchRootSubDir + highlight.firstChild.nextSibling.getAttribute("href");
		window.location = highlight.firstChild.firstChild.firstChild.firstChild.firstChild.nextSibling.getAttribute("href");
		return false;
	} 
	return true;
}

	