/**
 * Ajax framework
 *
 * @author Philip van Til
 * @copyright Philip van Til
 * @version 08 februari 2008
 */

function AjaxConnection(useLoadingPicture,loadingId) {	
	this.loadingPicture = "<img src=\"image.php?m=Main&image=loading.gif\" \>";
	this.useLoadingPicture = (typeof(useLoadingPicture) != "undefined") ? useLoadingPicture : true;
	this.loadingId = (typeof(loadingId) != "undefined") ? loadingId : "loading";
	this.aSync = true;
	this.isXML = true;
	
	this.isLoading = false;
	this.url = "";
	this.params = new Array();
	this.xmlHttp = null;
	this.receive = null;
	this.callBackFunction = null;
	
	this.loading();
}

AjaxConnection.prototype = {
	
	loading: function() {
		this.isLoading = !this.isLoading;
		if (this.useLoadingPicture) {
			var output = (this.isLoading) ? this.loadingPicture : "";
			document.getElementById(this.loadingId).innerHTML = output;
		}
	},
		
	setResponseAsText: function() {
		this.isXML = false;
	},
	
	disableASync: function() {
		this.aSync = false;
	},
	
	setCallBackFunction: function(callBackFunction) {
		if (callBackFunction != "" && typeof(eval(callBackFunction)) == "function") {
			this.callBackFunction = callBackFunction;
		}
	},
	
	setURL: function(url) {
		if(typeof(url) != "undefined") {
			if (url.indexOf('?') == -1)
				this.url = url+"?ajax=1";
			else
				this.url = url+"&ajax=1";
		}
	},
	
	addParam: function(param, value) {
		if(typeof(param) != "undefined" && typeof(value) != "undefined") {
			this.params[param] = escape(encodeURI(value));
		}
	},
	
	getXmlHttpObject: function() {
		try {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			return new XMLHttpRequest();
		}			
	},
	
	paramToString: function(isGet) {
		var str = "";
		for(var p in this.params) {
			str += (str == "" ? "?" : "&") + p + "=" + this.params[p];
		}		
		if (!isGet) {
			str = str.replace('?','');
		} else if (this.url.indexOf('?') != -1) {
			str = str.replace('?','&');
		}			
		return str;
	},
	
	GETrequest: function() {
		this.url += this.paramToString(true);
		
		if(this.url != "") {
			var tmpObj = this;
			this.xmlHttp = this.getXmlHttpObject();
			this.xmlHttp.onreadystatechange = function() { tmpObj.handleRequest(); }
			this.xmlHttp.open("GET", this.url, this.aSync);
			this.xmlHttp.send(null);
		}
	},
	
	POSTrequest: function() {
		var tmpObj = this;
		this.xmlHttp = this.getXmlHttpObject();
		this.xmlHttp.open("POST", this.url, this.aSync);
		this.xmlHttp.onreadystatechange = function() { tmpObj.handleRequest(); }
		
		var data = this.paramToString(false);
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.xmlHttp.setRequestHeader("Content-length", data.length);
		this.xmlHttp.setRequestHeader("Connection", "close");
		this.xmlHttp.send(data);
	},
	
	handleRequest: function() {
		try {
			if(this.xmlHttp.readyState == 4) {
				if(this.xmlHttp.status == 200) {
					
					this.loading();
					
					var responseData = "";
					if(this.isXML) {
						this.receive = this.xmlHttp.responseXML;
						responseData = this;
					} else {
						this.receive = this.xmlHttp.responseText;
						responseData = this.receive;
					}
					
					eval(this.callBackFunction + "(responseData)");
					
				} else if(this.xmlHttp.status == 404) {
					this.loading();
				}
			}
		} catch(e) {}
	},

	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;
	},

	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;
		}
	}
}
