	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").click(function() {	
			
			// First, disable the form from submitting
			$('form#subForm').submit(function() { return false; });
			
			var formAction = $("form#subForm").attr("action");
			
			// Hacking together id for email field
			// Replace the xxxxx below:
			var id = "ktkrdj";
			var emailId = id + "-" + id;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				var pink 			= {"color" : "#595757"};
				$("#subForm input.email").css(pink);//.attr("value", "Enter a valid email address");
				alert("Please enter a valid email address");
				return;
			}
			
			// Serialize form values to be submitted with POST
			var serialized = $("form#subForm").serialize();
			
			// Submit the form via ajax
			$.ajax({
                    type:       "POST",
                    url:        formAction,
                    data:       serialized,
                    dataType:   'html',
        			onLoading: function(){
			            $('#subscribe').fadeOut('slow');
       			        // this is where we append a loading image
       			        $('#subscribe').html('<div style="text-align:center;"><img src="/assets/images/loading1.gif" alt="Loading..." /></div>').fadeIn('fast');
       			    },
       			    error: function() {
       			    	$('#subscribe').fadeOut('slow');
       			    	$('#subscribe').html('<h2 class="subtitle">Newsletter subscription</h2><p style="margin-left: 10px; margin-top: 0;width:322px;color:#FB020A;">An error occurred. Please <a href="/" onclick="window.location=\'/#nlsubscribe\';window.location.reload()">try again</a>.</p>').fadeIn('fast');
       			    },
                    success: function(data) {
			            $('#subscribe').fadeOut('slow');
			            $('#subscribe').html(data).fadeIn('fast');
                    } //end success function
            }); //end ajax call
		});
	});
