/* Usage:
 *	Create a unique object that represents what you are loading:  example:	var loadOptions = null;
 *	Then when you need to load something do something like this:	loadOptions = new net.ContentLoader("options.jsp?type=cow",setOptions);
 *		The above will call a function called setOptions() when the data has loaded, so make sure you declare function setOptions() { ...
 *	Other examples:
 *		loadOptions = new net.ContentLoader("options.jsp?type=cow",setOptions,showProgress); // calls showProgress() each time the state changes
 *		loadOptions = new net.ContentLoader("options.jsp?type=cow",setOptions,showProgress,showError); // calls showError() if there is an error
 *		loadOptions = new net.ContentLoader("options.jsp",setOptions,showProgress,null,"type=cow");  // uses default error handler, sends values via POST
 */

// create new namespace called net
var net = new Object();

net.isAjaxCompatible = function() {
	var test = null;
	if(window.XMLHttpRequest) {
            test = new XMLHttpRequest();
        // Initialize IE
        } else if(window.ActiveXObject) {
            test = new ActiveXObject("Microsoft.XMLHTTP");
        }
	if(test != null) return true;
	else return false;
}

// create 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;

// constructor
net.ContentLoader=function(url,onload,onstatechange,onerror,send) {
    this.url=url;
    this.req=null;
    this.httpStatus=null;
    this.readyState=net.READY_STATE_UNINITIALIZED;
    this.send=(send) ? send : null;
    this.method=(send) ? "POST" : "GET";
    this.onload=(onload) ? onload : null;
    this.onstatechange=(onstatechange) ? onstatechange : null;
    this.onerror=(onerror) ? onerror : this.defaultError;
    if(this.onerror == null) this.onerror = this.defaultError;
    this.loadXMLDoc(url);
}

// prototype
net.ContentLoader.prototype={
    // download the doc
    loadXMLDoc:function(url) {
        // Initialize anything but IE
        if(window.XMLHttpRequest) {
            this.req = new XMLHttpRequest();
        // Initialize IE
        } else if(window.ActiveXObject) {
            this.req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        // if initialized
        if(this.req) {
            try {
                // register onreadystatechange listener
                var loader=this;
                this.req.onreadystatechange=function() {
                    loader.onReadyState.call(loader);
                }
                // request page/set parameters
                this.req.open(this.method,this.url,(this.method=="POST") ? false : true);
                // set content-type for post if necesary
                if(this.method == "POST")
                    this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                // send the request
                this.req.send(this.send);
            } catch(err) {
                this.onerror.call(this);
            }
        }
    },
    // onreadystate listener
    onReadyState:function() {
        // shorthand
        var req=this.req;
        // for use by onstate listener
        this.readyState=req.readyState;
        // check state
        if(this.readyState==net.READY_STATE_COMPLETE) {
            // for use by onstate listener
            this.httpStatus=req.status;
            if(this.httpStatus==200 || this.httpStatus==0) {
                this.httpStatus=200;
                // call onload function
                if (this.onload != null)
                    this.onload.call(this);
            } else {
                // call onerror function
                this.onerror.call(this);
            }
        } else if(this.onstatechange != null) {
            // call onstatechange listener
            this.onstatechange.call(this);
        }
    },
    defaultError:function() {
        alert(
            "error fetching data!" +
            "\n\nreadyState:  " +   this.req.readyState +
            "\nstatus:  " +         this.req.status +
            "\nurl:  " +            this.url +
            "\nheaders:  " +        this.req.getAllResponseHeaders()
        );
    }
}