// Define object
var net=new Object();

// XMLHttpRequest states
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(observer, url, method, params, contentType) {
    this.observer = observer;
    this.url = url;
    this.method = method;
    this.params = params;
    this.contentType = contentType;
}

net.ContentLoader.prototype.createRequest = function() {
    var r = null;
    // Non-Microsoft browsers
    try { r = new XMLHttpRequest(); }
    catch(ex) {
	// Microsoft browsers
	try { r = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch(ex1) {
	    // Other Microsoft versions
	    try { r = new ActiveXObject("Microsoft.XMLHTTP"); }
    	    catch(ex2) { r = null; }
	}
    }
    return r;
}

net.ContentLoader.prototype.sendRequest = function() {
    // Default method is GET
    if(!this.method) { this.method = "GET"; }

    // Make sure content type is correct if POST
    if(!this.contentType && this.method == "POST") { this.contentType = "application/x-www-form-urlencoded"; }

    // Get new request object
    var req = this.createRequest();
    if(req) {
	var loader = this;
	req.onreadystatechange = function() { loader.handleResponse(req); }
        req.open(this.method, this.url, true);
	if(this.contentType) { req.setRequestHeader("Content-Type", contentType); }
        // Send any URL encoded parameters
	req.send(this.params);
    }
}

net.ContentLoader.prototype.handleResponse = function(req) {
    var ready = req.readyState;
    if(ready == net.READY_STATE_COMPLETE) {
        // If HTTP status is OK, then invoke callback handler.
        // The "this" reference is implicitly passed to the function
        // and refers to the ContentLoader object 
        // see onreadystatechange event handler
        if(req.status == 200 || req.status == 0) { this.observer.notify(req); }
        else { this.observer.notifyError(req); }
    }
}

net.ContentLoader.prototype.getText = function() {
    return this.req.responseText;
}

net.ContentLoader.prototype.getXML = function() {
    return this.req.responseXML;
}


