var entAjax;
if (typeof entAjax == "undefined") {
  entAjax = {};
}

entAjax.XHRFactory = {
  createXHR: function() {
    var xhr;
    try {
      xhr = new XMLHttpRequest();
    } catch (ieBrowser) {
      try {
         xhr = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (ieBrowser2) {
         try {
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (failed) {
           alert("Unknown browser type! Talk to Prof. Hertz about your web browser.");
         }
      }
    }
    return xhr;
  }
};

entAjax.HttpRequest = function(url, callback) {
  this.handler = url;
  this.async = true;
  this.responseType = "xml";
  this.httpObj = entAjax.XHRFactory.createXHR();
  this.completeCallback = callback;
  this.params = {};
};

entAjax.close = function(context, func, params) {
  return function() { return func.call(context, params); };
};

entAjax.wrap = function(params) {
  var sData = "";
  for (var name in params) {
    sData += escape(name) + "=" + escape(params[name]) + "&";
  }
  return sData.substring(0, sData.length-1);
};

entAjax.HttpRequest.prototype.get = function() {
  var sData = entAjax.wrap(this.params);
  if (sData.length > 0) {
    this.handler += "?" + sData;
  }
  this.httpObj.open("GET", this.handler, this.async);
  this.httpObj.onreadystatechange = entAjax.close(this, this.requestComplete);
  if (this.responseType == "xml") {
    this.httpObj.setRequestHeader("Content-Type","text/xml");
  }
  this.httpObj.send(null);
};

entAjax.HttpRequest.prototype.post = function() {
  // Format the params to be POST-ed to the server
  var sData = this.httpObj.wrap(this.params);
  // Now send the data using a POST
  this.httpObj.open("POST", this.handler, this.async);
  this.httpObj.onreadystatechange = entAjax.close(this, this.requestComplete);
  if (this.responseType == "xml") {
    this.httpObj.setRequestHeader("Content-Type","text/xml");
  }
  this.httpObj.send(sData);
};

entAjax.HttpRequest.prototype.setParam = function(name, value) {
  if (value === null) {
    delete this.params[name];
  } else {
    this.params[name] = value;
  }
};

entAjax.HttpRequest.prototype.requestComplete = function() {
  if ((this.httpObj.readyState == 4) && (this.httpObj.status == 200)) {
    if (this.httpObj.responseXML !== null) {
      this.completeCallback.call(this, this);
    }
  }
};

entAjax.HttpRequest.prototype.abort = function() {
  this.httpObj.onreadystatechange = function () {};
  this.httpObj.abort();
};
