/* *************** 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.

sample callback function:
function myCallBack() {
	alert(this.url + " loaded! content:\n\n" + this.req.responseText);
}

************************************************************************ */

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) {
	this.url = url;
	this.req = null;
	this.onload = onload;
	this.onerror = (onerror) ? onerror : this.defaultError;
	this.loadXMLDoc(url);
}

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);
			}
		}
	},
	defaultError:function() {
		alert("error fetching data!" + "\n\nreadyState:" + this.req.readyState + "\nstatus: " + this.req.status + "\nheaders: " + this.req.getAllResponseHeaders());
	}
}


/* ****************** helper function ******************** */

// returns string if data exists, returns empty string "" otherwise
function getData(node, tag) {
	var get;
	get = node.getElementsByTagName(tag);
	if (get)
		if (get[0])
			if (get[0].firstChild)
				return trim(get[0].firstChild.data);

	return "";
}

function trim(string) {
	return string.replace(/^\s*|\s*$/g,"");
}


/*
nasty ajax hack for offline xml

		// xmlDOM is the resulting XML DOM to use
		var xmlDOM;
		
		// only do this if we're using IE ***AND*** we get the xml back from the filesystem (i.e. http status of 0)
		if (window.ActiveXObject && xmlObj.status == 0) {
			//alert('using a nasty hack');
			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(xmlObj.responseText); // force the xml into an xml dom by explicitly loading it
			
			xmlDOM = xmlDoc;
			
		} else if (xmlObj.status == 0 || xmlObj.status == 200) {
			xmlDOM = xmlObj.responseXML;
		}
		
		
		*/