//
//  AjaxReader
//  Version: 1.2
//  Author: Tom Peters (tapeters@buffalo.edu)
//  Last Modified: 18 December 2006
//
//  Three Recent Changes:
//    1.2: Had to change self variable to ajaxObj to allow usage in Safari
//    1.1: Added getXML() call as an alternative to getText()
//    1.0: Initial creation
//

function AjaxReader(url)
{
  this._url = url;
  this._xmlHttp = null;
  this._loading = false;
  var ajaxObj = this;

  this._text = null;
  this._xml = null;

  this.getXMLHttpObject = function()
  {
    var obj = null;

    if (window.XMLHttpRequest)
    {
      obj = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
      obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return obj;
  }
  this.execute = function()
  {
    this._xmlHttp = this.getXMLHttpObject();

    if (this._xmlHttp == null)
    {
      alert("Your browser does not support HTTP Request.  Please upgrade your browser.");
    }
    else
    {
      this._xmlHttp.onreadystatechange = this.stateChanged;
      this._xmlHttp.open("GET",this._url,true);
      this._xmlHttp.send(null);
    }
  }
  this.stateChanged = function()
  {
    if (ajaxObj._xmlHttp.readyState == 4 || ajaxObj._xmlHttp.readyState == "complete")
    {
      ajaxObj._text = ajaxObj._xmlHttp.responseText;
      ajaxObj._xml = ajaxObj._xmlHttp.responseXML;
      if (ajaxObj.loaded)
        ajaxObj.loaded();
    }
    else if (!ajaxObj._loading)
    {
      ajaxObj._loading = true;
      if (ajaxObj.loading)
        ajaxObj.loading();
    }
  }
  this.getText = function()
  {
    return this._text;
  }
  this.getXML = function()
  {
    return this._xml;
  }
}
