/*
 * AJAX Framework for 5Ohm
 * client side part
 */

/*
 * TODO:
 * check input parameters
 * do not override all get parametes instead append to it
 */

use("lib");

function getHttpReq(){
	var req = null;

	if(window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) { }
		}
	}

	return req;
}

var af5 = {
	queue: [],
	running: false,
	loadDiv: null,

	request: function(handler, data, callback, after) {
		this.queue.push(new Array(handler, data, callback, after));

		if(!this.running) {
			this.processNextReq();
		}
	},

	request_s: function(handler, data) {
		this.req = getHttpReq();
		var url = basePath + "?d=" + handler;

		this.req.open("POST", url, false);
		this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.req.send(data);
		return this.req.responseText;
	},

	processNextReq: function() {
		if(this.queue.length == 0) {
			this.running = false;
			if(this.loadDiv) {
				document.body.removeChild(this.loadDiv);
				this.loadDiv = null;
			}
			return;
		}

		if(!this.running) {
			this.loadDiv = document.createElement("div");
			this.loadDiv.id = "loadingBox";
			this.loadDiv.style.cssText = 
				"margin-top: 5px; margin-left: 5px; width: auto; height: auto; padding: 5px; " + 
				"text-align: center; z-index: 999; background-color: #333333; " + 
				"border: 1px solid #97DD00; color: #97DD00; font-weight: bold;" + 
				"position: fixed;";
			var loadGif = document.createElement("img");
			loadGif.src = basePath + "/images/ajax-loader-green-gray.gif";
			loadGif.align = "middle";
			this.loadDiv.appendChild(loadGif);
			this.loadDiv.appendChild(document.createTextNode(" Loading..."));
			document.body.appendChild(this.loadDiv);
		}

		this.running = true;
		var q = this.queue.shift()
		var handler = q[0];
		var data = q[1];
		var callback = q[2];
		var after = q[3];

		var cookies = document.cookie.split(";");
		for(var x = 0; x < cookies.length; x++) {
			var kv = cookies[x].split("=");
			var key = trim(kv[0]), value = trim(kv[1]);
			if(key == "PHPSESSID") {
				if(data.length > 0) {
					data += "&sess5="+value;
				} else {
					data = "sess5="+value;
				}
				break;
			}
		}

		this.cb = callback;
		this.aftercb = after;
		this.req = getHttpReq();

		if(this.req == null) {
			alert("Use a real browser");
			return;
		}

		this.req.onreadystatechange = function() {
			var state = af5.req.readyState;
			if(state == 4) {
				var respcode = af5.req.status;
				var resp = af5.req.responseText;
				if(respcode == 200) {
					try {
						af5.cb(resp);
					} catch(e) {
						alert("Javascript error!");
					}
				} else {
					alert("Af5 server callback error: " + respcode + "\n" + resp);
				}
				if((typeof af5.aftercb) == "function") {
					try {
						af5.aftercb();
					} catch(e) {
						alert("Javascript error!");
					}
				}
				af5.processNextReq();
			}
		}

		var url = basePath + "?d=" + handler;

		this.req.open("POST", url, true);
		this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.req.send(data);
	}
}
