$(document).ready (function (){

	//text replacement
	Cufon.replace('h1, h1 a, h2, h2 a, h3');

	//watermark email subscription field
	$("#dyiylh-dyiylh").Watermark("Your email address");
	
	//email submission
		$("#subscribe_submit").click(function() {	
			
			// First, disable the form from submitting
			$('form#f_subscribe').submit(function() { return false; });
			
			// Grab form action
			formAction = $("form").attr("action");
			
			// Hack together id for email field
			emailId = formAction.replace("http://bwpgroup.createsend.com/t/y/s/", "");
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;

			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return;
			}
			
			//disable the submit button
			$('input:submit').attr( { disabled : 'disabled' } ); 
			
			// Serialize form values to be submitted with POST
			var str = $("form#f_subscribe").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "/wp-content/themes/get-staines-growing/resources/proxy.php",
				type: "POST",
				data: final,
				success: function(html){
					// If successfully submitted hides the form
					$("#f_subscribe").hide("slow");
					// Shows "Thanks for subscribing" div
					$("#confirmation").show("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);
	}


		
});