//Modified by Ünal -> added multiple search capability
//GLOBAL values used to pass parameters to callback function liveSearchKeyPress    
var searchURL = null;       //server URL for data
var form_element = null;    //text box on the form
var div_result = null;      //div tag the displays returned data
var return_function = null; //javascript function executed when results returned to browser
    
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var isIE = false;

function liveSearchInitXMLHttpRequest()
{
    if (window.XMLHttpRequest) { liveSearchReq = new XMLHttpRequest();}
    else if (window.ActiveXObject) { liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");}
}

function liveSearchInit(form_element) {
    
    if (navigator.userAgent.indexOf("Safari") > 0) {
        form_element.addEventListener("keydown",liveSearchKeyPress,false);
    } else if (navigator.product == "Gecko") {        
        form_element.addEventListener("keypress",liveSearchKeyPress,false);
        form_element.addEventListener("blur",liveSearchHideDelayed,false);
    } else {
        form_element.attachEvent('onkeydown',liveSearchKeyPress);
        isIE = true;
    }
    form_element.setAttribute("autocomplete","off");
}

function liveSearchHideDelayed() { window.setTimeout("liveSearchHide()",1000); }
    
function liveSearchHide() {
    if (div_result){
        div_result.style.display = "none";
        var highlight = document.getElementById("LSHighlight");
        if (highlight) {
            highlight.removeAttribute("id");
        }
    }        
}

function liveSearchKeyPress(event) {
    if (event.keyCode == 40 ) {
        highlight = document.getElementById("LSHighlight");
        if (!highlight) { 
          if (div_result.firstChild.firstChild){ highlight = div_result.firstChild.firstChild.firstChild; }
        } else {
            highlight.removeAttribute("id");
            highlight = highlight.nextSibling;
        }
        if (highlight) { highlight.setAttribute("id","LSHighlight"); } 
        if (!isIE) { event.preventDefault(); }
    } 
    else if (event.keyCode == 38 ) {
        highlight = document.getElementById("LSHighlight");
        if (!highlight) { highlight = div_result.firstChild.firstChild.lastChild;
        } else {
            highlight.removeAttribute("id");
            highlight = highlight.previousSibling;
        }
        if (highlight) { highlight.setAttribute("id","LSHighlight"); }
        if (!isIE) { event.preventDefault(); }
    } 
    else if (event.keyCode == 27) {
        highlight = document.getElementById("LSHighlight");
        if (highlight) { highlight.removeAttribute("id"); }
        div_result.style.display = "none"; }
    else if (event.keyCode == 13) {
        if (typeof(highlight) != "undefined") { form_element.value = htmlEntityDecode(highlight.innerHTML); }
        liveSearchHide();
        doClickEvent(highlight);
        return false;
    } else if (event.keyCode == 9) {
        highlight = document.getElementById("LSHighlight");
        if (highlight) {
            form_element.value = htmlEntityDecode(highlight.innerHTML);
            highlight.removeAttribute("id");
            liveSearchHide();
            doClickEvent(highlight);
            return true;
        } else {
            next=form_element;
            next++
        }
        return false;
    }
}

function doClickEvent(el){
        var clickEvent;
        if (isIE) {
            var f  = el.getAttribute("onClick").toString();
            var c1 = f.indexOf('{') +1;
            var c2 = f.lastIndexOf('}');
            clickEvent = f.substring(c1,c2);
        } else { clickEvent = el.getAttribute("onClick"); }
        eval (clickEvent);
}

function liveSearchStart(event, element, s_url, result_div, rf) {
    if(event.keyCode == 27) {
        liveSearchHide();
        return;
    }
    else if ((event.keyCode == 8) || (event.keyCode == 46)) {
        if(element.value.length == 0) {
            liveSearchHide();
            return;
        }
    }
    if(event.keyCode != 9 && event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13) {
        if (t) { window.clearTimeout(t);}
        form_element = element;
        searchURL = s_url;
        div_result = result_div;
        return_function = rf;
        t = window.setTimeout("liveSearchDoSearch()",200);
    }
}

function liveSearchDoSearch() {
    if (typeof liveSearchRoot == "undefined") { liveSearchRoot = ""; }
    if (typeof liveSearchRootSubDir == "undefined") { liveSearchRootSubDir = ""; }
    if (typeof liveSearchParams == "undefined") { liveSearchParams = ""; }
    if (liveSearchLast != form_element) {
        if (liveSearchReq && liveSearchReq.readyState < 4) { liveSearchReq.abort(); }
        if ( form_element == "") {
            liveSearchHide();
            return false;
        }
        liveSearchInitXMLHttpRequest();
        liveSearchReq.onreadystatechange = liveSearchProcessReqChange;
        liveSearchReq.open("GET", buildSearchURL());
        liveSearchReq.send(null);
        liveSearchLast = form_element.value;
    }
}

function buildSearchURL()
{
    if (searchURL.indexOf("?") > 0){ url = liveSearchRoot + searchURL + "&q="+form_element.value; }
    else { url = liveSearchRoot + searchURL + "?q="+form_element.value; }
    return url;
}

function liveSearchProcessReqChange() {
    if (liveSearchReq.readyState == 4) {
        var  res = div_result;
        res.style.display = "block";
        var  sh = div_result.firstChild;
        sh.innerHTML = liveSearchReq.responseText;
    }
}

function liveSearchSubmit() {
    var highlight = document.getElementById("LSHighlight");
    if (highlight && highlight.firstChild) {
        liveSearchHide();
        return true;
    } 
    else { return false; }
}

function liveSearchHover(el) {
        highlight = document.getElementById("LSHighlight");
        if (highlight) { highlight.removeAttribute("id"); }
        el.setAttribute("id","LSHighlight");
}

function liveSearchClicked(el, returnParamList) {        
        if (typeof(el.innerHTML) != "undefined") { form_element.value = el.innerHTML; }
        liveSearchHide();
        if (typeof returnParamList == "undefined") { returnParamList = ""; }
}
