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);
}


function ShowHideContainer(container) {
        if (document.getElementById(container).style.display == '') {
                document.getElementById(container).style.display = 'none';
        }
        else {
                document.getElementById(container).style.display = '';
        }
        return false;
}

function LoadForm(container, url) {
        element = document.getElementById(container);
        //if (element.innerHTML == '') {
        if (element.childNodes.length < 3) {
                element.innerHTML = 'Loading form...';
                SendGetRequest(container, url);
        }
        else {
                element.innerHTML = '';
        }
}


var rated = new Array();

function UpdateRate(container, url) {
        if (rated[container] != 1) {
                SendGetRequest(container, url);
                rated[container] = 1;
        }
}
function Rate(id,class_prefix,rate) {
        document.getElementById(id).className = class_prefix + rate;
}

function UpdateContainerFromUrl(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 = 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/?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;
}

function CloseGoFrame() {
        parent.document.getElementById('fiframes').rows='0,*';
}

function ShowAdvanced() {
        e = document.getElementById('search-detail');
        if (e.style.display == 'block') {
                e.style.display = 'none';
        }
        else {
                e.style.display = 'block';
        }
}

function ShowTab(tabnum, tabcount, tab_prefix, tab_head_prefix) {
        for (i = 1; i <= tabcount; i++) {
                if (i == tabnum) {
                        document.getElementById(tab_prefix + i).style.display = '';
                        document.getElementById(tab_head_prefix + i).className = 'active';
                }
                else {
                        document.getElementById(tab_prefix + i).style.display = 'none';
                        document.getElementById(tab_head_prefix + i).className = '';
                }
        }
}

function ShowHideTab(tabnum, tabcount, tab_prefix, tab_head_prefix) {
        for (i = 1; i <= tabcount; i++) {
                if (i == tabnum) {
                        if (document.getElementById(tab_prefix + i).style.display == '') {
                                document.getElementById(tab_prefix + i).style.display = 'none';
                                document.getElementById(tab_head_prefix + i).className = '';
                        }
                        else {
                                document.getElementById(tab_prefix + i).style.display = '';
                                document.getElementById(tab_head_prefix + i).className = 'active';
                        }
                }
                else {
                        document.getElementById(tab_prefix + i).style.display = 'none';
                        document.getElementById(tab_head_prefix + i).className = '';
                }
        }
}

function ShowSpoilerContent(id_prefix) {
        document.getElementById(id_prefix + '_head').style.display = 'none';
        document.getElementById(id_prefix + '_content').style.display = '';
}
function HideSpoilerContent(id_prefix) {
        document.getElementById(id_prefix + '_head').style.display = '';
        document.getElementById(id_prefix + '_content').style.display = 'none';
}
function ShowBlock (id_prefix){
        document.getElementById(id_prefix + '_show').style.display = 'none';
        document.getElementById(id_prefix + '_hide').style.display = '';
        document.getElementById(id_prefix + '_content').style.display = '';
}
function HideBlock (id_prefix){
        document.getElementById(id_prefix + '_show').style.display = '';
        document.getElementById(id_prefix + '_hide').style.display = 'none';
        document.getElementById(id_prefix + '_content').style.display = 'none';
}

function clearIt (obj,id)
{
        if (typeof(id) == 'undefined'){
                id = 'search-span';
        }
	    document.getElementById(id).style.display = 'none';
}
function showIt (obj,id)
{
        if (typeof(id) == 'undefined'){
                id = 'search-span';
        }
    	if (obj.value=='')
    	{
    		document.getElementById(id).style.display = 'block';
    	}
        else 
    	{
    	    document.getElementById(id).style.display = 'none';
    	}
}
function rand(min, max){
        var argc = arguments.length;
        if (argc === 0){
                min = 0;
                max = 2147483647;
        }
        else if (argc === 1){
                throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
        }
        return Math.floor(Math.random() * (max - min + 1)) + min;
}
