var __picktitle = "enter one or more letter of the company's name";

function Picktext(element)
{
	this.selection = null;
	this.count = 0;
	this.node = document.createElement('div');
	this.node.style.padding = "0px";
	this.node.style.margin = "0px";
	
	this.pid = document.createElement('input');
	this.pid.type = 'hidden';
	this.pid.name = 'companyId';
	this.pid.value = 0;
	this.node.appendChild(this.pid);
	this.text = document.createElement('input');
	this.text.type = 'text';
	this.text.title = __picktitle;
	this.text.name = 'companyName';
	this.text.style.width = Picktext.width + "px";
	this.text.style.padding = "0px";
	this.text.style.margin = "0px";
	this.text.style.height = "18px";
	// this.text.style.position = 'absolute';
	// this.text.style.top = '0px';
	// this.text.style.left = '0px';
	this.node.appendChild(this.text);
	this.pick = document.createElement('img');
	this.pick.src = 'images/down.gif';
	this.pick.style.position = 'absolute';
	this.pick.style.top = "2px";
	this.pick.style.left = (Picktext.width - 14) + "px";
	this.pick.style.visibility = 'hidden';
	this.node.appendChild(this.pick);
	element.appendChild(this.node);
	this.list = document.createElement('div');
	this.list.className = 'picklist';
/*
	this.list.style.position = 'absolute';
	this.list.style.visibility = 'hidden';
	this.list.style.left = '0px';
	this.list.style.top = '21px';
	this.list.style.borderLeftWidth = "1px";
	this.list.style.borderTopWidth = "1px";
	this.list.style.borderRightWidth = "1px";
	this.list.style.borderBottomWidth = "1px";
	this.list.style.borderLeftColor = "#666666";
	this.list.style.borderTopColor = "#666666";
	this.list.style.borderRightColor = "#222222";
	this.list.style.borderBottomColor = "#222222";
	this.list.style.borderStyle = "solid";
	this.list.style.backgroundColor = '#ffffff';
	this.list.style.fontSize= '13px';
	this.list.style.lineHeight = '15px';
	this.list.style.zindex = 450;
*/
	
	this.list.style.left = (250 + element.offsetLeft) + "px";
	this.list.style.top = (element.offsetTop + element.offsetHeight) + "px";
	this.list.style.width = (Picktext.width + 2) + 'px';
	this.list.onmouseover = Picktext.isin;
	this.list.onmouseout = Picktext.isout;
	document.body.appendChild(this.list);

	this.table = document.createElement('table');
	this.table.style.width = '100%';
	this.list.appendChild(this.table);
	this.body = document.createElement('tbody');
	this.table.appendChild(this.body);

	this.text.picktext = this;
	this.text.onkeyup = Picktext.keyup;

	this.pick.picktext = this;
	this.pick.onmousedown = Picktext.mousedown;
}

Picktext.width = 250;
Picktext.words = new Array();
Picktext.selColor = '#444488';
Picktext.maxHeight = 310;

Picktext.isin = function()
{
	Picktext.out = false;
}

Picktext.isout = function()
{
	try
	{
		Picktext.out = true;
	}
	catch(x) {}
}

Picktext.keyup = function(evnt)
{
	if(!evnt)
	{
		evnt = window.event;
	}
	this.picktext.keyup(evnt);
}

Picktext.mousedown = function(evnt)
{
	if(!evnt)
	{
		evnt = window.event;
	}
	this.picktext.mousedown(evnt);
}

Picktext.xhide = function()
{
	if(!Picktext.out)
	{
		return;
	}
	Picktext.hide();
}

Picktext.hide = function()
{
	document.body.onmousedown = Picktext.bodydown;
	window.onmousedown = Picktext.windwodown;
	if(Picktext.curitem)
	{
		Picktext.curitem.list.style.visibility = 'hidden';
		Picktext.curitem = null;
	}
}

Picktext.prototype.show = function()
{
	this.mousedown(null);
}

Picktext.prototype.keyup = function(evnt)
{
	switch(evnt.keyCode)
	{
		case 38:	// up
			if(this.curitem)
			{
				if(this.curitem.previousSibling)
				{
					this.select(this.curitem.previousSibling);
					return;
				}
			}
			else
			{
				this.select(this.body.lastChild.firstChild);
			}
		case 40:	// down
			if(this.curitem)
			{
alert("got cur");
				if(this.curitem.nextSibling)
				{
					this.select(this.curitem.nextSibling);
					return;
				}
			}
			else
			{
// try{alert("take first");}catch(x){}
				this.select(this.body.firstChild.firstChild);
			}
	}
	var value = this.text.value;
	this.clear();
	if(value)
	{
		this.setWord(value);
		//add(value);
	}
}

Picktext.prototype.setSelection = function()
{
	var value = this.text.value;
	this.deselect();
	var p = this.body.firstChild;
	var pattern = new RegExp("^" + value + "$", "i");
	while(p)
	{
		if(p.firstChild.innerHTML.match(pattern))
		{
			this.select(p.firstChild);
			break;
		}
		p = p.nextSibling;
	}
}

Picktext.prototype.setWord = function(txt)
{
	var start = txt.substr(0, 1);
	var words = Picktext.words[start];
	if(words)
	{
		this.wordloaded(txt);
	}
	else
	{
		new ajax("action.php?version=" + __version + "&start=" + txt, null);
		// __getwords(txt, this);
	}
}

Picktext.prototype.wordloaded = function()
{
	var txt = this.text.value;
	var pattern = new RegExp("^" + txt, "i");
	this.clear();
	var words = Picktext.words[txt.substr(0, 1)];
	for(var i = 0; i < words.length; i++)
	{
		var a = words[i].split("\t");
		if(a[1].match(pattern))
		{
			this.add(a[0], a[1]);
		}
	}
	if(this.count)
	{
		this.show();
	}
}

Picktext.prototype.select = function(td)
{
//alert("select " + td);
	if(this.selection)
	{
		this.deselect();
	}
	td.style.backgroundColor = Picktext.selColor;
	td.style.color = '#ffffff';
	this.selection = td;
}

Picktext.prototype.deselect = function()
{
	if(this.selection)
	{
		this.selection.style.backgroundColor = '#ffffff';
		this.selection.style.color = '#000000';
		this.selection = null;
	}
}

Picktext.prototype.mousedown = function(evnt)
{
	if(evnt)
	{
		evnt.cancelBubble = true;
	}
	this.setSelection();
	this.list.style.visibility = 'visible';
	Picktext.bodydown = document.body.onmousedown;
	Picktext.windowdown = window.onmousedown;
	Picktext.curitem = this;
	document.body.onmousedown = Picktext.xhide;
	window.onmousedown = Picktext.xhide;
}

Picktext.prototype.clear = function()
{
	while(this.body.firstChild)
	{
		this.body.removeChild(this.body.firstChild);
	}
	this.table.style.width = '100%';
	this.list.style.height = '';
	this.pick.style.visibility = 'hidden';
	this.count = 0;
}

Picktext.onover = function()
{
	this.picktext.deselect();
	this.style.backgroundColor = Picktext.selColor;
	this.style.color = '#ffffff';
}

Picktext.onout = function()
{
	this.style.backgroundColor = '#ffffff';
	this.style.color = '#000000';
}

Picktext.click= function(evnt)
{
	if(!evnt)
	{
		evnt = window.event;
	}
	evnt.cancelBubble = true;
	this.picktext.click(this);
}

Picktext.prototype.click = function(td)
{
	this.text.value = td.innerHTML;
	this.pid.value = td.id;
	Picktext.hide();
	document.cpn.submit();
}

Picktext.prototype.add = function(id, text)
{
	var tr = document.createElement('tr');
	var td = document.createElement('td');
	tr.appendChild(td);
	this.body.appendChild(tr);
	td.innerHTML = text;
	td.id = id;
	td.onmouseover = Picktext.onover;
	td.onmouseout = Picktext.onout;
	td.onmousedown = Picktext.click;
	td.picktext = this;
	if(this.list.offsetHeight > Picktext.maxHeight)
	{
		this.list.style.height = Picktext.maxHeight + "px";
		this.list.style.overflow = "auto";
		this.table.style.width = (this.list.offsetWidth - 18) + "px";
	}
	this.pick.style.visibility = 'visible';
	this.count++;
}

var pt = null;

Picktext.init = function()
{
	var elem = document.getElementById('navcname');
	if(elem)
	{
		pt = new Picktext(elem);
	}
}
window.inits['picktext'] = Picktext.init;

