
$(function($){
	$.supersized({
		//Functionality
		slideshow : 0, //Slideshow on/off
		slides:  [
			{image :template_directory+'/images/bg.gif', title : '', url : ''}
		]
	});
	
	// Defaultvalue Funktion
	$("input, textarea").each(function() {
		if ($(this).attr("type") == "submit") { return false; }
		$(this).data("origin", $(this).attr("value")).focus(function() {
			if ($(this).val() == $(this).data("origin")) {
				$(this).val("");
			}
		}).blur(function() {
			if ($(this).val() == "") {
				$(this).val($(this).data("origin"));
			}
		});
	});
	
	// Neues Feld beim Bestellformular hinzufügen
	$("a.addField").click(function() {
		
		var elem = $("tr.entry.last").clone().removeClass("last");
		$("tr.entry.last").before(elem);
		
		return false;
	});
	
	// Fehlerhafte inputfelder verlieren ihre rote Farbe beim Focus
	$("form.bestellung input").focus(function() {
		$(this).removeClass("error");
	});
	
	// Submit check
	$("form.bestellung").submit(function() {
		var status = true;
		$(this).find("input").each(function() {
			
			if ($(this).attr('type') != "hidden" && !$(this).hasClass("optional")) {
				if ($(this).val() == "" || $(this).val() == $(this).data("origin")) {
					status = false;
					$(this).addClass('error');
				}
				else if ($(this).attr('name') == "Email" && !validate($(this).val())) {
					status = false;
					$(this).addClass('error');
				}
				else {
					$(this).removeClass('error');
				}
			}
			
		});

		return status;
	});
});

function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   if(reg.test(address) == false) {
      //alert('Invalid Email Address');
      return false;
   }
   return true;
}
