/** * AutoComplete Field - JavaScript Code */ // global variables var acListTotal = 0; var acListCurrent = -1; var acDelay = 0; var acURL = null; var acSearchId = null; var acResultsId = null; var acSearchField = null; var acResultsDiv = null; var acResultURL = null; // parameters: the id of the input field, the id of the results div, // the URL of the remote script and the Q&A function setAutoComplete(field_id, results_id, get_url, result_url){ // initialize vars acSearchId = "#" + field_id; acResultsId = "#" + results_id; acURL = get_url; acResultURL = result_url; // create the results div $("body").append('
'); // register mostly used vars acSearchField = $(acSearchId); acResultsDiv = $(acResultsId); // remove browsers autocomplete acSearchField.attr("autocomplete","off"); // reposition div repositionResultsDiv(); // on blur listener. Delays the blur event hiding the suggestion box // by 200 ms acSearchField.blur(function(){ setTimeout("clearAutoComplete()", 200) }); // on key up listener acSearchField.keyup(function (e) { //window.alert(e.which); var tmpKeyCode = e.which; if (tmpKeyCode == undefined) { tmpKeyCode = window.event.keyCode; } if (tmpKeyCode == 0){ if ($(this).attr('value').slice(-1).keyCode == "Å") { keyCode = 197; } else if ($(this).attr('value').slice(-1).keyCode == "Ä") { keyCode = 196; } else if ($(this).attr('value').slice(-1).keyCode == "Ö") { keyCode = 214; } else if ($(this).attr('value').slice(-1).keyCode == "å") { keyCode = 229; } else if ($(this).attr('value').slice(-1).keyCode == "ä") { keyCode = 228; } else if ($(this).attr('value').slice(-1).keyCode == "ö") { keyCode = 246; } } else { // get keyCode (window.event is for IE) var keyCode = e.keyCode || window.event.keyCode; } var lastVal = acSearchField.val(); // check and treat up and down arrows if(updownArrow(keyCode)){ return; } // check for ESC if(keyCode == 27){ clearAutoComplete(); return; } // check for ENTER if(keyCode == 13){ window.location = acResultURL+encodeURIComponent(acSearchField.val()); clearAutoComplete(); return; } // if is text, call with delay setTimeout(function () {autoComplete(lastVal)}, acDelay); }); } // treat the auto-complete action (delayed function) function autoComplete(lastValue) { // get the field value var part = acSearchField.val(); // if it's empty clear the resuts box and return if(part == ''){ clearAutoComplete(); return; } // if it's equal the value from the time of the call, allow if(lastValue != part){ return; } //alert(part); // get remote data as JSON $.getJSON(acURL + part, function(json){ // get the total of results var ansLength = acListTotal = json.length; //alert(ansLength); // if there are results populate the results div if(ansLength > 0){ var newData = ''; // create a div for each result for(i=0; i < ansLength; i++) { newData += '