var activeAce = null;
function AutoCompleteExtender(name, acePanelId, fieldId, selectingSite, renderingSite, additionalFields)
{
  this.selectedIdx = -1;
  this.items = new Array();

  this.http1 = GetHTTPObject();
  this.http2 = GetHTTPObject();

  this.name = name;
  this.fieldId = fieldId;
  this.acePanelId = acePanelId;
  this.renderingSite = renderingSite;
  this.selectingSite = selectingSite;

  this.lock = false;
  this.lockGet = false;
  if (additionalFields)
    this.additionalFields = additionalFields;
}

AutoCompleteExtender.prototype.ActivateField = function()
{
  activeAce = this;
  this.ResetSuggestions(true);
  $(this.fieldId).onkeydown = this.HandleKeyDown;
  $(this.fieldId).onkeyup = this.GetSuggestions;
}

AutoCompleteExtender.prototype.HandleKeyDown = function (evt)
{
  var key = KeyCode(evt);
  switch (key)
  {
    case 27: //escape
      activeAce.ResetSuggestions(true);
      break;

    case 40: //keyDown
      if (activeAce.items && activeAce.selectedIdx < activeAce.items.length - 1)
        activeAce.SetActive(activeAce.selectedIdx + 1);
      break;

    case 38: //keyUp
      if (activeAce.items && activeAce.selectedIdx > 0)
        activeAce.SetActive(activeAce.selectedIdx - 1);
      break;

    case 13: // return
      if (activeAce.selectedIdx >= 0 && activeAce.selectedIdx < activeAce.items.length)
      {
        activeAce.MakeChoice();
      }
      break;
  }
}

AutoCompleteExtender.prototype.MakeChoice = function()
{
  this.UnLock();
  $(this.fieldId).value = /*unescape(*/this.items[this.selectedIdx].Name/*)*/;
  this.ResetSuggestions(true);
}

AutoCompleteExtender.prototype.SetActive = function(selectedIdx)
{

  var node = $(this.acePanelId).childNodes[0];
  for (var idx = 0; idx < this.items.length; ++idx)
  {
      if (!node)
        break;

      while (node.nodeType != 1)//jump over text
      {
        node = node.nextSibling
      }
      if (idx == selectedIdx)
          node.style.backgroundColor = "lightgrey";
      else
          node.style.backgroundColor = "";

      node = node.nextSibling;
  }
  this.selectedIdx = selectedIdx;
  //$(acePanelId).childNodes[active].nodeValue.style.backgroundColor = "lightgray";

}

AutoCompleteExtender.prototype.IsControlKey = function(key)
{
  return key == 13 || key == 16 || key == 17 || key == 27 ||
         key == 40 || key == 38 || key == 37 || key == 39;
}

//Getting the pure item Infos via ajax
AutoCompleteExtender.prototype.GetSuggestions = function (evt)
{
  if (activeAce.lockGet)
    return;

  var key = KeyCode(evt);

  if (activeAce.IsControlKey(key))
    return;

  activeAce.ResetSuggestions(false);
  acePanel = $(activeAce.acePanelId);
  activeAce.http1.open("POST", activeAce.selectingSite, true);

  activeAce.http1.onreadystatechange = activeAce.FillSuggestions;
  activeAce.http1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  activeAce.LockGet();
  var sendParams = "prefix=" + $(activeAce.fieldId).value;
  if (activeAce.additionalFields)
  {
    for(var idx = 0; idx < activeAce.additionalFields.length; ++idx)
    {
      var currField = activeAce.additionalFields[idx];
      sendParams += "&" + currField + "=" + GetValue($(currField));
    }
  }
  activeAce.http1.send(sendParams);

}

// Resets the suggestion items to an empty Array.
AutoCompleteExtender.prototype.FillAceDiv = function ()
{
  FillWithResponse($(activeAce.acePanelId), activeAce.http2);
}


// Resets the suggestion items to an empty Array.
AutoCompleteExtender.prototype.ResetSuggestions = function(clearPanelContents)
{
  this.selectedIdx = -1;
  this.items = new Array();
  if (clearPanelContents)
    this.ClearPanelContents(false);
}

AutoCompleteExtender.prototype.Lock = function ()
{
  this.lock = true;
}

AutoCompleteExtender.prototype.UnLock = function ()
{
  this.lock = false;
}

AutoCompleteExtender.prototype.LockGet = function ()
{
this.lockGet = true;
}

AutoCompleteExtender.prototype.UnLockGet = function ()
{
 this.lockGet = false;
}


// Clears the AutoComplete panel
AutoCompleteExtender.prototype.ClearPanelContents = function (evenIfLocked)
{
  var locked = evenIfLocked ? false : this.lock;

  if (!locked && this.acePanelId && $(this.acePanelId))
  {
    $(this.acePanelId).innerHTML = "";
    $(this.acePanelId).style.display="none";
  }
}


// The Suggestion List Control is called here via ajax.
AutoCompleteExtender.prototype.CallSuggestionList = function ()
{
    this.http2.open("POST", this.renderingSite, true);
    this.http2.onreadystatechange = this.FillAceDiv;
    this.http2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    var request = "JSAce=" + this.name;
    request += "&prefix=" + $(this.fieldId).value;
    for (var idx = 0; idx < this.items.length; ++idx)
    {
      request += "&";
      request += "Items[]=" + this.items[idx].ID;
    }
    if (this.items.length == 0)
    {
      this.ClearPanelContents(true);
    }


    this.http2.send(request);
}

// The suggestions are taken from the first ajax call to fill the Suggestion list here.
AutoCompleteExtender.prototype.FillSuggestions = function ()
{
  if (activeAce.http1.readyState == 4 && activeAce.http1.responseText != "")
  {
    activeAce.UnLockGet();
    var itemsObj = eval("(" + activeAce.http1.responseText + ")");
    activeAce.items = itemsObj.Items;
    if (activeAce.items.length > 0)
    {
      $(activeAce.acePanelId).style.display = "";
    }
    activeAce.CallSuggestionList();
  }
}

