/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PostData %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
	/**
	 *	PostData
	 *	This class provides a way to send information to the server as a form post
	 *
	 *	@constructor
	 */
	function PostData() {
		this.i_params = Array();
	}
	
	/**
	 *	Get/Set a parameter on this post
	 *
	 *	@param name The name of the parameter
	 *	@param value (Optional) A new value for the parameter
	 *
	 *	@return the value of the parameter
	 */
	PostData.prototype.param = function(name, value) {
		if (value != undefined) {
			this.i_params['pd_' + name] = value;
		}
		return this.i_params['pd_' + name];
	}
	
	/**
	 *	Get the string representation of this post data
	 *
	 *	@return a string version of this data (which can be sent as the content body)
	 */
	PostData.prototype.toString = function() {
		var str = "";
		for (i in this.i_params) {
			if (i.substr(0, 3) == "pd_") {
				str+=(str != "" ? "&" : "") + escape(i.substr(3)) + "=" + escape(this.i_params[i]);
			}
		}
		return str;
	}
	
/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% AJAX %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
	function AJAX() {
	
	}
	
	AJAX.request = function(url, frm, callback, scope, param) {
		var req = new DataRequest(url, callback, scope, frm, param);
		req.initialize();
	}
	
/*
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DataRequest %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
	/**
	 *	DataRequest
	 *	This class is used for accessing external data at runtime
	 *
	 *	@param {String} url The URL of the file which needs to be accessed
	 *	@param {Function} callback (Optional) The method to execute when this request completes
	 *	@param {Object} scope (Optional) The scope to call the callback in
	 *	@param {PostData} postData (Optional) The data to post to the server when making this request
	 */
	function DataRequest(url, callback, scope, postData, param) {
		this.i_url = url; 
		this.i_callback = callback;
		this.i_scope = scope;
		this.i_postData = postData;
		this.i_complete = false;
		this.i_param = param;
	}
	
	DataRequest.prototype.complete = function(state) {
		if (state != undefined) {
			this.i_complete = state;
			if (this.oncomplete != undefined) {
				var o = new Object();
				o.type="complete";
				o.request = this;
				this.oncomplete(o);
			}
			if (this.i_callback != undefined) {
				if (this.i_scope != undefined) {
					this.i_callback.call(this.i_scope, this, this.i_param);
				}
				else {
					this.i_callback(this, this.i_param);
				}
			}
		}
		return this.i_complete;
	}
	
	/**
	 *	The handler which is called when the CSS import failed 
	 *
	 *	@return true
	 */
	DataRequest.prototype.failDownload = function() {
		var me = this;
		if (this.cssr != undefined) {
			me = this.cssr;
		}
		alert('Unable to download Data resource: ' + me.i_url);
		this.complete(true);
		
	}

	
	/** 
	 *	Update the status of a Data resource being downloaded
	 *
	 *	@param context The Data resource which is using this server
	 *	@param server The XMLHTTP request object
	 */
	DataRequest.updateDownloadStatus = function(context) {
		if (this.readyState == 4) {
			context.complete(true);
		}	
	}
	
	/**
	 *	Get the XML document associated with the given request (only if it was valid XML)
	 *
	 *	@return the XML data of this request
	 */
	DataRequest.prototype.getXML = function() {
		return this.i_server.responseXML;
	}
	
	/**
	 *	Get the raw data returned by this request
	 *
	 *	@return the raw data of this request
	 */
	DataRequest.prototype.getData = function() {
		return this.i_server.responseText;
	}
	

	/**
	 *	Start downloading and importing the css file into the active document
	 *
	 *	@return true
	 */
	DataRequest.prototype.initialize = function() {
		this.i_server = (document.all ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest());
		var server = this.i_server;
		var me = this;
		this.i_server.onreadystatechange = function() {
			if (typeof DataRequest != 'undefined') {
				DataRequest.updateDownloadStatus.call(server, me);
			}
		};

		try {
			this.i_server.open((this.i_postData != undefined ? "post" : "get"), this.i_url, true);
			if (this.i_postData != undefined) {
				this.i_server.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			}
			this.i_server.send((this.i_postData != undefined ? this.i_postData.toString() : null));
		}
		catch (e) { 
			console.log('XMLHTTP error: ' + e.message);
			this.failDownload();
		}
	}

