// Extension to jQuery to report errors in forms
$.fn.extend({
	report_error: function (message) {
		return this.each(function () {
			$(this).addClass('error')
				.find('input')
				.one('change', function () {
					$(this).closest('.error').removeClass('error').find('.error-message').remove();
				})
				.parent()
				.append('<span class="error-message">' + message + '</span>');
		});
	}
});

// Attach form checkers
$('form').live('submit', function () {
	$('.error', this).removeClass('error').find('.error-message').remove();

	$('input.required').each(function () {
		if ($(this).val().trim().length>0)
			return;
		$(this).closest('tr').report_error('Feld ist leer');
	});
	$('input.email:not(.error)', this).each(function() {
//		if ($(this).val().trim().length==0 || $(this).val().match(/^[^\(\)<>@,;:\\\"\[\]]+@(?:[^\(\)<>@,;:\\\".\[\]]+\.)+[a-z]{2,}$/i))
//			return;
		$(this).closest('tr').report_error('Bitte eine gültige eMail-Adresse angeben');
	});
	if ($('#confirmed', this).is(':not(:checked)')) {
		$('tr:has(#confirmed)', this).report_error('Bitte bestätige Deine Bestellung');
	}

	$('.error:first input').focus();

	return $('.error', this).length==0;
});

$(function () {
	$('a.lightbox').fancybox();
	$('a.confirm').click(function() {
		return confirm('Wirklich?');
	});
});
