$(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() == "") {
								 $("#errormessage1").hide();
                 $("#errormessage2").hide();
                 $("#errormessage3").hide();
								 $("#errormessage4").hide();
                 $("#errormessage1").fadeIn("slow");
            return;
      } 

      else if (!checkEmail(emailId))  
			{
			    $("#errormessage1").hide();
					$("#errormessage2").hide();
			    $("#errormessage3").hide();
			    $("#errormessage4").hide();
			    $("#errormessage2").fadeIn("slow");
				return;
			}

			else if ($('form#contactform input#phone').val() == "") 
			{
			    $("#errormessage1").hide();
			    $("#errormessage2").hide();
					$("#errormessage3").hide();
			    $("#errormessage4").hide();
			    $("#errormessage3").fadeIn("slow");
				return;
			}
			
			else if ($('form#contactform textarea#message').val() == "") 
			{
			    $("#errormessage1").hide();
			    $("#errormessage2").hide();
			    $("#errormessage3").hide();
					$("#errormessage4").hide();
			    $("#errormessage4").fadeIn("slow");
				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: "contact.php",
				type: "POST",
				data: final,
				success: function(html){
					$("#fields").hide(); // If successfully submitted hides the form
					$("#confirmation").fadeIn("slow");  // 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);
	}
