// JavaScript Document

$(document).ready(function() {
	$("form input.button").click(function() {
		//the error that will be displayed if the email entry is blank
		var errorBlank = 'Invalid Email';
		//the error that will be displayed if the email is not in valid email format
		var errorInvalid = 'Invalid Email';
		//the error that will be displayed when the email has been sent successfuly
		var success = 'Confirmed, thank you!';
		
		subscribeForm(errorBlank, errorInvalid, success);
	});
	
	function subscribeForm(errorBlank,errorInvalid,success) {
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				var email = $("input#email").val();
		
				if(!emailReg.test(email)) {			
					$("input#email").val(errorInvalid);
					return false;
				}		
				var dataString = 'email=' + email;  
					$.ajax({  
					  type: "POST",
					  url: "email-subscribe/subscribe.php",
					  data: dataString,  
					  success: function(msg) {  
						if (msg == 'failure') {
							$("input#email").val(errorBlank);
						} else $("input#email").val(success);
					  }  
					}).responseText;  
				return false;
	}
});
