$(document).ready(function() {
    var options = {
	    beforeSubmit:	checkForm,
		success:		showResponse
	};

    $('#ccoptin').ajaxForm(options);
});

function showResponse(responseText, statusText) {
    if( responseText == '0') {
		//alert('Thank You! You have sucessfully subscribed to our newsletter.');
		alert('Thank You, you have been added.');
	}
	else {
		//alert(responseText);
		alert('ERROR: There was a problem subscribing. Please check your address and try again.');
	}
}
function checkForm(formArray, jqForm, options) {
	var email = jqForm[0].elements['ea'].value;
	
	if ( checkEmail(email) ) {
		return true;
	}
	else {
		alert("ERROR: The email address you provided appears to be invalid. Please make sure your address is correct.");
		return false;
	}
}
function checkEmail(email) {
	var pattern = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);
	
	if ( pattern.test(email) ) {
		return true;
	}
	else {
		return false;
	}
}

