/* ### Customised for Livesearch Pilot - Please don't use this code without checking with Mark Galliers ### */

var suggestionsfile = "/en_GX/webadmin/esi/liveresults.xml?pwcGeo=DK&pwcLang=da";

var xmlHttp
	
function showResult(str) {
	if (str.length<=minchars) {
		document.getElementById("livesearch").innerHTML="";
		document.getElementById("livesearch").style.visibility="hidden";
		return
	}
	if (livesearchon!="true") {
		return
	}
	document.getElementById("livesearchbutton").src="/en_GX/webadmin/assets/image/ajaxload.gif";
	document.getElementById("livesearchbutton").className="";
	xmlhttp=null;
	url = suggestionsfile + "&q=" + str;

	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange=processsearch;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else {
		clearlivesearch();
		return;
	}
}

function processsearch() { // Check if file is valid & if so do something with its contents

	var str = document.getElementById("searchfield").value.toLowerCase();

	if(xmlhttp.readyState!=4) return;
	if(xmlhttp.status!=200) {
		//alert("Problem retrieving XML data");
		//alert(xmlhttp.readyState);
		//alert(xmlhttp.status);
		clearlivesearch();
		return;
	}

	x=xmlhttp.responseXML.documentElement.getElementsByTagName("link");
	var searchoutput = "";
	
	if (livesearchheader === undefined) { var livesearchheader = "Recommended results"; }
	if (livesearchlink === undefined) { var livesearchlink = "View all results"; }
	if (livesearchurl === undefined) { var livesearchurl = "/en_GX/webadmin/esi/search.jhtml?pwcGeo=DK&pwcLang=da&localeOverride=da_DK&pwcHideLevel=0&q="; }

	var searchoutputheader = "<div id='livesearchheader'>" + livesearchheader + "</div>";
	var searchoutputfooter = "<div id='livesearchfooter'><a href='" + livesearchurl + str + "'>" + livesearchlink + " >></a></div>"; 
	if (str.length > minchars) {

		for (i=0;i<x.length;i++) {
			if (i==5) { break; }
			xtitles=x[i].getElementsByTagName("title");
			xkeywords=x[i].getElementsByTagName("keyword");
			xurls=x[i].getElementsByTagName("url"); {
			try {
				var testquery = xkeywords[0].firstChild.nodeValue.toLowerCase();
				if (testquery.indexOf(str) !=-1) {
					searchoutput = searchoutput + "<a href='" + xurls[0].firstChild.nodeValue + "'>" + xtitles[0].firstChild.nodeValue + "</a><br />";
				} else {
					searchoutput = searchoutput + "";
				}
			}
				catch (er) { searchoutput = searchoutput + ""; }
			}				
		}

	}

	if (searchoutput=="") { 
		document.getElementById("livesearch").style.visibility="hidden";
		document.getElementById("livesearchbutton").src="/en_GX/webadmin/assets/image/icon_close.gif"; // Show the close button, replacing the ajax gif
		document.getElementById("livesearchbutton").className="livesearchclose";
	} else {
		if (xmlhttp.readyState==4 || xmlhttp.readyState=="complete") { 
			document.getElementById("livesearch").style.visibility="visible";
			document.getElementById("livesearch").innerHTML=searchoutputheader + searchoutput + searchoutputfooter;
			document.getElementById("livesearchbutton").src="/en_GX/webadmin/assets/image/icon_close.gif";
			document.getElementById("livesearchbutton").className="livesearchclose";
		} 
	}

}
