/* adapted from: Timothy Groves - http://www.brandspankingnew.net */ var cur_autosug; function hide_autosuggest() { if(cur_autosug)cur_autosug.clearSuggestions(); } function AutoSuggest(id, param) { this.fld = $i(id); if (!this.fld) {return 0; alert('AutoSuggest: No ID');} // init variables this.init(); // parameters object this.oP = param ? param : {}; // defaults var k, def = { minchars:1, meth:"get", varname:"input", className:"autosuggest", timeout:4000 ,delay:1000, offsety:-5, shownoresults: true, noresults: "No results!", maxheight: 250 ,cache: false, maxentries: 25, fixed_options: false, xdelta: 0, ydelta: 5 } for (k in def) { if (typeof(this.oP[k]) != typeof(def[k])) this.oP[k] = def[k]; } // set keyup handler for field // and prevent autocomplete from client var p = this; // NOTE: not using addEventListener because UpArrow fired twice in Safari //DOM.addEvent( this.fld, 'keyup', function(ev){ return me.onKeyPress(ev); } ); this.fld.onkeypress = function(ev){ if(!(selector && selector.display)) return p.onKeyPress(ev); }; this.fld.onkeyup = function(ev){ if(!(selector && selector.display)) return p.onKeyUp(ev); }; this.fld.setAttribute("autocomplete","off"); }; AutoSuggest.prototype.init = function() { this.sInp = ""; this.nInpC = 0; this.aSug = []; this.iHigh = 0; } AutoSuggest.prototype.onKeyPress = function(ev) { var key = (window.event) ? window.event.keyCode : ev.keyCode; //var ev = (window.event) ? window.event : ev; //var key = ev.keyCode ? ev.keyCode : ev.charCode; // set responses to keydown events in the field // this allows the user to use the arrow keys to scroll through the results // ESCAPE clears the list // TAB sets the current highlighted value // var RETURN = 13; var TAB = 9; var ESC = 27; var bubble = 1; switch(key) { case TAB: this.setHighlightedValue(); bubble = 0; break; case RETURN: this.setHighlightedValue(); bubble = 0; break; case ESC: this.clearSuggestions(); break; } return bubble; } AutoSuggest.prototype.onKeyUp = function(ev) { var key = (window.event) ? window.event.keyCode : ev.keyCode; //var ev = (window.event) ? window.event : ev; //var key = ev.keyCode ? ev.keyCode : ev.charCode; var ARRUP = 38; var ARRDN = 40; var bubble = 1; switch(key) { case ARRUP: this.changeHighlight(key); bubble = 0; break; case ARRDN: this.changeHighlight(key); bubble = 0; break; default: if(key!=13) { if(this.oP.fixed_options) this.find_nearest(key); else this.getSuggestions(this.fld.value); } } return bubble; } AutoSuggest.prototype.clear_user_inp = function() { this.user_inp = ''; } AutoSuggest.prototype.find_nearest = function (key) { var list = this.ul; var same_key = 0; // make the list if (!list) { if(this.aSug) { this.createList(this.aSug); } if(this.aSug[0].value.substr(0,this.user_inp.length).toLowerCase()==String.fromCharCode(key)) { this.resetTimeout(); return; } } // values for multiple keystrokes if((this.user_inp.length==1) && this.user_inp == String.fromCharCode(key).toLowerCase()) { same_key = 1; } else { this.user_inp += String.fromCharCode(key).toLowerCase(); } // clear user keys window.clearTimeout(this.clear_timer); // loop over the next after the current var st = this.iHigh; // continuation of typing, also check the current value if(!same_key) st--; for(var i = st; i ol && l && l