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);
}

$(function() {
	$('#subForm').submit(function(e) {
		e.preventDefault();
		
		// Grab form action
		var formAction = this.action;
		
		var id = "miutjh";
		var emailId = id + "-" + id;
		
		// 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 = $(this).serialize();
		
		// Add form action to end of serialized data
		// CDATA is used to avoid validation errors
		//<![CDATA[
		var serialized = str + "&action=" + formAction;
		// ]]>
		
		// Submit the form via ajax
		$.ajax({
			url: "http://www.wearecatch.com/application/www/assets/scripts/libs/newsletter.proxy.php",
			type: "POST",
			data: serialized,
			dataType: 'html',
			success: function(data){
				// Server-side validation
				if (data.search(/invalid/i) != -1) {
					alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
				}
			else
				{
				var $confirmation = $('#subFomCongrats');
				$("#subFomBox").hide(); // If successfully submitted hides the form
				$confirmation.slideDown("slow");  // Shows "Thanks for subscribing" div
				$confirmation.focus(); // For screen reader accessibility
				}
			}
		});
	});
});

