// Auto suggestion javascript // Copyright TheDeal LLC 2008 // By Kevin Yan // 4/22/2008 // Default suggestions url ; var suggestUrl = baseHref; // Current Active Suggestion Object var CURRENT_SUG = null; // insert new DIV element into page for displaying suggestions document.write("
"); // if browser is IE, show IFRAME under DIV layer, to layer over SELECT elements if (navigator.appName == 'Microsoft Internet Explorer') { document.write(""); } // Simple Suggestion Object, contains id and value function TD_Suggestion (i, v) { this.id = i; this.value = v; } // Simple Cached Suggestions object, contains, keyword and saved suggestions function TD_CachedSuggestions () { this.text = ""; this.suggestions = []; } // Auto Suggestion object, with default settings. function TD_AutoSuggest (id) { this.id = id; // Id of text element this.element = document.getElementById(id); // element object reference this.url = suggestUrl; // URL for getting suggestions this.minLength = 2; // Minimum lenght of keyword for suggestions this.currentVal = ""; // Current keyword value this.highlited = -1; // Current highlited value from suggestion list this.cached = true; // Is caching turn on/off this.hideAfterAction = true; // hide suggestions if user clicked on highlited item this.highlitedItem = null; this.basicAction = null // Script action when 'Enter' is hit this.matchAction = null // Script action when Mouse Click this.suggestions = []; // Current list of suggestions TD_Suggestion objects this.cachedList = []; // Cached list of suggestions TD_CachedSuggestions objects this.requestObj; // AJAX request object reference var ME = this; // Self reference this.element.onkeypress = function(ev){ return ME.onKeyPress(ev); } // Handle key press event, capture 'ENTER' key before borwser finishs this.element.onkeyup = function(ev){ return ME.onKeyUp(ev); } // Handle key up event, } // Update element object TD_AutoSuggest.prototype.updateElement = function(id) { this.id = id; // Id of text element this.element = document.getElementById(id); // element object reference var ME = this; // Self reference this.element.onkeypress = function(ev){ return ME.onKeyPress(ev); } // Handle key press event, capture 'ENTER' key before borwser finishs this.element.onkeyup = function(ev){ return ME.onKeyUp(ev); } // Handle key up event, } // Handle Key Press event TD_AutoSuggest.prototype.onKeyPress = function(ev) { var key = (window.event) ? window.event.keyCode : ev.keyCode; var rtn = true; switch(key) { // Return key, set current highlited value to text box case 13: this.setHighlitedValue(); break; // Escape key, hide suggestions case 27: hideSuggestions(); break; } CURRENT_SUG = this; return rtn; } // Handle Key Up event TD_AutoSuggest.prototype.onKeyUp = function(ev) { var key = (window.event) ? window.event.keyCode : ev.keyCode; var rtn = true; switch(key) { // Return key, do nothing case 13: this.executeAction(); rtn = false; break; // Up key, move highlited value up case 38: this.hightLiteSuggestion(this.highlited - 1); rtn = false; break; // Down Key, move highlited value down case 40: this.hightLiteSuggestion(this.highlited + 1); rtn = false; break; // Default, get suggestions for current value default: this.getSuggestions(this.element.value); } CURRENT_SUG = this; return rtn; } // Get suggestions for current value TD_AutoSuggest.prototype.getSuggestions = function (val) { // Trim text value first val = val.replace(/^\s*|\s*$/g,""); // if no change in text, return if (val == this.currentVal) return false; // if text is less than required length, hide suggestions, then return if (val.length < this.minLength) { hideSuggestions(); return false; } this.currentVal = val; // Get cached suggestions if available var cachedSug = this.getCachedSuggestions(this.currentVal); if ( cachedSug == null ) { var p = this; try { // Firefox, Opera 8.0+, Safari this.requestObj = new XMLHttpRequest(); } catch (e) { // Internet Explorer try { this.requestObj = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { this.requestObj = new ActiveXObject("Microsoft.XMLHTTP"); } } // Open AJAX request object. var url = this.url + escape(this.currentVal); this.requestObj.onreadystatechange = function () { p.processReturnXML() }; this.requestObj.open("GET", url, true); this.requestObj.send(null); } else { // Display cached suggestions this.suggestions = cachedSug; this.showSuggestions(); } return false; } // Process return suggestion XML from server TD_AutoSuggest.prototype.processReturnXML = function () { if (this.requestObj.readyState == 4) { // only if "OK" if (this.requestObj.status == 200) { //this.onComplete( this.req ); var results = this.requestObj.responseXML.getElementsByTagName('results')[0].childNodes; // Create new list of suggestions var tempList = []; for (var i=0;i| " + txt + " |
| Close |