/* ********************************************************************
 * Creates a valid, cross-browser HTTP request object for use in Ajax
 * web scripts.
 *
 * Modified from Jim Ley's implementation at jibbering.com
 *   http://www.jibbering.com/2002/4/httprequest.html
 *
 */
function getHTTPObject() {
  var xmlhttp = false;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) { xmlhttp = false; }
    }
  @end @*/

  if (!xmlhttp) {
    try {
       xmlhttp = new XMLHttpRequest();
    } catch (e) {
      try {
        xmlhttp = window.createRequest();
      } catch (e) { xmlhttp = false; }
    }
  }
  return xmlhttp;
}



/* ********************************************************************
 * The code below shows how to request and recieve a response.  This is
 * a quick intro to Ajax, by B. Huisman of Evertz Microsystems Ltd.
 *
 *

var http = getHTTPObject();

/* ********************************************************************
 * This function sends the Ajax request.  It is usually invoked by some
 * user action.
 *
 *
function sendRequest() {

  // The server-side script URI
  var uri = "includes/ajax/example.php?arg=";

  // Collect values from page
  var values = document.getElementById("element1").value;

  // You can string elements together too
  var values = [
   document.getElementById("element1").value,
   document.getElementById("element2").value
  ];
  values = values.join("::");

  // Send the Ajax request
  http.open("GET", uri + encodeURIComponent(values), true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);
}

/* ********************************************************************
 * Then this function receives the response.
 *
 *
function handleHttpResponse() {

  // If the request has completed
  if (http.readyState == 4) {

    // Then do something with received text...
    var response = http.responseText;

    // If the response is valid XML, you can also traverse it with DOM
    // methods using the responseXML property
    var responseXML = http.responseXML;
    alert(responseXML.getElementsByTagName('item').length);

  }
}


/* ********************************************************************
 * If the same function always handles the response to this request,
 * you can avoid creating two separate functions by embedding the
 * response handler.
 *
 * In this fashion, you can create the HTTP object on the fly within
 * the scope of the function only.
 *
 *
function sendRequestEmbedded() {
  var values = "sample";

  // Create HTTP Object within the scope of this function only
  var http = getHTTPObject();

  http.open("GET", "includes/ajax/example.php?arg=" + encodeURIComponent(values), true);
  http.onreadystatechange = function() {
    if (http.readyState == 4) {
      var response = http.responseText;

      // Do things...
    }
  };
  http.send(null);
}


/* ********************************************************************
 * Sending many values via GET can make for a URI which is 10km long!
 * In such a case, you should use a POST request instead, which sends
 * the data in one of the request headers.  Doing this requires setting
 * a Content-Type header before sending.
 *
 *
function sendRequestPost() {
  var values = [
    "a","b","c","d","e","f","g","h","i","j","k","l","m",
    "n","o","p","q","r","s","t","u","v","w","x","y","z"
  ].join("::");

  var http = getHTTPObject();

  http.open("POST", "includes/ajax/example.php", true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  http.onreadystatechange = function() {
    if (http.readyState == 4) {
      var response = http.responseText;

      // Do things...
    }
  };
  http.send("arg=" + encodeURIComponent(values));
}


*/