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() {
	alert("tttt" + navigator.product);
	if (navigator.userAgent.indexOf("Safari") > 0) {
		getElem('livesearch').addEventListener("keydown",liveSearchKeyPress,false);
//		getElem('livesearch').addEventListener("blur",liveSearchHide,false);
	} else if (navigator.product == "Gecko") {
		getElem('livesearch').addEventListener("keypress",liveSearchKeyPress,false);
		getElem('livesearch').addEventListener("blur",liveSearchHideDelayed,false);
		
	} else {
		getElem('livesearch').attachEvent('onkeydown',liveSearchKeyPress);
//		getElem('livesearch').attachEvent("onblur",liveSearchHide,false);
		isIE = true;
	}
	
	getElem('livesearch').setAttribute("autocomplete","off");

}

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

function liveSearchKeyPress(event) {
	
	//alert(event.keyCode);
	
	//ENTER
	if (event.keyCode == 13) {
		highlight = getElem("LSHighlight")
		if (highlight) {
			var codeSearch = highlight.getAttribute("UND_ID");
			//if (codeSearch != "") validateClick (BasePath + "/help/search.php?code=" + codeSearch);
			if (codeSearch != "") validateClick (codeSearch);
		}
	}
	else if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = getElem("LSHighlight");
		if (!highlight) {
			highlight = getElem("LSShadow").firstChild.firstChild;
		} else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
			// a terminer : faire afficher le contenu dans l'input text
			//getElem("livesearch").value=getElem("LSHighlight").InnerHtml;
		} 
		if (!isIE) { event.preventDefault(); }
	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight = getElem("LSHighlight");
		if (!highlight) {
			highlight = getElem("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 = getElem("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		getElem("LSResult").style.display = "none";
	} 
}
var oldInnerHTML = '';
function liveSearchStart() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveSearchDoSearch()",200);
	//getElem('solutions').style.visibility='hidden';getElem('solutions').style.display='none';
	//getElem('solutions2').style.visibility='visible';getElem('solutions2').style.display='block';
	//	oldInnerHTML = escape(getElem('solutions').innerHTML);
//	getElem('solutions').innerHTML='';
}

function liveSearchDoSearch() {
	if (document.forms.searchform.search.value.length > 1) {
		if (typeof liveSearchRoot == "undefined") {
			liveSearchRoot = "";
		}
		if (typeof liveSearchRootSubDir == "undefined") {
			liveSearchRootSubDir = "";
		}
		if (typeof liveSearchParams == "undefined") {
			liveSearchParams = "";
		}
		if (liveSearchLast != document.forms.searchform.search.value) {
			if (liveSearchReq && liveSearchReq.readyState < 4) {
				liveSearchReq.abort();
			}
			if ( document.forms.searchform.search.value == "") {
				liveSearchHide();
				return false;
			}
			if (window.XMLHttpRequest) liveSearchReq = new XMLHttpRequest();
			if (window.XMLHttpRequest) {
			// branch for IE/Windows ActiveX version
			} else if (window.ActiveXObject) {
				liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			liveSearchReq.onreadystatechange = liveSearchProcessReqChange;
			
//			liveSearchReq.open("GET", liveSearchRoot + "livesearch.php?language=" + Language + "&searchparam=" + getSelect(document.forms.searchform.searchparam) + "&search=" + document.forms.searchform.search.value + liveSearchParams);
			liveSearchReq.open("GET", liveSearchRoot + "get_livesearch.php?search=" + escape(document.forms.searchform.search.value) + liveSearchParams);
			
			liveSearchLast = document.forms.searchform.search.value;
			liveSearchReq.send(null);
		}
	}
}

/*
* @param pDivObject - Object qui possède la méthode .innerHTML
* @param pHTML - le code HTML a rajouter dans l'object pDivObject
*/
function setInnerHTML(pDivObject, pHTML) 
{
	// mise du contenu HTML dans pDivObject
	pDivObject.innerHTML=pHTML; 
	var All=pDivObject.getElementsByTagName("*");
	for (var i=0; i<All.length; i++) 
	{
	All[i].id=All[i].getAttribute("id");
	All[i].name=All[i].getAttribute("name");
	All[i].className=All[i].getAttribute("class");
	}
	var AllScripts=pDivObject.getElementsByTagName("script");
	// parcours du tableau à l'envers pour n'être pas perturbé par l'effet du removeChild 
	for (var i=AllScripts.length-1; i>=0; i--)
	{
		var s = AllScripts[i];

		if (s.src && s.src!="") 
		{
		var oScript = document.createElement("script");
		oScript.type='text/javascript';
		oScript.src = s.src;
		// rajoute dans le body du document pere ce qui assure son execution
		// et la disponibilité des fonctions ainsi ajoutées
		// de plus, cela evite un appel Ajax dans le cas ou s.src est spécifié 
		document.body.appendChild(oScript);
		} else 
		{		
		eval(s.innerHTML);
		}
		
		
	}
}


function liveSearchProcessReqChange() {
	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById("LSResult");
		res.style.display = "block";
		var  sh = getElem("LSShadow");
		setInnerHTML(sh,liveSearchReq.responseText);
//		sh.innerHTML = liveSearchReq.responseText;		 
	}
}

function liveSearchSubmit() {
	return false;
	
	var highlight = getElem("LSHighlight");
	if (highlight && highlight.firstChild) {
		window.location = highlight.firstChild.nextSibling.getAttribute("id");
		return false;
	} 
	else
		return false;
}

function liveSearchHover(el) {
	highlight = getElem("LSHighlight");
	if (highlight) highlight.removeAttribute("id");
	el.setAttribute("id","LSHighlight");
}

function liveSearchLeave(el) { el.removeAttribute("id"); }

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

function closeResults() {
	liveSearchLast = '';
    getElem("LSResult").style.display = "none";
	getElem("livesearch").value = '     Bolsa, ISIN o nombre'; 
	getElem("livesearch").className = 'quicksearchWithLabel';
	getElem('solutions2').style.visibility='hidden';getElem('solutions2').style.display='none';
	getElem('solutions').style.visibility='visible';getElem('solutions').style.display='block';
//	getElem('solutions').innerHTML = unescape(oldInnerHTML);
}

getElem = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };