
//include ajax code
document.write('<script type="text/javascript" language="JavaScript" src="/common/ajsSearchObj/ajax.js"></script>');
//include browser code
document.write('<script type="text/javascript" language="JavaScript" src="/common/jsBrowserStuff.php"></script>');
		
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++ ajsSearch Object
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function ajsSearch(baseTBName, objName){
	
	this.baseObj=document.getElementById(baseTBName);
	this.name=objName;
	
	this.ajax = new sack();
	this.timAJAX=false;
	this.inRefresh=false;
	
	this.curHoverItem=0;
	
	this.lastActivity=0;
	this.timActivity=false;
	
	//init ajax state
	this.ajax.responseStatus[0]=0;
	
	this.timCheckResults=false;
	
	//set input functions
	//this.baseObj.onkeyup=this.getResults;
	
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ createSearchObj()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.createSearchObj=function(){
		
		//create output div
		document.write('<div id="'+this.name+'Results" class="ajsResultsHolder" style="position:absolute; left:0px; top:0px; width:100px; height:10em; overflow:auto; display:none; background-color:#ffffff; border:solid 1px #000000; font-family:Verdana; font-size:9pt; font-weight:400;">');
		document.write('loading');
		document.write('</div>');
		
		this.objResults=document.getElementById(this.name+'Results');
		this.lastActivity=this.currentTS();
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ getResults()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.getResults=function(){
		
		if (this.inRefresh==true){
			//already loading, try again later
			//this.timAJAX=setTimeout(this.name+'.getResults()', 200);
			return false;
		}
		
		this.inRefresh=true;
		clearTimeout(this.timAJAX);
		
		//reset cur hover item
		this.curHoverItem=0;
		
		//this.ajax.requestFile='/common/ajsSearchObj/_ajsGetResults.php?o='+this.name+'&s='+this.baseObj.value;
		
		this.ajax.requestFile=this.requestFile+'?o='+this.name+'&s='+this.baseObj.value;
		this.timCheckResults=setTimeout(this.name+'.dispResults()', 100);
		this.ajax.runAJAX();
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ setRequestFile()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.setRequestFile=function(inFile){
		
		this.requestFile=inFile;
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ setSubmitRoutine()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.setSubmitRoutine=function(inFuncName){
		
		this.submitRoutine=inFuncName;
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ submitResult()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.submitResult=function(){
		
		setTimeout(this.submitRoutine, 50);
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ dispResults()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.dispResults=function(){
		
		aState=this.ajax.responseStatus;
		if (aState[0]==200){
			var result=this.ajax.response;
			//window.alert(result);
			
			this.objResults.innerHTML=result;
		}else{
			this.timCheckResults=setTimeout(this.name+'.dispResults()', 100);
		}
		
		this.showResults();
		
		this.inRefresh=false;
		
		this.ajax.responseStatus[0]=0;
		
		clearTimeout(this.timAJAX);
		this.lastActivity=this.currentTS();
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ showResults()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.showResults=function(){
		
		//get num items returned
		tmpObj = document.getElementById(this.name+'ItemHolder');
		if (!tmpObj){
			this.hideResults();
			return false;
		}
		
		numItems=tmpObj.getElementsByTagName("li").length;
		if (parseInt(numItems)==0){
			this.hideResults();
			return false;
		}
		
		tmpLeft=this.getAbsoluteLeft(this.baseObj);
		tmpTop=this.getAbsoluteTop(this.baseObj);
		tmpHeightOffset=this.getAbsoluteHeight(this.baseObj);
		tmpWidth=parseInt(this.getAbsoluteWidth(this.baseObj))-2;	//subtract the border
		
		this.objResults.style.left=tmpLeft+'px';
		this.objResults.style.top=parseInt(tmpTop)+parseInt(tmpHeightOffset)+'px';
		this.objResults.style.width=tmpWidth+'px';
		
		if (parseInt(numItems)>8){
			this.objResults.style.height='12em';
		}else{
			this.objResults.style.height=(parseInt(numItems)*1.5)+'em';
		}
		
		this.objResults.style.display='block';
		this.lastActivity=this.currentTS();
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ hideResults()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.hideResults=function(){
		
		this.objResults.style.display='none';
		this.lastActivity=0; //this.currentTS();
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ outResults()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.outResults=function(){
		//this routine is called when the base obj loses focus
		//check in a few seconds to see if no other activity
		//like an item click
		
		//window.alert(this.currentTS());
		this.lastActivity=this.currentTS();
		
		this.timActivity=setTimeout(this.name+'.checkForInactivity()', 1000);
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ checkForInactivity()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.checkForInactivity=function(){
		
		tmpNow=this.currentTS();
		lapsedTime=parseInt(tmpNow)-parseInt(this.lastActivity);
		//window.alert(lapsedTime);
		if (parseInt(lapsedTime)<980){
			//window.alert('more activity');
		}else{
			//window.alert('ok, hide');
			this.hideResults();
		}
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ currentTS()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.currentTS=function(){
		
		var tmpNow=new Date();
		return tmpNow.getTime();
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ hoverItem()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.hoverItem=function(inKey, inMode){
		
		if (parseInt(this.curHoverItem)>0){
			//has current hover, reset
			tmpObj=document.getElementById(this.name+'Item'+this.curHoverItem);
			if (tmpObj){
				tmpObj.style.backgroundColor='#ffffff';
				tmpObj.style.color='#000000';
			}
		}
		
		this.curHoverItem=inKey;
		tmpObj=document.getElementById(this.name+'Item'+this.curHoverItem);
		tmpObj.style.backgroundColor='#3399ff';
		tmpObj.style.color='#ffffff';
		this.lastActivity=this.currentTS();
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ clickItem()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.clickItem=function(inKey){
		
		tmpObj=document.getElementById(this.name+'Item'+inKey);
		this.baseObj.value=tmpObj.innerHTML;
		
		this.hideResults();
		this.baseObj.focus();
		this.lastActivity=this.currentTS();
		
		this.submitResult();
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ checkKey()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.checkKey=function(inObj, inEvent){
		
		if (inObj.value==''){
			return false;
		}
		
		switch (parseInt(inEvent.keyCode)){
		case 38:
			//up arrow
			this.moveSelItem('u');
			break;
		case 40:
			//down arrow
			if (this.objResults.style.display!='block'){
				//re-show search
				this.showResults();
			}else{
				this.moveSelItem('d');
			}
			break;
		case 27:
			//esc;
			this.curHoverItem=0;
			this.hideResults();
			break;
		case 13:
			//enter key
			//if (this.curHoverItem==0){
			if (this.objResults.style.display!='block'){
				//submit
				this.submitResult();
			}else{
				if (this.curHoverItem==0){
					//showing results, none selected
					this.submitResult();
				}else{
					this.clickItem(this.curHoverItem);
				}
			}
			return false;
			break;
		default:
			this.getResults();
			break;
		}
		this.lastActivity=this.currentTS();
		
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ moveSelItem()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.moveSelItem=function(inDir){
		
		if (parseInt(this.curHoverItem)>0){
			//has current hover, reset
			tmpObj=document.getElementById(this.name+'Item'+this.curHoverItem);
			if (tmpObj){
				tmpObj.style.backgroundColor='#ffffff';
				tmpObj.style.color='#000000';
			}
		}
		
		if (inDir=='u'){
			//move up
			if (parseInt(this.curHoverItem)<=1){
				//at first
				this.curHoverItem=1;
			}else{
				this.curHoverItem=parseInt(this.curHoverItem)-1;
			}
		}else{
			//move down
			//get num items returned
			tmpObj = document.getElementById(this.name+'ItemHolder');
			numItems=tmpObj.getElementsByTagName("li").length;
			if (parseInt(this.curHoverItem)>=numItems){
				this.curHoverItem=numItems;
			}else{
				this.curHoverItem=parseInt(this.curHoverItem)+1;
			}
		}
		tmpObj=document.getElementById(this.name+'Item'+this.curHoverItem);
		tmpObj.style.backgroundColor='#3399ff';
		tmpObj.style.color='#ffffff';
		
		this.lastActivity=this.currentTS();
		
	}
	
	
	
	
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ getAbsoluteLeft()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.getAbsoluteLeft=function(obj){
		var curleft = 0;
	  
	  if(obj.offsetParent)
	   	while(1){
	   		curleft += parseInt(obj.offsetLeft);
	      if(!obj.offsetParent)
	      	break;
	      obj = obj.offsetParent;
	    }
	  else if(obj.x)
	  	curleft += parseInt(obj.x);
	  
	 return curleft;
	  
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ getAbsoluteTop()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.getAbsoluteTop=function(obj){
		var curtop = 0;
		
		if(obj.offsetParent)
			while(1){
				curtop += parseInt(obj.offsetTop);
	      if(!obj.offsetParent)
	      	break;
	      obj = obj.offsetParent;
	    }
	  else if(obj.y)
	  	curtop += parseInt(obj.y);
	  
	  return curtop;
	  
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ getAbsoluteHeight()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.getAbsoluteHeight=function(obj){
		
		if (gJSBrowserType=='op5'){ 
			xPos = obj.style.pixelHeight;
		}else{
			xPos = obj.offsetHeight;
		}
		return xPos;
	
	}
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	//++ getAbsoluteWidth()
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	this.getAbsoluteWidth=function(obj){
		
		if (gJSBrowserType=='op5'){
			xPos = obj.style.pixelWidth;
		}else{
			xPos = obj.offsetWidth;
		}
		return xPos;
		
	}
	
	
	
}

