﻿var Ajax = {
	CallStack: new Array(),
	ShowLoadingIndicator: function () {
		if(Ajax.CallStack.length > 0) { $("LoadingIndicator").style.display = "block"; }
	},
	HideLoadingIndicator: function (XHR) {
		var len = Ajax.CallStack.length, i = 0;
		while (i < len) { if(Ajax.CallStack[i] == XHR) { Ajax.CallStack.splice(i, 1); break; } i++; }
		if(Ajax.CallStack.length == 0) { $("LoadingIndicator").style.display = "none"; }
		XHR = null;
	},
	ToQuery: function () {
		var args = Ajax.ToQuery.arguments;
		var data = new Array();
		var len = args.length, i = 0;
		while (i < len) { data.push(args[i] + "=" + encodeURIComponent(args[i+1])); i += 2; }
		return data.join("&");
	},
	CreateRequestResponse: function (response, args) {
		var Obj = new Object();
		Obj.Response = response;
		if(args) { Obj.Args = args; }
		return Obj;
	},
	AsyncRequest: function (method, page, data, callback, indicateLoading, returnXHR) {
		var XHR;
		if(window.XMLHttpRequest) { XHR = new XMLHttpRequest(); }
		else if(window.ActiveXObject) { XHR = new ActiveXObject("Microsoft.XMLHTTP"); }
		XHR.open(method, location.protocol + "//" + location.hostname + page + ((method == "GET")?((page.indexOf("?") == -1)?"?":"&") + "SID=" + sid + "&TimeStamp=" + new Date().getTime():""), true);
		XHR.onreadystatechange = function () {
			switch (XHR.readyState) {
				case 1:
					if(indicateLoading) { Ajax.CallStack.push(XHR); window.setTimeout(Ajax.ShowLoadingIndicator, 500); }
				break;
				case 4:
					if(indicateLoading) { Ajax.HideLoadingIndicator(XHR); }
					try {
						if(XHR.status == 200) {
							if(callback) {
								var RequestResponse = Ajax.CreateRequestResponse(XHR.responseText, callback.Args);
								if(!callback.Scope){ callback.Success(RequestResponse); }
								else { callback.Success.apply(callback.Scope, [RequestResponse]); }
							}
						}
						else {
							var error = ((XHR.status == 500)?XHR.responseText:XHR.statusText);
							if(callback && callback.Failure) { callback.Failure(error); }
							else if(Windows.Count > 0) { Windows.Errors.Show(error); }
							else { alert(error); }
						}
					}
					catch(e) { XHR.abort(); }
					callback = null;
					XHR = null;
				break;
			}
		}
		if(method == "POST") {
			XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			XHR.send("SID=" + sid + ((data)?"&" + data:""));
		}
		else { XHR.send(null); }
		if(returnXHR) { return XHR; }
	},
	SyncRequest: function (method, page, data) {
		var XHR;
		if(window.XMLHttpRequest) { XHR = new XMLHttpRequest(); }
		else if(window.ActiveXObject) { XHR = new ActiveXObject("Microsoft.XMLHTTP"); }
		XHR.open(method, location.protocol + "//" + location.hostname + page + ((method == "GET")?((page.indexOf("?") == -1)?"?":"&") + "SID=" + sid + "&TimeStamp=" + new Date().getTime():""), false);
		if(method == "POST") {
			XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			XHR.send("SID=" + sid + ((data)?"&" + data:""));
		}
		else { XHR.send(null); }
		var response = XHR.responseText;
		if(XHR.status == 200) { return response; }
		else { alert(XHR.statusText); }
		XHR = null;
	},
	AbortRequest: function (XHR) {
		if(XHR != "") { XHR.onreadystatechange = function () {}; XHR.abort(); Ajax.HideLoadingIndicator(XHR); XHR = null; }
	}
}