/**
* Json key/value autocomplete for jQuery
* Provides a transparent way to have key/value autocomplete
* Copyright (C) 2008 Ziadin Givan www.CodeAssembly.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
*
* Examples
* $("input#example").autocomplete("autocomplete.php");//using default parameters
* $("input#example").autocomplete("autocomplete.php",{minChars:3,timeout:3000,validSelection:false,parameters:{'myparam':'myvalue'},before : function(input,text) {},after : function(input,text) {}});
* minChars = Minimum characters the input must have for the ajax request to be made
* timeOut = Number of miliseconds passed after user entered text to make the ajax request
* validSelection = If set to true then will invalidate (set to empty) the value field if the text is not selected (or modified) from the list of items.
* parameters = Custom parameters to be passed
* after, before = a function that will be caled before/after the ajax request
*/
jQuery.fn.autocomplete = function(url, settings )
{
return this.each( function()//do it for each matched element
{
//this is the original input
var textInput = jQuery(this);
//create a new hidden input that will be used for holding the return value when posting the form, then swap names with the original input
textInput.after('').attr("name", textInput.attr("name") + "_text");
var valueInput = jQuery(this).next();
//create the ul that will hold the text and values
valueInput.after('
');
var list = valueInput.next().children();
var oldText = '';
var typingTimeout;
var size = 0;
var selected = -1;
settings = jQuery.extend(//provide default settings
{
minChars : 1,
timeout: 300,
width: null,
after : null,
before : null,
select : null,
error: null,
cleanSelection: null,
validSelection : true,
parameters : {'inputName' : valueInput.attr('name'), 'inputId' : textInput.attr('id')}
} , settings);
function getData(text)
{
window.clearInterval(typingTimeout);
if (text != oldText && (settings.minChars != null && text.length >= settings.minChars))
{
clear();
if (settings.before != null)
{
settings.before(textInput,text);
}
textInput.addClass('autocomplete-loading');
settings.parameters.value = text;
jQuery.ajax({
url: url,
data: settings.parameters,
contentType: "application/json; charset=utf-8",
scriptCharset: "utf-8",
dataType: 'json',
success: function(data) {
var items = '';
if (data)
{
size = data.length;
for (i = 0; i < data.length; i++)//iterate over all options
{
items += '