/*
  JavaScript functions to do generic Ajax calls.
  Ajax.js, version 1.0

  This script should be usable for any basic need to update database through Ajax
  without any changes needed to the script.

  The public function is
    function ajaxUpdate(ajaxscript, displayelem)

    where ajaxscript is the script to be submitted to the server.  It must contain
    the script name itself plus the data to be submitted as parameters.

    eg. "ajax.epl?id=5&data=the data"

    displayelem is a div (or similar element) tag name where the output from the
    script will be displayed.  If it is an empty string, nothing will be displayed
    when script completes.

    The JS function will disally any string that begins with http:// or https://
*/
var ajaxReq = false;
var t = null;

ajaxReq = getReqObject()

function getReqObject() {
  var ajaxReq = null
  if (window.XMLHttpRequest) { // If IE7, Mozilla, Safari, and so on: Use native object.
    ajaxReq = new XMLHttpRequest();
  } else {
    if (window.ActiveXObject) { // ...otherwise, use the ActiveX control for IE5.x and IE6.
       ajaxReq = new ActiveXObject('MSXML2.XMLHTTP.3.0');
    }
  }
  return ajaxReq
}

function ajaxHideDelayed(elemname,timeout) {
  window.setTimeout("ajaxHide('" + elemname + "')",timeout);
}

function ajaxHide(elemname) {
  document.getElementById(elemname).style.display = "none";
}

function ajaxUpdate(ajaxscript, displayelem) {
  if ( (ajaxscript.substring(0,7) == "http://") || (ajaxscript.substring(0,8)=="https://") ) {
    alert("Ajax Script must be a local file")
    return
  }

  t = window.setTimeout("ajaxDoUpdate(ajaxReq,'" + ajaxscript + "','" + displayelem + "')",200);
}

function ajaxDoUpdate(ajaxReq,ajaxscript,displayelem) {
  if ( (ajaxscript.substring(0,7) == "http://") || (ajaxscript.substring(0,8)=="https://") ) {
    alert("Ajax Script must be a local file")
    return
  }

  if (ajaxReq && ajaxReq.readyState < 4) {
    ajaxReq.abort();
  }

  ajaxReq = getReqObject()

  ajaxReq.onreadystatechange= function() {
    if (ajaxReq.readyState == 4) {
      if ( displayelem.length > 0 ) {
        var  sh = document.getElementById(displayelem);
        if ( sh.type == "text" ) {
          var newdata = ajaxReq.responseText
          var data2 = newdata.replace(/\n/,"")
//          alert(newdata + "; " + data2)
          sh.value = data2
        } else {
          sh.innerHTML = ajaxReq.responseText;
        }
      }
    }
  }

  ajaxReq.open("GET", ajaxscript );
  ajaxReq.send(null);
}

// Same as above, but using POST rather than GET
function ajaxPostUpdate(url,params, displayelem) {
  if ( (url.substring(0,7) == "http://") || (url.substring(0,8)=="https://") ) {
    alert("Ajax Script must be a local file")
    return
  }
  t = window.setTimeout("ajaxDoPostUpdate(ajaxReq,'" + url + "','" + params + "','" + displayelem + "')",200);
}

function ajaxDoPostUpdate(ajaxReq,url,params,displayelem) {
  if ( (url.substring(0,7) == "http://") || (url.substring(0,8)=="https://") ) {
    alert("Ajax Script must be a local file")
    return
  }

  if (ajaxReq && ajaxReq.readyState < 4) {
    ajaxReq.abort();
  }

  ajaxReq = getReqObject()

  ajaxReq.onreadystatechange= function() {
    if (ajaxReq.readyState == 4) {
      if ( displayelem.length > 0 ) {
// alert("displayelem = " + displayelem)
        var  sh = document.getElementById(displayelem);
        if ( sh.type == "text" ) {
          var newdata = ajaxReq.responseText
          var data2 = newdata.replace(/\n/,"")
//          alert(newdata + "; " + data2)
          sh.value = data2
        } else {
          sh.innerHTML = ajaxReq.responseText;
        }
      }
    }
  }

  ajaxReq.open("POST",url,true);
  //Send the proper header information along with the request
  ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  ajaxReq.setRequestHeader("Content-length", params.length);
  ajaxReq.setRequestHeader("Connection", "close");
  ajaxReq.send(params);
}

