/****************************************************************************
 * 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("Microsoft.XMLHTTP");
	else
		this.dl = new XMLHttpRequest;

	if (typeof(_downloader_prototype_called) == 'undefined') {
		_downloader_prototype_called = true;
/*
		if(typeof this.dl.Open == 'function') {
			Downloader.prototype.startDownload = _startDownload_IE;
			Downloader.prototype.getURL = _getURL_IE;
			Downloader.prototype.postURL = _postURL_IE;
		}
		else {
*/
			Downloader.prototype.startDownload = _startDownload_Moz;
			Downloader.prototype.getURL = _getURL_Moz;
			Downloader.prototype.postURL = _postURL_Moz;
//		}
	}

	function _startDownload_IE(url, callback) {
		this.dl.Open("GET", url, false);
		this.dl.Send('');
		callback(this.dl.responseText);
	}

	function _startDownload_Moz(url, callback) {
		this.dl.open("GET", url, false);
		this.dl.send('');
		callback(this.dl.responseText);
	}

	function _getURL_IE(url) {
		this.dl.Open("GET", url, false);
		this.dl.Send('');
		return this.dl.responseText;
	}

	function _getURL_Moz(url) {
		this.dl.open("GET", url, false);
		this.dl.send('');
		return this.dl.responseText;
	}

	function _postURL_IE(url, postdata) {
		var poststring = '';
		if(typeof postdata == 'array')
			for(item in postdata)
				poststring += (poststring.length > 0 ? '&' : '') + escape(item) + '=' + escape(postdata)
		else
			poststring = postdata;

		this.dl.Open("POST", url, false);
		this.dl.Send(poststring);
		return this.dl.responseText;
	}

	function _postURL_Moz(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);
		return this.dl.responseText;
	}
}
