/* ~~~~~~~~~~~~~ Schlagwortsuche mit AJAX ~~~~~~~~~~~~ */
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

var _ms_XMLHttpRequest_ActiveX = "";

String.prototype.trim = function() {
  a = this.replace(/^\s+/, '');
  return a.replace(/\s+$/, '');
};
function handleKeyDown(/*Control*/ control, /*Event*/ event)
{
  if (event.keyCode == 13)
  {
    control.skipHide = false;
    hideChoicesLayer(control);
    if (control.selectionPerformsPost) { postForm(control); return false; }
    return control.selectionPerformsPost ? false : true;
  }
  return true;
}
function postForm(/*Control*/ control)
{
  while (control != null)
  {
    if (control.nodeName == "FORM") {
      
      control.submit();
      break;
    }
    control = control.parentNode;
  }
}
function handleMouseOver(/*Node*/ target, /*Boolean*/ takeText)
{
  var control = findDataBinder(target);
  if (control.selectorIndex != -1)
  {
    var choicesLayer = findChoicesLayer(target);
    if (choicesLayer)
    {
      var childNodes = choicesLayer.childNodes[0].childNodes;
      if (childNodes[control.selectorIndex].nodeName == "LI") childNodes[control.selectorIndex].className = '';              
    }
  }
  if (target.nodeName == "LI") target.className = 'active';
  control.selectorIndex = $('#search-results').children().index(target);
  $("input[id=keywordId]").attr("value", target.getElementsByTagName("a")[0].id);
  if (takeText)
  {
    control.value = target.getElementsByTagName("a")[0].innerText ? target.getElementsByTagName("a")[0].innerText : target.getElementsByTagName("a")[0].textContent.trim();
  }
}
function findDataBinder(/*Control*/ control)
{
  while (control)
  {
    if (control.dataBinding) return control.dataBinding;
    control = control.parentNode;
  }
  return null;
}
function findChoicesLayer(/*Control*/ control)
{
  while (control)
  {
    if (control.dataBinding) return control;
    control = control.parentNode;
  }
  return null;
}
function handleMouseOut(/*Node*/ target)
{
  var control = target.choicesLayer ? target: findDataBinder(target);
  if (control.selectorIndex != -1)
  {
    var choicesLayer = control.choicesLayer;
      var childNodes = choicesLayer.childNodes[0].childNodes;
      if (childNodes[control.selectorIndex].nodeName == "LI") childNodes[control.selectorIndex].className = '';              
      control.selectorIndex = -1;
  }
}
function handleSelectorUp(/*Control*/ control, event)
{
  var choicesLayer = control.choicesLayer;
  if (choicesLayer.style.display == 'block')
  {
    var childNodes = choicesLayer.childNodes[0].childNodes;
    if (control.selectorIndex > 0)
    {
      handleMouseOver(childNodes[control.selectorIndex - 1], true);
    }
  }
  return false;
}
function handleSelectorDown(/*Control*/ control, event)
{
  var choicesLayer = control.choicesLayer;
  if (choicesLayer.style.display == 'block')
  {
    var childNodes = choicesLayer.childNodes[0].childNodes;
    if ((control.selectorIndex + 1) < childNodes.length)
    {
      handleMouseOver(childNodes[control.selectorIndex + 1], true);
    }
  }
  return false;
}
function hideChoicesLayer(/*Node*/ node, /*int*/ delay)
{
  if (delay)
  {
    node.skipHide = false;
    window.setTimeout("hideChoicesLayer(document.getElementById('" + node.id + "'))", delay);
  }
  else
  {
    if ((node.choicesLayer) && (!node.skipHide))
    {
      node.choicesLayer.style.display = "none";
    }
  }
}
function registerChoicesNode(/*Node*/ target, /*ChoicesElement*/ choices, /*bool*/ selectionPerformsPost, /*Function*/ createUrlFunction, /*String*/ initialValue)
{
  if (!target.onkeydown)
  {
    target.onkeydown = function(event) { return handleKeyDown(this, event ? event : window.event); };
    target.onkeyup = function(event) { return lookup(this, event ? event : window.event); };
    target.onblur = function(event) { 
      hideChoicesLayer(this, 250); if(this.value == '') { this.value=initialValue; this.className += ' hint'} 
    };
    target.onfocus = function(event) { 
      if(this.value == initialValue) { 
        this.value = ''; this.className = this.className.substring(0, this.className.length - 5); 
      } 
      return lookup(this);  
    };
    target.selectionPerformsPost = selectionPerformsPost;
    target.createUrl = createUrlFunction;
    var choicesLayer = document.getElementById(choices);
    target.choicesLayer = choicesLayer;
    target.selectorIndex = -1;
    choicesLayer.dataBinding = target;
  }
}
function lookup(/*Control*/ control, /*Event*/ event)
{
  if (event)
  {
    switch (event.keyCode)
    {
      case 13:
        return true;
      case 38:
        return handleSelectorUp(control, event);
      case 40:
        return handleSelectorDown(control, event);
      default:
        handleMouseOut(control);
    }
  }
  var choicesLayer = control.choicesLayer;
  var text = control.value;
  control.skipHide = true;
  if ((control.currentLookup != null) && (control.currentLookup.AJAX.readyState != 4))
  {
    control.currentLookup.AJAX.abort();
    control.currentLookup = null;
  }
  if (text != "")
  {
    control.currentLookup = new AJAXRequest( "GET", control.createUrl(text), null, displayResult, true, false);
    control.currentLookup.choicesLayer = choicesLayer;
    control.currentLookup.AJAX.send("");
  }
  else
  {
    control.skipHide = false;
    hideChoicesLayer(control);
  }
  return true;
}
function displayResult(request)
{
  var choicesLayer = request.choicesLayer;
  if (choicesLayer)
  {
    var responseText = request.AJAX.responseText;

    if (responseText.trim() != "")
    {      
      choicesLayer.innerHTML = responseText;
      choicesLayer.style.display = "block";

      var childNodes = choicesLayer.childNodes[0].childNodes;
      for (var i=0; i < childNodes.length; i++) 
      {
        var node = childNodes[i];
        node.style.cursor = 'hand';
        node.style.width = '100%';
        node.onmouseover = function(event) { handleMouseOver(this, false); };
        node.onmouseout = function(event) { handleMouseOut(this, false); };
        node.onclick = function(event) {
          handleMouseOver(this, true);
          var control = findDataBinder(node);
          if (control && control.selectionPerformsPost) postForm(control);
        };
        node.index = i;
      }
    }
    else
    {
      choicesLayer.style.display = "none";
    }
  }
}
function handleClick(/*Node*/ node)
{
  var control = findDataBinder(node);
  if (control)
  {
    if (control.selectionPerformsPost) { postForm(control); return false; }
  }
}
function AJAXRequest( method, url, data, process, async, dosend) {
    var self = this;
    if (window.XMLHttpRequest) {
        self.AJAX = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        if (_ms_XMLHttpRequest_ActiveX) {
            self.AJAX = new ActiveXObject(_ms_XMLHttpRequest_ActiveX);
        } else {
      var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
                        "Microsoft.XMLHTTP"];
            for (var i = 0; i < versions.length ; i++) {
                try {
                    self.AJAX = new ActiveXObject(versions[i]);
                    if (self.AJAX) {
                        _ms_XMLHttpRequest_ActiveX = versions[i];
                        break;
                    }
                }
                catch (objException) {
                } ;
            }
        }
    }
    if (typeof process == 'undefined' || process == null) {
        process = executeReturn;
    }
    self.process = process;
    self.AJAX.onreadystatechange = function( ) {
        if ((self.AJAX.readyState == 4) && (self.AJAX.status == 200)) self.process(self);
    }
    if (!method) {
        method = "POST";
    }
    method = method.toUpperCase();
    if (typeof async == 'undefined' || async == null) {
        async = true;
    }
    self.AJAX.open(method, url, async);
    if (method == "POST") {
        self.AJAX.setRequestHeader("Connection", "close");
        self.AJAX.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        self.AJAX.setRequestHeader("Method", "POST " + url + "HTTP/1.1");
    }
    if ( dosend || typeof dosend == 'undefined' ) {
      if ( !data ) data=""; 
      self.AJAX.send(data);
    }
    return self;
}

