/**
* Filename................: autocomplete.js
* Project.................: web pages SDK
* Last Modified...........: $Date: 12/10/2005 01:46:39 $
* CVS Revision............: $Revision: 0.2.1 $
* Idea and Developed by...: Maxim Bulygin (sailormax@gmail.com)
* Require.................: lib.js
*/

var query_own_queue = new Array();

function getXMLObj()
{
	var res = null;
	try
		{ res = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e)
	{
		try
			{ res = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch(sc)
			{ res = null; }
	}
	
	if (!res && typeof(XMLHttpRequest) != "undefined")
		res = new XMLHttpRequest()
	return res;
}

function ac_AutoComplete(evt)
{
	if (typeof(evt) == "undefined") evt = window.event;
	var ac_field = document.getElementById(this.inputId);
	var is_ac_open = (document.getElementById(this.aclistId).style.visibility == "visible");
	if (is_ac_open && (evt.keyCode == 38))	//up
	{
		if (this.active_div > -1)
		{
//			if (!this.active_div && this.user_variant != "")
//				ac_field.value = this.user_variant;
			if (this.active_div >= 0)
				this.active_div--;
			this.RedrawList();
//			if ((this.active_div >= 0) && (ac_field.value.length < 7))
//				ac_field.value = this.values[this.active_div];
		}
	}
	else if (is_ac_open && (evt.keyCode == 40))	//down
	{
		if (this.active_div < (this.divs_cnt-1))
		{
			this.active_div++;
			this.RedrawList();
//			if (ac_field.value.length < 7)
//				ac_field.value = this.values[this.active_div];
		}
	}
	else if ((evt.keyCode == 27) || (evt.keyCode == 13))
	{
	}
	else if (ac_field.value != "")
	{
		if (this.timeOutId) window.clearTimeout(this.timeOutId);
		this.timeOutId = window.setTimeout(this.name+".SendRequest()", 300);
	}
	else if (ac_field.value == "")
	{
		if (this.timeOutId) window.clearTimeout(this.timeOutId);
		this.active_div = -1;
		document.getElementById("ac_container").style.visibility = "hidden";
	}
}

function ac_InsertString()
{
	var el = document.getElementById(this.inputId);
	var value = this.values[this.active_div];

	el.focus();
	var txt = el.value;

	if (this.last_start > 0) value = " " + value;

	var future_pos = this.last_start + value.length;
	if (el.setSelectionRange)
	{
		el.value = txt.substring(0, this.last_start) + value + el.value.substring(this.last_end, txt.length)
		el.setSelectionRange(future_pos, future_pos);
	}
	else
	{
		var rng = document.selection.createRange();
		rng.moveStart("character", 0-el.value.length);
		rng.moveEnd("character", 0-el.value.length);
		rng.moveStart("character", this.last_start);
		rng.moveEnd("character", this.last_end-this.last_start);
		rng.text = value;
	}
}

function ac_MouseClick(evt)
{
	if (typeof(evt) == "undefined") evt = window.event;
	if (evt.srcElement)
		obj = evt.srcElement.itsobj;
	else
		obj = evt.target.itsobj;

	if (obj.active_div >= 0)
	{
		document.getElementById(obj.aclistId).style.visibility = "hidden";
		obj.InsertString();
	}
}

function ac_RedrawList()
{
	for (i=0; i<this.divs_cnt; i++)
		if (i === this.active_div)
		{
			this.divs[i].style.backgroundColor = this.selBg;
			this.divs[i].childNodes[1].style.color = this.selColor;
			this.divs[i].childNodes[2].style.color = this.selColor;
		}
		else
		{
			this.divs[i].style.backgroundColor = this.defBg;
			this.divs[i].childNodes[1].style.color = this.defColor;
			this.divs[i].childNodes[2].style.color = this.defColor;
		}
}

function ac_MouseOver(evt)
{
	if (typeof(evt) == "undefined") evt = window.event;
	if (evt.srcElement)
		obj = evt.srcElement.itsobj;
	else
		obj = evt.target.itsobj;

	for (i=0; i<obj.divs_cnt; i++)
		if (obj.divs[i].childNodes[0] === this)
		{
			obj.active_div = i;
			break;
		}
	obj.RedrawList();
}

function ac_ProcessReqChange()
{
	if (typeof(this.divs) == "undefined")
	{
		if (obj = query_own_queue.shift())
			obj.ProcessReqChange();
		return;
	}

	/*
		this.req.readyState:
		0 = uninitialized
		1 = loading
		2 = loaded
		3 = interactive
		4 = complete
	*/
	if (this.req.readyState == 4 && !this.recieve)
	{
		this.recieve = true;
		if (this.req.status == 200)
		{
			var container = document.getElementById(this.aclistId);
			if (this.req.responseText)
			{
				this.user_variant = document.getElementById(this.inputId).value;
				container.style.visibility = "hidden";
				this.active_div = -1;

				rows = this.req.responseText.split("\n");
				len = (this.divs.length < rows.length ? this.divs.length : rows.length);
				for (i=0; i<len; i++)
				{
					if (rows[i] == "")
					{
						len = i;
						break;
					}

					cols = rows[i].split("\t");
					if (typeof(cols[2]) == "undefined") cols[2] = "";
					this.values[i] = cols[0];

					if (!this.divs[i])
					{
						this.divs_cnt++;
						this.divs[i] = document.createElement("DIV");
						setStyle(this.divs[i], "width:" + this.maxWidth + "; height:14px;");

							var overDiv = document.createElement("IMG");
								overDiv.src = this.empty_img;
								setStyle(overDiv, "position:absolute; left:0; width:100%; height:14px;");
								overDiv.itsobj = this;
								overDiv.onmouseover = ac_MouseOver;
								overDiv.onclick = ac_MouseClick;
							this.divs[i].appendChild(overDiv);

							var leftSpan = document.createElement("SPAN");
								setStyle(leftSpan, "float:left; height:14px; overflow:hidden; padding:0 3px 0 3px; color:" + this.defColor + "; cursor:default;");
								leftSpan.innerHTML = cols[1];
							this.divs[i].appendChild(leftSpan);

							var rightSpan = document.createElement("SPAN");
								setStyle(rightSpan, "float:right; height:14px; overflow:hidden; padding:0 3px 0 3px; color:" + this.defColor + "; cursor:default;");
								if (cols[2] == "")
									setStyle(rightSpan, "display:none;");
								rightSpan.innerHTML = cols[2];
							this.divs[i].appendChild(rightSpan);

						container.appendChild(this.divs[i]);
					}
					else
					{
						spans = this.divs[i].getElementsByTagName("SPAN");

						spans[0].innerHTML = cols[1];
						spans[0].style.color = this.defColor;

						spans[1].innerHTML = cols[2];
						spans[1].style.color = this.defColor;

						this.divs[i].style.display = "block";
					}
					this.divs[i].style.backgroundColor = this.defBg;
				}

				for (i=len; i<this.divs_cnt; i++)
					this.divs[i].style.display = "none";
				this.divs_cnt = len;

				container.style.visibility = "visible";
			}
			else
			{
				this.active_div = -1;
				container.style.visibility = "hidden";
			}
		}
		else
		{
			if (!this.timeOutId)
				this.timeOutId = window.setTimeOut(this.name+".SendRequest()", 300);
		}
	}
	else
	{
		query_own_queue.unshift(this);
	}
}

function ac_SendRequest()
{
	var el = document.getElementById(this.inputId);
	this.recieve = false;

	if (this.req && this.req.readyState != 0)
		this.req.abort();
	this.req = getXMLObj();
	if (this.req)
	{
		var txt = el.value;

		var start = end = GetCaretPosition(el);
		if (txt.substr(start,1) == ",") start--;
		for (start; start>0; start--)
			if (txt.substr(start,1) == ",") break;
		for (end; end<txt.length; end++)
			if (txt.substr(end,1) == ",") break;

		if (start) start++;
		this.last_start = start;
		this.last_end = end;
		data = txt.substring(start, end);

		query_own_queue.push(this);
		this.req.onreadystatechange = ac_ProcessReqChange;
//		this.req.open("GET", "{func:URL2PAGE('getxml', 'ac_phonenum')}" + "?rand="+Math.random()+"&amp;data=" + data, true);
		this.req.open("GET", this.url + data + "&rand="+Math.random(), true);
		this.req.send(null);
	}
	this.timeOutId = 0;
}

function ac_KeyDown(evt, num)
{
	if (typeof(evt) == "undefined") evt = window.event;

	if ((evt.keyCode == 38) || (evt.keyCode == 40) || (evt.keyCode == 27) || (evt.keyCode == 13) || (evt.keyCode == 9))
	{
		if (evt.keyCode == 27)
		{
			this.active_div = -1;
/*
			if (this.user_variant != "")
			{
				document.getElementById(this.inputId).value = this.user_variant;
				this.user_variant = "";
			}
*/
			document.getElementById(this.aclistId).style.visibility = "hidden";
		}
		else if (evt.keyCode == 13)
		{
			if (this.active_div >= 0)
			{
				this.InsertString();
				this.active_div = -1;
				document.getElementById(this.aclistId).style.visibility = "hidden";
			}
			else if ((num==1) && this.formId)
				document.getElementById(this.formId).submit();
		}

		if (evt.keyCode == 9)
		{
			if (this.active_div >= 0)
				this.InsertString();
			this.active_div = -1;
			document.getElementById(this.aclistId).style.visibility = "hidden";
		}
		else
		{
			if (evt.preventDefault)
				evt.preventDefault();
			else
			{
				evt.cancelBubble = true;
				evt.returnValue = false;
				return false;
			}
		}
	}
}



function cAutoComplete(thisName, inputId, aclistId, p_url, p_maxRows, pmaxWidth, p_formId, p_defColor, p_defBg, p_selColor, p_selBg)
{
	this.timeOutId	= 0;

	this.empty_img	= "/direct/images/empty.gif";

	this.name			= thisName;
	this.inputId		= inputId;
	this.aclistId		= aclistId;
	this.url			= p_url;
	this.maxRows		= p_maxRows;
	if (typeof(p_maxWidth) != "undefined") this.maxWidth = p_maxWidth;
	else this.maxWidth	= "170px";
	if (typeof(p_formId) != "undefined") this.formId = p_formId;
	else this.formId	= "";
	if (typeof(p_defColor) != "undefined") this.defColor = p_defColor;
	else this.defColor	= "black";
	if (typeof(p_defBg) != "undefined")	this.defBg = p_defBg;
	else this.defBg		= "white";
	if (typeof(p_selColor) != "undefined") this.selColor = p_selColor;
	else this.selColor	= "white";
	if (typeof(p_selBg) != "undefined") this.selBg = p_selBg;
	else this.selBg		= "#0067CE";

	this.divs			= new Array(this.maxRows);
	this.values			= new Array(this.maxRows);
	this.divs_cnt		= 0;
	this.active_div		= -1;
	this.user_variant	= "";
	this.last_start		= 0;
	this.last_end		= 0;

	this.req			= "";
	this.recieve		= false;

	this.InsertString		= ac_InsertString;
//	this.MouseClick			= ac_MouseClick;
	this.RedrawList			= ac_RedrawList;
//	this.MouseOver			= ac_MouseOver;
	this.ProcessReqChange	= ac_ProcessReqChange;
	this.SendRequest		= ac_SendRequest;

	this.KeyDown			= ac_KeyDown;
	this.AutoComplete		= ac_AutoComplete;

//	this.input.onkeyup		= this.autocomplete;
//	this.input.onkeydown	= this.ac_keydown;
//	this.input.onkeypress	= this.ac_keydown;
}

