var http_request;

//
// Define a list of Microsoft XML HTTP ProgIDs.
//
var XMLHTTPREQUEST_MS_PROGIDS = new Array(
  "Msxml2.XMLHTTP.7.0",
  "Msxml2.XMLHTTP.6.0",
  "Msxml2.XMLHTTP.5.0",
  "Msxml2.XMLHTTP.4.0",
  "MSXML2.XMLHTTP.3.0",
  "MSXML2.XMLHTTP",
  "Microsoft.XMLHTTP"
);

//
// Define ready state constants.
//
var XMLHTTPREQUEST_READY_STATE_UNINITIALIZED = 0;
var XMLHTTPREQUEST_READY_STATE_LOADING       = 1;
var XMLHTTPREQUEST_READY_STATE_LOADED        = 2;
var XMLHTTPREQUEST_READY_STATE_INTERACTIVE   = 3;
var XMLHTTPREQUEST_READY_STATE_COMPLETED     = 4;

//
// Returns XMLHttpRequest object.
//
function getXMLHttpRequest()
{
  var httpRequest = null;

  // Create the appropriate HttpRequest object for the browser.
  if (window.XMLHttpRequest != null)
    httpRequest = new window.XMLHttpRequest();
  else if (window.ActiveXObject != null)
  {
    // Must be IE, find the right ActiveXObject.
    var success = false;
    for (var i = 0;
         i < XMLHTTPREQUEST_MS_PROGIDS.length && !success;
         i++)
    {
      try
      {
        httpRequest = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
        success = true;
      }
      catch (ex)
      {}
    }
  }

  // Return it.
  return httpRequest;
}


function updatepreFetch(){
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            if ( http_request.responseText.indexOf("NotFound") != -1 ){
                if ( document.getElementById("presrcdiv").style.display == "block" ){
                    document.getElementById("presrcdiv").innerHTML="";
                    document.getElementById("presrcdiv").style.display = "none"
                }

            }else if ( http_request.responseText.indexOf("MULTIPLE") != -1 ){
                document.getElementById("presrcdiv").style.display = "block";
                document.getElementById("presrcdiv").innerHTML=http_request.responseText;
            }
        }
    }
}
function preFetch(lname,searchtype){

    var parameters = "method=prefetch&searchtype="+searchtype+"&lname="+escape(lname);
    http_request = getXMLHttpRequest();

    http_request = getXMLHttpRequest();

    if ( http_request != null ){
        // i do a true here so i can refresh the page
        http_request.open('POST', "/servlet/utilServlet", true);
        http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http_request.setRequestHeader("Content-length", parameters.length);
        http_request.setRequestHeader("Connection", "close");
        http_request.onreadystatechange = updatepreFetch;
        http_request.send(parameters);

    }else{
              return;
    }

}
