var bShowAjaxPopup;

function sack(file) {
	this.xmlhttp = null;

	this.resetData = function() {
		this.Asynchron = true;
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
		this.setVar('ajaxrequest', 1);
		bShowAjaxPopup = false;
  	};

	this.resetFunctions = function() {
  		onLoadingAJAX		= function() { };
  		onLoadedAJAX		= function() { };
  		onInteractiveAJAX	= function() { };
  		onCompletionAJAX	= function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		this.createURLString(urlstring);

		if (this.method == "POST") {
			totalurlstring = this.requestFile;
		} else {
			totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
			this.method = "GET";
		}
		runAJAX(this.method, totalurlstring, this.URLString, this.Asynchron);
	}

	this.showAjaxPopup = function() {
		bShowAjaxPopup = true;
	}

	this.reset();
}

var oAJAX = createAJAX();
var oThis = "";

function createAJAX() {
	var xmlhttp = null;

	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e1) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			xmlhttp = null;
		}
	}

	if (! xmlhttp) {
		if (typeof XMLHttpRequest != "undefined") {
			xmlhttp = new XMLHttpRequest();
		}
	}
	
	return xmlhttp;
}

function checkBrowserRestictions()
{
	alert("Ihre Browsereinstellungen sind zu restriktiv. Bitte überprüfen Sie, ob Ihr Browser ActiveX-Steuerelemente zuläßt ('ActiveX-Steuerelemente und Plugins ausführen' muss auf 'Aktivieren' stehen).");
}

function runAJAX(method, AjaxURLString, AjaxURLVars, asynchron) {
	if(oAJAX != null && oAJAX.readyState < 4 && oAJAX.readyState > 1) {
		setTimeout("runAJAX();", 100);
	} else {
		// prevents caching of URLString
		oAJAX = createAJAX();
		if (oAJAX != null) {
			oAJAX.open(method, AjaxURLString, asynchron);

			if (method == "POST") {
				try {oAJAX.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")} catch (e) { }
			}

			oAJAX.onreadystatechange = checkStatusAJAX;
			oAJAX.send(AjaxURLVars);
		} else {
			checkBrowserRestictions();
		}
	}
}

function checkStatusAJAX() {
	switch (oAJAX.readyState) {
		case 1:
			AJAXLoaderShow();
			onLoadingAJAX();
			break;
		case 2:
			onLoadedAJAX();
			break;
		case 3:
			onInteractiveAJAX();
			break;
		case 4:
			if (oAJAX.status == "200") {
				onCompletionAJAX();
			} else {
				onErrorAJAX();
			}
			AJAXLoaderHide();
			break;
	}
}

function onLoadingAJAX() {}

function onLoadedAJAX() {}

function onInteractiveAJAX() {}

function onCompletionAJAX() {}

function AJAXLoaderShow() {
	try {
		_gel('ajax_loading').style.display = 'block';
	} catch (e) {
		// maybe it is in the parent frame
		try {
			parent._gel('ajax_loading').style.display = 'block';
		} catch (e) {
		}
	}
	if (bShowAjaxPopup) {
		document.popup_ajax.hide_ajax();
		document.popup_ajax.show("OK", sAjaxWaitPopupHeadline, 300, 150, sAjaxWaitPopupContent);
	}
}

function AJAXLoaderHide() {
	try {
		_gel('ajax_loading').style.display = 'none';
	} catch (e) {
		// maybe it is in the parent frame
		try {
			parent._gel('ajax_loading').style.display = 'none';
		} catch (e) {
		}
	}
	if (document.popup_ajax) {
		document.popup_ajax.hide_ajax();
	}
}

function onErrorAJAX() {
	var sResponse		= oAJAX.responseText;
	var sResponseStatus	= oAJAX.status;

	if (sResponseStatus == "403") {
		var aResponse = eval(sResponse);
		var sMessage = "Fatal Error";

		try {
			if (aResponse.sFatalError != "" && aResponse.sFatalError != undefined) {
				sMessage = aResponse.sFatalError;
			}
		} catch (e) {
		}

		if (document.popup == null) document.popup = new isPopup();
		document.popup.hide();
		document.popup.show("OK", "Error", 400, 240, sMessage);
	}
}