var ThePopupDiv;             // reference to the ThePopupDiv that contains iframe
var oInput;           // current input field
var timer = null;    // timer reference (needed to cancel previous call)
var delay = 500;     // time to wait after last typed char to send request (milliseconds)
var stayVisible = 0; // ThePopupDiv visibility flag
var url = '/php_includes/weaveautocomplete.call.php?query='; // autocomplete url

// set reference to the invisible ThePopupDiv (after page is loaded)
// window.onload = function () {ThePopupDiv = document.getElementById('autocomplete')}
// Moved to BODY onload element

// called on keydown event in input field
function doAutoComplete(field, e) {
  oInput = field; // set current input field
  switch(e.keyCode || window.event.keyCode){
    // tab, enter, shift, ctrl, alt, pause, caps lock
    case 9: case 13: case 16: case 17: case 18: case 19: case 20:
    // page up, page down, end, home, arrow left, arrow up, arrow right
    case 33:  case 34:  case 35:  case 36:  case 37:  case 38: case 39:
    case 45:  case 145:  // insert, scroll lock
    case 112:  case 113:  case 114: case 115: case 116: case 117: // F1,F2,F3,F4,F5,F6
    case 118: case 119: case 120: case 121: case 122: case 123: // F7,F8,F9,F10,F11,F12
      break;
    case 27: hideAutoPopup(); break; // escape (hide ThePopupDiv element)
    case 40: setAutoFocus();  break; // arrow down (set focus to the multiple select)
    case 8: case 46: default: // backspace, delete and default
      if (timer != null) {clearTimeout(timer);}         // cancel previous call
      timer = setTimeout(function(){AutoPopup();}, delay); // set new popup call
  }
}

// ThePopupDiv popup
function AutoPopup(){
  // if user deletes content from input box, then hide popup and return
  if (oInput.value.length == 0) {hideAutoPopup(); return;}
  // define oTop, oLeft and box variables
  var oTop = 0;
  var oLeft = 0;
  var box = oInput;
  // find input field position and set popup position
  do {oLeft+=box.offsetLeft; oTop+=box.offsetTop;} while (box=box.offsetParent);
  ThePopupDiv.style.top  = (oTop + 22) + 'px';
  ThePopupDiv.style.left = oLeft + 'px';
  // find iframe in ThePopupDiv and set src attribute with parameters query and field name
  ThePopupDiv.getElementsByTagName('iframe')[0].src = url+oInput.value+'&fname='+oInput.name;
}

// set focus to the multiple select (contentWindow works for IE6 and FF)
function setAutoFocus(){
  stayVisible = 1; // set stayVisible flag (needed for onblur event on input field)
  ThePopupDiv.firstChild.contentWindow.document.getElementsByTagName('select')[0].focus();
}

// hide popup
function hideAutoPopup(){
  if (stayVisible == 1) {stayVisible = 0; return;} // don't hide if flag exisits
  if (timer != null) {clearTimeout(timer);}         // cancel previous call
  ThePopupDiv.style.visibility = 'hidden';                // hide ThePopupDiv
}

// called on keydown event in multiple select (from iframe)
function AutoKeydown(oSelect, e){
  switch(e.keyCode || window.event.keyCode){
    // escape, backspace (hide popup and set focus to the input field)
    case 27: case 8:  hideAutoPopup(); oInput.focus(); break;
    // tab, return (set value to the input field)
    case 9:  case 13: setAutoValue(oSelect); break;
  }
}

// set value to the input box (used by keydown and ondblclick event)
function setAutoValue(oSelect){
  var idx = oSelect.selectedIndex;
  oInput.value = oSelect.options[idx].text;
  ThePopupDiv.style.visibility = 'hidden';
  oInput.focus();
}
