var theBox;
var searchBox;
var beenChanged=false;

window.onload=function(){
	theBox=document.getElementById('SearchTerm');
	theBox.onkeyup=updateAuto;
	window.setInterval("shouldSend()",1000);
}

function updateAuto(){

	beenChanged=true;

}

function shouldSend(){

	if(!beenChanged) return;

	beenChanged=false;


	var theThing=createAjaxObjectForSearch();
	

	theThing.open('GET','GetAutocomplete.php?search='+theBox.value);
	
	theThing.onreadystatechange=function(){
	
	
		if(this.readyState==4){
			searchBox=document.getElementById('AutoCompleteBox');
			var theValue='';
			try{
			
				termList=eval('('+this.responseText+')')
			
				theValue="<ul style='padding-top:0px;margin-top:0'>";
			
				for(var i=0;i<termList.length;i++){
					theValue+="<li><a onclick=\"selectAutoComplete('"+termList[i]+"')\">"+termList[i]+"</a></li>";
				}
				theValue+="</ul>";
				//alert(theValue);
			
				if(theBox.value=='') theValue='';

			}catch(err){
				theValue='';
			}

			searchBox.innerHTML=theValue;
			
			
			
		}
	
	}
	
	theThing.send(null);

}

function selectAutoComplete(theVal){
	theBox.value=theVal;
	document.getElementById('QuickSearchForm').submit();

}



function createAjaxObjectForSearch(){var ajax=false;if(window.XMLHttpRequest){	ajax= new XMLHttpRequest();}else if(window.ActiveXObject){	try{ajax=new ActiveXObject("Msxml2.XMLHTTP");	}catch(e){		try{ajax=new ActiveXObject("Microsoft.XMLHTTP");		}catch(e){}	}}return ajax;}

