/* *************** easy xml http parsing object ************************

use:
var loader = new net.ContentLoader("organizations.xml", myCallBack);

Note that the callback function (myCallBack) is called within the object
as follows: this.onload.call(this); In javascript, the first argument
to this .call() becomes the context of the function. Thus, within the
callback function, we are able to refer to the net.ContentLoader
object.

************************************************************************ */

var net = new Object();

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(url,onload,onerror,msg) {
	var dtmNow = new Date().getTime();
	var nc = "?uu="+dtmNow;
	
	this.url = url + nc;	// To keep browsers from caching files, we append a unique query string to the end of the file being loaded to make sure it isn't cached. In this case we are appending the date in milliseconds from January 1, 1970. This guarantees that the URL that is loaded is always unique (unprecedented).
	this.req = null;
	this.onload = onload;
	this.onerror = (onerror) ? onerror : this.defaultError;
	this.loadXMLDoc(this.url);
	this.msg = msg;
}

net.ContentLoader.prototype = {
	loadXMLDoc:function(url) {
		if (window.XMLHttpRequest) {
			this.req = new XMLHttpRequest(); // mozilla & safari
		} else if (typeof ActiveXObject != "undefined") {
			this.req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		if (this.req) {
			try {
				var loader = this;
				this.req.onreadystatechange = function() {
					loader.onReadyState.call(loader);
				}
				this.req.open("GET", url, true);
				this.req.send(null);
			} catch (err) {
				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);
			}
		}

		else if ((ready == net.READY_STATE_UNINITIALIZED) || (ready == net.READY_STATE_LOADING) || (ready == net.READY_STATE_LOADED) || (ready == net.READY_STATE_INTERACTIVE)) {
			if(this.msg == "yes"){
				//loadingXML('loading');
				//alert('loading');
			}
		}
	},
	defaultError:function() {
		alert("error fetching data!" + "\n\nreadyState:" + this.req.readyState + "\nstatus: " + this.req.status + "\nheaders: " + this.reg.getAllResponseHeaders());
	}
}