var autocomplete = {
  initialize: function(){
    var theBody = document.getElementsByTagName('body').item(0);
    var theResults = document.createElement('div');
    theResults.setAttribute('id', 'theResults');
    theResults.style.left = '0px';
    theResults.style.top = '0px';
    theResults.style.position = 'absolute';
    theResults.style.zIndex = '99';
    theResults.style.border = '1px solid #aaaaaa';
    theResults.style.display = 'none';
    theBody.appendChild(theResults);
  },

  fill: function(val){
    this.theInput.value = val;
    this.theInput.style.background = '#ffffff';
    theResults.innerHTML = '';
    theResults.style.display = 'none';
  },

  change: function(obj, event, arr){
    var theInput = obj;
    this.theInput = theInput;

    var theResults = document.getElementById('theResults');
    if(theInput.value == ''){
      theInput.style.background = '#ffffff';
      theResults.innerHTML = '';
      theResults.style.display = 'none';
    }
    else{
      var obj = theInput;
      if(obj.offsetParent){
        x = obj.offsetLeft;
        y = obj.offsetTop;
        h = obj.offsetHeight;
        w = obj.offsetWidth;
        while(obj = obj.offsetParent){
          x += obj.offsetLeft;
          y += obj.offsetTop;
        }
      }

      var totalChars = theInput.value.length;
      var resultsTotal = 0;
      theResults.innerHTML = '';
      var exactItem = false;
      for(i=0;i<arr.length;i++){
        if(arr[i].substr(0, totalChars).toLowerCase() == theInput.value.substr(0, totalChars).toLowerCase()){
          if(resultsTotal == 0 && event.keyCode !== 8){theInput.value = arr[i];}
          resultsStyle = 'font-family: arial; font-size: 12px; color: #000000; background:#00A0FF; text-decoration: none; padding: 5px; display: block';
          theResults.innerHTML += '<a href="javascript:autocomplete.fill(\'' + arr[i] + '\')" style="' + resultsStyle + '">' + arr[i] + '</a>';
          resultsTotal++;
        }
        if(arr[i].toLowerCase() == theInput.value.toLowerCase()){exactItem = true;}
      }
      if(resultsTotal == 0){theInput.style.background = '#ffaaaa';}
      else{theInput.style.background = '#ffffff';}

      if(event.keyCode !== 8){
        if(document.all){
          var theRange = theInput.createTextRange();
          theRange.moveStart('character', totalChars);
          theRange.moveEnd('character', theInput.value.length);
          theRange.select();
        }
        else{
          theInput.setSelectionRange(totalChars, theInput.value.length);
        }
        theInput.focus();
      }

      if(exactItem && resultsTotal < 2){
        theInput.style.background = '#ffffff';
        theResults.innerHTML = '';
        theResults.style.display = 'none';
      }
      else if(resultsTotal == 0){
        theInput.style.background = '#ffaaaa';
        theResults.innerHTML = '';
        theResults.style.display = 'none';
      }
      else{
        theResults.style.left = x + 'px';
        theResults.style.top = (y + h) + 'px';
        theResults.style.display = 'block';
      }
    }
  }
}
