/************************************************
** 描 述:	组合筐
** 版 本:	1.0
************************************************/
function combo(comboname,width,obj,enable,originvalue)
{
	if(comboname)	{
			var s='window.'+comboname+'=this';
			eval(s);
		}else	{
			return; 
		}
	var disb = "";
	if(!enable)
		disb = "disabled";
	this.enable = enable;
	this.width = width;
	this.cname = comboname;
	this._combocur=null;
	var combodiv = '<div style="width:'+width+'px;" class="combobox"></div>';
	var comboinput = '<div style="height:17;border-style:inset;border-width:2px;"><input style="border:0;" '+disb+' type="text" name="'+this.cname+'" value="'+originvalue+'" onkeydown="'+this.cname+'.dokeydown(this);" onclick="'+this.cname+'.showdown(this);"/><button '+disb+' style="" hidefocus="true" onclick="'+this.cname+'.showdown(this.previousSibling)">6</button><div>';
	
	var comboborder = '<div style="position:absolute;z-index:100;display:none;width:100%;height:100;background:white;border:1 solid dimgray;filter:progid:DXImageTransform.Microsoft.Shadow(color=#666666, Direction=135, Strength=3);"></div>';
	var combotbl = '<TABLE border="0" width="100%" onclick="'+this.cname+'.doclick(this);" onmouseover="'+this.cname+'.domouseover();" onmouseout="'+this.cname+'.domouseout();"></TABLE>';
	obj.insertAdjacentHTML("beforeEnd",combodiv);
	var ch = obj.childNodes(obj.childNodes.length-1);
	ch.insertAdjacentHTML("beforeEnd",comboinput);
	ch.insertAdjacentHTML("beforeEnd",comboborder);
	ch = ch.childNodes(ch.childNodes.length-1);
	this.dropdown = ch;
	ch.insertAdjacentHTML("beforeEnd",combotbl);
	ch = ch.childNodes(ch.childNodes.length-1);

	this.tbl = ch;//表格
}
combo.prototype.dokeydown=function(ob)//用户输入事件捕捉
{
	this.dropdown.style.display = "block";
	var tmp = -1;
	var tbl = this.tbl;
	var obv = ob.value;
	

	switch(event.keyCode)
	{
		case 38:
			for(var i=0;i<tbl.rows.length;i++)
			{	
				if(tbl.rows[i].className == "selected")
				{
					tmp = 1;
					if(i==0)
						return;
					else
					{
						tbl.rows[i].className = "";
						tbl.rows[i-1].className = "selected";
						break;
					}
				}
			}
			if(tmp==-1)
				tbl.rows[tbl.rows.length-1].className = "selected";
			break;
		case 40:
			for(var i=0;i<tbl.rows.length;i++)
			{
				if(tbl.rows[i].className == "selected")
				{
					tmp = 1;
					if(i==tbl.rows.length-1)
						return;
					else
					{
						tbl.rows[i].className = "";
						tbl.rows[i+1].className = "selected";
						break;
					}
				}
			}
			if(tmp==-1)
				tbl.rows[0].className = "selected";
			break;
		default:
			window.setTimeout(this.cname+".showcur()",100);
			this._combocur = ob;
	}


	if(event.keyCode == 13)
	{
		for(var i=0;i<tbl.rows.length;i++)
		{
			if(tbl.rows[i].className == "selected")
			{
				ob.value = tbl.rows[i].cells[0].innerText
			}
		}
		this.dropdown.style.display = "none";
	}
}
combo.prototype.showcur=function()//显示当前建议项
{
	var obv = this._combocur.value;
	var tbl = this.tbl;
	for(var i=0;i<tbl.rows.length;i++)
	{	
		var l = obv.length;
		if(l==0) return;
		for(var m=0;m<tbl.rows.length;m++)
		{	
			tbl.rows[m].className = "";
		}
		if(tbl.rows[i].cells[0].innerText.substr(0,l)==obv)
		{
			tbl.rows[i].className = "selected";
			cur = i;
			break;
		}
	}
}
combo.prototype.domouseover=function()
{
	window.event.cancelBubble = true;
	var ob = event.srcElement;
	if(ob.tagName=="LABEL")
	{
		ob.parentElement.parentElement.className = "selected";
	}
}
combo.prototype.domouseout=function()
{
	window.event.cancelBubble = true;
	var ob = event.srcElement;
	if(ob.tagName=="LABEL")
	{
		ob.parentElement.parentElement.className = "none";
	}
}
combo.prototype.doclick=function(obclik)//用户点击建议项
{
	var ob = event.srcElement;
	if(ob.tagName=="LABEL")
	{
		this.dropdown.style.display = "none";
		obclik.parentElement.previousSibling.children[0].value = ob.innerText;
		this.comboclick(ob.innerText,ob.parentElement.getAttribute("val"));
	}
}
combo.prototype.showdown=function(ob)
{
	ob.select();
	this.dropdown.style.display = (this.dropdown.style.display=="block")?"none":"block";
}
combo.prototype.addline=function(str,val)//增加行
{
	var ob = this.tbl;
	var row = ob.insertRow();
	var cell = row.insertCell();
	cell.innerHTML = '<label style="width:'+(this.width-20)+';overflow:hidden;white-space:nowrap;text-overflow:ellipsis" title="'+str+'">'+str+'</label>';
	cell.setAttribute("val",val);
}
combo.prototype.comboclick=function(str,val)
{}