//create a cross-browser XML HTTP Request Object
/***********************************************
 * This script calls an external ajax handling *
 * function which must be embedded in any page *
 * which you want to use ajax.                 *
 * This callback function determines what it   *
 * is that you actually want to DO with the    *
 * returned ajax object.                       *
 * This script will merely pass either a       *
 * String or XML DOM Object to the callback    *
 * function.                                   *
 *                                             *
 * copyright Chris Cheale 2007                 *
 * http://www.xenotaph.net                     *
 **********************************************/
function XMLHTTPRequestObject() {
  //-- START --
  //method to get the "request" object relevant to the client browser
  this.requestObject = function () {
    this.xmlHttpRq = false;

    //native browser support - Mozilla (or Firefox), Safari, Opera 8(?)
    if(window.XMLHttpRequest!==undefined&&window.XMLHttpRequest) {
      this.xmlHttpRq = new XMLHttpRequest();
    }

    //Internet Explorer uses an ActiveX object
    else if(window.ActiveXObject!==undefined&&window.ActiveXObject) {
      //-- additionally MS has 2 versions of the ActiveX object
      try {
        this.xmlHttpRq = new ActiveXObject("Msxml2.XMLHTTP"); // <-- this one
      }
      catch(e) {
        try {
          this.xmlHttpRq = new ActiveXObject("Microsoft.XMLHTTP"); // <-- and this one
        }
        catch(e) {
          this.xmlHttpRq = false;
        }
      }
    }

    //here's another one called window.createRequest() used by IceBrowser - nothing's ever easy is it?
    else if(window.createRequest!==undefined&&window.createRequest) {
      this.xmlHttpRq = window.createRequest();
    }

    //return the XML HTTP Request Object (or false)
    return this.xmlHttpRq;
  }
  //-- END --

  //-- START --
  //(event handler) method to "load" the HTTP request
  this.processRequest = function () {
    /***************************
     * Object status integer:  *
     * 0 = uninitialized       *
     * 1 = loading             *
     * 2 = loaded              *
     * 3 = interactive         *
     * 4 = complete            *
     **************************/

    if(xmlHttpRq.readyState==4) { //loaded
      if(xmlHttpRq.status==200) { //http response header; 200 = OK
        //once you've got the data back return true
        if(returnType==="txt") { //return as plain text (probably XML in plain text format)
          //alert(xmlHttpRq.responseText); //DEBUGGING
          if(callbackFunction) {
            eval(callbackFunction + "(xmlHttpRq.responseText)");
          }
        }
        else { //return as DOM compatible document object - DEFAULT
          if(callbackFunction) {
            eval(callbackFunction + "(xmlHttpRq.responseXML)");
          }
        }
      }
      else { //http response header NOT OK
        //since statusText is not defined in Opera
        this.strStatusOutput = "ERROR\nThe server failed to connect to the AJaX handling script.\nThe following information was returned:\n\nSERVER RESPONSE: " + xmlHttpRq.status;
        this.strStatusOutput += xmlHttpRq.statusText!==undefined&&xmlHttpRq.statusText ? " - " + xmlHttpRq.statusText : "";
        alert(this.strStatusOutput);
      }
    }
  }
  //-- END --

  //-- START FINAL INITIALISATION --
  var xmlHttpRq = this.requestObject();
  var callbackFunction = false;
  var returnType = "xml";

  //method to load in data from a URI
  //should be an XML document or XML output from server script (PHP, ASP etc...) with Content-Type: text/xml
  this.loadURI = function (uri, callbackFunctionName, submitMethod, requestVars, outputAs) {
    //parameters
    callbackFunction = callbackFunctionName;
    requestVars = requestVars===undefined ? "" : requestVars;
    outputAs = outputAs===undefined ? "xml" : outputAs;

    //redefine the return type based upon the outputAs parameter
    if(outputAs.match(/txt/i)) {
      returnType = "txt";
    }
    else {
      returnType = "XML";
    }

    //process
    if(xmlHttpRq) {
      xmlHttpRq.onreadystatechange = this.processRequest;
      xmlHttpRq.open(submitMethod, uri, true);
      xmlHttpRq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttpRq.setRequestHeader("Connection", "close");
      xmlHttpRq.send(requestVars);
    }
    else {
      alert("Could not instantiate the XML HTTP Request Object");
    }
  }
  //-- END --
}

function noCallback(responseObject) {
  return true;
}
