var Try =
{
	these: function()
	{
		var returnValue;

		for (var i = 0, length = arguments.length; i < length; i++)
		{
			var lambda = arguments[i];

			try
			{
				returnValue = lambda();
				break;
			}
			catch (e)
			{
			}
		}

		return returnValue;
	}
}

var Ajax =
{
	getTransport: function()
	{
		return Try.these(
							function() {return new XMLHttpRequest()},
							function() {return new ActiveXObject('Msxml2.XMLHTTP.6.0')},
							function() {return new ActiveXObject('Msxml2.XMLHTTP.5.0')},
							function() {return new ActiveXObject('Msxml2.XMLHTTP.4.0')},
							function() {return new ActiveXObject('Msxml2.XMLHTTP.3.0')},
							function() {return new ActiveXObject('Msxml2.XMLHTTP')},
							function() {return new ActiveXObject('Microsoft.XMLHTTP')}
						) || false;
	},

	activeRequestCount: 0
}

function xml_http2(open_string, cb_fn,cb_param)
{
	this.xmlhttp 		= null;
	this.cb_function 	= cb_fn;
	this.open_str 		= open_string;
	this._complete		= false;

	this.resetData = function()
	{
		this.failed		= false;
	}

	this.resetFunctions = function()
	{
  		this.onLoading	 	= function(){ };		//ueberschreiben wenn sie gebraucht werden
  		this.onLoaded 		= function(){ };		//ueberschreiben wenn sie gebraucht werden
  		this.onInteractive 	= function(){ };		//ueberschreiben wenn sie gebraucht werden
		this.onFail 		= function(){ };		//ueberschreiben wenn sie gebraucht werden
	};

	this.reset = function()
	{
		this.resetFunctions();
		this.resetData();
	};

	this.create = function()
	{
		this.xmlhttp = Ajax.getTransport();

		if(this.xmlhttp === false)
			this.failed = true;
	};

	this.open = function(open_string,post_values,cb_fn,cb_param)
	{
		this._open(open_string, post_values, true, cb_fn,cb_param);
	};

	this.opensync = function(open_string,post_values,cb_fn,cb_param)
	{
		this._open(open_string, post_values, false, cb_fn,cb_param);

		if(typeof cb_fn == 'function')
		{
			if(this.xmlhttp.overrideMimeType)
				this.onStateChange();
		}
		else
			return this.xmlhttp.responseText;

		return "";
	};

	this._open = function(urlstr,post_values,bASync,cb_fn,cb_param)
	{
		if(this.xmlhttp == null || this.failed)
		{
			this.onFail();
		}

		if(this.xmlhttp && urlstr.length)
		{
			this.cb_function 	= cb_fn;
			this.cb_param		= cb_param;

			//if(bASync) setTimeout(function() { this.handle_response(1); }.bind(this), 1);

			this.xmlhttp.onreadystatechange = this.onStateChange.bind(this);

			var params = null;

			if(post_values && post_values.length)
			{
				this.xmlhttp.open("POST", urlstr, bASync);
				this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
				params = post_values;
			}
			else
			{
				this.xmlhttp.open('GET', urlstr, bASync);
			}

			this.xmlhttp.send(params);
		}
	};

	this.onStateChange = function()
	{
		var readyState = this.xmlhttp.readyState;

		if (readyState >= 1 && !((readyState == 4) && this._complete))
			this.handle_response(this.xmlhttp.readyState);
	};

	this.handle_response = function(readyState)
	{
		switch(readyState)
		{
			case 1:
				this.onLoading();
			break;
			case 2:
				this.onLoaded();
			break;
			case 3:
				this.onInteractive();
			break;
			case 4:
			{
				this._complete = true;

				if(typeof this.cb_function == 'function')
					this.cb_function(this.xmlhttp.responseText,this.cb_param);

				this.xmlhttp.onreadystatechange = function() { };
			}
			break;
		}
	};

	this.getHeader = function(name)
	{
		try
		{
			return this.xmlhttp.getResponseHeader(name);
		}
		catch (e) { return null }
	};

	function success()
	{
		return !this.xmlhttp.status || (this.xmlhttp.status >= 200 && this.xmlhttp.status < 300);
	};

	this.reset();
	this.create();
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------//

ajax_handler.get 	= 1;
ajax_handler.post 	= 2;

function ajax_handler(suppress_use_wait_div)
{
	this.m_param		= {};
	this.m_classname	= "";
	this.m_functionname	= "";

	this.m_suppress_use_wait_div = suppress_use_wait_div;

	this.onLoading	 	= null;		//ueberschreiben wenn sie gebraucht werden
	this.onLoaded 		= null;		//ueberschreiben wenn sie gebraucht werden
	
	this.m_onLoading	= false;

	this.open = function(method,cb_fn,cb_param)
	{
		var a 		= new xml_http2();

		if(this.onLoading != null && isFunction(this.onLoading))
		{
			a.onLoading = this.onLoading;
		}
		else
		{
			if(!this.m_suppress_use_wait_div)
			{
				a.onLoading = function()
				{
					if(!this.m_onLoading)
					{
						this.m_onLoading = true;
						
						if("showwaitdiv" in window)
							showwaitdiv();
					}
				}.bind(this)
			}
		}

		if(this.onLoaded != null && isFunction(this.onLoaded))
		{
			a.onLoaded = this.onLoaded;
		}
		else
		{
			if(!this.m_suppress_use_wait_div)
			{
				a.onLoaded = function()
				{
					if("hidewaitdiv" in window)
						hidewaitdiv();
					this.m_onLoading = true;
				}.bind(this)
			}
		}

		if(!isNull(a))
		{
			var url 		= "/ext/extAjax.php";
			var url_param 	= this._buildURL(method);

			if(method == ajax_handler.get)
				a.open(url+url_param,'',cb_fn,cb_param);
			else
				a.open(url,url_param,cb_fn,cb_param);
		}
	};

	this.opensync = function(method,cb_fn,cb_param)
	{
		var a 		= new xml_http2();

		if(this.onLoading != null && isFunction(this.onLoading))
		{
			a.onLoading = this.onLoading;
		}
		else
		{
			if(!this.m_suppress_use_wait_div)
			{
				a.onLoading = function()
				{
					if(!this.m_onLoading)
					{
						this.m_onLoading = true;
						
						if("showwaitdiv" in window)
							showwaitdiv();
					}
				}.bind(this)
			}
		}

		if(this.onLoaded != null && isFunction(this.onLoaded))
		{
			a.onLoaded = this.onLoaded;
		}
		else
		{
			if(!this.m_suppress_use_wait_div)
			{
				a.onLoaded = function()
				{
					if("hidewaitdiv" in window)
						hidewaitdiv();
					this.m_onLoading = true;
				}.bind(this)
			}
		}

		if(!isNull(a))
		{
			var url 		= "/ext/extAjax.php";
			var url_param 	= this._buildURL(method);

			if(method == ajax_handler.get)
				return a.opensync(url+url_param,'',cb_fn,cb_param);
			else if(method == ajax_handler.post)
				return a.opensync(url,url_param,cb_fn,cb_param);
		}

		return "";
	};

	this.setFunction = function(external_functionname)
	{
		this.m_functionname = external_functionname;
	};

	this.setClass = function(external_classname)
	{
		this.m_classname = external_classname;
	};

	this.setParameter = function(external_variablename,value)
	{
		this.m_param[external_variablename] = value;
	};
	
	this.setParameterArray = function(parameter_array)
	{
		this.m_param = parameter_array;
	};

	this._buildURL = function(method)
	{
		var url = "";

		if(method == ajax_handler.get)
		{
			url += "?g_class=";
			url += this.m_classname;
			url += "&g_function=";
			url += this.m_functionname;
			url += "&g_parameter=";
						
			if (isObject(Object) && isFunction(Object.toJSON))
				url += Object.toJSON(this.m_param);
			else
				url += this.m_param.toJSONString();
		}
		else if(method == ajax_handler.post)
		{
			url += "g_class=";
			url += this.m_classname;
			url += "&g_function=";
			url += this.m_functionname;
			url += "&g_parameter=";
			
			if (isObject(Object) && isFunction(Object.toJSON))
				url += Object.toJSON(this.m_param);
			else
				url += this.m_param.toJSONString();
		}
		else
			alert("Unknown method!");

		return url;
	};
}

//-----------------------------------------------------------------------------------------------------------------------------------------------------//

var returncodes = 
{
	AJAX_RETCODE_FAILURE				: 0,
	AJAX_RETCODE_SUCCESSFULLY			: 1,
	AJAX_RETCODE_OTHER					: 2
}

var returnpart = 
{
	AJAX_RESULT_RETCODE					: 0,
	AJAX_RESULT_TEXT					: 1,
	AJAX_RESULT_USERPARAM				: 2
}

/*
function cRetcode(result)
{
	this.m_retCode 		= "";
	this.m_errorTxt 	= "";
	this.m_userParam 	= "";

	if(typeof result != "undefined" && !isNull(result))
	{
		if(isJSON(result))
			result = xmlString2Array(result);
		
		this.set(result[returnpart.AJAX_RESULT_RETCODE],result[returnpart.AJAX_RESULT_TEXT],result[returnpart.AJAX_RESULT_USERPARAM]);
	}
}
*/

function cRetcode(result)
{
	this.m_retCode 		= "";
	this.m_errorTxt 	= "";
	this.m_userParam 	= "";

	if(typeof result != "undefined" && !isNull(result))
	{
		if(isJSON(result))
			result = xmlString2Array(result);

		if(isArray(result) || isObject(result))
			this.set(result[returnpart.AJAX_RESULT_RETCODE],result[returnpart.AJAX_RESULT_TEXT],result[returnpart.AJAX_RESULT_USERPARAM]);
		else
			throw result;
	}
}

cRetcode.prototype.getRetCode = function()
{
	return this.m_retCode;
}

cRetcode.prototype.getErrorTxt = function()
{
	return this.m_errorTxt;
}

cRetcode.prototype.getUserParam = function()
{
	return this.m_userParam;
}

cRetcode.prototype.isSuccessfully = function()
{
	return (this.getRetCode() == returncodes.AJAX_RETCODE_SUCCESSFULLY);
}

cRetcode.prototype.isFailure = function()
{
	return (this.getRetCode() == returncodes.AJAX_RETCODE_FAILURE);
}

cRetcode.prototype.isOther = function()
{
	return (this.getRetCode() == returncodes.AJAX_RETCODE_OTHER);
}

cRetcode.prototype.set = function(retCode,errorTxt,userParam)
{
	this.m_retCode 		= retCode;
	this.m_errorTxt 	= errorTxt;
	this.m_userParam 	= userParam;
}

cRetcode.prototype.setRetCode = function(retCode)
{
	this.m_retCode 		= retCode;
}

cRetcode.prototype.setErrorTxt = function(errorTxt)
{
	this.m_errorTxt		= errorTxt;
}

cRetcode.prototype.setUserParam = function(userParam)
{
	this.m_userParam 	= userParam;
}