

function tag_complete(eid,bid,output_)
{
	this.last_key=0;
	this.id=eid;
	this.input = document.getElementById(eid);
	//alert(bid);
	this.bshow = document.getElementById(bid);
	this.bclass_hide='hidden_tag_help';
	this.bclass_show='showed_tag_help';
	this.binshow=document.getElementById(output_);
	this.sel_row=0;
	this.is_selection=false;
	
	this.n_destroy = function ()
	{
		this.clear_help();
		this.bshow.className=this.bclass_hide;
	}
	
	this.clear_help = function ()
	{
		this.last_key=0;
		this.is_selection=false;
		this.sel_row=0;
		this.bshow.className=this.bclass_hide;
		while(this.bshow.childNodes.length)
		{
			for(ind in this.bshow.childNodes)
			{
			  try
			  {
				this.bshow.removeChild(this.bshow.childNodes[ind]);
			  }
			  catch(e)
			  {
				continue;
			  }
			}
		}
	}
	
	this.move_select = function (where)
	{
		if(where=='down')
		{
			if(!this.is_selection) 
			{
				this.sel_row=0;
				nrow=0;
			}else nrow=this.sel_row+1;
			
			try
			{
				//if(nrow!=0) alert(nrow);
				this.bshow.childNodes[nrow].className='output_in_selected';
				if(nrow>0) this.bshow.childNodes[this.sel_row].className='output_in';
				this.sel_row=nrow;
				this.is_selection=true;
			}
			catch(e)
			{
				
				try
				{
					this.bshow.childNodes[0].className='output_in_selected';
					if(this.sel_row) this.bshow.childNodes[this.sel_row].className='output_in';
					this.sel_row=0;
					this.is_selection=true;
				}
				catch(e)
				{
					this.is_selection=false;
				}
			}
		}
		if(where=='up')
		{
			if(this.sel_row==0)  
			{
				nrow=this.bshow.childNodes.length-1;
			}else
				nrow=this.sel_row-1;
			
			try
			{
				this.bshow.childNodes[nrow].className='output_in_selected';
				this.bshow.childNodes[this.sel_row].className='output_in';
				this.sel_row=nrow;
				this.is_selection=true;
			}
			catch(e)
			{
				this.is_selection=false;
			}
		}
	}
	
	this.complete = function (txt)
	{
		nval=this.input.value;
		lind=nval.lastIndexOf(',');
		if(lind!=false) nval=nval.substr(0,lind+1);
		else nval='';
		this.input.value=nval+txt+',';
		this.tag_complete_go();
	}
	
	this.onenter = function ()
	{
		if(this.is_selection)
		{
			try
			{
				txt_compl=this.bshow.childNodes[this.sel_row].textContent;
				if(!txt_compl)txt_compl=this.bshow.childNodes[this.sel_row].innerText;
			}catch(e)
			{
				txt_compl=this.bshow.childNodes[this.sel_row].innerText;
			}
			
			try
			{
				this.complete(txt_compl);
			}catch(e)
			{
				
			}
		}
		return false;
	}
	
	this.listener = function(ev_id)
	{
		tg_ev = (ev_id) ? ev_id : event;
		tg=(ev_id.target)?ev_id.target:ev_id.srcElement;
		
		
		code=tg_ev.keyCode;
		//alert(code);
		if(code==40||code==38)
		{
			where=(code==40)?'down':'up';
			this.move_select(where);
		}else if(code==13)
		{
			return this.onenter();
		}
		else
		{
			this.last_key=code;
			this.bshow.className=this.bclass_hide;
			setTimeout('tag_compliter_docomp(\''+tg.id+'\',\''+code+'\');', 250);
		}
	}

	this.tag_complete = function(elid,ccode)
	{
		if(this.last_key!=ccode) return false;
		this.tag_complete_go();
	}
	
	
	this.tag_complete_go = function()
	{
		if(this.input.value.length<2) return false;
		ttt=escape(this.input.value);
		turl='/tags.html?mid='+this.id+'&start='+ttt;
		this.get_resource(turl);
	}

	
	this.get_resource=function(url)
	{
		req = false;
		if(window.XMLHttpRequest)
		{
			try
			{
				req = new XMLHttpRequest();
			}
			catch(e)
			{
				req = false;
			}
		}
		else if(window.ActiveXObject)
		{
			try
			{
				req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e)
				{
					req = false;
				}
			}
		}

		if(req)
		{
			req.onreadystatechange = this.read_resource;
			//alert(url);
			req.open("GET", url, true);
			req.send("");
		}
	}
	
	this.read_resource = function()
	{
		if (req.readyState == 4)
		{
        	if (req.status == 200)
			{
				txt=req.responseText;
				txts=txt.split("\n");
				mid=txts[0];
				txts[0]=false;
				tag_compliter[mid].show_help(txts);
        	}
			else
			{
				alert("Error:"+req.statusText);
			}
	    }
	}
	
	this.show_help = function (txts)
	{
		this.clear_help();
		this.bshow.className=this.bclass_show;
		this.bshow.style.width=this.input.clientWidth+'px';
		was=0;
		for(i=0;i<txts.length;i++)
		{
			if(txts[i].length<1) continue;
			if(!txts[i]) continue;
			if(txts[i]=='false') continue;
			var n_el = this.binshow.cloneNode(true);
			if(this.bshow.childNodes.length) this.bshow.insertBefore(n_el,this.bshow.childNodes[this.bshow.childNodes.length-1]);
			else
			{
				try
				{
					this.bshow.insertBefore(n_el)
				}
				catch(e)
				{
					this.bshow.insertBefore(n_el,this.bshow.childNodes[0]);
				}
			}
			n_el.style.display='block';
			n_el.innerHTML=txts[i];
			was++;
		}
		if(was<1) 
		{
			this.clear_help();
		}
		
	}
	
}



var tag_compliter=new Array();
var compliter_eid=new Array();
/*var compliter_oid=false;
var compliter_output_in=false;*/
function tag_compliter_init(eid,oid,output_in)
{
	compliter_eid[eid]=new Array();
	compliter_eid[eid][0]=oid;
	compliter_eid[eid][1]=output_in;
	//alert(eid+'-'+compliter_eid[eid][0]+'-'+compliter_eid[eid][1]);
	tag_compliter_add_event(document.getElementById(eid), "keydown", function (eievent) { tag_compliter_listener(eievent);});
	tag_compliter_add_event(document.getElementById(eid), "change", function (eievent) { tag_compliter_desctroyer(eievent);});
	//alert(document.activeElement);
}

function tag_compliter_listener(eievent)
{
	
	tg_ev = (eievent) ? eievent : event;
	tg=(eievent.target)?eievent.target:eievent.srcElement;
	//alert(tg.id+'-'+compliter_eid[tg.id][0]+'-'+compliter_eid[tg.id][1]);
	if(!tag_compliter[tg.id]) tag_compliter[tg.id] = new tag_complete(tg.id,compliter_eid[tg.id][0],compliter_eid[tg.id][1]);
	tag_compliter[tg.id].listener(eievent);
}

function tag_compliter_docomp(elid,ccode)
{
	if(tag_compliter[elid])
	{
		tag_compliter[elid].tag_complete(elid,ccode);
	}
}

function tag_compliter_desctroyer(eievent)
{
	tg=(eievent.target)?eievent.target:eievent.srcElement;
	if(tag_compliter[tg.id])
	{
		tag_compliter[tg.id].n_destroy();
	}
	tag_compliter[tg.id]=false;
}

function tag_compliter_add_event (el, event, func) {
	if (el.addEventListener) 
	{
		el.addEventListener(event, func, false);
	} 
	else if (el.attachEvent) 
	{
		mfunc = function() {
			func(window.event);
		}
		el.attachEvent("on" + event, mfunc);
	}
	return true;
}






