/**
*	karte repeat..
*/

function toggleWoche2Visibility()
{

	if ( $("woche2").getStyle('display') == 'block') {
		$("woche2").hide();
	}
	else {
		$("woche2").show();
	}
	
}


/**
* allgemeines form-objekt
*/

mycom_form_obj = function(form_id, conf) {
	this.form_id          = form_id;
	this.form_fields_must = {};
	this.rpc_url          = '';
	this.error_css        = '';
	this.context          = [];
	this.fixedcenter      = true;
	this.has_file         = false;
	this.load_after_success = '';
	
	this.callback = {
		success:this.handle_success,
		failure:this.handle_failure,
		upload:this.handle_upload,
		scope: this
	};
	
	if (conf) {
		if (conf.form_fields_must) this.form_fields_must = conf.form_fields_must;
		if (conf.error_css) this.error_css = conf.error_css;
		if (conf.has_file) this.has_file = conf.has_file;
		if (conf.rpc_url) this.rpc_url = conf.rpc_url;
		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.setFooter('<div class="right">'+
			'<a href="#" id="'+this.form_id+'okbtn" onclick="mycom_form_obj.prototype.dialog.hide();return false;" class="OKbutton">'+
			'OK</a></div>');
	this.dialog.showEvent.subscribe(this.btn_focus, this);
	this.dialog.hideEvent.subscribe(this._after_dialog, this);

}

mycom_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);
	}
}

mycom_form_obj.prototype.btn_focus = function (e, a, o) {
	//$(o.form_id+'okbtn').focus();
}


mycom_form_obj.prototype.dialog = new YAHOO.widget.SimpleDialog("simpledialog1",  { 
	width: "270px",
	effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}, 
	visible: false,
	modal:true,
	zIndex:99,
	close:false,
	mystate:null,
	icon: YAHOO.widget.SimpleDialog.ICON_WARN
	//buttons: [ { text:'OK', handler:function() { this.hide(); } , isDefault:true} ]
} );


mycom_form_obj.prototype.on_success2 = function() {
	
}

mycom_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()) {
		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;
	}
	YAHOO.util.Connect.setForm(this.form_id, this.has_file);
	YAHOO.util.Connect.asyncRequest('POST', this.rpc_url, this.callback, action); 
}

mycom_form_obj.prototype.handle_success = function(o) {
	if (!o.responseText) {
		res = {'status':0, 'html':'Keine Antwort'};
	}
	else {
		try {
			eval('var res = ' + o.responseText);
		}
		catch(e) {
			res = {'status':0, 'html':'Syntax-Error'+o.responseText};
			this.dialog.cfg.queueProperty('width', '100%');
		}
	}

	if (res.status == 0) {	// fehler
		this.dialog.setHeader("Fehler");
		this.dialog.setBody(res.html);
		this.dialog.render(document.body);
		this.dialog.show();
	}
	else if (res.status == 1) {	// ok
		this.on_success(res);
	}
}
mycom_form_obj.prototype.handle_upload = function (o) {
	if (!o.responseText) {
		res = {'status':0, 'html':'Keine Antwort'};
	}
	else {
		try {
			eval('var res = ' + o.responseText);
		}
		catch(e) {
			res = {'status':0, 'html':'Syntax-Error'+o.responseText};
			this.dialog.cfg.queueProperty('width', '100%');
		}
	}

	if (res.status == 0) {	// fehler
		this.dialog.setHeader("Fehler");
		this.dialog.setBody(res.html);
		this.dialog.render(document.body);
		this.dialog.show();
	}
	else if (res.status == 1) {	// ok
		this.on_success(res);
	}
}

mycom_form_obj.prototype.on_success = function(res) {
	this.dialog.cfg.queueProperty('icon', YAHOO.widget.SimpleDialog.ICON_INFO);
	this.dialog.setHeader("Erfolg");
	this.dialog.setBody(res.html);
	this.dialog.render(document.body);
	this.dialog.show();
	this.dialog.mystate = 1;
	this.on_success2();
}

mycom_form_obj.prototype.handle_failure = function(o) {
	this.dialog.setHeader("Server Fehler");
	this.dialog.setBody("Ein interner Fehler trat auf. ["+o.statusText+"]. Bitte versuchen Sie es sp&auml;ter noch einmal");
	this.dialog.render(document.body);
	this.dialog.show();
}
	
mycom_form_obj.prototype.form_is_valid = function() {
	var form = $(this.form_id);
	this.form_errors = [];
	var form_field, form_val;

	for(var field in this.form_fields_must) {
		form_field = form.elements[field];
		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(this.form_fields_must[field] + ' - Feld ist leer');
			if (this.error_css) YAHOO.util.Dom.addClass(form_field, this.error_css);
		}
		else {
			if (this.error_css) YAHOO.util.Dom.removeClass(form_field, this.error_css);
		}
	}
	
	return (this.form_errors.length == 0);
}