/////////////////////////////////////////////
// net: Custom JavaScript Network Object
/////////////////////////////////////////////
var net = new Object();

// Ready State Constants
net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;

net.ContentLoader = function(link, onload, onerror, method, params, contentType) {
  this.link = link;
  this.req = null;
  this.onload = onload;
  this.onerror = (onerror) ? onerror : this.defaultError;
  this.loadXMLDoc(link, method, params, contentType);
}
net.ContentLoader.prototype = {
  loadXMLDoc:function(link, method, params, contentType) {
    try {
      this.req = new XMLHttpRequest();
    }
    catch (e) {
      var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                      'MSXML2.XMLHTTP.5.0',
                                      'MSXML2.XMLHTTP.4.0',
                                      'MSXML2.XMLHTTP.3.0',
                                      'MSXML2.XMLHTTP',
                                      'Microsoft.XMLHTTP');

      for (var i = 0; i < XmlHttpVersions.length && !this.req; i++) {
        try {
          this.req = new ActiveXObject(XmlHttpVersions[i]);
        }
        catch (e) {}
      }
    }
    if (this.req) {
      try {
        method = (method) ? method : 'GET';
        contentType = (!contentType && method == "POST") ? 'application/x-www-form-urlencoded' : contentType;
        var loader = this;
        this.req.onreadystatechange = function () {
          loader.onReadyState.call(loader);
        }
        this.req.open(method, link, true);

        if (contentType) {
          this.req.setRequestHeader('Content-Type', contentType);
        }

        this.req.send(params);
      }
      catch (e) {
        this.onerror.call(this);
      }
    }
  },
  onReadyState:function() {
    var req = this.req;
    var ready = req.readyState;
    if (ready == net.READY_STATE_COMPLETE) {
      var httpStatus = req.status;
      if (httpStatus == 200 || httpStatus == 0) {
        this.onload.call(this);
      }
      else {
        this.onerror.call(this);
      }
    }
  },
  defaultError:function() {
    alert("Error retrieving data!"
          + "\n\nLocation: " + this.link
          + "\nreadyState: " + this.req.readyState
          + "\nstatus: " + this.req.status
          + "\nheaders: " + this.req.getAllResponseHeaders());
  }
}
