if(typeof String.prototype.trim==='undefined'){String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,'');}}

// writes to DIV#searchPredictOutput
// <a class="predict">
// requires addEvent() func
// requires HTTPObj

var searchPredict={

active:false,
text:'',
formField:null,
http:null,
searchPredictOutput:null,
cycleTmr:null,
cycleDelay:500,
maxResults:6,
searchPredictOptionIndex:0,
searchPredictOptionMax:0,
dir:'./',

init:function(ao)
{	if(typeof(ao.formField)==="undefined"){return;}
	if(typeof(ao.maxResults)!=="undefined"){this.maxResults=parseInt(ao.maxResults,10);}
	if(typeof(ao.dir)!=="undefined"){this.dir=ao.dir;}
	try{this.formField=document.getElementById(ao.formField);}catch(e){return;}
	try{this.searchPredictOutput=document.getElementById('searchPredictOutput');}catch(e){return;}
	try{this.http=new HTTPObj();}catch(e){return;}
	try{addEvent(searchPredict.formField,'focus',searchPredict.start);}catch(e){return;}
	try{addEvent(searchPredict.formField,'blur',searchPredict.end);}catch(e){return;}
},

start:function()
{ searchPredict.active=true;
	searchPredict.cycle();
},

end:function()
{	clearTimeout(searchPredict.cycleTmr);
	searchPredict.active=false;
	searchPredict.hide();
	searchPredict.text='';
},

cycle:function()
{	var t=searchPredict.formField.value.trim();
	if((t==searchPredict.text)||(searchPredict.http.readyState!=0)||(t.length<1))
	{ if(t.length<1){searchPredict.hide();}
		searchPredict.cycleTmr=setTimeout('searchPredict.cycle()',searchPredict.cycleDelay);
	}
	else
	{ searchPredict.text=t;
		searchPredict.submitText();
	}
},

submitText:function()
{	var qstr='action=_PREDICT_';
	qstr+='&text='+encodeURIComponent(this.text);
	qstr+='&max='+this.maxResults;
	this.http.open('POST',this.dir+'http_searchPredict.php',true);
	this.http.onreadystatechange=this.submitTextHandleResponse;
	this.http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	this.http.send(qstr);
},

submitTextHandleResponse:function()
{	var i=0,im=0,h='';
	if(!searchPredict.active){searchPredict.http=new HTTPObj();return;}
	if(searchPredict.http.readyState==4)
	{	var ro=eval('('+searchPredict.http.responseText+')');
		if((ro.predictions.length>0)&&(ro.result=="_GOOD_"))
		{	searchPredict.searchPredictOutput.style.display='block';
			for(i=0,im=ro.predictions.length;i<im;i++)
			{ h+='<a href="javascript:void(0)" id="searchPredictOption'+(i+1)+'" onmousedown="searchPredict.predictUse(\''+escape(ro.predictions[i])+'\')" class="predict">'+ro.predictions[i]+'</a>';
			}
			searchPredict.searchPredictOutput.innerHTML=h;h='';
		}
		else
		{	searchPredict.searchPredictOutput.style.display='none';
		}		
		searchPredict.searchPredictOptionIndex=0;
		searchPredict.searchPredictOptionMax=ro.predictions.length;
		searchPredict.http=new HTTPObj();
		if(searchPredict.active)
		{	searchPredict.cycleTmr=setTimeout('searchPredict.cycle()',searchPredict.cycleDelay);
		}
	}
},

predictKeySelect:function(ev)
{	if(!ev){ev=window.event;}
	if((ev.charCode)&&(ev.keyCode==0)){var code=ev.charCode;}else{var code=ev.keyCode;}
	if(code==38) // up
	{ 
	}
	if(code==40) // down
	{ if(this.searchPredictOptionIndex<this.searchPredictOptionMax)
		{ this.searchPredictOptionIndex++;
			document.getElementById('searchPredictOption'+this.searchPredictOptionIndex).focus();
		}
	}
},

predictUse:function(t)
{	searchPredict.formField.value=unescape(t);	
	this.end();
},

hide:function()
{	this.searchPredictOutput.style.display='none';	
}
	
	
};