var xmlhttp=false;
var kwicelm  = null;
var inputelm = null;
var oldkey = "";

function createXmlHttp ()
{
  xmlhttp = false;

  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
      xmlhttp = false;
    }
  }
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  return xmlhttp;
}

function load() {
  if (!xmlhttp) {
    xmlhttp = createXmlHttp();
  }
  
  if (! xmlhttp || xmlhttp.readyState == 1 ||
      xmlhttp.readyState == 2 || xmlhttp.readyState == 3){
      setTimeout("load()", 400);
      return; 
  }

  if (! kwicelm) {
    kwicelm = document.getElementById("kwicresult");
    inputelm = document.getElementById("kwicinput");
  }

  var key = encodeURI(inputelm.value);
  if (key == "") {
    kwicelm.style.display = "none";
    xmlhttp = null;    
  } else if (oldkey != key) {
    oldkey = key;
    kwicelm.style.display = "block";
    xmlhttp.open("GET", "./kwic/kwic.cgi?"+key, true);
    xmlhttp.onreadystatechange=function() {
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
         kwicelm.innerHTML = xmlhttp.responseText;
       }
     }
     xmlhttp.send(null)
  }
  setTimeout("load()", 400);
}

function closekwic () {
  if (kwicelm) kwicelm.style.display = "none";
  if (inputelm) inputelm.value = "";
  xmlhttp = null;
}
