var arrResults; var autobox = null; var textbox = null; var currentPosition = -1; /* Add event listeners The function below adds the event listeners to the passed element Parameters element - the element to add an event to eventType - the type of event to add (Example: "click" for the onclick event) functionName - the name of the function the event will call */ AutoSuggest.prototype.AddEvent = function (element, eventType, functionName) { if (element.addEventListener) /* Adds events for Mozilla browsers */ { element.addEventListener(eventType, functionName, false); return true; } else if (element.attachEvent) /* Adds events for IE browsers */{ var r = element.attachEvent('on' + eventType, functionName); return r; } else { element['on' + eventType] = functionName; } }; /* This object creates an autosuggest box for the given textbox Parameters textBoxToAddAutoSuggestTo - the textbox to add the autosuggest dropdown to */ function AutoSuggest(TextBoxToAddAutoSuggestTo, AutoSuggestCssClass) { textbox = document.getElementById(TextBoxToAddAutoSuggestTo); textbox.focus(); GetSuggestions(); this.Load(AutoSuggestCssClass); } /* Loading the autosuggest box This function below creates keyup, keydown and keypress events for the given textbox and creates the autosuggest dropdown and assigns mousedown, mouseover and mouseup events. */ AutoSuggest.prototype.Load = function(AutoSuggestCssClass) { this.AddEvent(textbox, "keyup", this.ShowSuggest); this.AddEvent(textbox, "blur", this.HideSuggestions); var divAuto = document.getElementById("divAuto"); autobox = document.createElement("div"); autobox.id = "divSuggest"; autobox.className = AutoSuggestCssClass; autobox.onmousedown = autobox.onmouseup = autobox.onmouseover = function (oEvent) { oEvent = oEvent || window.event; oTarget = oEvent.target || oEvent.srcElement; if (oEvent.type == "mousedown") { textbox.value = oTarget.firstChild.nodeValue; AutoSuggest.prototype.HideSuggestions(); } else if (oEvent.type == "mouseover") { AutoSuggest.prototype.HighlightSuggestion(oTarget); } else { textbox.focus(); } }; divAuto.appendChild(autobox); }; /* Highlighting the suggestions This function highlights the suggestion in the autosuggest dropdown Parameters suggestion - the suggestion to highlight */ AutoSuggest.prototype.HighlightSuggestion = function(suggestion) { for (var i=0; i < autobox.childNodes.length; i++) { var oNode = autobox.childNodes[i]; if (oNode == suggestion) { oNode.className = "current"; currentPosition = i; } else if (oNode.className == "current") { oNode.className = ""; } } }; /* Hiding the suggestions This function hides the suggestion when a selection is made or if no matches are found */ AutoSuggest.prototype.HideSuggestions = function() { autobox.style.visibility = "hidden"; autobox.style.display = "none"; }; /* Highlights the next suggestion in the dropdown and places the suggestion into the textbox. */ AutoSuggest.prototype.NextSuggestion = function () { var cSuggestionNodes = autobox.childNodes; if (cSuggestionNodes.length > 0 && currentPosition < cSuggestionNodes.length-1) { var oNode = cSuggestionNodes[++currentPosition]; AutoSuggest.prototype.HighlightSuggestion(oNode); textbox.value = oNode.firstChild.nodeValue; } }; /* Highlights the previous suggestion in the dropdown and places the suggestion into the textbox. */ AutoSuggest.prototype.PreviousSuggestion = function () { var cSuggestionNodes = autobox.childNodes; if (cSuggestionNodes.length > 0 && currentPosition > 0) { var oNode = cSuggestionNodes[--currentPosition]; AutoSuggest.prototype.HighlightSuggestion(oNode); textbox.value = oNode.firstChild.nodeValue; } }; /* Find position of textbox This function finds the position of the given textbox to know where to create the autosuggest dropdown Parameters obj - the textbox to create the autosuggest dropdown under */ AutoSuggest.prototype.FindPos = function(obj) { var curleft = 0; var curtop = 0; if (obj.offsetParent) { curleft = obj.offsetLeft curtop = obj.offsetTop while (obj == obj.offsetParent) { curleft += obj.offsetLeft curtop += obj.offsetTop } } return [curtop,curleft]; }; /* Show the suggestions This function fills the autosuggest dropdown with results that match the text that was entered in the given textbox. */ AutoSuggest.prototype.ShowSuggest = function(e) { var keycode; if (window.event) /* IE */ { keycode = window.event.keyCode; } else if (e) /* Mozilla */ { keycode = e.which; } if(keycode == 38 || keycode == 40 || keycode == 13) { switch(keycode) { case 38: AutoSuggest.prototype.PreviousSuggestion(); break; case 40: AutoSuggest.prototype.NextSuggestion(); break; case 13: AutoSuggest.prototype.HideSuggestions(); break; } } else { currentPosition = -1; var suggest = null; autobox.innerHTML = ""; var location = AutoSuggest.prototype.FindPos(textbox); var diffTop = location[0] - textbox.offsetTop; var diffLeft = location[1] - textbox.offsetLeft; autobox.style.top = location[0] - diffTop + textbox.offsetHeight + "px"; autobox.style.left = location[1] - diffLeft + "px"; autobox.style.width = textbox.style.width; arrSuggest = new Array(); if(textbox.value.length > 0) { for (var i=0; i < arrResults.length; i++) { if (arrResults[i].toLowerCase().indexOf(textbox.value.toLowerCase()) == 0) { arrSuggest.push(arrResults[i]); } } if(arrSuggest.length > 0) { for(var j = 0; j< arrSuggest.length; j++) { suggest = document.createElement("div"); suggest.style.padding = "5px"; suggest.appendChild(document.createTextNode(arrSuggest[j])); autobox.style.visibility = "visible"; autobox.style.display = "block"; autobox.appendChild(suggest); } } else { AutoSuggest.prototype.HideSuggestions(); } } else { AutoSuggest.prototype.HideSuggestions(); } } }; /* Gets the suggestions This functions calls a webservice and return the values that match the text being entered Parameters text - text being entered in the textbox */ function GetSuggestions() { arrResults = new Array(); arrResults[0] = "Aleica"; arrResults[1] = "Auto X"; arrResults[2] = "Blitz"; arrResults[3] = "BM8"; arrResults[4] = "Boost"; arrResults[5] = "Boost Drift"; arrResults[6] = "Circuit 10"; arrResults[7] = "Circuit 8"; arrResults[8] = "Cup"; arrResults[9] = "D1"; arrResults[10] = "Fighter"; arrResults[11] = "Fighter Drift"; arrResults[12] = "Flashback"; arrResults[13] = "Force"; arrResults[14] = "Force Drift"; arrResults[15] = "GKR"; arrResults[16] = "GR6"; arrResults[17] = "GRA"; arrResults[18] = "Gravel"; arrResults[19] = "Grid"; arrResults[20] = "Grid - 18\" Van"; arrResults[21] = "Grid Drift"; arrResults[22] = "Grid Offroad"; arrResults[23] = "Grid-V"; arrResults[24] = "GT3"; arrResults[25] = "GTR"; arrResults[26] = "GTR-D"; arrResults[27] = "HM3"; arrResults[28] = "K7"; arrResults[29] = "MSR"; arrResults[30] = "MXR"; arrResults[31] = "OSR"; arrResults[32] = "P1"; arrResults[33] = "R Spec"; arrResults[34] = "RB"; arrResults[35] = "RB"; arrResults[36] = "RB-X"; arrResults[37] = "Rev"; arrResults[38] = "RKR"; arrResults[39] = "SDM"; arrResults[40] = "SDR"; arrResults[41] = "Slipstream"; arrResults[42] = "Sub Zero"; arrResults[43] = "SVN"; arrResults[44] = "T2R"; arrResults[45] = "Torque"; arrResults[46] = "Torque Drift"; arrResults[47] = "Track R"; arrResults[48] = "Zero Plus"; arrResults[49] = "Zero Plus"; }