
var AjaxProxy = new Class({
	Implements: [Options],

	options: {
		className: null,
		methodName: null,
		paramsArray: null,
		data: null,
		method: "get",
		base64encoded: true,
		processId: null,
		async: true,
		mainUrl: "",
		processBarClass: "pageloader",
		processBar: "<img src=\"/resources/icons/progressbar_microsoft.gif\" alt=\"wait ...\" />"
	},

	initialize: function(className, methodName, options) {
		this.options.className = className;
		this.options.methodName = methodName;
		this.options.paramsArray = options;

		var args = Array.link(arguments, {options: Object.type, links: Array.type});
		this.setOptions(args.options);
	},

	getUrl: function() {
		var url = this.options.mainUrl+"?class="+this.options.className+"&method="+this.options.methodName;
		if (this.options.paramsArray != null) {
			for (i=0; i<this.options.paramsArray.length; i++) {
				url += "&param[]="+ Base64.encode(this.options.paramsArray[i]+'');
			}
		}
		return url;
	},

	execute: function(callback) {
		this.startProcess();
		var req = new Request({
			method: this.options.method,
			url: this.getUrl(),
			async: this.options.async,
			data: this.options.data,
			onComplete: function(reqContent) {
				this.endProcess();
				if (this.options.base64encoded) reqContent = Base64.decode(reqContent);
				if (reqContent.substr(0, 1) == "{") {
					callback(JSON.decode(reqContent), true, reqContent);
				} else {
					callback(reqContent, false);
				}
			}.bind(this)
		}).send();
	},

	sendForm: function(formId, callback) {
		this.startProcess();
		$(formId).set("send", {
			onComplete: function(reqContent) {
				this.endProcess();
				if (this.options.base64encoded) reqContent = Base64.decode(reqContent);
				if (reqContent.substr(0, 1) == "{") {
					callback(JSON.decode(reqContent), true, reqContent);
				} else {
					callback(reqContent, false);
				}
			}.bind(this)
		});
		$(formId).send();
	},

	reloadCombo: function(targetComboId, sourceId) {
		this.source2params(sourceId);

		this.execute(function(data, isJson) {
			if (!isJson) {
				alert("Die Rückgabe ist falsch:\n"+ data);
				return;
			}
			var combo = $(targetComboId);
			while (combo.length > 0) {
				combo.options[combo.length - 1] = null;
			}
			$each(data, function(value, id) {
				var item = new Option(value, id, false, true);
				combo.options[combo.length] = item;
			});
			combo.selectedIndex = 0;
		});
	},

	replaceElement: function(targetElementId, sourceId) {
		this.source2params(sourceId);
		this.execute(function(data) {
			$(targetElementId).innerHTML = data;
		});
	},

	source2params: function(elementId) {
		var element = $(elementId);
		if (!element) return;
		//
		if (element.get("tag") == "form") {
			this.options.method = "post";
			this.options.data = element;
		} else {
			this.options.paramsArray = new Array(element.value);
		}
	},

	startProcess: function() {
		if (this.options.processId == null) return;
		var processEl = $(this.options.processId);
		if (processEl == null) return;
		//
		var size = processEl.getSize();
		var tb = (parseInt(processEl.getStyle("padding-top").replace("px", "")) + parseInt(processEl.getStyle("padding-bottom").replace("px", "")));
		var lr = (parseInt(processEl.getStyle("padding-left").replace("px", "")) + parseInt(processEl.getStyle("padding-right").replace("px", "")));
		var daElement = new Element("div", {style: "width: "+ (size.x-lr) +"px; line-height: "+ (size.y-tb) +"px;  position: absolute; z-index: 5; background-color: #CCC; opacity: 0.6; text-align: center;"});
		daElement.set("class", this.options.processBarClass);
		daElement.innerHTML = this.options.processBar;
		daElement.inject(processEl, "top");
	},

	endProcess: function() {
		if (this.options.processId == null) return;
		//
		$(this.options.processId).empty();
	}

});

function prepareValue(formId) {
	return $(formId).value.replace(/"/g, '\\"');
}

