$(function()
	{
		$("#contactform input:submit").click(function() {	
			
			// First, disable the form from submitting
			$('form#contactform').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form#contactform").attr("action");
			
			emailId = "email";
			emailId = emailId.replace("/", "");
			
			
			if ($('form#contactform input#name').val() == "") {
				 $("#confirmation").hide();
				 $("#errormessage1").hide();
                 $("#errormessage2").hide();
                 $("#errormessage3").hide();
			     $("#errormessage4").hide();
                 $("#errormessage1").fadeIn("500");
                 $("input#name").focus();
            return;
      } 

      else if (!checkEmail(emailId))  
			{
			    $("#confirmation").hide();
			    $("#errormessage1").hide();
				$("#errormessage2").hide();
			    $("#errormessage3").hide();
			    $("#errormessage4").hide();
			    $("#errormessage2").fadeIn("500");
			    $("input#email").focus(); 
				return;
			}

			
			else if ($('form#contactform textarea#message').val() == "") 
			{
				$("#confirmation").hide();
			    $("#errormessage1").hide();
			    $("#errormessage2").hide();
			    $("#errormessage3").hide();
				$("#errormessage4").hide();
			    $("#errormessage4").fadeIn("500");
			    $("textarea#message").focus();
				return;
			}
			
			
			
			
			// Serialize form values to be submitted with POST
			var str = $("form#contactform").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "http://www.klingebiel-kfz.de/mail.php",
				type: "POST",
				data: final,
				success: function(html){
					//$("#fields").hide(); // If successfully submitted hides the form
					$("#errormessage1").hide();
					$("#errormessage2").hide();
					$("#errormessage3").hide();
					$("#errormessage4").hide();
					$("#errormessage4").hide();
					$( "form#contactform" )[ 0 ].reset();
					$("#confirmation").fadeIn("500");  // Shows "Thanks for subscribing" div
				}
			});
		});
	});
	
	function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}
