$(document).ready(function () {
	
	// Campaign Monitor AJAX subscribe
	$("#subForm #submit_email").click(function() {	
		
		// First, disable the form from submitting
		$('form#subForm').submit(function() { return false; });
		
		// Grab form action
		formAction = $("form#subForm").attr("action");
		
		// Hacking together id for email field
		// Replace the xxxxx below:
		// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
		emailId = "jyyukk";
		emailId = emailId.replace("/", "");
		emailId = emailId + "-" + emailId;
		
		// Validate email address with regex
		if (!checkEmail(emailId)) 
		{
			alert("Please enter a valid email address");
			return;
		}
		
		// Serialize form values to be submitted with POST
		var str = $("form#subForm").serialize();
		
		// Add form action to end of serialized data
		final = str + "&action=" + formAction;
		
		// Submit the form via ajax
		$.ajax({
			url: "../bin/proxy.php",
			type: "POST",
			data: final,
			success: function(html){
			
				$('form#subForm').slideUp("slow");
      			$('p.subscribe_text').slideUp("slow", function() {
      				$(this).html('You have <b>successfully subscribed</b> to our mailing list!');
      				$('p.subscribe_text').slideDown("slow");
      			});
	      		
			}
		});
	});

	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);
	}
	
	// AJAX Contact form scripts...
	$("#email_form p.error").remove();
	
	// Reset field style if showing red because of error
	$("#email_form .field").keydown(function() {
		$(this).css({"background" : "#fff", "color" : "#524640"});
	});
	
	//Remove styling from phone field on focus
	$("#form_phone").focus(function() {
		$(this).val("").css({"font-style" : "normal", "color" : "#524640"});
	});
	
	$(".contact_form .submit").click(function() {
		
		$("form p.error").slideUp("slow");
		$("#form_name").removeClass("error");
		$("#form_email").removeClass("error");
		$("#form_msg").removeClass("error");
			
		var name = $("input#form_name").val();
		if (name == "") {
		  	$(".contact_form").prepend("<p class='error'>You didn't enter your <em>Name.</em></p>");
		  	$("p.error").slideDown("slow");
		  	$("input#form_name").focus().addClass("error");
		  	return false;
		}
				
		var email = $("input#form_email").val();
		if (email == "") {
			$(".contact_form").prepend("<p class='error'>You didn't enter your <em>Email.</em></p>");
		  	$("p.error").slideDown("slow");
		  	$("input#form_email").focus().addClass("error");
		 	return false;
		}
		
		var phone = $("input#form_phone").val();
			
		var msg = $("textarea#form_msg").val();
		if (msg == "") {
		  	$(".contact_form").prepend("<p class='error'>You didn't enter a <em>Message.</em></p>");
		  	$("p.error").slideDown("slow");
		  	$("textarea#form_msg").focus().css("background","#e48c8f");
		  	return false;
		}
		
		$("#email_form img.loader").show();
		$("#email_form input.submit").attr("value","Sending...");
		
		//$("#contact input, #contact textarea").animate( { opacity: 0.5 }, 400 );
		
		
		var dataString = 'name='+ name + '&phone=' + phone + '&email=' + email + '&msg=' + msg;
		//alert (dataString);
		//return false;
			
		$.ajax({
		  type: "POST",
		  url: "../bin/process.php",
		  data: dataString,
		  success: function() {
		     	$('#email_form .submit').slideUp("slow");
		     	$('#email_form .loader').slideUp("slow");
		     	$('#email_form').find("input, textarea, label").slideUp("slow");
		  		$('#email_form').append("<h3>Message Sent!</h3><p>Thank's for getting in touch <b>"+name+"</b>. We'll get back to you as soon as we can</p>");	
		  }
		 });
		 
		return false;
		
	});
		
});