// Class: Ajax
// Author: ???
// Last edited: VScherbakov 12/13/05, reason: #1427
/*
Description:
	set of AJAX related functions
Depends on:
	/-/Scripts/Tools.js: addLoadEvent
	DOM element: id="defBusyDiv"
Usage:

Global Functions:
	Ajax_GetXMLHttpRequest
	Ajax_CallBack(
		bUseStatic, 
		type, 
		id, 
		method, 
		args, 
		clientCallBack, 
		debugRequestText, 
		debugResponseText, 
		debugErrors, 
		includeControlValuesWithCallBack)
	Ajax_PreCallBack
	Ajax_PostCallBack
	ajaxLoadingSignal(visible, optional busyDivId)
	ajaxLoadingTimer()
	
Global Variables:
	sAjaxStaticUrl
	sAjaxCallbackUrl
	ajaxCallBackCount: 0
	ajaxDisableIndicator: false
	ajaxTimer
	ajaxLoadingDiv
	
*/

function Ajax_GetXMLHttpRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		if (window.Ajax_XMLHttpRequestProgID) {
			return new ActiveXObject(window.Ajax_XMLHttpRequestProgID);
		} else {
			var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
			for (var i = 0; i < progIDs.length; ++i) {
				var progID = progIDs[i];
				try {
					var x = new ActiveXObject(progID);
					window.Ajax_XMLHttpRequestProgID = progID;
					return x;
				} catch (e) {
				}
			}
		}
	}
	return null;
}

var sAjaxStaticUrl;
var sAjaxCallbackUrl;

function Ajax_CallBack(bUseStatic, type, id, method, args, clientCallBack, debugRequestText, debugResponseText, debugErrors, includeControlValuesWithCallBack) {
	if (window.Ajax_PreCallBack) window.Ajax_PreCallBack();
	var x = Ajax_GetXMLHttpRequest();
	var result = null;
	if (!x) {
		result = { "value":null, "error": "NOXMLHTTP"};
		if (debugErrors) {
			alert("error: " + result.error);
		}
		if (clientCallBack) {
			clientCallBack(result);
		}
		if(window.Ajax_PostCallBack) window.Ajax_PostCallBack();

		return result;
	}
	var url = (bUseStatic ? sAjaxStaticUrl : sAjaxCallbackUrl)
				+ "&Type="+type+"&Method="+method;
	
	x.open("POST", url, clientCallBack ? true : false);
	x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
	if (clientCallBack) {
		x.onreadystatechange = function() {
			if (x.readyState != 4) {
				return;
			}
			if (debugResponseText) {
				alert("#1 Response text: " + x.responseText);
			}
			var result = null;
			try {
				result = eval("(" + x.responseText + ")");
			} catch(ex) {
				result = {value:null,error:"Client-side parse error: " + ex.message};
			}
			if (debugErrors && result.error) {
				alert("error: " + result.error);
			}
			clientCallBack(result);
			if (window.Ajax_PostCallBack) window.Ajax_PostCallBack();
		}
	}
	var encodedData = "Ajax_CallBackType=" + type;
	if (id) {
		encodedData += "&Ajax_CallBackID=" + id.split("$").join(":");
	}
	encodedData += "&Ajax_CallBackMethod=" + method;
	if (args) {
		for (var i in args) {
			encodedData += "&Ajax_CallBackArgument" + i + "=" + encodeURIComponent(args[i]);
		}
	}
	if (includeControlValuesWithCallBack && document.forms.length > 0) {
		var form = document.forms[0];
		for (var i = 0; i < form.length; ++i) {
			var element = form.elements[i];
			if (element.name) {
				var elementValue = null;
				if (element.nodeName == "INPUT") {
					var inputType = element.getAttribute("TYPE").toUpperCase();
					if (inputType == "TEXT" || inputType == "PASSWORD" || inputType == "HIDDEN") {
						elementValue = element.value;
					} else if (inputType == "CHECKBOX" || inputType == "RADIO") {
						if (element.checked) {
							elementValue = element.value;
						}
					}
				} else if (element.nodeName == "SELECT") {
					elementValue = element.value;
				} else if (element.nodeName == "TEXTAREA") {
					elementValue = element.value;
				}
				if (elementValue) {
					encodedData += "&" + element.name + "=" + encodeURIComponent(elementValue);
				}
			}
		}
	}
	if (debugRequestText) {
		alert("encoded data: " + encodedData);
	}
	x.send(encodedData);
	if (!clientCallBack) {
		if (debugResponseText) {
			alert("#2 Response Text: " + x.responseText);
		}
		try {
			result = eval("(" + x.responseText + ")");
		} catch(ex) {
			result = {value:null,error:"Client-side parse error: " + ex.message};
		}
		if (debugErrors && result.error) {
			alert("error: " + result.error);
		}
		if (window.Ajax_PostCallBack) window.Ajax_PostCallBack();
	}
	delete x;
	return result;
}

var ajaxCallBackCount = 0;
var ajaxDisableIndicator = false;
function Ajax_PreCallBack() {
	if (!ajaxCallBackCount && !ajaxDisableIndicator) // show only at first call
		ajaxLoadingSignal(true);
	ajaxCallBackCount++;
}

function Ajax_PostCallBack() {
	if (!ajaxCallBackCount) return;
	ajaxCallBackCount--;
	if(!ajaxCallBackCount && !ajaxDisableIndicator)
		ajaxLoadingSignal(false);
}


var ajaxTimer;
var ajaxLoadingDiv = null;

function ajaxLoadingSignal(visible, busyDivId) {
	if (busyDivId) 
		ajaxLoadingDiv = document.getElementById(busyDivId);
	else
		ajaxLoadingDiv = document.getElementById("defBusyDiv");
	if(!ajaxLoadingDiv) return;
	if (typeof(visible) == 'undefined') visible = true;
	if (visible) {
		ajaxLoadingDiv.style.top = document.body.parentNode.scrollTop + "px";
		ajaxLoadingDiv.style.display = "block";
		ajaxTimer = window.setInterval(ajaxLoadingTimer, 200);
	} else {
		ajaxLoadingDiv.style.display = "none";
		if (ajaxTimer) window.clearInterval(ajaxTimer);
	}
}

function Browser(){
if(navigator.userAgent.indexOf("Opera")>=0)return "Opera";
if (navigator.appName=="Microsoft Internet Explorer")return "IE";
else return "Other";
}


function ajaxLoadingTimer() {
	if (!ajaxLoadingDiv) return;
	if (ajaxLoadingDiv.style.backgroundColor ==  'maroon')
		ajaxLoadingDiv.style.borderColor = ajaxLoadingDiv.style.backgroundColor = 'darkred'
	else
		ajaxLoadingDiv.style.borderColor = ajaxLoadingDiv.style.backgroundColor = 'maroon';
}

