// Implementers should append the following JavaScript patch
// to the 2.3.1 AutoComplete build file as a workaround for the bug being tracked at
// http://sourceforge.net/tracker/index.php?func=detail&aid=1779618&group_id=165715&atid=836476
YAHOO.widget.AutoComplete.prototype._onTextboxKeyDown = function(v,oSelf) {
    var nKeyCode = v.keyCode;
    switch (nKeyCode) {
        case 9:
            if(oSelf._oCurItem) {
                if(oSelf.delimChar && (oSelf._nKeyCode != nKeyCode)) {
                    if(oSelf._bContainerOpen) {YAHOO.util.Event.stopEvent(v);}
                }
                oSelf._selectItem(oSelf._oCurItem);
            }
            else {oSelf._toggleContainer(false);}
            break;
        case 13:
            var isMac = (navigator.userAgent.toLowerCase().indexOf("mac") != -1);
            if(!isMac) {
                if(oSelf._oCurItem) {
                    if(oSelf._nKeyCode != nKeyCode) {
                        if(oSelf._bContainerOpen) {YAHOO.util.Event.stopEvent(v);}
                    }
                    oSelf._selectItem(oSelf._oCurItem);
                }
                else {oSelf._toggleContainer(false);}
            }
            break;
        case 27:
            oSelf._toggleContainer(false);
            return;
        case 39:
            oSelf._jumpSelection();
            break;
        case 38:
            YAHOO.util.Event.stopEvent(v);
            oSelf._moveSelection(nKeyCode);
            break;
        case 40:
            YAHOO.util.Event.stopEvent(v);
            oSelf._moveSelection(nKeyCode);
            break;
        default:
            break;
    }
};

YAHOO.widget.AutoComplete.prototype._onTextboxKeyPress = function(v,oSelf) {
    var nKeyCode = v.keyCode;
    var isMac = (navigator.userAgent.toLowerCase().indexOf("mac") != -1);
    if(isMac) {
        switch (nKeyCode) {
        case 9:
            if(oSelf._oCurItem) {
                if(oSelf.delimChar && (oSelf._nKeyCode != nKeyCode)) {YAHOO.util.Event.stopEvent(v);}
            }
            break;
        case 13:
            if(oSelf._oCurItem) {
                if(oSelf._nKeyCode != nKeyCode) {
                    if(oSelf._bContainerOpen) {YAHOO.util.Event.stopEvent(v);}
                }
                oSelf._selectItem(oSelf._oCurItem);
            }
            else {oSelf._toggleContainer(false);}
            break;
        case 38:
        case 40:
            YAHOO.util.Event.stopEvent(v);
            break;
        default:
            break;
        }
    }
    else if(nKeyCode == 229) {oSelf._queryInterval = setInterval(function() { oSelf._onIMEDetected(oSelf); },500);}
};

YAHOO.example.ACXml = new function(){
    // Instantiate an XHR DataSource and define schema as an array:
    //     ["Multi-depth.object.notation.to.find.a.single.result.item",
    //     "Query Key",
    //     "Additional Param Name 1",
    //     ...
    //     "Additional Param Name n"]
    this.oACDS = new YAHOO.widget.DS_XHR("/ajaxsuggest.php", ["entry", "name"]);
    this.oACDS.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
    this.oACDS.prehighlightClassName = "yui-ac-prehighlight";
    this.oACDS.typeAhead = true;
    this.oACDS.useShadow = true;
    this.oACDS.queryMatchContains = true;
    this.oACDS.scriptQueryParam = "q";
    this.oACDS.scriptQueryAppend = "results=100"; // Needed for YWS

    // Instantiate AutoComplete
    this.oAutoComp = new YAHOO.widget.AutoComplete("ysearchinput","ysearchcontainer", this.oACDS);

    // Stub for AutoComplete form validation
    this.validateForm = function() {
        // Validation code goes here
        return true;
    };
};
// JavaScript Document
