var xmlHttp, t, lineHeight, theOutput, lastString;
var hintActive = false;

function showHint(str1)
{
	//query has changed, proceed (prevents 'search' on non alphanumeric/char keys like alt, ctrl, etc)
	if (str1!=lastString)
	{
		lastString = str1;
		if (str1.length >= CONFIG_strLength)
		{//Show 'Searching...' message
			document.getElementById("txtHint").style.display='';
			document.getElementById("txtHint").style.width='150px';
			document.getElementById("txtHint").style.height=CONFIG_lineHeight + 8 + 'px';
			document.getElementById("txtHint").innerHTML=CONFIG_searchingStr;
		}
		
		//after an x millisecond delay, process search (a lil easier on the server)
		if (t) {clearTimeout(t);}		
		t=setTimeout("showHint2('" + escape(str1) + "')",CONFIG_delay);
	}
}

function showHint2(str)
{
	if (str.length==0)
	{ 
		  document.getElementById("txtHint").style.display='none';
		  document.getElementById("txtHint").innerHTML='';
		  return;
	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{alert ("Your browser does not support AJAX!"); return;} 
	var url=CONFIG_XMLPath + "?strLength=" + CONFIG_strLength + "&lineHeight=" + CONFIG_lineHeight + "&q=" + str;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{//get the amount of items, adjust div height accordingly, and populate it.
		theOutput = xmlHttp.responseText
		
		lineHeight = theOutput.substring((theOutput.indexOf("[found]") + 7),theOutput.indexOf("[/found]"))
		
		//limit div height to a size of 10 items
		if (parseInt(lineHeight))
		{
			if (parseInt(lineHeight)>10)
			{
				lineHeight=10;
				document.getElementById("txtHint").style.overflowY='scroll';
			}
			lineHeight = (lineHeight * CONFIG_lineHeight) + 8;
		}
		else {lineHeight = CONFIG_lineHeight + 8;}			

		theOutput = theOutput.substring(0, theOutput.indexOf("[found]"));
		
		document.getElementById("txtHint").style.display='';
		document.getElementById("txtHint").style.width='';
		document.getElementById("txtHint").style.height=lineHeight + 'px';
		document.getElementById("txtHint").innerHTML=theOutput;
	}
}
	
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
	  catch (e)
		{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
	  }
	return xmlHttp;
}

 var docClick = function()
{
	if (!(hintActive))
	{document.getElementById('txtHint').style.display='none';}
}

if (document.addEventListener) {
	document.addEventListener('click', docClick, false);
} else if (document.attachEvent) {
	document.attachEvent('onclick', docClick, false);
}
