﻿AutoComplete = function(input, suggestions)
{
	var timer;

	input.attachEvent("onkeyup", handleKeyUp);
	 
	input.attachEvent("onkeydown", handleArrowKeys);

	function handleKeyUp(event)
	{
		if (event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13)
		{
			if (input.value.length == 0)
				suggestions.innerHTML = "";

			if (timer)
				clearTimeout(timer);

			if (input.value.length > 0)
				timer = setTimeout(reload, 400);
		}
		else if (event.keyCode == 13)
		{
			var cursor = getCursor();
			var parent = suggestions.firstChild;

			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 = "";
			}
		}
	}

	function reload()
	{
		suggestions.reload({ prefix : input.value });
	}

	function handleArrowKeys(event)
	{
		try
		{
			var cursor = getCursor();
			var parent = suggestions.firstChild;
			var con = "";
			 
			if (cursor != -1 && (event.keyCode == 40 || event.keyCode == 38))
			{
				if (event.keyCode == 40)
				{
					if (cursor == parent.childNodes.length)
					{
						parent.childNodes[0].style.backgroundColor = "#d1e5df";
						if (IS_IE)
							con = parent.childNodes[0].innerText+" ";
						else
							con = parent.childNodes[0].textContent+" ";	
						
					}
					else if (cursor < parent.childNodes.length - 1)
					{
						parent.childNodes[cursor].style.backgroundColor = "";
						parent.childNodes[cursor + 1].style.backgroundColor = "#d1e5df";
						if (IS_IE)
							con = parent.childNodes[cursor+1].innerText+" ";
						else
							con = parent.childNodes[cursor+1].textContent+" ";	
					}
				}
				else
				{
					if (cursor > 0)
					{
						parent.childNodes[cursor].style.backgroundColor = "";
						parent.childNodes[cursor - 1].style.backgroundColor = "#d1e5df";
						if (IS_IE)
							con = parent.childNodes[cursor-1].innerText+" ";
						else
							con = parent.childNodes[cursor-1].textContent+" ";	
					}
				}
				
				if (con != " ")
				{
				if (con == "Close ")
				{
				document.getElementById("auto").value ="";
				}
				else{
					document.getElementById("auto").value =con;
					}
				}
			}
			else if (cursor != -1 && event.keyCode == 13)	//13
			{
				
				if (IS_IE)
					con = parent.childNodes[cursor].innerText+" ";
				else
					con = parent.childNodes[cursor].textContent+" ";	
					
				if (con != " ")
				{
				if (con == "Close ")
				{
				document.getElementById("auto").value = "";
				}
				else{
					document.getElementById("auto").value =con;
				}	}		
			} 
		}
		catch (e) { }
	}

	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 == "#d1e5df" ||
					parent.childNodes[i].style.backgroundColor == "rgb(209, 229, 223)"
			   )
				return i;

		return parent.childNodes.length;
	}
}

