jaex.frm.ajaxsubmit = function() {}

jaex.frm.ajaxsubmit.Form = function(editId, subId, cnlId, lodId, rurl, frm) {
	
	this.editl = $(editId);
	this.sub = $(subId);
	this.cnl = $(cnlId);
	this.lod = $(lodId);
	this.inputs = new Array();
	
	this.editable = false;
	this.frm = frm;
	this.rurl = rurl;
	
	jaex.registerEvent(editId, this.editl, 'onclick', this, 'edit', 'false');
	jaex.registerEvent(subId, this.sub, 'onclick', this, 'save', 'false');
	jaex.registerEvent(cnlId, this.cnl, 'onclick', this, 'cancel', 'false');
	
	this.addInput = function(input) {
		this.inputs.push(input);
	}
		
	this.edit = function() {
		this.editable = true;
		for(var ic = 0; ic < this.inputs.length; ic++) {
			this.inputs[ic].edit();
		}
		this.sub.style.display = "block";
		this.cnl.style.display = "block";
		this.editl.style.display = "none";
	}
	
	this.save = function() {
		if(this.frm.isValid()) {
			var params = "";
		
			this.sub.style.display = "none";
			this.cnl.style.display = "none";
			this.lod.style.display = "block";
			
			for(var ic = 0; ic < this.inputs.length; ic++) {
				if(ic > 0) params += "&";
				params += this.inputs[ic].input.serialize(true);
				this.inputs[ic].setDisabled(true);
			}
			
			jaex.ajax.openPostRequest(this.rurl, this, 'saveComplete', params);
		}
	}
	
	this.saveComplete = function(status, responseText) {
		if(status == 200) {	
			var result = responseText.evalJSON();
			if(result.result != "done") {
				alert(result.reason);
				return;
			}
			
			for(var ic = 0; ic < this.inputs.length; ic++) {
				this.inputs[ic].save();
				this.inputs[ic].setDisabled(false);
			}
			
			this.lod.style.display = "none";
			this.editl.style.display = "block";
		} else {
			this.lod.style.display = "none";
			this.sub.style.display = "block";
			this.cnl.style.display = "block";
			for(var ic = 0; ic < this.inputs.length; ic++) {
				this.inputs[ic].setDisabled(false);
			}
			alert("Server konnte nicht erreicht werden...");
		}
	}
	
	this.cancel = function() {
		for(var ic = 0; ic < this.inputs.length; ic++) {
			this.inputs[ic].cancel();
		}
		
		this.sub.style.display = "none";
		this.cnl.style.display = "none";
		this.editl.style.display = "block";
	}
}

jaex.frm.ajaxsubmit.Input = function(labelId, divId, inputId) {
	this.label = $(labelId);
	this.div = $(divId);
	this.input = $(inputId);
	
	this.getValue = function() {
		return this.input.value;
	}
	
	this.setDisabled = function(disabled) {
		this.input.disabled = disabled;
	}
	
	this.edit = function() {
		this.div.style.display = "block";
		this.label.style.display = "none";
	}
		
	this.save = function() {
		this.label.innerHTML = this.getValue();
		this.div.style.display = "none";
		this.label.style.display = "block";
	}
	
	this.cancel = function() {
		this.div.style.display = "none";
		this.label.style.display = "block";
	}
}
