
var Ajax = {
	Init: function() {
		this.DEBUG = false;
		
		this.InitXmlHttp();
		
		this.Loading = {
			Init: function() {
				this.clipRight = 0;
			},	
			Set: function(elem) {
				this.elem = elem;
				this.clipRight = 0;
			},
			Show: function() {
				//alert('Showing' + this.timeout);
				if(this.timeout) clearTimeout(this.timeout);
				this.Toggle(90);
			},
			Hide: function() {
				if(this.timeout != null) {
					setTimeout('Ajax.Loading.Hide()', 1000);
				} else {
					this.Toggle(0);
				}
			},
			Toggle: function(targetWidth) {
				var delta = 10;
				var timespan = 10;
				var widthSuffix = (Global.Browser.Browser == 'Explorer') ? '' : 'pt';
				
				if(this.elem) {
					if(this.clipRight < targetWidth) {
						this.clipRight += delta;
					} else if(this.clipRight > targetWidth) {
						this.clipRight -= delta;
					}
					
					this.elem.style.width = this.clipRight + widthSuffix;
					
					if(this.clipRight != targetWidth) {
						this.timeout = setTimeout('Ajax.Loading.Toggle('+targetWidth+')', timespan);
					} else {
						this.timeout = null;
					}
					if(this.clipRight > 0) {
						this.elem.style.display = 'block';
					} else if(this.clipRight == 0) {
						this.elem.style.display = 'none';
					}
				}
			}
		}
		this.Loading.Init();
	},
	InitXmlHttp: function() {
		this.XmlHttp = false;
		
		if (window.XMLHttpRequest) {
			this.XmlHttp = new XMLHttpRequest()
		} else if (window.ActiveXObject) { // code for IE
			try {
				this.XmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
			} catch (e) {
				try {
					this.XmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
				} catch (E) {
					this.XmlHttp = false
				}
			}
		}
	},
	Pass: function(url, callbackFunction) {
		this.PassResponseToFunction(url, callbackFunction, null, null, null, null);
	},
	Pass: function(url, callbackFunction, params) {
		this.PassResponseToFunction(url, callbackFunction, params, null, null, null);
	},
	Pass: function(url, callbackFunction, params, loadingElem) {
		this.PassResponseToFunction(url, callbackFunction, params, loadingElem, null, null);
	},
	Pass: function(url, callbackFunction, params, loadingElem, loadingFunctionStart, loadingFunctionEnd) {
		this.PassResponseToFunction(url, callbackFunction, params, loadingElem, loadingFunctionStart, loadingFunctionEnd);
	},
	PassResponseToFunction: function(url, callbackFunction, params, loadingElem, loadingFunctionStart, loadingFunctionEnd) {
		if(this.DEBUG) {
			alert('Pass.url='+url);
		}
		var oLoading = document.getElementById(loadingElem)
		if(oLoading) {
			this.Loading.Set(oLoading);
			this.Loading.Show();
		} else if(loadingFunctionStart) {
			eval(loadingFunctionStart);
		}
		this.InitXmlHttp();
		//send the request.
		if (this.XmlHttp) {
			this.XmlHttp.onreadystatechange = 
				function () {
					// out of scope: use Ajax. instead of this.
					if (Ajax.XmlHttp && Ajax.XmlHttp.readyState==4) { //we got something back..
						if (Ajax.XmlHttp.status==200) {
							var response = Ajax.XmlHttp.responseText;
							var functionToCall;
							
							functionToCall = callbackFunction + '(response';
							if(params != null) {
								functionToCall += ','+params;
							}
							functionToCall += ')';
							
							if(Ajax.DEBUG) {
								alert('Pass.reponse='+response);
								alert('Pass.functionToCall='+functionToCall);
							}
							if(oLoading) {
								Ajax.Loading.Hide();
							} else if(loadingFunctionEnd) {
								eval(loadingFunctionEnd);
							}
							eval(functionToCall);
						} else {
							document.write(Ajax.XmlHttp.responseText);
						}
					}
				}
			this.XmlHttp.open("GET",url,true);
			this.XmlHttp.send(null);
		}
	},
	Set: function() {
		var args = this.Set.arguments;
		var url = args[0];
		var obj_id = args[1];
		var o = document.getElementById(obj_id);
		var oLoading = null;
		var fnCallback = null;
		
		if(this.DEBUG) {
			alert('SET.url = '+url);
		}
		if(args[2]) {
			oLoading = document.getElementById(args[2]);
			if(oLoading) {
				this.Loading.Set(oLoading);
				this.Loading.Show();
			}
		}
		if(args[3]) {
			fnCallback = args[3];
		}
		this.SetInnerHTMLFromResponse(url, obj_id, oLoading, fnCallback);
	},
	SetInnerHTMLFromResponse: function(url, obj_id, oLoading, fnCallback) {
		var responseText;
		//now we got the this.XmlHttpRequest object, send the request.
		this.InitXmlHttp();

		if (this.XmlHttp) {
			this.XmlHttp.onreadystatechange = 
				function () {
					// out of scope: use Ajax. instead of this.
					if (Ajax.XmlHttp && Ajax.XmlHttp.readyState==4) { //we got something back..
						if (Ajax.XmlHttp.status==200) {
							if(Ajax.DEBUG) {
								alert('SET.responseText = '+Ajax.XmlHttp.responseText);
							}
							// test for session timeout; Login.aspx page adds X-Login header;
							if(Ajax.XmlHttp.getResponseHeader('X-Login') == 'P21.Admin') {
								responseText = 'Session timed out: click refresh to login again.';	
							} else {
								responseText = Ajax.XmlHttp.responseText;
							}
							if(typeof obj_id == 'object') {
								obj_id.innerHTML = responseText;
							} else {
								document.getElementById(obj_id).innerHTML = responseText;
							}
							if(oLoading) {
								Ajax.Loading.Hide();
							}
							if(fnCallback) {
								eval(fnCallback);
							}
						} else {
							document.write(Ajax.XmlHttp.responseText);
						}
					}
				}
			this.XmlHttp.open("GET",url,true);
			this.XmlHttp.send(null);
		}
		return true;
	},
	Refresh: function(obj_id, oLoading) {
		this.Refresh(obj_id, oLoading, null);
	},
	Refresh: function(obj_id, oLoading, fnCallback) {
		if(this.LastUrl != '') {
			this.Set(this.LastUrl, obj_id, oLoading, fnCallback);
		}
	},
	LoginCheck: function() {
		var args = this.LoginCheck.arguments;
		var url;
		var handle;
		
		if(args[0]) {
			url = args[0];
		} else {
			url = "LoginCheck.aspx";
		}
		if(this.DEBUG) {
			alert('LC.url='+url);
		}
		if(args[1]) {
			handle = args[1];
			this.Pass(url, 'Ajax.LoginCheckHandler', '\''+handle+'\'');
		} else {
			this.Pass(url, 'Ajax.LoginCheckHandler');
		}
	},
	LoginCheckHandler: function() {
		var args = this.LoginCheckHandler.arguments;
		var response = args[0];
		var handle = (args[1]) ? args[1] : null;
		
		if(this.DEBUG) {
			alert('LCH.response='+response);
		}
		if(response == 0) { // not logged in...reload window;
			window.location.reload();
		} else { // logged in...eval handle;
			if(handle != null) {
				if(this.DEBUG) {
					alert('LCH.handle = '+handle);
				}
				eval(handle);
			}
		}
	},
	ListRowOver: function(elem) {
		for(var i=0; i<elem.childNodes.length; i++) {
			if(elem.childNodes[i].className) {
				elem.childNodes[i].className = Global.StringReplace(elem.childNodes[i].className, 'Out', 'Over');
			}
		}
	},
	ListRowOut: function(elem) {
		for(var i=0; i<elem.childNodes.length; i++) {
			if(elem.childNodes[i].className) {
				elem.childNodes[i].className = Global.StringReplace(elem.childNodes[i].className, 'Over', 'Out');
			}
		}
	}
}
Ajax.Init();
