var GLOBAL_xmlHttp, GLOBAL_t, GLOBAL_lineHeight, GLOBAL_theOutput, GLOBAL_lastString;
var highlight_counter;
var GLOBAL_hintActive = false;

function GLOBAL_showHint(GLOBAL_str1)
{
	//query has changed, proceed (prevents 'search' on non alphanumeric/char keys like alt, ctrl, etc)
	if (GLOBAL_str1!=GLOBAL_lastString && GLOBAL_str1!='')
	{
		//reset highlight counter
		highlight_counter = -1;
		
		//hide vertical scrollbar on text change. if necessary, it will be added below to accommodate items
		document.getElementById('GLOBAL_txtHint').style.overflowY='hidden';
		
		GLOBAL_lastString = GLOBAL_str1;
		if (GLOBAL_str1.length >= GLOBAL_CONFIG_strLength)
		{//Show 'Searching...' message
			document.getElementById("GLOBAL_txtHint").style.display='';
			document.getElementById("GLOBAL_txtHint").style.width='150px';
			document.getElementById("GLOBAL_txtHint").style.height=GLOBAL_CONFIG_lineHeight + 8 + 'px';
			document.getElementById("GLOBAL_txtHint").innerHTML=GLOBAL_CONFIG_searchingStr;
		}
		
		//after an x millisecond delay, process search (a lil easier on the server)
		if (GLOBAL_t) {clearTimeout(GLOBAL_t);}		
		GLOBAL_t=setTimeout("GLOBAL_showHint2('" + escape(GLOBAL_str1) + "')",GLOBAL_CONFIG_delay);
	}
	else
	{		
		//if (document.addEventListener){ 
		//document.addEventListener('keydown', checkKey, false); 
		//} else if (document.attachEvent){ 
		//document.attachEvent('onkeydown', checkKey); 
		//} 

	}
}

function GLOBAL_showHint2(GLOBAL_str)
{
	if (GLOBAL_str.length==0)
	{ 
		  document.getElementById("GLOBAL_txtHint").style.display='none';
		  document.getElementById("GLOBAL_txtHint").innerHTML='';
		  return;
	}
	GLOBAL_xmlHttp=GLOBAL_GetXmlHttpObject()
	if (GLOBAL_xmlHttp==null)
	{alert ("Your browser does not support AJAX!"); return;} 
	var GLOBAL_url=GLOBAL_CONFIG_XMLPath + "?strLength=" + GLOBAL_CONFIG_strLength + "&lineHeight=" + GLOBAL_CONFIG_lineHeight + "&q=" + GLOBAL_str;
	GLOBAL_xmlHttp.onreadystatechange=GLOBAL_stateChanged;
	GLOBAL_xmlHttp.open("GET",GLOBAL_url,true);
	GLOBAL_xmlHttp.send(null);
} 

function GLOBAL_stateChanged() 
{ 
	if (GLOBAL_xmlHttp.readyState==4)
	{//get the amount of items, adjust div height accordingly, and populate it.
		GLOBAL_theOutput = GLOBAL_xmlHttp.responseText
		
		GLOBAL_lineHeight = GLOBAL_theOutput.substring((GLOBAL_theOutput.indexOf("[found]") + 7),GLOBAL_theOutput.indexOf("[/found]"))
		
		//limit div height to a size of 10 items
		if (parseInt(GLOBAL_lineHeight))
		{
			if (parseInt(GLOBAL_lineHeight)>10)
			{
				GLOBAL_lineHeight=10;
				document.getElementById("GLOBAL_txtHint").style.overflowY='scroll';
			}
			GLOBAL_lineHeight = (GLOBAL_lineHeight * GLOBAL_CONFIG_lineHeight) + 8;
		}
		else {GLOBAL_lineHeight = GLOBAL_CONFIG_lineHeight + 8;}			

		GLOBAL_theOutput = GLOBAL_theOutput.substring(0, GLOBAL_theOutput.indexOf("[found]"));
		
		document.getElementById("GLOBAL_txtHint").style.display='';
		document.getElementById("GLOBAL_txtHint").style.width='';
		document.getElementById("GLOBAL_txtHint").style.height=GLOBAL_lineHeight + 'px';
		document.getElementById("GLOBAL_txtHint").innerHTML=GLOBAL_theOutput;
	}
}
	
function GLOBAL_GetXmlHttpObject()
{
	var GLOBAL_xmlHttp=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  GLOBAL_xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{GLOBAL_xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
	  catch (e)
		{GLOBAL_xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
	  }
	return GLOBAL_xmlHttp;
}

 var GLOBAL_docClick = function()
{
	if (!(GLOBAL_hintActive))
	{document.getElementById('GLOBAL_txtHint').style.display='none';}
}

if (document.addEventListener) {
	document.addEventListener('click', GLOBAL_docClick, false);
} else if (document.attachEvent) {
	document.attachEvent('onclick', GLOBAL_docClick, false);
}


function checkKey(e)
{ 
	if (!e) var e = window.event; 
	if (e.keyCode) { 
	code = e.keyCode; 
	} else if (e.which) { 
	code = e.which; 
	} 
		
	if (code == 40)
	{
		//increment (down the list)		
		if (document.getElementById(highlight_counter + 1 + ''))
		{
			if (highlight_counter > -1){document.getElementById(highlight_counter + '').style.backgroundColor='#ffffff';}
			highlight_counter = highlight_counter + 1;
			document.getElementById(highlight_counter + '').style.backgroundColor='#d5e2ff';
		}

	}
	else if (code == 38)
	{
		//decrement (up the list)
		if (document.getElementById(highlight_counter - 1 + ''))
		{
			document.getElementById(highlight_counter + '').style.backgroundColor='#ffffff';
			highlight_counter = highlight_counter - 1;
			document.getElementById(highlight_counter + '').style.backgroundColor='#d5e2ff';
		}
	}
}
