var ajaxRequest;
var links;

function createRequest()
{
	try
	{
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
	}

	return true;
}

function init()
{
	if (createRequest())
		document.getElementById("searchbox").style.display = "";
}

function clearHint()
{
	for (i = 0; i < links.length; i++)
	{
		node = document.getElementById("n" + links[i]);
		if (node)
			node.style.background = "";

		node = document.getElementById("t" + links[i]);
		if (node)
			node.style.background = "";

		node = document.getElementById("l" + links[i]);
		if (node)
			node.style.background = "";
	}
}

function setHint(color)
{
	for (i = 0; i < links.length; i++)
	{
		node = document.getElementById("n" + links[i]);
		if (node)
			node.style.backgroundColor = color;

		node = document.getElementById("t" + links[i]);
		if (node)
			node.style.backgroundColor = color;

		node = document.getElementById("l" + links[i]);
		if (node)
			node.style.backgroundColor = color;
	}
}

function hint(text)
{
	if (text.length > 2 && createRequest())
	{
		ajaxRequest.onreadystatechange = function()
		{
			if (ajaxRequest.readyState == 4)
			{
				if (links)
				{
					clearHint();
					delete links;
				}

				links = ajaxRequest.responseText.split(";");

				setHint("#ffde00");
			}
		}
		ajaxRequest.open("GET", "search.php?q=" + escape(text), true);
		ajaxRequest.send(null);
	}
	else if (links)
	{
		clearHint();
		delete links;
	}
}
