var xhreq,xhcallback;

function loadDoc(url,cb,method,params) {
  if (typeof method=="undefined") method="GET";
  if (typeof params=="undefined") params="";
  if (typeof cb=="undefined") cb=function(res){ };

  xhreq = false;
  // branch for native XMLHttpRequest object
  if(window.XMLHttpRequest) {
    try {
      xhreq = new XMLHttpRequest();
    } 
    catch(e) {
      xhreq = false;
    }
  // branch for IE/Windows ActiveX version
  } else if(window.ActiveXObject) {
    try {
      xhreq = new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch(e) {
      try {
	xhreq = new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch(e) {
	xhreq = false;
      }
    }
  }
  if(xhreq) {
    document.body.style.cursor='wait';
    xhcallback=cb;
    xhreq.onreadystatechange = xhprocessReqChange;
    xhreq.open(method, url, true);
    if (method=="POST") {
      xhreq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//      xhreq.setRequestHeader("Content-length", params.length);
//      xhreq.setRequestHeader("Connection", "close");
    }
    xhreq.send(params);
  }
  else {
    alert('Problem running Ajax fetch on your browser.');
  }
}
function xhprocessReqChange() {
  if (xhreq.readyState == 4) {
    // only if "OK"
    if (xhreq.status == 200) {
      ctype=xhreq.getResponseHeader("Content-Type");
      if (ctype.indexOf('xml')>=0) xhcallback(xhreq.responseXML);
      else xhcallback(xhreq.responseText);
    } 
    else {
      alert("There was a problem retrieving the XML data:\n" + xhreq.statusText);
    }
    document.body.style.cursor='default';
  }
}
