FSite2._autoCompleteClose = function(el)
{
	if (this && !el)
		el = this;
	if (el._autocomplete._send)
	{
		el._autocomplete._stopRequest=true;
	}
	if (el._autocomplete._sendtimeout)
	{
		el._autocomplete._stopRequest=true;
//		clearTimeout(el._autocomplete._sendtimeout);
//		el._autocomplete._sendtimeout=null;
	}
	if (el._autocomplete._div)
	{
		el._autocomplete._div.style.display = 'none';
		el._autocomplete._div.style.visibility = 'hidden';
		el._autocomplete._div.innerHTML='';
		el._autocomplete._table=null;
	}
}

FSite2._autoCompleteInitClose = function()
{
	this._timeout = setTimeout(FSite2._callRef(this.autoCompleteClose, this), 200);
}

FSite2._autoCompleteCancelClose = function(el)
{
	if (this && !el)
		el = this;
	if (el._timeout)
	{
		clearTimeout(el._timeout);
		el._timeout = null;
	}
}

FSite2._autoCompleteInitCancelClose = function()
{
	setTimeout(FSite2._callRef(FSite2._autoCompleteCancelClose, this), 100);
}

FSite2._autoCompleteSetSroll = function()
{
	if (this._autocomplete._rows[(this._selected<0)?0:this._selected].offsetTop + this._autocomplete._rows[(this._selected<0)?0:this._selected].offsetHeight > this._autocomplete._div.scrollTop + this._autocomplete._div.clientHeight)
		this._autocomplete._div.scrollTop = this._autocomplete._rows[(this._selected<0)?0:this._selected].offsetTop + this._autocomplete._rows[(this._selected<0)?0:this._selected].offsetHeight - this._autocomplete._div.clientHeight;
	if (this._autocomplete._rows[(this._selected<0)?0:this._selected].offsetTop < this._autocomplete._div.scrollTop)
		this._autocomplete._div.scrollTop = this._autocomplete._rows[(this._selected<0)?0:this._selected].offsetTop;
	return false;
}	 
 
FSite2._autoCompleteSetClassName = function (classname,rows,remove)
{
	cells=rows.getElementsByTagName('td');
	for (a=0;a<cells.length;a++)
	{
		if (remove)
			cells[a].className = cells[a].className.replace(new RegExp('\\s*\\b' + classname + '\\b\\s*', 'gi'), '');
		else
			cells[a].className = cells[a].className + ' ' + classname;
	}
}

FSite2._autoCompleteOnClick = function (el)
{
	if (this && !el)
		el=this;
}

FSite2._autoCompleteOnKeyDown = function (event)
{
	if (!event) var event = window.event;
	//ESC
	if (event.keyCode==27)
	{
		this.autoCompleteInitClose();
	}
	//TAB, ENTER 
	else if ((event.keyCode==13)||(event.keyCode==9))
	{
		if ((this._selected>=0)&&(this._selected<this._autocomplete._rows.length))
		{
			td=this._autocomplete._rows[this._selected].getElementsByTagName('td')
			if (td.length>0)
			{
				if (td[0].onclick)
				{
					td[0].onclick();
					this.autoCompleteClose();
				}
			}  
			if (this._autocomplete._rows[this._selected].onclick)
			{
				this._autocomplete._rows[this._selected].onclick();
				this.autoCompleteClose();
			}
		}
		this._autocomplete.returnFalse=false;
		if (event.keyCode==13)
		{
			this._autocomplete.returnFalse=true;
			event.cancelBubble = true;
			event.returnValue = false;
			if (event.preventDefault)
				event.preventDefault();
			return false;
		}
		else
			return true;
	}
	//PAGE UP
	else if (event.keyCode==33)
	{
		page = Math.floor (this._autocomplete._div.clientHeight / this._autocomplete._rows[0].offsetHeight);
		if (this._selected>=0)
		{
		//unmark
		this.autoCompleteSetClassName('selected',this._autocomplete._rows[this._selected],true);
		this._selected=this._selected-page;
		if (this._selected<0)
			this._selected=0;
		this.autoCompleteSetClassName('selected',this._autocomplete._rows[this._selected]);
		this.autoCompleteSetSroll();
		}
		return false;  
	}
	//PAGE DOWN
	else if (event.keyCode==34)
	{
		page = Math.floor (this._autocomplete._div.clientHeight / this._autocomplete._rows[0].offsetHeight);
		if (this._selected<0)
			this._selected=page-1;
		else if (this._selected<(this._autocomplete._rows.length-1))
		{
			//unmark
			this.autoCompleteSetClassName('selected',this._autocomplete._rows[this._selected],true);
			this._selected=this._selected+page;
		}
		if (this._selected>=this._autocomplete._rows.length) this._selected=this._autocomplete._rows.length-1;
		//mark
		this.autoCompleteSetClassName('selected',this._autocomplete._rows[this._selected]);
		this.autoCompleteSetSroll();
			return false;
	}
	//ARROW DOWN
	else if (event.keyCode==40)
	{
		if (this._autocomplete._rows.length>0)
		{
			if ((this._selected<0))
				this._selected=0;
			else if (this._selected<(this._autocomplete._rows.length-1))
				this._selected++;
		}
		else
			return false;  
		//unmark
		if (this._selected>0)
		{
			this.autoCompleteSetClassName('selected',this._autocomplete._rows[this._selected-1],true);
		}
		//mark 
		this.autoCompleteSetClassName('selected',this._autocomplete._rows[this._selected]);
		this.autoCompleteSetSroll();
		return false;
	}
	//ARROW UP
	else if (event.keyCode==38)
	{
		if (this._selected<0) return false;
		if (this._selected>this._autocomplete._rows.length) this._selected=this._autocomplete._rows.length;
		this.autoCompleteSetClassName('selected',this._autocomplete._rows[this._selected],true);
		if (this._selected>0)
		{
			this._selected--;
			this.autoCompleteSetClassName('selected',this._autocomplete._rows[this._selected]);
		}
		else
		{
			this._selected=-1;
			this.focus();
		} 
		this.autoCompleteSetSroll(); 
		return false;
	}
	return true; 
}

FSite2._autoCompleteBuildRequest = function (absoluteSend)
{
	this._autocomplete._hidden=false;
	this.autoCompleteSendRequest(absoluteSend);
}

FSite2._autoCompleteSetUrl = function (url)
{
	this._autocomplete._url = url;
	var Re = new RegExp(":([a-zA-Z0-9]+):", "g");
	var match = url.match(Re);
	var length = match.length;
	var paramName;
	for (var i=0;i<length;i++)
	{
		paramName=match[i].toString();
		this._autocomplete._urlParams[paramName.substr(1,paramName.length-2)]='';
	}
}

FSite2._autoCompleteSetUrlParam = function (param, value)
{
	this._autocomplete._urlParams[param]=value;
}

FSite2._autoCompleteSendRequest = function (absoluteSend)
{
	var urlParam = FSite2.cloneObject(this._autocomplete._urlParams);
	var url=this._autocomplete._url;
	urlParam['value']=this.value;
	urlParam['name']=this.name;
	urlParam['id']=(this.id)?this.id:'';
	urlParam['form']=(this.form)?this.form.name:'';
	for (var param in urlParam)
		url=url.replace(':'+param+':',encodeURIComponent(urlParam[param]));
	this.HTTPRequest=new FSite2.HTTPRequest(url,function()
	{
		this.object._autocomplete._send=false;
		this.object._selected=-1;
		this.object._autocomplete._div.innerHTML=this.httpRequest.responseText;
		var table = this.object._autocomplete._div.getElementsByTagName('table');
		if (table.length>0)
		{
			this.object._autocomplete._table=table[0];
			this.object._autocomplete._rows=table[0].getElementsByTagName('tr');
		
			for (r=0;r<this.object._autocomplete._rows.length;r++)
			{
				this.object._autocomplete._rows[r]._number=r;
				this.object._autocomplete._rows[r]._object=this.object;

				this.object._autocomplete._rows[r].onmouseover = function ()
				{
					FSite2._autoCompleteSetClassName('hover',this);
				}

				this.object._autocomplete._rows[r].onmouseout = function ()
				{
					FSite2._autoCompleteSetClassName('hover',this,true);
				}
				
			}
			if (!this.object._autocomplete._stopRequest)
			{

				if (this.object._autocomplete._rows.length>0)
				{
					if(absoluteSend)
					{
						this.object._selected=0;
						this.object.autoCompleteSetClassName('selected',this.object._autocomplete._rows[this.object._selected]);
						this.object.autoCompleteSetSroll();
					}
					if (absoluteSend!=-1)
					{
						this.object._autocomplete._div.style.display='block';
						this.object._autocomplete._div.style.visibility='visible';
					}
				}

				if (this.object._autocomplete._table.clientHeight > this.object._autocomplete._div.clientHeight)
				this.object._autocomplete._table.style.width = (this.object._autocomplete._div.offsetWidth - 20) + 'px';
				
				//loading with starting value
				if ((absoluteSend==-1)&&(this.object._autocomplete._rows.length==1))
				{
					td=this.object._autocomplete._rows[this.object._selected].getElementsByTagName('td')
					if (td.length>0)
					{
						if (td[0].onclick)
						{
							td[0].onclick();
							this.object.autoCompleteClose();
						}
					}  
					if (this.object._autocomplete._rows[this.object._selected].onclick)
					{
						this.object._autocomplete._rows[this.object._selected].onclick();
						this.object.autoCompleteClose();
					}
				}
				if ((absoluteSend!=-1) && (typeof this.object.autoCompleteOnLoad == 'function'))
					this.object.autoCompleteOnLoad();

				if (this.object._autocomplete._rows.length==0)
					this.object.autoCompleteInitClose();
				return;
			}
		}
		if (this.object._autocomplete._stopRequest)
		{
			if (typeof this.object.autoCompleteOnCancel == 'function')
				this.object.autoCompleteOnCancel();
			this.object._autocomplete._div.innerHTML='';
			this.object._autocomplete._stopRequest=false;
		}
		this.object.autoCompleteInitClose();
	},false,false,true);
	if (this._autocomplete._sendtimeout)
	{
		clearTimeout(this._autocomplete._sendtimeout);
		this._autocomplete._sendtimeout=null;
	}
	this.HTTPRequest.object=this;
	if (absoluteSend)
	{
		this._autocomplete._send=true;
		this.HTTPRequest.ajaxSendRequest();
	}
	else
	{
		this._autocomplete._sendtimeout=setTimeout(FSite2._callRef(function (el) {
			el._autocomplete._send=true;
			el.HTTPRequest.ajaxSendRequest();
			el._autocomplete._sendtimeout=null;
		},this),this._autocomplete._interval);
	}
}

//FSite2.extendAutocomplete = function (el,classcomplete,url,minChar,interval)
FSite2.extendAutocomplete = function (el,params)
{
	if (el._autocomplete)
		return;
	el._autocomplete = new Object();
	el._onfocus_auto=el.onfocus;
	el._onblur_auto=el.onblur;

	el._autocomplete._class=params.autoCompleteHelperClass;//classcomplete;
	var minChar = (params.autoCompleteMinChar)?(params.autoCompleteMinChar):0;
	el._autocomplete._minChar=minChar;
	
	el._autocomplete._interval=(params.autoCompleteInterval)?params.autoCompleteInterval:500;//interval;
	el._autocomplete._urlParams = new Object();
	el._selected=-1;
	el._autocomplete.returnFalse=false;
	el._autocomplete._stopRequest=false;

	el.autoCompleteClose = FSite2._autoCompleteClose;
	el.autoCompleteInitClose = FSite2._autoCompleteInitClose;
	el.autoCompleteCancelClose = FSite2._autoCompleteCancelClose;
	el.autoCompleteInitCancelClose = FSite2._autoCompleteInitCancelClose;
	el.autoCompleteBuildRequest = FSite2._autoCompleteBuildRequest;
	el.autoCompleteSendRequest = FSite2._autoCompleteSendRequest;
	el.autoCompleteOnKeyDown = FSite2._autoCompleteOnKeyDown;
	el.autoCompleteSetSroll = FSite2._autoCompleteSetSroll;
	el.autoCompleteOnClick = FSite2._autoCompleteOnClick;
	el.autoCompleteSetClassName = FSite2._autoCompleteSetClassName;
	el.autoCompleteSetUrl = FSite2._autoCompleteSetUrl;
	el.autoCompleteSetUrlParam = FSite2._autoCompleteSetUrlParam;
	if (typeof params.autoCompleteOnLoad == 'function')
		el.autoCompleteOnLoad = params.autoCompleteOnLoad;
	if (typeof params.autoCompleteOnCancel == 'function')
		el.autoCompleteOnCancel = params.autoCompleteOnCancel;
	
	el.autoCompleteSetUrl(params.autoCompleteUrl);
	if (params.autoCompleteUrlDefault)
	{
		for (var paramDef in params.autoCompleteUrlDefault)
			el._autocomplete._urlParams[paramDef]=params.autoCompleteUrlDefault[paramDef];
	}
	
	if (!el._autocomplete._div)
	{
		if (!el.id)
		{
			el._autocomplete._div=document.createElement('div');
			el.parentNode.appendChild(el._autocomplete._div,el);
		}
		else if (!el._autocomplete._div && document.getElementById(el.id+'_autocomplete'))
		{
			el._autocomplete._div = document.getElementById(el.id+'_autocomplete');
		}
		else if (!el._autocomplete._div)
		{
			el._autocomplete._div=document.createElement('div');
			el._autocomplete._div.id=el.id+'_autocomplete';
			el.parentNode.appendChild(el._autocomplete._div,el);
		} 
	}
	el.setAttribute('autocomplete','off');
	el._autocomplete._div.setAttribute('tabindex','');
	el._autocomplete._div._input=el; 
	el._autocomplete._div.className=el._autocomplete._class;
	el._autocomplete._div.style.display='none';
	el._autocomplete._div.style.visibility='hidden';
	el._autocomplete._div.style.overflow='auto';

	el.onkeydown = function (e)
	{ 
		if (e) event=e;
		
		this._autocomplete._keydownvalue=this.value;
		if (this._autocomplete._table)
		{
			return this.autoCompleteOnKeyDown(event);
		}
		else
		{
			if ((event.keyCode==38)||(event.keyCode==40))
			{
				if (this.value.length>=minChar)
						this.autoCompleteBuildRequest(true);
			}
		}
	}
	el.onkeyup = function (e)
	{
		if (e) event=e;
		if ((this._autocomplete._keydownvalue!=this.value)&&(event.keyCode!=9)&&(event.keyCode!=13))
		{
			if (this.value.length>=minChar)
				this.autoCompleteBuildRequest();
			else
				this.autoCompleteClose();
		}
		else if (this.value.length<minChar)
			this.autoCompleteClose();
		this._autocomplete._keydownvalue=null;
	}
	
	el.onkeypress = function (e)
	{
		if (e) event=e;
		if ((event.keyCode==13)&&(this._autocomplete.returnFalse))
		{
			this._autocomplete.returnFalse=false;
			event.cancelBubble = true;
			event.returnValue = false;
			if (event.preventDefault)
				event.preventDefault();
			return false;
		}
	}
	
	el.onfocus = function (ev)
	{
		if (!ev) ev = window.event;
		el.autoCompleteCancelClose();
		if(el._onfocus_auto) el._onfocus_auto(ev);
	}
	el.onblur = function (ev)
	{
		if (!ev) ev = window.event;
		el.autoCompleteInitClose();
		if (el._onblur_auto) el._onblur_auto(ev);
	}
	el._autocomplete._div.onclick = function ()
	{
		setTimeout(FSite2._callRef(el.autoCompleteOnClick,el),30);
	}
	el._autocomplete._div.onscroll = function(ev) {el.focus(); el.autoCompleteInitCancelClose();}
	el.autoCompleteSendRequest(-1);
	return el; 
}
