/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	@author Ronald Everts
*	@class ST_HTTPDataConnection
*	@version v0.1
*	@copyright Copyright © 2007-2008.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ST_HTTPDataConnection() {
	/** @private */ this.url = "";
	/** @private */ this.aSync = true;
	/** @private */ this.handleFunction = null;
	/** @private */ this.parameters = new Array();
	/** @private */ this.xmlHttp = null;
	/** @private */ this.receive = null;
	/** @private */ this.isXML = true;
	/** @private */ this.loading = null;
	
	//this.init();
}                    
ST_HTTPDataConnection.prototype = {
	/**
	*	@private
	*/
	init: function() {
		/*if(ST_Class.getSetting("xmlWaiting")) {
			if(this.loading == null) {
				this.loading = new ST_Create("div", {className:"loading"}, "Loading...");
				if(typeof(this.loading) == "object" && this.loading != null) {
					document.body.appendChild(this.loading);
				}
			}
		}*/
	},
	/**
	*	Set return xml as text
	*/
	setResponseAsText: function() {
		this.isXML = false;
	},
	/**
	*	Give a url to an file that's handle the request
	*	@param <url> Link 
	*/
	setURL: function(url) {
		if(typeof(url) != "undefined") {
			this.url = url;
		}
	},
	/**
	*	Set sync
	*/
	setDisabledASync: function() {
		this.aSync = false;
	},
	/**
	*	Set function that execute after the request
	*/
	setFunction: function() {
		if(typeof(arguments) == "object" && arguments.length > 0) {// || typeof(funct) == "function") {
			this.handleFunction = arguments;
		}
	},
	/**
	*	Add a parameter to the url
	*	@param <param> Set a parameter
	*	@param <value> Set value of the given parameter
	*/
	addParam: function(param, value) {
		if(typeof(param) != "undefined" && typeof(value) != "undefined") {
			this.parameters[param] = value;
		}
	},
	/**
	*	Execute the request
	*/
	requestData: function() {
		/*if(ST_Class.getSetting("xmlWaiting")) {
			if(ST_Class.isObject(this.loading)) {
				this.loading.style.display = 'block';
			}
		}*/
		/*var extraParams = "";
		for(var a in this.parameters) {
			//alert(a)
			extraParams += (extraParams == "" ? "?" : "&") + a + "=" + this.parameters[a];
		}
		if(extraParams != "") {
			this.url = this.url + extraParams;
		}*/
		
		//alert(extraParams)
		//alert(this.url)
		if(this.url != "") {
			try {
				var tmpObj = this;
				
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				this.xmlHttp.onreadystatechange = function() {tmpObj.handleData();}
				this.xmlHttp.open('GET', this.url, this.aSync);
				this.xmlHttp.send();
			} catch(e) {
				try {
					var tmpObj = this;
					
					this.xmlHttp = new XMLHttpRequest();
					this.xmlHttp.open("GET", this.url, this.aSync);
					this.xmlHttp.send(null);
					if(this.aSync) {
						this.xmlHttp.onreadystatechange = function() {
							if(tmpObj.xmlHttp.readyState == 4) {
								tmpObj.handleData();
							}
						}
					} else {
						this.xmlHttp.onreadystatechange = tmpObj.handleData();
					}
				} catch(f) {
				}
			}
		}
	},
	/**
	*	Handles the request
	*	@private
	*/
	handleData: function() {
		try {
			//alert(this.xmlHttp.status)
			if(this.xmlHttp.readyState == 4) {
				if(this.xmlHttp.status == 200) {
					//this._closeLoading();
					var responseData = "";
					if(this.isXML) {
						this.receive = this.xmlHttp.responseXML;
						responseData = this;
					} else {
						this.receive = this.xmlHttp.responseText;
						responseData = this.receive;
					}
					if(this.handleFunction != null && typeof(this.handleFunction) == "object") {
						for(var i = 0; i < this.handleFunction.length; i ++) {
							if(typeof(this.handleFunction[i]) == "function") {
								this.handleFunction[i](responseData);
							}
						}
					}
				} else if(this.xmlHttp.status == 404) {
					//this._closeLoading();
				}
			}
		} catch(e) {
		}
	},
	returnXml: function() {
		return this.receive;
	}, 
	/**
	*	@private
	*/
	_closeLoading: function() {
		/*if(ST_Class.getSetting("xmlWaiting")) {
			if(ST_Class.isObject(this.loading)) {
				this.loading.style.display = 'none';
			}
		}*/
	}
}

