AutoComplete = function(input, suggestions)
{
	var timer;

	input.attachEvent("onkeydown", handleKeyDown);	
//	input.attachEvent("onkeyup", handleKeyUp);
//	input.attachEvent("onkeydown", handleArrowKeys);
//	input.attachEvent("onmouseup", handleMouseUp);
//	input.attachEvent("onmouseover", handleMouseOver);
	//function handleArrowKeysn(event)
	function handleKeyDown(event)
	{
		try
		{
			//if (event.keyCode == 9 || event.keyCode == 13) alert("event.keyCode = " + event.keyCode);
			if (event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13 && event.keyCode != 9)
			{
				//if (input.value.length == 0)
				if (input.value.length <= 1)				
					suggestions.innerHTML = "";

				if (timer)
					clearTimeout(timer);

				//if (input.value.length > 0)
				if (input.value.length >= 2)
					timer = setTimeout(reload, 400);
			}
			else if (event.keyCode == 13 || event.keyCode == 9 || event.keyCode == 40 || event.keyCode == 38)
			{
				var cursor = getCursor();
				
				var parent = suggestions.firstChild;


				if (cursor != -1 && (event.keyCode == 40 || event.keyCode == 38))
				{
					if (event.keyCode == 40)
					{
						if (cursor == parent.childNodes.length)
							parent.childNodes[0].style.backgroundColor = "#a3ceff";
						else if (cursor < parent.childNodes.length - 1)
						{
							parent.childNodes[cursor].style.backgroundColor = "";
							parent.childNodes[cursor + 1].style.backgroundColor = "#a3ceff";
						}
					}
					else
					{
						if (cursor > 0)
						{
							parent.childNodes[cursor].style.backgroundColor = "";
							parent.childNodes[cursor - 1].style.backgroundColor = "#a3ceff";
						}
					}
				}

				if (cursor != -1 && (event.keyCode == 9 || event.keyCode == 13))
				{
					/*					 
						if (IS_IE)
							input.value = parent.childNodes[cursor].innerText;
						else
							input.value = parent.childNodes[cursor].textContent;

						suggestions.innerHTML = "";
					*/	

					if (cursor != -1 && cursor < parent.childNodes.length)
					{
						if (IS_IE)
							input.value = parent.childNodes[cursor].innerText;
						else
							input.value = parent.childNodes[cursor].textContent;

						suggestions.innerHTML = "";
					}

					
				}
			}
		}
		catch (e) { }			
	}

	function reload()
	{
		suggestions.reload({ prefix : input.value });
	}
	function getCursor()
	{
		if (suggestions.innerHTML.length == 0)
			return -1;

		var parent = suggestions.firstChild;

		for (var i = 0; i < parent.childNodes.length; i++)
			if (
					parent.childNodes[i].style.backgroundColor == "#a3ceff" ||
					parent.childNodes[i].style.backgroundColor == "rgb(163, 206, 255)"
			   )
				return i;

		//if (parent.childNodes.length > 0) return 0;
		return parent.childNodes.length;
	}
}
