// CAPXOUS JavaScript Library (Version 1.1.6 r20060818002754)
var isKHTML = navigator.appVersion.match(/Konqueror|Safari|KHTML/);
var isOpera = navigator.userAgent.indexOf('Opera') > -1;
var isIE = !isOpera && navigator.userAgent.indexOf('MSIE') > 1;
var isMoz = !isOpera && !isKHTML && navigator.userAgent.indexOf('Mozilla/5.') == 0;
var CAPXOUS = new Object();


CAPXOUS = {removeBraces:function(text) {
  return text.substring(text.indexOf('{') + 1, text.lastIndexOf('}'));
},updateStyle:function(e) {
  while (e = e.parentNode) {
    if (e.style) {
      if (e.style.overflow == 'hidden')e.style.overflow = 'visible';
      if (e.style.tableLayout == 'fixed')e.style.tableLayout = 'auto';
    }
  }
}};

CAPXOUS.AutoComplete = Class.create();
Object.extend(CAPXOUS.AutoComplete, {
  style:{wait:'CAPXOUS_AutoComplete_waiting'},findPopup:function(v) {
  var e = Event.element(v);
  while (e && e.parentNode && !e.object)e = e.parentNode;
  if (e == null)return null;
  return e.parentNode?e:null;
},isSelectable:function(e) {
  return(e.nodeType == 1) && (e.getAttribute('onselect'));
},findSelectable:function(v, p) {
  var e = Event.element(v);
  while (e.parentNode && (e != p) && (!CAPXOUS.AutoComplete.isSelectable(e)))e = e.parentNode;
  return(e.parentNode && (e != p))?e:null;
},process:function(e, o) {
  var ajaxHref = e.getAttribute('ajaxHref');
  if (!Element.hasClassName(e, 'usual')) {
    var url;
    if (ajaxHref)url = e.getAttribute('ajaxHref'); else url = e.getAttribute('href');
    o.request(url);
  }
},click:function(v) {
  var cls = CAPXOUS.AutoComplete;
  var e = Event.element(v);
  var p = cls.findPopup(v);
  if (p) {
    var s = cls.findSelectable(v, p);
    if (s) {
      p.object.i = s.getAttribute(cls.index);
      p.object.select();
    } else {
      while (e.parentNode && (e != p) && (!e.tagName || e.tagName.toUpperCase() != 'A'))e = e.parentNode;
      if (e.parentNode && (e != p))cls.process(e, p.object, v);
    }
  } else {
    cls.inst.each(function(i) {
      if (i.text != e && i.update != e)setTimeout(i.hide.bind(i), 10);
    });
  }
},mouseover:function(v) {
  var cls = CAPXOUS.AutoComplete;
  var p = cls.findPopup(v);
  if (p) {
    var s = cls.findSelectable(v, p);
    if (s)p.object.highlight(s.getAttribute(cls.index));
  }
},init:function() {
  var p = document.createElement('div');
  p.className = CAPXOUS.AutoComplete.style.wait;
  var s = p.style;
  s.display = 'inline';
  s.position = 'absolute';
  s.width = s.height = s.top = s.left = '0px';
  document.body.appendChild(p);
  if (isIE)CAPXOUS.selfName = self.name;
},index:'index',inst:new Array(),name:'',key:'',getWindowHeight:function() {
  var h = 0;
  if (typeof(window.innerHeight) == 'number') {
    h = window.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    h = document.documentElement.clientHeight;
  } else if (document.body && document.body.clientHeight) {
    h = document.body.clientHeight;
  }
  ;
  return parseInt(h);
},getStyle:function(e) {
  if (!isKHTML && document.defaultView && document.defaultView.getComputedStyle) {
    return document.defaultView.getComputedStyle(e, null);
  } else if (e.currentStyle) {
    return e.currentStyle;
  } else {
    return e.style;
  }
},getInt:function(s) {
  var i = parseInt(s);
  return isNaN(i)?0:i;
}});
Event.observe(window, 'load', CAPXOUS.AutoComplete.init);

CAPXOUS.AutoComplete.prototype = {visible:false,complete:false,initialized:false,initialize:function(text, f, options) {
  text = $(text)?$(text):document.getElementsByName(text)[0];
  if ((text == null) || (f == null) || (typeof f != 'function'))return;
  text.setAttribute('autocomplete', 'off');
  this.txtBox = this.text = text;
  this.keydownX = this.keydown.bindAsEventListener(this);
  this.prepareX = this.prepare.bind(this);
  Event.observe(this.text, 'keydown', this.keydownX);
  Event.observe(this.text, 'keyup', this.keydownX);
  Event.observe(this.text, 'dblclick', this.prepareX);
  this.options = options || {};
  this.options.frequency = this.options.frequency || 0.4;
  this.options.minChars = this.options.minChars || 1;
  this.timeout = 0;
  this.getURL = f;
  this.buffer = document.createElement('div');
  var p = document.createElement('div');
  p.object = this;
  Element.addClassName(p, 'CAPXOUS_AutoComplete');
  var ps = p.style;
  ps.position = 'absolute';
  ps.top = '-999px';
  ps.height = 'auto';
  Element.hide(p);
  this.update = p;
  this.i = -1;
  var cls = CAPXOUS.AutoComplete;
  cls.inst.push(this);
  if (cls.inst.length == 1) {
    Event.observe(document, 'click', cls.click);
    Event.observe(document, 'mouseover', cls.mouseover);
  };

  this.cls = cls;
},page:function(name) {
  var s = document.getElementsByClassName(name);
  var e = s.first();
  if (e && e.tagName && e.tagName.toUpperCase() == 'A')this.cls.process(e, this);
},keyPress:function(event) {
  var keyCode = event.keyCode;
  if (keyCode == 38 || keyCode == 40) {
    if (this.complete) {
      (keyCode == 38)?this.up():this.down();
      this.show();
    };
  } ;

  if (keyCode == 33 || keyCode == 34) {
    if (this.complete)(keyCode == 33)?this.page('page_up'):this.page('page_down');
  };

  if (keyCode == 27)this.hide();
  if (keyCode == 38 || keyCode == 40 || keyCode == 33 || keyCode == 34 || keyCode == 27 || keyCode == 13) {
    Event.stop(event);
    return;
  }
  ;
  switch (keyCode) {case 9:case 37:case 39:case 35:case 36:case 45:case 16:case 17:case 18:break;default:if (this.timeout != 0)clearTimeout(this.timeout);this.clean();this.timeout = setTimeout(this.prepare.bind(this), this.options.frequency * 1000);this.hide();}
},keydown:function(event) {
  var keyCode = event.keyCode;
  if (keyCode == 13) {
    Event.stop(event);
    if (event.type == 'keyup') {
      if (this.latestKeyCode == 13 && this.latestType == 'keydown') {
        this.visible?this.select():this.request();
      } else {
        this.request();
      }
    }
  }
  ;
  this.latestType = event.type;
  this.latestKeyCode = event.keyCode;
  if (event.type == 'keydown' && keyCode != 13)this.keyPress(event);
},clean:function() {
  this.hide();
  if (this.latestRequest)this.latestRequest.transport.abort();
},select:function() {
  if (this.getCurrentEntry()) {
    var stat = this.getCurrentEntry().getAttribute('onselect');
    try {
      eval(stat);
    } catch(e) {
    }
    ;
    this.hide();
  }
},getCurrentEntry:function() {
  return this.children?this.children[this.i]:null;
},highlight:function(i) {
  if (!this.complete)return;
  Element.removeClassName(this.getCurrentEntry(), 'current');
  this.i = i;
  Element.addClassName(this.getCurrentEntry(), 'current');
},up:function() {
  if (this.i > -1)this.highlight(this.i - 1);
},down:function() {
  if (this.i < this.children.length - 1)this.highlight(this.i + 1);
},preRequest:function() {
  return this.text.value.length >= this.options.minChars;
},prepare:function() {
  this.request();
},request:function(url) {
  if (this.preRequest()) {

    // Niet zoeken als URL de boolean waarde false heeft.
    if (!this.getURL()) {
      return;
    }
    if (this.getAjaxURL)this.getAjaxURL();
    if (url)this.onLoading(true); else this.onLoading();
    var requestURL = this.getURL();
    if (typeof requestURL == 'string') {
      if (url) {
        if ((url.charAt(0) == '?') || ((url.indexOf(location.href) == 0) && (url.charAt(location.href.length) == '?'))) {
          if (url.charAt(0) != '?')url = url.substr(location.href.length);
          requestURL += '&' + url.substr(1);
        } else {
          requestURL = url;
        }
        ;
      }
      ;
      this.requestURL = requestURL = encodeURI(requestURL);
      this.latestRequest = new Ajax.Updater(this.buffer, requestURL, {method:'get',onComplete:this.onComplete.bind(this),onFailure:this.onFailure.bind(this)});
    }
  }
},onFailure:function(transport) {
},onLoading:function() {
  this.complete = false;
  this.i = -1;
  if (!arguments[0]) {
    this.hide();
    this.update.innerHTML = '';
  }
  ;
  this.startIndicator();
},onComplete:function() {
  setTimeout(this.updateContent.bind(this, arguments[0]), 10);
},core:function() {
  if (!this.initialized) {
    this.initialized = true;
    document.body.appendChild(this.update);
  }
  ;
  this.i = -1;
  this.children = new Array();
  if (isIE)this.fixIE();
  $A(this.update.getElementsByTagName('a')).each(function(a) {
    if (!Element.hasClassName(a, 'usual')) {
      a.onclick = function() {
        return false;
      };
    }
    ;
  });
  $A(this.update.getElementsByTagName('*')).each(function(c) {
    if (this.cls.isSelectable(c)) {
      c.setAttribute(this.cls.index, this.children.length);
      Element.addClassName(c, 'selectable');
      this.children.push(c);
    }
  }.bind(this));
  this.complete = true;
  this.down();
  this.show();
  this.stopIndicator();
},updateContent:function() {
  var tx = this.latestRequest.transport;
  var t = ((this.latestRequest == null) || (tx == arguments[0]));
  if (t) {
    if (this.latestRequest.url != this.requestURL)return;
    this.complete = true;
    try {
      var success = this.latestRequest.responseIsSuccess();
    } catch(e) {
      return;
    }
    ;
    if (success) {
      var text = null;
      if ((tx.responseXML) && (tx.responseXML.documentElement)) {
        var docE = tx.responseXML.documentElement;
        if (docE.nodeName == 'string') {
          if (docE.text) {
            text = docE.text;
          } else if (docE.textContent) {
            text = docE.textContent;
          } else if (docE.firstChild.nodeValue) {
            text = docE.firstChild.nodeValue;
          }
        }
      }
      ;
      if (text == null)text = this.buffer.innerHTML;
      this.update.innerHTML = text;
    } else {
      this.update.innerHTML = '<h1 align="center">' + tx.status + ' ' + (tx.statusText?tx.statusText:'') + '</h1>';
    }
    ;
    this.buffer.innerHTML = '';
  }
  ;
  this.core();
},offset:function() {
  var o = 0;
  if (isMoz || isKHTML || (isIE && (document.compatMode != 'BackCompat'))) {
    var bl = 'border-left-width';
    var br = 'border-right-width';
    var pl = 'padding-left';
    var pr = 'padding-right';
    var f = new Function('e', 'p', 'return CAPXOUS.AutoComplete.getInt(Element.getStyle(e, p));');
    o = f(this.update, bl) + f(this.update, br) + f(this.update, pl) + f(this.update, pr);
  }
  ;
  return o;
},fixIE:function() {
  this.update.innerHTML += "<img style='width:0px;height:0px;clear:both' align='right'/>";
},fixIEOverlapping:function() {
  var f;
  if (!(f = this.iefix)) {
    f = document.createElement('iframe');
    f.src = 'javascript:false;';
    var fs = f.style;
    fs.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity = 0)";
    fs.position = 'absolute';
    fs.margin = fs.padding = '0px';
    Element.hide(f);
    document.body.appendChild(f);
    this.iefix = f;
  }
  ;
  self.name = CAPXOUS.selfName;
  Position.clone(this.update, f);
  f.style.zIndex = 1;
  this.update.style.zIndex = 2;
  Element.show(f);
},show:function() {
  var pos = Position.cumulativeOffset(this.text);
  var tt = pos[1];
  var th = this.text.offsetHeight;
  var tl = pos[0];
  var tw = this.text.offsetWidth;
  var wh = this.cls.getWindowHeight();
  Element.setStyle(this.update, {top:'-999px',left:'-999px',width:tw + 'px',height:'auto'});
  Element.show(this.update);
  var ph = this.update.offsetHeight;
  Element.hide(this.update);
  var pt;
  var of;
  if ((Position.page(this.text)[1] + th + ph <= wh) || (tt - ph < 0)) {
    pt = tt + th;
    of = th;
  } else {
    pt = tt - ph;
    of = -ph;
  }
  ;
  tw = tw - this.offset();
  Element.setStyle(this.update, {top:pt + 'px',left:tl + 'px',width:tw + 'px',height:'auto'});
  if (isIE && this.update.filters.length == 0)this.update.style.filter = "filter: progid:DXImageTransform.Microsoft.DropShadow(OffX=2, OffY=2, Color='#c0c0c0', Positive='true') progid:DXImageTransform.Microsoft.Fade(duration=0.25,overlap=1.0)";
  if (isIE && !this.visible) {
    this.update.filters[1].apply();
    this.update.filters[1].play();
  }
  ;
  Element.show(this.update);
  if (isIE)this.fixIEOverlapping();
  this.visible = true;
},hide:function() {
  if (this.visible) {
    Element.hide(this.update);
    if (isIE)Element.hide(this.iefix);
    this.visible = false;
  }
},startIndicator:function() {
  Element.addClassName(this.text, this.cls.style.wait);
  if (this.options.indicator)Element.show(this.options.indicator);
},stopIndicator:function() {
  Element.removeClassName(this.text, this.cls.style.wait);
  if (this.options.indicator)Element.hide(this.options.indicator);
}};
if (typeof AjaxPro != 'undefined') {
  Object.extend(CAPXOUS.AutoComplete.prototype, {isAjaxPro:false,forAJAXPro:function(text, b) {
    if (this.ajaxRequest == b) {
      this.update.innerHTML = text.value;
      this.core();
    }
  },getAjaxURL:function() {
    if (!this.isAjaxPro) {
      var source = this.getURL.toString();
      source = CAPXOUS.removeBraces(source);
      source = source.replace('CAPXOUS', 'this.forAJAXPro.bind(this), null, function() { this.doStateChange() }, null, null, function(a, b) { if (a == 0) this.ajaxRequest = b; }.bind(this)');
      this.getURL = new Function(source).bind(this);
      this.isAjaxPro = true;
    }
  }});
}
;