$(document).ready(function(){
	/* Formulare */

	new function() {
		$.fn.validate = {
			init: function(o) {
				if($(o).attr("rel") == 'email') { this.email(o) }
				if($(o).attr("rel") == 'required') { this.required(o) }
			},
			email: function(o) {
				var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (o.value.match(email)) {
					doSuccess(o);
				} else {
					doError(o);
				}
			},
			required: function(o) {
				var check_for;
				// LABEL VOR INPUT? -> auf LEER prüfen, andernfalls [title]
				if ($(o).parents('form').hasClass('form-with-labels')) {
					check_for = "";
				} else {
					check_for = $(o).attr('title');
				}
				if (o.value == check_for) {
					doError(o);
				} else {
					doSuccess(o);
				}
			}
		};
		function doSuccess(o) {
			$(o).removeClass("error");			
		}
		function doError(o) {
			$(o).addClass("error");
		}
	};

	$("form").live('submit',function() {
		var error = false;
		$(this).find(".validate").each(function(){
			$(this).validate.init(this);
			if($(this).hasClass("error")) {
				error = true;
			}
		});
		if(error) {
			alert(_js_alert);
			return false;
		} else {
			// Handlen Submit verschiedener Formulare
			//alert($(this).attr('id') + ' -> ' + $(this).attr('action'));
			if(typeof $(this).attr('id') != "undefined" && $(this).attr('id') != "") {
				if(typeof $(this).attr('action') != "undefined" && $(this).attr('action') != "") {
					var url = $(this).attr('action') + '?ajax=1';
					var form_data = $(this).serializeArray();
					switch($(this).attr('id')) {
						// Kommentar eintragen
						case "contact-form":
							$.post(url,form_data,function(response) {
								$('div#contact_content').empty().html(response);
							});
							return false;

							break;

						default:
							return true;
					}
				}
			} else {
				return true;
			}
		}
	});
});
