
hf_form_obj = function(form_id, conf) {
	this.form_id          = form_id;
	this.form_fields_must = [];
	this.form_array       = false;
	this.error_css        = '';
	this.error_elem       = false;
	this.context          = [];
	this.has_file         = false;
	this.load_after_success = '';

	this.D                = YAHOO.util.Dom;

	this.callback = {
		success:this.handle_success,
		failure:this.handle_failure,
		upload:this.handle_success,
		scope: this
	};
	
	if (conf) {
		if (conf.form_fields_must) this.form_fields_must = conf.form_fields_must;
		if (conf.form_array) this.form_array = conf.form_array;
		if (conf.error_css) this.error_css = conf.error_css;
		if (conf.error_elem) this.error_elem = conf.error_elem;
		if (conf.has_file) this.has_file = conf.has_file;
		if (conf.load_after_success) this.load_after_success = conf.load_after_success;
		
		if (conf.context) {
			this.context = conf.context;
			this.fixedcenter = false;
		}
	}
	
//	this.dialog.showEvent.subscribe(this.btn_focus, this);
	this.dialog.hideEvent.subscribe(this._after_dialog, this);

	this.dialog.onBeforeShow = function() {
		this.div_tl = document.createElement("DIV");
		this.div_tl.className = 'tl';
		this.appendToHeader(this.div_tl);
		this.div_tr = document.createElement("DIV");
		this.div_tr.className = 'tr';
		this.appendToHeader(this.div_tr);
	}
	this.dialog.subscribe("beforeShow", this.dialog.onBeforeShow);

}

hf_form_obj.prototype._after_dialog = function(e, a, o) {
	if (o.load_after_success && o.dialog.mystate) {
		var url = o.load_after_success;
		if (url == '_self') url = window.location.href;
		window.location.replace(url);
	}
}

hf_form_obj.prototype.btn_focus = function (e, a, o) {
	//$(o.form_id+'okbtn').focus();
}


hf_form_obj.prototype.dialog = new YAHOO.widget.SimpleDialog("simpledialog1",  { 
	width: "380px",
	effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}, 
	visible: false,
	modal:true,
	zIndex:99,
	close:true,
	fixedcenter:true,
	constraintoviewport:true,
	mystate:null,
	dragOnly:true,
	icon: YAHOO.widget.SimpleDialog.ICON_WARN,
	buttons: [ { text:'OK', handler:function() { this.hide(); } , isDefault:true} ]
} );


hf_form_obj.prototype.submit = function(action) {
	this.dialog.cfg.queueProperty('fixedcenter', this.fixedcenter);
	this.dialog.cfg.queueProperty('context', this.context);
	this.dialog.cfg.queueProperty('icon', YAHOO.widget.SimpleDialog.ICON_WARN);
	this.dialog.mystate = null;
	
	if (!this.form_is_valid()) {
		//-- gibts ein 'haupt-fehler-text' ?
		if (this.error_elem && (elem = $(this.error_elem + '-error'))) this.D.setStyle(elem, 'display', 'block');
		
		/*
		this.dialog.setHeader("Fehler");
		this.dialog.setBody("Das Formular konnte nicht bearbeitet werden.<br>"+
						"Folgende Fehler traten auf:<ul><li>"+this.form_errors.join("</li><li>")+"</li></ul>");
		this.dialog.render(document.body);
		this.dialog.show();
		*/
		return;
	}

	// auf client-seite sind jetzt alle werte ok. deshalb alle fehler-textfelder ausblenden
	this.D.setStyle(this.D.getElementsByClassName('err-txt-hidden', null, this.form_id), 'display', 'none');
	
	YAHOO.util.Connect.setForm(this.form_id, this.has_file);
	//YAHOO.util.Connect.asyncRequest('POST', this.rpc_url, this.callback, params); 
	YAHOO.util.Connect.asyncRequest('POST', action, this.callback); 
}

hf_form_obj.prototype.handle_success = function(o) {

	if (!o.responseText) {
		res = {'status':0, 'html':'Keine Antwort'};
	}
	else {
		
		try {
		    res = YAHOO.lang.JSON.parse(o.responseText);
		}
		catch (e) {
			res = {'status':0, 'html':'Syntax-Error-JS:<pre style="white-space:pre;">'+o.responseText+'</pre>'};
			this.dialog.cfg.queueProperty('width', '100%');
		}

		/*
		try {
			eval('var res = ' + o.responseText);
		}
		catch(e) {
			res = {'status':0, 'html':'Syntax-Error'+o.responseText};
			this.dialog.cfg.queueProperty('width', '100%');
		}
		*/
	}
	if (res.write_id) {
		for(var d in res.write_id) {
			var elem;
			if (elem = $(d)) elem.innerHTML = (this.has_file) ? unescape(res.write_id[d]) : res.write_id[d];
		}
		
	}

	if (res.status == 0) {	// fehler

		// gibts fehlerhafte form-felder ?
		if (res.error_form && res.error_form.length > 0 && this.error_css) {
			var form = $(this.form_id);
			var field, field_name;
			
			for(var i=0, len=res.error_form.length-1; i<=len; i++) {
				field = res.error_form[i];
				field_name = this.form_array ? this.form_array + '[' + field + ']' : field;
				this.D.addClass(form.elements[field_name], this.error_css);
			}
		}

		// gibts fehler-text anzuzeigen ?
		if (res.error_text && res.error_text.length > 0 && this.error_elem) {
			var field, elem;
			for(var i=0, len=res.error_text.length-1; i<=len; i++) {
				field = res.error_text[i];
				if (elem = $(this.error_elem + '-' + field)) this.D.setStyle(elem, 'display', 'block');
			}
		}
		if (res.html) {
			var header = (res.header) ? res.header : 'Erfolg';
			this.dialog.setHeader(header);
			this.dialog.setBody(res.html);
			this.dialog.render(document.body);
			this.dialog.show();
		}
	}
	else if (res.status == 1) {	// ok
		this.on_success(res);
	}
}

hf_form_obj.prototype.on_success = function(res) {
	if (res.load_after_success) this.load_after_success = res.load_after_success;
	if (res.html) {
		var header = (res.header) ? res.header : 'Erfolg';
		this.dialog.cfg.queueProperty('icon', YAHOO.widget.SimpleDialog.ICON_INFO);
		this.dialog.setHeader(header);
		this.dialog.setBody(res.html);
		this.dialog.render(document.body);
		this.dialog.show();
		this.dialog.mystate = 1;
	}
	else {
		this.dialog.mystate = 1;
		this._after_dialog(null, null, this);
	}
}

hf_form_obj.prototype.handle_failure = function(o) {
	this.dialog.setHeader("Fehler");
	this.dialog.setBody("Ein interner Fehler trat auf. ["+o.statusText+"]. Bitte versuchen Sie es spaeter noch einmal");
	this.dialog.render(document.body);
	this.dialog.show();
}
	
hf_form_obj.prototype.form_is_valid = function() {
	var form = $(this.form_id);
	if (!form) this.handle_failure({'statusText': 'Formular nicht gefunden:'+this.form_id});
	
	this.form_errors = [];
	var form_field, form_val, field_name, field, elem;
	var form_len = this.form_fields_must.length-1;
	if (form_len < 0) return true;
	
	for(var i=0;i<=form_len;i++) {
		field = this.form_fields_must[i];

		field_name = this.form_array ? this.form_array + '[' + field + ']' : field;
		form_field = form.elements[field_name];
		
		if (!form_field) { alert('not found:'+form_field); continue; }

		if (form_field.type == 'checkbox') form_val = (form_field.checked) ? 'true' : '';
		else form_val = form_field.value;

		if (form_val == '') {
			this.form_errors.push(field + ' - Feld ist leer');
			if (this.error_css) this.D.addClass(form_field, this.error_css);
			if (this.error_elem && (elem = $(this.error_elem + '-' + field))) this.D.setStyle(elem, 'display', 'block');
		}
		else {
			if (this.error_css) this.D.removeClass(form_field, this.error_css);
			if (this.error_elem && (elem = $(this.error_elem + '-' + field))) this.D.setStyle(elem, 'display', 'none');
		}
	}
	
	return (this.form_errors.length == 0);
}
