//This class used to handle textbox auto complete events.
function AutoComplete()
{
	//Declare functions.
	this.object				= null;
	this.event				= null;
	this.keyCode			= null;
	this.isInDelay			= false;
	//this.isSame				= false;
	this.timerID			= 0;
	this.assignEvent		= assignEvent;
	this.activateTimer		= activateTimer;
	this.divShow			= divShow;
	this.divInnerHTML		= divInnerHTML;
	this.divInnerText		= divInnerText;
	this.divChangePosition	= divChangePosition;

	//Set the event arguments.
	function assignEvent(e)
	{ 
		try
		{
			this.event		= e;
			this.object		= (e.target != null)	? e.target	: e.srcElement;
			//alert(this.object.id);
			this.keyCode	= (e.keyCode != null)	? e.keyCode	: e.which;
		}
		catch (e)
		{}
		
		/*
		if (!this.object)
		{
			alert("assignEvent Error");
		}
		*/		
	}

	//Active and resets the timer each time called.
	//When timer ends, call the given method.
	function activateTimer(callFunction, miliSecondsDelay)
	{		
		try
		{
			clearTimeout(this.timerID);		
			
			timerID			= setTimeout(callFunction, miliSecondsDelay);			
			this.isInDelay	= true;
			
			setTimeout("clearTimeout(this.timerID); this.isInDelay = false;", (miliSecondsDelay * 5));			
		}
		catch (e)
		{
			alert("activateTimer: " + e.description);
		}
	}

	//Show/Hide the results div.
	function divShow(show)
	{ 
		if (show)
			divAutoComplete.style.display = "block"; 
		else
			divAutoComplete.style.display = "none"; 
	}
	
	//Insert html/text into auto complete div.
	function divInnerHTML(html) 
	{
		try
		{
			divAutoComplete.innerHTML = html; 
			
			var s = document.getElementById("lbCities");
			
			if (!s) return;
			for (i = 0; i < s.length; i++)
			{
				if (s[i].text == this.object.value)
				{
					//alert(true);
					//alert(this.object.id);
				}
			}
		}
		catch (e)
		{
			//alert("divInnerHTML: " + e.description);
		}		
	}
	
	
	function divInnerText(text) 
	{
		try
		{
			divAutoComplete.innerText = text; 
		}
		catch (e)
		{
			//alert("divInnerText: " + e.description);
		}					
	}

	//Responsible for repositioning the results div under given object.
	function divChangePosition(showDiv)
	{
		var obj = this.object;

		//Check if we should display div.
		divShow(showDiv);

		//Clear div inner text and html.
		divAutoComplete.innerHTML = "";
		divAutoComplete.innerText = "";

		var objTop	= (getOffsetTop(obj) + obj.offsetHeight -1) + "px";
		var objLeft	= LANG == "he" ? (getOffsetLeft(obj) - 20) + "px" : (getOffsetLeft(obj) + 20) + "px";

		divAutoComplete.style.left	= objLeft;
		divAutoComplete.style.top	= objTop;
		divAutoComplete.style.width	= obj.offsetWidth;
	}
	
	//Get the left offset.
	function getOffsetLeft(obj) {
		var left = 0;

		while(obj.tagName != "BODY") {
			left += obj.offsetLeft;
			obj = obj.offsetParent; 
		}

		return left;
	}

	//Get the top offset.
	function getOffsetTop(obj) {
		var top = 0;

		while(obj.tagName != "BODY") {
			top += obj.offsetTop;
			obj = obj.offsetParent; 
		}

		return top;
	}

	function isBigLetter(chr)
	{
		return (chr >= "A" && chr <= "Z")
	}
	
	function isSmallLetter(chr)
	{
		return (chr >= "a" && chr <= "z")
	}
	
	function isHebrewLetter(chr)
	{
		return (chr.charCodeAt(0) >= 1488 && chr.charCodeAt(0) <= 1514)
	}
	
}

