/*
 * DON'T REMOVE THE FOLLOWING LICENSE
 *           INFORMATION!
 * ----------------------------------
 * Copyright by 
 * Global Cube Business Solutions
 * Author: Dennis Ritz
 * info@global-cube.de
 * 2007-2008
 * ----------------------------------
 */

function Ajax() {
	var _method	= "POST";
	var _requests 	= new Array();
	var _finished	= 0;
	var _timeout	= 30; 

	//var _url		= window.location.protocol+"//"+window.location.host+":"+window.location.port+"/"+window.location.pathname;
	var _url		= window.location.protocol+"//"+window.location.host+window.location.pathname;
	function request(p_class,p_fcnName) {
		var args = new Array();
		for(var i=0; i<arguments.length;i++) {
			args[i] = arguments[i];		
		}
		call.apply(this,args);
	}
	this.request = request;
	
	function call(p_className,p_fcnName) {
		var i;
		var x;

		var postData;

		var args = arguments;
		Browser.setDisplay(Browser.getElementById("retrieve"),true);
		Browser.setZIndexToMax(Browser.getElementById("retrieve"));

		if (typeof(_method) == "undefined" || _method == "")
			_method = "GET";			
		if (_method == "GET") {
			if (_url.indexOf("?") == -1) {
				_url += "?rc=" + escape(p_className);
			} else {
				_url += "&rc=" + escape(p_className);
			}
			_url += "&rm=" + escape(p_fcnName);				
			_url += "&rd=" + new Date().getTime();
				for (i = 2; i < args.length-1; i++)
				_url += "&ras[]=" + escape(args[i]);
				postData = null;
		}
		else if (_method == "POST") {
			postData = "rc=" + escape(p_className);
			postData += "&rm=" + escape(p_fcnName);
			postData += "&rd=" + new Date().getTime();
				for (i = 2; i < args.length-1; i++)
				postData = postData + "&ras[]=" + encodeURIComponent(args[i]);
		}
		else {
			Log.warning('AJAX REQUEST\nIllegal request type: "' + _method + '"');
		}
		var x = initObject();
		
		if (x == null) {
			if (sajax_failure_redirect != "") {
				location.href = sajax_failure_redirect;
				return false;
			} else {
				Log.warning('AJAX REQUEST\nCan\'t initialize Ajax object for user agent:"' + navigator.userAgent + '"');
				return false;
			}
		} else {
			x.open(_method, _url, true);
			
			_requests[_requests.length] = x;
				if (_method == "POST") {
					x.setRequestHeader("Method", "POST " + _url + " HTTP/1.1");
					x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
					x.setRequestHeader("encoding", "UTF-8");
					x.setRequestHeader("send-method", "application/x-www-form-urlencoded; charset=utf-8");
			}

			x.onreadystatechange = function onreadystatechange() {
				if (x.readyState != 4)
					return;
					var txt = x.responseText.replace(/^\s*|\s*$/g,"");

				var status = txt.charAt(0);
				if (status != "+") {
					Log.warning('AJAX REQUEST RESPONSE<br><font color=#ff0000>Status: FAILURE (unexpected response format)</font><br>Evaluate status: NOT EXECUTED<br>' + txt);
				} else {
					var data = txt.substring(2);
					try {
						var callback;
						var calldata = false;
						if (typeof args[args.length-1] == "object") {
							callback = args[args.length-1].callback;
							calldata = args[args.length-1].calldata;
						} else {
							callback = args[args.length-1];
						}
						callback(eval(data), calldata);
						Log.notice('AJAX REQUEST RESPONSE<br>Response status: OK<br>Evaluate status: OK<br>' + txt);							
					} catch (e) {
						Log.error('AJAX REQUEST RESPONSE<br>Response status: OK<br><font color=#ff0000>Evaluate status: FAILURE (' + e + ')</font><br>' + txt);
					}
				}
				var sajax_active = false;
				var k = _finished;
				for(var i=0; i<_requests.length;i++) {
					if(_requests[i].readyState != 4){
						sajax_active = true;
					}
				}
				if(!sajax_active){
					_requests = new array();

					Browser.setDisplay(Browser.getElementById("retrieve"),false);
				}
			}
		}
		Log.notice('AJAX REQUEST\nRequest function: ' + p_className + '->' + p_fcnName + '\nRequest type: ' + _method + '\nRequest URL: ' + _url + '\nRequest data: ' + postData + '\n\nWaiting for response...\nPress "OK" to continue.');
		x.send(postData);
		delete x;
		return true;
	}
	this.call = call;
	
 	function initObject() {
			var A;
 			var msxmlhttp = new Array(
			'Msxml2.XMLHTTP.5.0',
			'Msxml2.XMLHTTP.4.0',
			'Msxml2.XMLHTTP.3.0',
			'Msxml2.XMLHTTP',
			'Microsoft.XMLHTTP');
		for (var i = 0; i < msxmlhttp.length; i++) {
			try {
				A = new ActiveXObject(msxmlhttp[i]);
			} catch (e) {
				A = null;
			}
		}
			if(!A && typeof XMLHttpRequest != "undefined")
			A = new XMLHttpRequest();
		if (!A)
			Log.warning('Ajax:initObject\nCould not create connection object.');
		return A;
	}
	this.initObject = initObject;
	
	this.log = function log(){
	
	}


}

var Ajax = new Ajax();
