/****************************************************************************
 * Class:   Downloader
 * Author:  Rodd
 * Purpose: Create an HTTP downloader object compatible with IE and Mozilla
 * Methods:
 *
 *   startDownload(<URI>, <callback>)
 *         Makes an HTTP request to <URI>
 *         Passes the HTTP response to the function <callback>
 *
 *   getURL(<URI>)
 *         Makes an HTTP request to <URI>
 *         Return the HTTP response
 *
 ****************************************************************************/

function Downloader() {

	if (typeof ActiveXObject == 'function')
		this.dl = new ActiveXObject("MSXML2.XMLHTTP");
	else
		this.dl = new XMLHttpRequest;

		this.startDownload = _startDownload;
		this.getURL = _getURL;
		this.getXML = _getXML;
		this.postURL = _postURL;
		this.postXML = _postXML;
		this.doPost = _doPost;

	function _startDownload(url, callback, forceReload) {
		if(forceReload) {
			var cacheBuster = (new Date()).getTime();
			if(url.indexOf('?') >= 0)
				url += '&' + cacheBuster
			else
				url += '?' + cacheBuster
		}
		this.dl.open("GET", url, false);
		this.dl.send('');
		callback(this.dl.responseText);
	}

	function _getURL(url, forceReload) {
		if(forceReload) {
			var cacheBuster = (new Date()).getTime();
			if(url.indexOf('?') >= 0)
				url += '&' + cacheBuster
			else
				url += '?' + cacheBuster
		}

		this.dl.open("GET", url, false);
		this.dl.send('');
		return this.dl.responseText;
	}


	function _doPost(url, postdata) {
		var poststring = '';
		if(typeof postdata == 'object') {
			for(var item in postdata)
				poststring += (poststring.length > 0 ? '&' : '') + escape(item) + '=' + escape(postdata[item]);
		}
		else
			poststring = postdata;
		this.dl.open("POST", url, false);
		this.dl.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.dl.send(poststring);
	}

	function _postURL(url, postdata) {
		this.doPost(url, postdata);
		return this.dl.responseText;
	}

	function _postXML(url, postdata) {
		this.doPost(url, postdata);
		return this.dl.responseXML;
	}

	function _getXML(url, forceReload) {
		if(forceReload) {
			var cacheBuster = (new Date()).getTime();
			if(url.indexOf('?') >= 0)
				url += '&' + cacheBuster
			else
				url += '?' + cacheBuster
		}

		this.dl.open("GET", url, false);
		this.dl.send('');
		return this.dl.responseXML;
	}
}
