// code for suggest boxes in restaurant online

function suggestHandler (sName, sIdInputText, sIdDivSuggest, sLookupScript, sParamName, fOnUpdate) {

    
    try {
        
        this.name = sName;
        this.cache = new Object;

        this.createAjax();

        this.inputText = document.getElementById(sIdInputText);
        this.inputText.suggestHandler = this;
        this.inputText.setAttribute("autocomplete", "off");

        this.lastSearchTerm = this.inputText.value;

        this.divSuggest = document.getElementById(sIdDivSuggest);
        this.divSuggest.suggestHandler = this;
        
        if (window.addEventListener) {
            //moz et. al.
            this.inputText.addEventListener("keyup", inputText_onKeyUp, false);
            window.addEventListener("click", window_onClick, false);
        } else {
            //ie
            
            this.inputText.attachEvent("onkeyup", inputText_onKeyUp);

            //scope gets lost if functions are not defined inline 
            var oS = this;
            document.attachEvent("onmousedown",  function(e) {
                    e = (!e) ? window.event : e;
                            
                    if ((e.srcElement != oS.divSuggest) && (e.srcElement != oS.textInput)
                        && (!e.srcElement.rowid)) {
                        oS.lastSearchTerm = oS.inputText.value;
                        oS.hideBox();    
                    }
                }                   
            );
        }

        this.lookupScript = sLookupScript;
        this.paramName = sParamName;
        this.onUpdate = fOnUpdate;
    
        setTimeout("window." + this.name + ".checkForChanges()", 500);

    } catch (e) {
        handleJsError(e);
        return;            
    }
    
    var oS = this;
    
    function window_onClick(e) {
    
        e = (!e) ? window.event : e;
                
        if ((e.target != oS.divSuggest) && (e.target != oS.textInput)) {
            oS.lastSearchTerm = oS.inputText.value;
            oS.hideBox();    
        }
    }                   
}

suggestHandler.prototype.createAjax = function() {

	if (window.XMLHttpRequest) {
	   this.ajax = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
        var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                        "MSXML2.XMLHTTP.5.0",
                                        "MSXML2.XMLHTTP.4.0",
                                        "MSXML2.XMLHTTP.3.0",
                                        "MSXML2.XMLHTTP",
                                        "Microsoft.XMLHTTP");
                                        
        for (var i=0; i<XmlHttpVersions.length && !this.ajax; i++) {
            try { 
                this.ajax = new ActiveXObject(XmlHttpVersions[i]);
            } catch (e) {
            }
        }
	}

    if (!this.ajax) {
        throw("Ajax konnte nicht initialisiert werden!");
    }
    
}

suggestHandler.prototype.checkForChanges = function () {

    if (this.inputText.value == "") {
        this.hideBox();
    } else {
        if (this.inputText.value != this.lastSearchTerm) {
            this.sendQuery(this.inputText.value);
        }
    }
    
    setTimeout("window." + this.name + ".checkForChanges()", 75);

}

suggestHandler.prototype.sendQuery = function(sParam) {

    if (!this.pauseQuery) {
        if (this.ajax.readyState == 4 || this.ajax.readyState == 0) {
            try {
                var oAjax = this.ajax;
                var oThis = this;
                var sKey = escape(sParam);            
                var sUrl = this.lookupScript + "?" + this.paramName + "=" + sKey;
                
                if (sParam == this.lastSearchTerm) {
                    return;
                }
                
                this.lastSearchTerm = sParam;
                
                if (this.cache[sKey]){
                    oThis.fillBox(this.cache[sKey]);                
                } else {             
                    this.ajax.open("GET", sUrl, true);
                    this.ajax.onreadystatechange = function () {
                        if (oAjax.readyState == 4) {
                            var sR = oAjax.responseText;
                            if (sR != "") {
                                try {
                                    oData = eval("(" + sR + ")");
                                    oThis.fillBox(oData);
                                    oThis.cache[sKey] = oData;
                                } catch (e) {
                                    handleJsError(e);
                                }
                            }
                        } else {
                            oThis.hideBox();
                        }
                    }
                    this.ajax.send(null);
                }
            } catch (e) {
                handleJsError(e);
            }
        }
    }
}

suggestHandler.prototype.showBox = function() {
    
    var oS = this.divSuggest.style;
    
    try { 
        if (oS.visibility != "visible") {
            oS.position = "absolute";                
            oS.left = getX(this.inputText) + "px";
            oS.top = getY(this.inputText) + this.inputText.offsetHeight + "px"; 
            oS.height = "180px";
            var iWidth = (this.inputText.offsetWidth < 200) ? 200 : this.inputText.offsetWidth                      
            oS.width = (iWidth - 2) + "px"; 
            oS.visibility = "visible";
        }
    } catch (e) {
        handleJsError(e);
        return false;    
    }    
    
    return true;        
    
}

suggestHandler.prototype.hideBox = function () {
    
    var oS = this.divSuggest.style;
    
    try { 
        this.lastSearchTerm = this.inputText.value;
        this.divSuggest.style.visibility = "hidden";
    } catch (e) {
        handleJsError(e);
        return false;    
    }    
    
    return true;        
    
}

suggestHandler.prototype.fillBox = function(oData) {
    
    try {
        if (oData.ids && oData.names) {
            listHtml = "";
                        
            if (oData.ids) {
                for (i=0; i<oData.ids.length; i++) {
                    listHtml += "<div onMouseOver=\"rollOver(this, true);style.cursor='default'\" "
                        + "onMouseOut='rollOver(this, false)' "
                        + "onClick='" + this.name + ".passSelection(this);' "
                        + "rowid='" + oData.ids[i] + "'>" 
                        + oData.names[i] + "</div>";
                }
                this.curSelection = null;
                this.divSuggest.scrollTop = 0;
                this.divSuggest.innerHTML = listHtml;
                this.showBox();  
            }
        }
    } catch (e) {
        
        handleJsError(e);
        return false;    

    }
    
    return true;

}

suggestHandler.prototype.passSelection = function(eP) {
    
    this.inputText.value = eP.innerHTML.replace(/\&amp;/g, "&");
    this.inputText.setAttribute("rowid", eP.getAttribute("rowid"));
    this.hideBox();
    if (this.onUpdate) {
        this.onUpdate();
    }
    
}

function inputText_onKeyUp(e) {

    e = (!e) ? window.event : e;
    tgt = (!e.target) ? e.srcElement : e.target; 
            
    oS = tgt.suggestHandler; 
    
    if (e.type == "keyup") {
        if (e.keyCode == 40) { // keyup
            if (oS.curSelection) {
                if (oS.curSelection.nextSibling) {
                    rollOver(oS.curSelection, false);
                    oS.curSelection = oS.curSelection.nextSibling;
                    rollOver(oS.curSelection, true);
                    checkScroll(oS.curSelection);
                }                    
            } else if (oS.divSuggest.firstChild) {
                oS.curSelection = oS.divSuggest.firstChild;
                rollOver(oS.divSuggest.firstChild, true);
            }
        } else if (e.keyCode == 38) { // keyup
            if (oS.curSelection) {
                if (oS.curSelection.previousSibling) {
                    rollOver(oS.curSelection, false);
                    oS.curSelection = oS.curSelection.previousSibling;
                    rollOver(oS.curSelection, true);
                    checkScroll(oS.curSelection);
                } else {
                    rollOver(oS.curSelection, false);
                    oS.curSelection = null;
                }
            }                                   
        } else if (e.keyCode == 27) { //  escape
            oS.lastSearchTerm = oS.inputText.value;
            oS.hideBox();
        } else if (e.keyCode == 13) { //  return
            oS.pauseQuery = true;
            if (oS.curSelection) {
                oS.inputText.value = oS.curSelection.innerHTML.replace(/\&amp;/g, "&");
                oS.inputText.setAttribute("rowid", oS.curSelection.getAttribute("rowid"));
            }
            oS.hideBox();
            if (oS.onUpdate) {
                oS.onUpdate();
            }
            if (window.attachEvent) {
                e.preventDefault = false;
            } else {
                e.preventDefault();
            }
        } else if (e.keyCode == 9) { //  tab
            oS.pauseQuery = true;
            if (oS.curSelection) {
                oS.inputText.value = oS.curSelection.innerHTML.replace(/\&amp;/g, "&");
                oS.inputText.setAttribute("rowid", oS.curSelection.getAttribute("rowid"));
            }
            oS.hideBox();            
            if (oS.onUpdate) {
                oS.onUpdate();
            }
        } else {
            oS.sendQuery(oS.inputText.value);
        }

        if ((e.keyCode == 38) || (e.keyCode == 40)) {
            oS.pauseQuery = true;
            if (oS.curSelection) {
                oS.inputText.value = oS.curSelection.innerHTML.replace(/\&amp;/g, "&");
            }
        } else {
            oS.pauseQuery = false;
        }
    }    
}

function checkScroll(eP) {
    
    var ihScrollBarHeight = 18;
    
    eDiv = eP.parentNode;
    
    iBottomList = getY(eDiv) + eDiv.offsetHeight;
    iBottomCurSel = getY(eP) + eP.offsetHeight;
    iTopList = getY(eDiv);
    iTopCurSel = getY(eP);
        
    if (iBottomCurSel > (iBottomList - ihScrollBarHeight + eDiv.scrollTop)) {
        eDiv.scrollTop += (eP.offsetHeight + ihScrollBarHeight);
    }        

    if (iTopCurSel < (iTopList + eDiv.scrollTop)) {
        eDiv.scrollTop -= (2*eP.offsetHeight);
    }        
    
}