/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*	@author Ronald Everts
*	@class ST_HTTPDataConnection
*	@version v0.1
*	@copyright Copyright © 2007-2008.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function ST_HTTPDataConnection(hideloading) {
	/** @private */ this.url = "";
	/** @private */ this.aSync = true;
	/** @private */ this.handleFunction = null;
	/** @private */ this.params = new Array();
	/** @private */ this.xmlHttp = null;
	/** @private */ this.receive = null;
	/** @private */ this.isXML = true;
	/** @privete */ this.isPostRequest = false;
	/** @private */ this.loading = null;
	/** @private */ this.showLoading = true;
	/** @private */ this.loadingIframe = null;
	if (hideloading) {
		this.showLoading = false;
	}

	this.init();
}                    
ST_HTTPDataConnection.prototype = {
	/**
	*	@private
	*/
	init: function() {
		if(this.showLoading && this.loading == null) {
			
			this.loading =  document.createElement('div');
			this.loading.setAttribute('id', 'waitDiv');
			this.loading.className = 'waitDiv';
			this.loading.innerHTML = 'Loading....';

			if(IE) {
				this.loadingIframe = document.createElement("iframe");
				this.loadingIframe.setAttribute('id', 'waitIframe');
				this.loadingIframe.className= 'waitDiv';
				this.loadingIframe.style.zIndex = '999998';
			}
			
			if(typeof(this.loading) == "object" && this.loading != null) {
				document.body.appendChild(this.loading);
				if(IE)
					document.body.appendChild(this.loadingIframe);
			}
			
		}
	},
	/**
	*	Set return xml as text
	*/
	setResponseAsText: function() {
		this.isXML = false;
	},
	/**
	*	Set sync
	*/
	setDisabledASync: function() {
		this.aSync = false;
	},
	/** 
	*	Set request as POST 
	*/
	setRequestAsPost: function(isPost) {
		this.isPostRequest = isPost; 
	},
	/**
	*	Set function that execute after the request
	*/
	setFunction: function() {
		if(typeof(arguments) == "object" && arguments.length > 0) {
			this.handleFunction = arguments;
		}
	},
	/**
	*	Give a url to an file that's handle the request
	*	@param <url> Link 
	*/
	setURL: function(url) {
		if(typeof(url) != "undefined") {
			this.url = url;
		}
	},
	/**
	*	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.params[param] = value;
		}
	},
	/**
	*	Execute the request
	*/
	requestData: function() {
		if(this.showLoading && typeof(this.loading) == "object" && this.loading != null) {
			this.loading.style.display = "block";
		}

		var extraParams = "";
		for(var a in this.params) {
			extraParams += (extraParams == "" ? "" : "&") + a + "=" + this.params[a];
		}
		if(!this.isPostRequest && extraParams != "") {
			this.url = this.url + "?" + extraParams;
		}
		
		if(this.url != "") {
			try {
				var tmpObj = this;
				
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				this.xmlHttp.onreadystatechange = function() {tmpObj.handleData();}
				if(this.isPostRequest) {
					this.xmlHttp.open("POST", this.url, this.aSync);
					this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					this.xmlHttp.setRequestHeader("Content-length", extraParams.length);
					this.xmlHttp.setRequestHeader("Connection", "close");
					this.xmlHttp.send(extraParams);
				}
				else {
					this.xmlHttp.open("GET", this.url, this.aSync);
					this.xmlHttp.send();
				}
				
			} catch(e) {
				try {
					var tmpObj = this;
					this.xmlHttp = new XMLHttpRequest();

					if(this.isPostRequest) {
						this.xmlHttp.open("POST", this.url, this.aSync);
						this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
						this.xmlHttp.setRequestHeader("Content-length", extraParams.length);
						this.xmlHttp.setRequestHeader("Connection", "close");
						this.xmlHttp.send(extraParams);
					}
					else {
						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 {
			if(this.xmlHttp.readyState == 4) {
				if(this.xmlHttp.status == 200) {
					if (this.showLoading) 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) {
					if (this.showLoading) this._closeLoading();
				}
			}
		} catch(e) {
		}
	},
	/**
	*	@private
	*/
	_closeLoading: function() {
		if(typeof(this.loading) == "object" && this.loading != null) {
			document.body.removeChild(this.loading);
		}
		if(typeof(this.loadingIframe) == "object" && this.loadingIframe != null) {
			document.body.removeChild(this.loadingIframe);
		}
	},
	/**
	*	Get value of asked data
	*	@param <data> Name of xml tree
	*/
	getDataValue: function(data) {
		if(typeof(data) != "undefined") {
			if(this.receive.getElementsByTagName(data).length > 0) {
				var obj = this.receive.getElementsByTagName(data)[0];
				var tmpVal = '';
				if(obj != null) {
					tmpVal = obj.childNodes[0].nodeValue;
					return tmpVal;
				}
			}
		}
		return null;
	},
	/**
	*	Hide or show loading div
	*/
	setShowLoading: function(show) {
		this.showLoading = show;
	},
	/**
	*	Get value of asked data
	*	@param <data> Name of xml tree
	*/
	getDataAsArray: function(data) {
		if(typeof(data) != "undefined") {
			var obj = this.receive.getElementsByTagName(data);

			var tmpArray = new Array();

			for(var k = 0; k < obj.length; k++) {
				tmpArray[k] = new Array();
				for(var i = 0; i < obj[k].childNodes.length; i++) {
					if(obj[k].childNodes[i].nodeType != 3) {
						tmpArray[k][obj[k].childNodes[i].nodeName] = null;
						for(var j = 0; j < obj[k].childNodes[i].childNodes.length; j++) {
							if(obj[k].childNodes[i].childNodes[j].nodeType == 3 || obj[k].childNodes[i].childNodes[j].nodeType == 4) {
								tmpArray[k][obj[k].childNodes[i].nodeName] = obj[k].childNodes[i].childNodes[j].nodeValue;
							}
						}
					}
				}
			}
			return tmpArray;
		}
	}
}
