var xmlHttp;
var keyUpDelay = null;

function stateChanged() {
	if (xmlHttp.readyState == 4)
		document.getElementById("txtHint").innerHTML = xmlHttp.responseText;
}

function clearHints(field) {
	while (field.hasChildNodes())
		field.removeChild(field.lastChild);
}

function showHints() {
	var hintDiv = getDiv("hintDiv");
	if (hintDiv != null && hintDiv.div != null)
		clearHints(hintDiv.div);
	var keywords = document.getElementById("searchBox").value;
	if (keywords.length > 0) {
		if (keyUpDelay != null)
			clearTimeout(keyUpDelay);
		keyUpDelay = setTimeout("getHints()", 250);
	} else
		hintDiv.div.style.display = "none";
}

function getHints() {
	var keywords = document.getElementById("searchBox").value;
	ajaxClient.sendHttpRequest("GET", "hints.jsp?keywords="
			+ encodeURIComponent(keywords), updateHints);
	document.getElementById("hintDiv").style.display = "block";
}

function updateHints(xmlHttp) {
	var doc = xmlHttp.responseXML.documentElement;
	if (doc.nodeName == "suggestions") {
		var hintDiv = getDiv("hintDiv");
		var items = doc.childNodes;
		if (items.length == 0)
			hintDiv.div.style.display = "none";
		else {
			var hideDiv = true;
			for ( var n = 0; n < items.length; ++n) {
				if (items[n].nodeType == 1 && items[n].nodeName == "keyword") // skip
																				// text
																				// nodes
				{
					hideDiv = false;
					break;
				}
			}
			if (hideDiv)
				hintDiv.div.style.display = "none";
			else {
				for ( var n = 0; n < items.length; ++n) {
					if (items[n].nodeType == 1
							&& items[n].nodeName == "keyword") // skip text
																// nodes
					{
						var div = document.createElement("div");
						div.style.width = "100%";
						div.style.styleFloat = "left";
						div.setAttribute("tabindex", "0", 0);
						div.setAttribute("id","hint"+n);
			            if (items[n+1] != null && items[n+1].nodeType == 1 && items[n+1].nodeName == "keyword") div.setAttribute("nextid","hint"+(n+1));
			            if (items[n-1] != null && items[n-1].nodeType == 1 && items[n-1].nodeName == "keyword") div.setAttribute("previousid","hint"+(n-1));

						div.onmouseover = function() {
							this.style.backgroundColor = "#f78f1e";
							this.style.cursor = "pointer";
						};
						div.onmouseout = function() {this.style.backgroundColor = "";};
						div.onfocus = function() {
							this.style.backgroundColor = "#f78f1e";
							this.style.cursor = "pointer";
						};
						div.onblur = function() {this.style.backgroundColor = "";};
						div.onclick = function() {useHint(this.firstChild.firstChild.data);};
						div.onkeydown   = function(e) { hintKeyFunction(this, hintKey(e), this.firstChild.firstChild.data,e); };						
						hintDiv.div.appendChild(div);
						div2 = document.createElement("div");
						text = document.createTextNode(items[n].firstChild.data);
						div2.appendChild(text);
						div2.style.width = "100%";
						div2.style.margin = "2px 5px";
						div.appendChild(div2);
					}
				}
			}
		}
	}
}

function useHint(word) {
	if (word.indexOf(" ") > -1)
		word = "'" + word + "'";
	document.getElementById("searchBox").value = word;
	document.getElementById("hintDiv").style.display = "none";
	document.forms.searchForm.submit();
}

function hintKey(e) {
	if (!e)
		var e = window.event
	if (e.keyCode)
		return e.keyCode;
	else if (e.which)
		return e.which;
	return null;
}

function checkFocus(key,e)
{
  if (key==40 && document.getElementById("hint0")!=null) { document.getElementById("hint0").focus(); e&&e.preventDefault?e.preventDefault():event.returnValue = false; }        // IE
  else if (key==40 && document.getElementById("hint1")!=null) { document.getElementById("hint1").focus(); e&&e.preventDefault?e.preventDefault():event.returnValue = false; } // Firefox
}

function hintKeyFunction(el,key,word,e)
{
  if (key==13) useHint(word);
  if (key==38)
  {
    if (el.getAttribute("previousid")) document.getElementById(el.getAttribute("previousid")).focus();
     e&&e.preventDefault?e.preventDefault():event.returnValue = false;
     return false;
  }
  if (key==40)
  {
    if (el.getAttribute("nextid")) document.getElementById(el.getAttribute("nextid")).focus();
     e&&e.preventDefault?e.preventDefault():event.returnValue = false;
    return false;
  }
  return true;
}


function hintKeySubmit(key, elID) {
	if (key == 13)
		document.getElementById(elID).submit();
}