var req;

function DivRequest(url,destination) {
  url += '&ddiv='+destination;
  alert('Qry: '+url);
  var ro;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer"){
    req = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
     req = new XMLHttpRequest();
  }
  alert('Waiting for response...');
  // Call the processChange() function when the page has loaded
  if (req != null) {
    req.onreadystatechange = DivFlash;
    req.open("GET", url, true);
    req.send(null);
  }
}



function DivFlash() {
   // The page has loaded and the HTTP status code is 200 OK
   if (req.readyState == 4 && req.status == 200) {
      // Break apart the matching strings
      alert('Feedback from server:'+req.responseText);
      var aStr = req.responseText.split('^');
      if(aStr.length == 1){
        alert('Only one response from server:'+req.responseText);
      }
      if(aStr[0].length == ""){
        alert('Error: No destination specified to return XML to');
      }
      if(aStr[1].length = ""){
        aStr[1] = 'No Results from Server';
      } else {
        //alert('DivContent='+aStr[1]);
      }
      // Write the contents of this URL to the searchResult layer
      // Prepare the contents of the div(Click handlers, etc)
      // Enlarge the div
      //getObject(aStr[0]).style.height='400px';
      getObject(aStr[0]).innerHTML = aStr[1];
   }
}


function doComboSearch(url,input,destination) {
  //window.getObject(destination).innerHTML = 'doComboSearch Launched...<br>';
  if(getObject(input).value.length > 0){
    // We always put a request stamp on the end of the URL - helps prevent caching problems
    dateobj = new Date()
    stamp = dateobj.getYear()+'-'+dateobj.getMonth()+'-'+dateobj.getHours()+'-'+dateobj.getMinutes()+'-'+dateobj.getSeconds();
    // Turn spaces into %20's
    value = getObject(input).value.replace(" ","%20");
      //window.getObject(destination).innerHTML += 'Search Term: \''+value+'\'<br>';

/*
if(ev){ 
  var iKeyCode = ev.keyCode;
}

if(window.event){
  var iKeyCode = window.event.keyCode;
}

    //alert('Key:'+iKeyCode);
  
    // If keycode 8 (Backspace) ocurrs, shorten the value by one
    if(iKeyCode == 8){
      value = value.substr(0,value.length-1);
    }
  
    if ((iKeyCode < 32
      || (iKeyCode >= 32 && iKeyCode <= 46) 
      || (iKeyCode >= 112 && iKeyCode <= 123)
      ) || iKeyCode == 8 || iKeyCode == 46
    ) {
      return;
    } else {
*/
      url += value+'&rt='+stamp+'&dest='+destination;
      //alert('Qry: '+url);
      window.getObject(destination).innerHTML += 'Sending Query...'+url;
      var ro;
      var browser = navigator.appName;
      if(browser == "Microsoft Internet Explorer"){
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
         req = new XMLHttpRequest();
      }
  
      //alert('Waiting for response from server...');
      // Call the processChange() function when the page has loaded
      if (req != null) {
        req.onreadystatechange = pushComboResults;
        req.open("GET", url, true);
        req.send(null);
      }
//    }
  } // End of input length greater than 0
}

function pushComboResults() {
   // The page has loaded and the HTTP status code is 200 OK
   if (req.readyState == 4 && req.status == 200) {

      
      // Break apart the matching strings
      //alert('Feedback from server:'+req.responseText);
      var aStr = req.responseText.split('^');
      if(aStr.length > 0){
        //alert('Bound Data Field='+aStr[0]);
        //alert('Display Field='+aStr[1]);
        //alert('DestinationDiv='+aStr[2]);
        //alert('DivContent='+aStr[3]);
        // Peel off the first suggestion
        var sugg = aStr[3].split('\n');
        // Add to the suggestion if a result is received
        //if(sugg[0].length > 0){
          //typeAhead(getObject(aStr[1]),sugg[0]);
        //}
        // Write the contents of this URL to the searchResult layer
        // Prepare the contents of the div(Click handlers, etc)
        var slength = sugg.length;
        var content = '';
        for(a=0;a<slength;a++){
          content += '<span style="cursor:pointer;" onMouseOver="'+aStr[0]+'.value = \''+sugg[a]+'\';" onBeforeClick="'+aStr[1]+'.style.height = \'1px\';">'+sugg[a]+'</span><br>';
        } 
        //alert('Content ='+content);
        // Enlarge the div
        getObject(aStr[2]).style.height='200px';
        getObject(aStr[2]).innerHTML = content;
      } else {
        alert('Not enough sections in return');
      }
   }
}

function typeAhead(element,suggestion) {

  if(element.createTextRange || element.setSelectionRange){
    // Get the initial length
    var iLen = element.value.length;
    //status += 'OL:'+iLen+', ';
    var sLen = suggestion.length;
    //status += 'NL:'+sLen+', ';
    // Put in the whole word
    element.value = suggestion;
    // Create a new range
    var oRange = element.createTextRange();
    // Start the range at the initial end
    oRange.moveStart("character",iLen);
    // End the range at the new end
    oRange.moveEnd("character",sLen - iLen);
    // Select it
    oRange.select();
    // Focus it
    element.focus();
  }

}


function getObject(name) {
   var ns4 = (document.layers) ? true : false;
   var w3c = (document.getElementById) ? true : false;
   var ie4 = (document.all) ? true : false;

   if (ns4) return eval('document.' + name);
   if (w3c) return document.getElementById(name);
   if (ie4) return eval('document.all.' + name);
   return false;
}


