function getXmlHttp() {
		var xmlhttp;
		if (window.XMLHttpRequest) {
	xmlhttp = new XMLHttpRequest();
		}
		else {
	xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		}
		return xmlhttp;
}


function SendGetRequest(container, url) {
		var xmlhttp = getXmlHttp();
		xmlhttp.open('GET', url, true);
		xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
		try {
				restxt = xmlhttp.responseText;
				document.getElementById(container).innerHTML = restxt;
		}
		catch(err) {
		}
			}
	}
		};
		xmlhttp.send(null);
}


var no_sug = 0;
var suggs = Array();
var suggs_cache = Array();
var sugg_index = -1;





function NotEmptyLine(element, index, array) {
        return (element != '');
}

function Suggest(query, container, container_2) {
        // Hack for IE
        if (!Array.prototype.filter) {
                Array.prototype.filter = function(fun /*, thisp*/) {
                        var len = this.length;
                        if (typeof fun != "function")
                                throw new TypeError();
                        var res = new Array();
                        var thisp = arguments[1];
                        for (var i = 0; i < len; i++) {
                                if (i in this) {
                                        var val = this[i];
                                        if (fun.call(thisp, val, i, this))
                                                res.push(val);
                                }
                        }
                        return res;
                };
        }
        if (no_sug == 0 && query.length > 1) {
                if (suggs_cache[query]) {
                        suggs = suggs_cache[query];
                        sugg_index = -1;
                        DisplaySuggs(container, container_2);
                        document.getElementById(container_2).style.display = '';
                }
                else {
                        var xmlhttp = getXmlHttp();
                        xmlhttp.open('GET', '/hint.php?qs='+escape(query), true);
                        xmlhttp.onreadystatechange = function() {
                                if (xmlhttp.readyState == 4) {
                                        if(xmlhttp.status == 200) {
                                                try {
                                                        restxt = xmlhttp.responseText;
                                                        suggs = restxt.split("\n").filter(NotEmptyLine).sort();
                                                        suggs_cache[query] = suggs;
                                                        sugg_index = -1;
                                                        DisplaySuggs(container, container_2);
                                                        document.getElementById(container_2).style.display = '';
                                                }
                                                catch(err) {
                                                }
                                        }
                                }
                        };
                        xmlhttp.send(null);
                }
        }
        else {
                document.getElementById(container_2).style.display = 'none';
                document.getElementById(container_2).innerHTML = '';
        }
        no_sug = 0;
}

function SetQuery(query, container, container_2) {
        no_sug = 1;
        document.getElementById(container).value = query;
        document.getElementById(container_2).style.display = 'none';
        document.getElementById(container_2).innerHTML = '';
}

function HideSuggest(container) {
        document.getElementById(container).style.display = 'none';
        document.getElementById(container).innerHTML = '';
}

function SuggestKeyPress(e, query, container, container_2, searchform) {
        var evtobj = window.event? event : e;
        var unicode = evtobj.charCode? evtobj.charCode : evtobj.keyCode;
        // Up
        if (unicode == 38 && sugg_index > -1) {
                sugg_index--;
                DisplaySuggs(container, container_2);
        }
        // Down
        else if (unicode == 40 && sugg_index < suggs.length-1) {
                sugg_index++;
                DisplaySuggs(container, container_2);
        }
        // Enter
        else if (unicode == 13) {
                if (document.getElementById(container_2).style.display == '' && sugg_index > -1) {
                        SetQuery(suggs[sugg_index], container, container_2);
                }
                else if (document.getElementById(container).value != '') {
                        document.forms[searchform].submit();
                };
        }
        // ESCAPE
        else if (unicode == 27) {
                HideSuggest(container_2);
        }
        // Symbol or Backspace or DEL or Space
        else if (unicode >= 48 || unicode == 8 || unicode == 46 || unicode == 32) {
                Suggest(query, container, container_2);
        }
        else {
        }
        return false;
}


function DisplaySuggs(container, container_2) {
        if (suggs.length > 0) {
                line = '';
                for (i = 0; i < suggs.length; i++) {
                        id = 'sugg_'+i;
                        onclick = 'onclick="javascript:SetQuery(\''+suggs[i]+'\', \''+container+'\', \''+container_2+'\')"';
                        if (i == sugg_index) {
                                line += '<span '+onclick+' id="'+id+'" class="request_hint_item_active">'+suggs[i]+"</span><br>\n";
                        }
                        else {
                                line += '<span '+onclick+' id="'+id+'" class="request_hint_item">'+suggs[i]+"</span><br>\n";
                        }
                }
        }
        else {
                line = "<span style=\"font-style:italic;\">No suggestions found...</span>\n";
        }
        document.getElementById(container_2).innerHTML = line;
}






HideSuggest('request-hint');
