//Sourcebook AJAX for Autocompletion
//2006-06-08
//S. Andrew Sheppard

var inputName;
var acVisible = false; 
var acList = new Array;
var acCache = new Array;
var acSelected = -1;
var http;

if(window.XMLHttpRequest){
  //Normal browsers
  http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
  //IE < 7.0
  http = new ActiveXObject("Microsoft.XMLHTTP");
}

//Handle keys pressed on specified input box
function handleKey(input, mode, event) {
  if(inputName && inputName != input.id)
    hideList();
  inputName = input.id;
  switch(event.keyCode){
    
    case 27: //escape
      hideList();
      break;
      
    case 38: //up
      if(acSelected > 0){
        acSelected--;
        input.value = acList[acSelected];
        drawList();
      }
      break;
      
    case 40: //down
      if(acSelected < acList.length - 1 && acSelected < 19){
        acSelected++;
        input.value = acList[acSelected];
        drawList();
      }
      break;
      
    case 13: //return
    case 37: //left
    case 39: //right
      break;
      
    default:
      getList(input, mode);
  }
}

//Do an AJAX call for the specified input box
function getList(input, mode){
  if(window.XMLHttpRequest){
    http = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    http = new ActiveXObject("Microsoft.XMLHTTP");
  }
  http.onreadystatechange = buildList;
  http.open("GET", "?state=xml_features&mode=" + mode + "&q=" + input.value, true);
  http.send("");
}

//Save the results returned by AJAX call
function buildList(){
  var input = document.getElementById(inputName);
  if(http && http.readyState == 4 && http.status == 200 && input && input.value){
    var results = http.responseXML.getElementsByTagName("feature");
    acList = new Array();
    acSelected = -1;
    if(results.length > 0){
      for(var i = 0; i < results.length; i++)
        acList[i] = results[i].firstChild.nodeValue;
      drawList();
    } else {
      hideList();
    }
  }
}

//Build the dropdown list based on the contents of acList
function drawList(){
  
  var ac = document.getElementById("ac_" + inputName);
  ac.innerHTML = "";
  for(var i = 0; i < acList.length && i < 20; i++){
    var style = (i == acSelected) ? " style='color:blue'" : "";
    ac.innerHTML = ac.innerHTML + "<span " + style + " onclick='setValue(" + i + ");'>" + acList[i] + "</span><br>";
  }
  
  if(acList.length > 20)
    ac.innerHTML = ac.innerHTML + "<font color='#999999''>Continued...</font>";
  ac.style.visibility = "visible";
  acVisible = true;

  //Handle z-index bug for IE < 7.0
  //http://dotnetjunkies.com/WebLog/jking/archive/2003/10/30/2975.aspx
  var iebg = document.getElementById("iebg_" + inputName);
  iebg.style.top = ac.style.top;
  iebg.style.left = ac.style.left;
  iebg.style.width = ac.offsetWidth;
  iebg.style.height = ac.offsetHeight;
  iebg.style.zIndex = ac.style.zIndex-1;
  iebg.style.display = ac.style.display;
  iebg.style.visibility = "visible";
}

//Hide the dropdown list
function hideList(){
  var ac = document.getElementById("ac_" + inputName);
  var iebg = document.getElementById("iebg_" + inputName);
  ac.style.visibility = "hidden";
  iebg.style.visibility = "hidden";
  acVisible = false;	
}

//Set current input to the value clicked on from the dropdown list
function setValue(id){
  var input = document.getElementById(inputName);
  input.value = acList[id];
  hideList();
}

//Cache contents of input box
function addFeature(){
  var cache = document.getElementById("cache_" + inputName);
  var input = document.getElementById(inputName);
  if(input.value){
    acCache[acCache.length] = input.value;
    cache.innerHTML += input.value + "<br>";
    input.value = "";
  }
  hideList();
}

//Cancel submit if ac is visible; save cache into input otherwise
function doSubmit(mode) {
  if(acVisible){
    hideList();
    return false;
  } else {
    if(mode == "yesno"){
      var input = document.getElementById(inputName);
      if(input.value && acCache.length > 0)
        input.value += ", ";
      for(var i = 0; i < acCache.length; i++){
        input.value += acCache[i];
        if(i < acCache.length - 1)
          input.value += ", ";
      }
    }
    return true;
  }
}

