datasheet = false; forceappend = true; show_suggestions = true;

function addFilter(frm,loc) {
  var key = '*';
  if ( frm.val.value == 'add filter' ) {
    frm.val.focus();
    return false;
  }
  var val = encodeURIComponent(frm.val.value);
  if ( val == 'add filter' || val == '' ) return false;
  if ( frm.val.value.indexOf(":") > -1) { 
    var x = frm.val.value.split(":");
    key = encodeURIComponent(x[0]);val = encodeURIComponent(x[1]);
  }
  window.location = loc+key+'/'+val+'/';
  return false;
}
function e(a) {
  return document.getElementById(a);
}
function c(a) {
  return document.createElement(a);
}
function cc(s) {
  ns = s.split('|');
  r = [];
  for( i in ns ) { n = ns[i];
    var as = n.split(','); var f = true;
    for ( j in as ) { a = as[j];
      if (f) { el = c(a); r.push(el); }
      else {
        a=a.split('=');
        if ( a[0] == 'w' ) el.style.width = a[1];
        else if ( a[0] == 'c' ) el.className = a[1];
        else if ( a[0] == 'v' ) el.value = a[1];
        else el.setAttribute(a[0],a[1]);
      }
      f = false;
    }
  }
  return r; 
}
function ai() {
  forceappend = false; as(''); forceappend = true;
}
function as(nm) {
  f = document.getElementById('addb_container');
  even = false; oddempty = false; prev = null; ind = 0;
  for ( el in f.childNodes ) {
    cn = f.childNodes[el];
    if ( cn.tagName == 'INPUT' ) {
      if ( forceappend && cn.value == '' && cn.className != 'imagesubmit' ) {
        if ( even && oddempty ) {
          prev.value = nm;
          sync.call(sgs[ind-1]);
          return;
        } 
      }
      if ( ! even ) oddempty = cn.value == '';
      even = ! even;
      if ( even ) { prev = cn; ind ++; } 
    }
  }
  ns = cc('BR,c=clear|DIV,c=input_left|INPUT,name=key[],v='+nm+',w=200px|DIV,c=input_right|DIV,c=input_left|INPUT,name=val[],w=400px|DIV,c=input_right');
  sgs.push( new KeySuggest(ns[2],new ValueSuggest(ns[5])) );sync.call(sgs[sgs.length-1]);
  for ( i in ns ) { n = ns[i];
    f.insertBefore(n,prev);
  }  
}
function focusnext(i) {
  f = document.getElementById('addb_container');
  n = false;
  for ( el in f.childNodes ) {
    cn = f.childNodes[el];
    if ( cn.className == 'imagesubmit' ) break;
    if ( n && cn.tagName == 'INPUT') { cn.focus(); return; }
    if ( cn == i ) n = true;
  }
  forceappend = false; as(''); forceappend = true;
  focusnext(i);
}
function ajax(a,p,f,r) {
  p = typeof(p) != 'undefined' ? p : null;  
  var xmlhttp;
  if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    return false;
  }
  xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4){
      if ( typeof(r) != 'undefined' ) {
        f.call(r,eval(xmlhttp.responseText));
      } else {
        f(eval(xmlhttp.responseText));
      }  
    }
  }
  var t = (p==null) ? "GET" : "POST";
  xmlhttp.open(t, "/ajax/"+a,true);
  if ( t == "POST" ) {
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", p.length);
    xmlhttp.setRequestHeader("Connection", "close");
  }
  xmlhttp.send(p);
}

function fill(res) {
  if ( this.canceldrop ) return;
  var o = this;
  el = this.dropdown;
  this.selecteditem = null;
  while (el.hasChildNodes()) {
    el.removeChild(el.firstChild);
  }
  for( var a in res ) {
    i = document.createElement('li');
    i.appendChild(document.createTextNode(res[a]));
    i.style.cursor = 'default';
    i.onmouseover = function() {change.call(o,this);}
    i.onmousedown = function() {typein.call(o);}
    el.appendChild(i);
  }
  if ( (! this.shown) || (res.length > 0) || ((res.length==1)&&(res[0]!=this.input.value)) ) show.call(this);
  if ( (this.shown) && ((res.length == 0) || ((res.length==1)&&(res[0]==this.input.value))) ) hide.call(this);
}

function change(d) {
  if (typeof d == 'object' ) {
    if ( this.selecteditem != null ) this.selecteditem.className = '';
    this.selecteditem = d;
    d.className = 'active';
    return;
  }
  el = this.dropdown;
  c = el.childNodes.length;
  if ( c < 1 ) return;
  for( i = 0; i < c; i++ ) {
    li = el.childNodes[i];
    if ( li.className == 'active' ) {
      li.className = '';
      j = i - d;
      if ( j < 0 ) j = 0;
      if ( j > c-1) j = c - 1;
      el.childNodes[j].className = 'active';
      this.selecteditem = el.childNodes[j];
      return;
    }
  }
  el.childNodes[0].className = 'active';
  this.selecteditem = el.childNodes[0];
}

function typein() {
  if ( this.selecteditem != null ) {
    if ( this.placeholdertext == 'add filter' ) {
      var kn = '';var va = this.input.value;
      if ( va.indexOf(":") > -1 ) {
        var x = va.split(":");
        kn = x[0]; va = x[1];
        this.input.value = kn + ":" + this.selecteditem.firstChild.nodeValue;
      } else {
        this.input.value = this.selecteditem.firstChild.nodeValue;
      }
    } else {
      this.input.value = this.selecteditem.firstChild.nodeValue;
    }
    sync.call(this);
    this.selecteditem = null;
    this.forcehide = true;
    hide.call(this);
    return false;
  }
  if ( datasheet ) {
    focusnext(this.input);
    return false;
  }
  return true;
}

function sync() {
  if ( this.valuesuggest ) {
    this.valuesuggest.keyname = this.input.value == '' ? '*' : this.input.value;
  }
}

function suggest() {
  if ( ! this.hasdropdown ) initDropdown.call(this);
  if ( this.forcehide == true ) return;
  if ( this.shown && this.oldvalue == this.input.value ) return;
  this.oldvalue = this.input.value;
  if ( this.placeholdertext == 'add filter' ) {
    var kn = '';var va = this.input.value;
    if ( va.indexOf(":") > -1 ) {
      var x = va.split(":");
      kn = x[0]; va = x[1];
    }
    ajax("suggest/value/"+kn+"/"+va,null,fill,this);
  } else if ( typeof this.keyname != 'undefined' ) {
    ajax("suggest/value/"+this.keyname+"/"+this.input.value,null,fill,this);
  } else {
    ajax("suggest/key/"+this.input.value,null,fill,this);
  }
}

function hide() {
  this.shown = false;
  this.dropdown.style.display = 'none';
}

function show() {
  this.shown = true;
  this.dropdown.style.display = 'block';
}

function getElLeft() {
  var n = this.input;
  var l = -2;
  while(n.tagName != "BODY") {
    l += n.offsetLeft;
    n = n.offsetParent;
  }
  return l;
}

function getElTop() {
  var n = this.input;
  var t = n.offsetHeight -1;
  while(n.tagName != "BODY") {
    t += n.offsetTop;
    n = n.offsetParent;
  }
  return t;
}

function initDropdown() {
  this.dropdown = document.createElement('ul');
  this.dropdown.className = 'suggestions';
  oleft = getElLeft.call(this);
  owidth = this.input.offsetWidth + 2;
  if ( this.input.className.indexOf('simple') > -1 ) {
    oleft += 2;
    owidth -= 4;
  }
  this.dropdown.style.left = oleft + 'px';
  this.dropdown.style.top = getElTop.call(this) + 'px';
  this.dropdown.style.width = owidth + 'px';
  document.body.appendChild(this.dropdown);
  this.hasdropdown = true;
}

function init(place) {
  var o = this;
  var i = this.input;
  this.shown = false;
  this.hasdropdown = false;
  this.selecteditem = null;
  this.forcehide = false;
  this.canceldrop = false;
  if ( place ) {
    this.placeholder = true;
    this.placeholdertext = this.input.value;
    if ( i.className == 'simple' ) {
      i.className = 'simple placeholder';
    } else {
      i.className = 'placeholder';
    }
  } else {
    this.placeholdertext = false;
  }
  this.input.onfocus = function() {
    if ( place && o.placeholdertext == i.value ) { 
      i.value = '';
      if (i.className == 'placeholder') {
        i.className = '';
      } else {
        i.className = 'simple';
      }
    }
    if ( ! show_suggestions ) return;
    o.canceldrop = false;
    suggest.call(o);
  }
  this.input.onblur = function() {
    if ( place && i.value == '' ) {
      i.value = o.placeholdertext;
      if ( i.className == '' ) {
        i.className = 'placeholder';
      } else {
        i.className = 'simple placeholder';
      }
    }
    if ( ! show_suggestions ) return;
    o.canceldrop = true;
    hide.call(o);
  }
  if ( ! show_suggestions ) return;
  this.input.onkeyup = function() {
    suggest.call(o);
  }
  this.input.onkeydown = function (e) {
    e = window.event ? window.event : e;
    switch ( e.keyCode ) {
     case 38:
      change.call(o,1);
      break;
     case 40:      
      change.call(o,-1);
      break;
     case 13:
      return typein.call(o);
      break;       
     default:
      o.forcehide = false;
      sync.call(o);
      return true; 
    }
    return false;
  }
}

function KeySuggest(input,value) {
  this.input = input;
  this.valuesuggest = value;
  init.call(this,this.input.value=='key');
}

function ValueSuggest(input) {
  this.input = input;
  this.keyname = '*';
  init.call(this,this.input.value=='value');
}

function FixedKeyValueSuggest(input,keyname) {
  this.input = input;
  this.keyname = keyname;
  init.call(this,false);
}

function KeyValueSuggest(input) {
  this.input = input;
  this.keyname = '*';
  init.call(this,this.input.value=='add filter');
}
