/*
	Angling International
	subNewsletter.js

	Copyright Exists (c), Brave Creative, all rights reserved.
*/

function checkEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};

$(function() {

	$('#email, #ema').focus(function(){$(this).val('');})

	$('#subscribe').click(function(){
		var email = $('.newsletterPanel #email');
		if(email.val() == '') {
			alert('Please enter an email address!');
			return false;
		} 
		if(!checkEmail(email.val())) {
			alert('Please enter a valid email address!');
			return false;
		}
		sendAJAXEmail(email.val());
	});

	$('#sub').click(function(){
		var email = $('#ema');
		if(email.val() == '') {
			alert('Please enter an email address!');
			return false;
		} 
		if(!checkEmail(email.val())) {
			alert('Please enter a valid email address!');
			return false;
		}
		sendAJAXEmail(email.val());
	});
		
	var sendAJAXEmail = function(params) {
	
		var dataString = 'email=' + params;
		
		$.ajax({
      		type: "POST",
      		url: "http://www.angling-international.com/ajax/newsletterSub.php",
      		data: dataString,
      		success: function() {alert('Thank You! You have been subscribed to the Newsletter.'); $('#email, #ema').val('Enter your email address..');},
			error: function() {alert('An unexpected error has occurred! Please try again.');}
     	});	

	};
	
});
