//
// Code by Guido Scognamiglio
//

function AjaxMe(url)
{
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	} else {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.open("GET", url, false);
	xmlhttp.send();
	return xmlhttp.responseText;
}

function getObj(objName) {
	/*
	if (document.getElementById) {
		return document.getElementById(objName);
	} else if (document.all) {
		return document.all[objName];
	} else if (document.layers) {
		return document.layers[objName];
	}
	*/
	return document.all[objName];
}

var STitem = 0;

function getMaxSearchTips()
{
	if (document.all['searchTips'].style.visibility == "hidden") return 0;

	var a = 0;
	while (a++ < 100)
	{
		try
		{
			var x = document.all['stc'+a].innerHTML;
		}
		catch (err)
		{
			break;
		}
	}
	return a;
}

function clearAllStSel()
{
	var maxItem = getMaxSearchTips();
	for (var a=1; a<maxItem; a++)
	{
		document.all['stc'+a].style.backgroundColor = "transparent";
		document.all['stc'+a].style.color = "#000000";
	}
}


function submitSearch()
{
	var q = getObj("searchQuery").value;
	if (q != "" && q != "Search") top.location.href = "/?a=search&q=" + q;
}

function searchTip(text)
{
	var stbox = getObj('searchTips');
	kword = AjaxMe("/ajax_searchtips.asp?w="+text)
	if (kword.length > 0)
	{
		stbox.style.visibility = "visible";
		stbox.innerHTML = "<a href=''>" + kword + "</a>";
	}
	else
		stbox.style.visibility = "hidden";
}

function searchKeyDown()
{
	var KeyPress = window.event.keyCode;

	// submit search
	if (KeyPress == 13) submitSearch();

	// arrow up
	else if (KeyPress == 38)
	{
		var maxItem = getMaxSearchTips();
		if (maxItem == 0) return;

		if (--STitem < 1) { STitem = 1; return; }
		try {
			clearAllStSel();
			document.all['stc'+STitem].style.backgroundColor = "#404040";
			document.all['stc'+STitem].style.color = "#ffffff";
			document.all['searchQuery'].value = document.all['stc'+STitem].innerHTML;
		} catch (err) { return; }
	}

	// arrow down
	else if (KeyPress == 40)
	{
		var maxItem = getMaxSearchTips();
		if (maxItem == 0) return;

		if (++STitem == maxItem) { STitem = maxItem-1; return; }
		try {
			clearAllStSel();
			document.all['stc'+STitem].style.backgroundColor = "#404040";
			document.all['stc'+STitem].style.color = "#ffffff";
			document.all['searchQuery'].value = document.all['stc'+STitem].innerHTML;
		} catch (err) { return; }
	}

	// start searching the keyword tips
	else
		searchTip(document.all['searchQuery'].value);
}

