$(document).ready(function(){
  var minOptions = 20;
  var idVals = [];
  var selectText = [];
  var count = 0;
  var currID;
  values = $('select.autofill'); 
  for(var ii =0; ii < values.length; ii++)
  {
    currID = $(values[ii]).attr("id");
    if($("#"+currID+" option").length > minOptions)
    {
      idVals[count] = currID;
      selectText[count] = $("#" + currID + " option:selected").attr("text");
      count++;
    }
  }
  var optionsArr = [];  
  var text = ''; 
  var value = '';
  var id = '';
  for(var ii = 0; ii < idVals.length; ii++)
  {
    var responseText = [];
    var responseVals = [];
    id = idVals[ii];
    optionsArr = ($('#' + id + ' option'));
    var style = $("select#"+id).attr("style");
    $("select#"+id).before('<input type="text" id="'+id+'IN" name="'+id+'IN" style="'+style+'" />');
    $("select#"+id).hide();

    $("#"+id+"IN").attr("value",selectText[ii]);
    $("#"+id+"IN").click(function(){ 
      var tmpTxt = ($(this).attr("id"));
      tmpTxt = tmpTxt.substring(0,tmpTxt.length-2);
      prevVal = $("#"+tmpTxt+" option:selected").text();
      $(this).val('');
      $(this).blur(function(){ $(this).val(prevVal); });
    });
    //var t1 = new TextboxList('#'+id, {max:'1',unique: true, plugins: {autocomplete: {minLength:'1'}}});
    //t1.getContainer().addClass('textboxlist-loading');				


    for(var jj = 0; jj < optionsArr.length; jj++)
    {
      //[31, 'Bit Plain Text', 'Bit html', 'Suggestion item html']
      text = $(optionsArr[jj]).text(); 
      value = $(optionsArr[jj]).val();
      responseText[jj] = javaTrim(text);
      responseVals[jj] = value;
      //response[jj] = [jj,text,text,''];
    }
    
var data = "Core Selectors Attributes Traversing Manipulation CSS EMMM everest effort Events Effects Ajax Utilities".split(" ");

    var action = '';
    $("#"+id+"IN").autocomplete({
      width: 300,
      delimiter: /(,|;)\s*/,
      data:responseVals,
      lookup:responseText,
      onSelect: function(value, data, me){  
        var tmpTxt = ($(me.el).attr("id"));
        tmpTxt = tmpTxt.substring(0,tmpTxt.length-2);
        $("select#"+tmpTxt).val(value);
        $("select#" + tmpTxt + " option[value='"+data+"']").attr("selected",true);
        action = $("select#"+tmpTxt).attr("onChange");
        eval(action);
        prevVal = value;
      }
    });
  }
  
})