function AutoMultiSelect(name,objId,objrId)
{
	this.name = name;
	this.objId = objId;
	this.obj;
	this.objResponseId = objrId;
	this.objResponse;
	
	this.Init = function()
	{		
		this.obj = this.GetObject(this.objId);
		this.objResponse = this.GetObject(this.objResponseId);				
		var f1 = this.name+".Display()";
		AddEvent(this.obj,"click",function(){
			eval(f1);		
		},false);			
	}
	
	this.Display = function()
	{				
		if(this.objResponse.style.display == "none")
			this.ResponseShow();
		else
			this.ResponseHide();
	}
	
	this.ResponseHide = function()
	{
		this.objResponse.style.display = "none";	
		this.obj.className = "atrtn bg";
	}
	
	this.ResponseShow = function()
	{
		this.objResponse.style.display = "block";	
		this.obj.className = "atrtn bg2";
	}	
	
	this.GetObject = function(id)
	{
		return document.getElementById(id);
	}	
	
	this.Init();
}

function AutoComplete(name,objId,objhdnId,urlResponse)
{
	this.name = name;
	this.objId = objId;
	this.obj;
	this.garbage = Array();
	this.objResponse;
	this.urlResponse = urlResponse;
	this.objSelected;
	this.objCurrentSelected = null;
	this.cache = Array();
	
	this.Init = function()
	{		
		this.obj = this.GetObject(this.objId);
		if(this.obj == null)
			return;
		this.obj.setAttribute("autocomplete","off");
		this.objResponse = this.GetObject("div"+this.objId);
		this.objResponse.setAttribute('class','ac_results')
		this.objResponse.setAttribute('className','ac_results')
		this.objSelected = this.GetObject(objhdnId);		
		this.ResponseHide();
		
		var f1 = this.name+".KeyUp(evt)";
		AddEvent(this.obj,"keyup",function(e){
			var evt = window.event || e;
			eval(f1);		
		},false);	
		
		var f2 = this.name+".ResponseHide()";
		AddEvent(document,"click",function(){
			eval(f2);		
		},false);			
	}
		
	this.KeyUp = function(e)
	{		
		if(e && e.keyCode == 40)
		{
			this.MoveDown();
			return;
		}	
		
		if(e && e.keyCode == 38)
		{
			this.MoveUp();
			return;
		}	

		if(e && e.keyCode == 13 && this.objCurrentSelected != null && this.objCurrentSelected != undefined)
		{
			this.SelectItem(this.objCurrentSelected.id);
			return;
		}		
		 
		if(this.obj.value.length == 0)
		{
			this.ResponseHide();
			return;
		}
		
		this.Process();	
	}
	
	this.Process = function()
	{
		this.objCurrentSelected = null;
		var content = this.FindInCache(this.obj.value);
		if(content.length > 0)
		{
			this.Render(null,content);
			return;
		}

		var url = this.urlResponse +"&search="+encodeURIComponent(this.obj.value);
		var http_request = MakeRequest(url);
		
		if(http_request != null)
		{
			var runFunName = this.name+".Render(http_request,null)";
			http_request.onreadystatechange = function(){eval(runFunName);};
			http_request.open('GET', url, true);
			http_request.send(null);		
		}		
	}
	
	this.MoveDown = function()
	{		
		if(this.objResponse.style.display == "block")
		{
			if(this.objCurrentSelected != null && this.objCurrentSelected != undefined)
			{
				if(this.objCurrentSelected.nextSibling == null)
				{
					return;
				}
				this.objCurrentSelected.setAttribute('class','ac_out');	
				this.objCurrentSelected.setAttribute('className','ac_out');					
				this.objCurrentSelected = this.objCurrentSelected.nextSibling;					
				this.objCurrentSelected.setAttribute('class','ac_over');
				this.objCurrentSelected.setAttribute('className','ac_over');
				this.obj.value = this.objCurrentSelected.innerHTML;
				this.objSelected.value = this.objCurrentSelected.getAttribute('id').replace("p_","");
			}
			else
			{
				var ul = this.objResponse.childNodes[0];
				if(ul != null && ul != undefined && ul.tagName == "UL");
				{
					var li = ul.childNodes[0];					
					li.setAttribute('class','ac_over');	
					li.setAttribute('className','ac_over');					
					this.objCurrentSelected = li;
					this.obj.value = li.innerHTML;
					this.objSelected.value = li.getAttribute('id').replace("p_","");
				}
			}
		}
	}
	
	this.MoveUp = function()
	{		
		if(this.objResponse.style.display == "block")
		{
			if(this.objCurrentSelected != null && this.objCurrentSelected != undefined)
			{
				if(this.objCurrentSelected.previousSibling == null)
				{
					return;
				}
				this.objCurrentSelected.setAttribute('class','ac_out');	
				this.objCurrentSelected.setAttribute('className','ac_out');					
				this.objCurrentSelected = this.objCurrentSelected.previousSibling;					
				this.objCurrentSelected.setAttribute('class','ac_over');
				this.objCurrentSelected.setAttribute('className','ac_over');
				this.obj.value = this.objCurrentSelected.innerHTML;
				this.objSelected.value = this.objCurrentSelected.getAttribute('id').replace("p_","");
			}
		}
	}	
	
	this.GetObject = function(id)
	{
		return document.getElementById(id);
	}
	
	this.Render = function(http_request,content)
	{
		this.objResponse.innerHTML = "";
		if(content == null || content.lenght == 0)
		{
			if (http_request.readyState == 4) 
			{
				if (http_request.status == 200) 
				{
					content = http_request.responseText;	
					if(content != null || content.length != 0)		
					{
						var co = new CacheObject();
						co.key = this.obj.value;
						co.value = content;
						this.cache.push(co);
					}
				} 
				else {}
			}
		}
		
		if(content == null || content.length == 0)
		{			
			this.ResponseHide();
			return;
		}
		var resp = content.split("#");
		var ul = document.createElement('ul');
		
		for(var i = 0; i < resp.length;i++)
		{
			var row = resp[i].split("|");
			var rowid = 'p_'+row[0];
			var rowname = row[1];
			var li = document.createElement('li');
			li.setAttribute('id',rowid);	
			li.setAttribute('name',rowid);				
			li.innerHTML = rowname;				
			AddEvent(li,"click",new Function(this.name+'.SelectItem("'+rowid+'");'),false);	
			AddEvent(li,"mouseover",new Function(this.name+'.OverItem("'+rowid+'");'),false);	
			AddEvent(li,"mouseout",new Function(this.name+'.OutItem("'+rowid+'");'),false);
			ul.appendChild(li);	
			//this.garbage.push(li);
		}
		
		this.objResponse.appendChild(ul);		
		this.ResponseShow();			
	}
	
	this.FindInCache = function(findKey)
	{
		for(var i = 0; i < this.cache.length;i++)
		{
			var co = this.cache[i];
			if(findKey == co.key)
				return co.value;
		}
		return "";
	}
	
	this.SelectItem = function(itemId)
	{
		var item = this.GetObject(itemId);
		this.obj.value = item.innerHTML;
		this.objSelected.value = item.getAttribute('id').replace("p_","");
		this.ResponseHide();
		//this.CleanUp();
	}
	
	this.OverItem = function (itemId)
	{		
		var item = this.GetObject(itemId);		
		item.setAttribute('class','ac_over');
		item.setAttribute('className','ac_over');
		this.obj.value = item.innerHTML;		
	}	
	
	this.OutItem = function (itemId)
	{	
		var item = this.GetObject(itemId);		
		item.setAttribute('class','ac_out');
		item.setAttribute('className','ac_out');		
	}
	
	this.ResponseHide = function()
	{
		this.objResponse.innerHTML = "";
		this.objResponse.style.display = "none";	
	}
	
	this.ResponseShow = function()
	{
		this.objResponse.style.display = "block";	
	}
	
	this.CleanUp = function()
	{
		for(var i = 0; i < this.garbage.length;i++)
		{
			this.garbage[i] = null;
		}
		this.garbage.splice(0,this.garbage.length-1);
	}
	
	this.Init();
}

function CacheObject() {
  this.key = "";
  this.value = "";
}

