/*
  ADD my scripts to your page with this line:
  	<script type="text/javascript" src="myAjax.js"></script>

  This library supports these functions for using Ajax (most commonly 
  used: getDataReturnText and getDataReturnXml):

  getDataReturnText(url, callback) 
    ** Uses the GET method to get text from the server. **
    Gets text from url, calls function named callback with that text.
    Use when you just want to get data from an URL, or can easily   
    encode the data you want to pass to the server in an URL, such as 
    "http://localhost/script.php?a=1&b=2&c=hello+there".
    Example: getDataReturnText("http://localhost/data.txt", doWork); 
    Here, the URL is a string, and doWork is a function in your own 
    script.

  getDataReturnXml(url, callback) 
    ** Uses the GET method to get XML from the server. **
    Gets XML from url, calls function named callback with that XML.
    Use when you just want to get data from an URL, or can easily   
    encode the data you want to pass to the server in an URL, such as 
    "http://localhost/script.php?a=1&b=2&c=hello+there".
    Example: getDataReturnXml("http://localhost/data.txt", doWork); 
    Here, the URL is a string, and doWork is a function in your 
    own script. You can recover XML elements from the XML object 
    passed to your callback function using JavaScript methods like 
    getElementsByTagName.

  postDataReturnText(url, data, callback) 
    ** Uses the POST method to send data to server, gets text back. **
    Posts data to url, calls function callback with the returned text.
    Uses the POST method, use this when you have more text data to send 
    to the server than can be easily encoded into an URL.
    Example: postDataReturnText("http://localhost/data.php", 
      "parameter=5", doWork); 
    Here, the URL is a string, the data sent to the server 
    ("parameter=5") is a string, and doWork is a function in 
    your own script.

  postDataReturnXml(url, data, callback) 
    ** Uses the POST method to send data to server, gets XML back. **
    Posts data to url, calls function callback with the returned XML.
    Uses the POST method, use this when you have more text data to send 
    to the server than can be easily encoded into an URL.
    Example: postDataReturnXml("http://localhost/data.php", 
      "parameter=5", doWork); 
    Here, the URL is a string, the data sent to the server 
    ("parameter=5") is a string, and doWork is a function in 
    your own script. You can recover XML elements from the XML object 
    passed to your callback function using JavaScript methods like 
    getElementsByTagName.

*/

function getDataReturnText(url, callback){ 
  var XMLHttpRequestObject = false; 

  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("GET", url); 

    XMLHttpRequestObject.onreadystatechange = function() { 
      if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
          callback(XMLHttpRequestObject.responseText); 
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    } 

    XMLHttpRequestObject.send(null); 
  }
}

function getDataReturnXml(url, callback){ 
  var XMLHttpRequestObject = false; 

  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("GET", url); 

    XMLHttpRequestObject.onreadystatechange = function(){ 
      if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { 
          callback(XMLHttpRequestObject.responseXML); 
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    } 

    XMLHttpRequestObject.send(null); 
  }
}

function postDataReturnText(url, data, callback){ 
  var XMLHttpRequestObject = false; 

  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("POST", url); 
    XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

    XMLHttpRequestObject.onreadystatechange = function(){ 
      if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
          callback(XMLHttpRequestObject.responseText); 
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    }

    XMLHttpRequestObject.send(data); 
  }
}

function postDataReturnXml(url, data, callback){ 
  var XMLHttpRequestObject = false; 

  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  }

  if(XMLHttpRequestObject) {
    XMLHttpRequestObject.open("POST", url); 
    XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 

    XMLHttpRequestObject.onreadystatechange = function(){ 
      if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
          callback(XMLHttpRequestObject.responseXML); 
          delete XMLHttpRequestObject;
          XMLHttpRequestObject = null;
      } 
    }

    XMLHttpRequestObject.send(data); 
  }
}

/**
	This passes you the entire
	XMLHttpRequest object instead of just the data from that object. You can
	get the data yourself by using the XMLHttpRequest object’s responseText
	and responseXML properties.
**/
function XHConn(){
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone){
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET"){
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }else{
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete){
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}
