function isValidEmailAddress(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 OptRedirect(){
    window.location = "Book_Chapter.htm";
}

function QuickContact()
	{
		var error = false;
		var name = $('#name').val();
		var phone = $('#phone').val();
		var email = $('#email').val();
		var question = $('#question').val();
		var check = $('#check').val();
		var errors = 'Please enter the folowing:\n\n';
		
		if(name==''){ error=true; errors+=' * Full Name\n'; }
		if(email==''){ error=true; errors+=' * E-mail\n'; }
		if(phone==''){ error=true; errors+=' * Phone\n'; }
		if(question==''){ error=true; errors+=' * Question\n'; }
		if(check==''){ error=true; errors+=' * 2+3 spam check\n'; } else if (check!='5'){ error=true; errors+=' * Correct the 2+3 spam check\n'; } 
		
		if(error) 
		{ 
			alert(errors); 
		}
		else
		{
			$.ajax(
			{
				url: "quickmail.php",
				type:"POST",
				data:
				({
					action:'send_mail',
					name:name,
					phone:phone,
					email:email,
					question:question,
					check:check
				}),			
				success: function(data)
					{
						if(data!='ok')
						{ 
							alert("There was an error sending the email. Sorry.");
						}
						else
						{
							$('#name').val('');
							$('#phone').val('');
							$('#email').val('');
							$('#question').val('');
							$('#check').val('');
							$('#check').val('');
							alert('Thank you for submiting your question.\n We will try to anwser it as soon as possible.');
							$.post("newsletter_add.php", 
							{
								email:email,
								type:'Home Contact Form'
							});
						}
						
					}
			});	
		}
	}
function OptIn()
	{
		var error = false;
		var email = $('#email').val();
		var check = $('#check').val();
		var errors = 'Please enter the folowing:\n\n';
		
		if(email==''){ error=true; errors+=' * E-mail\n'; } else if (!isValidEmailAddress(email)) { error=true; errors+=' * Enter a valid email address\n'; }
		if(check==''){ error=true; errors+=' * 2+3 spam check\n'; } else if (check!='5'){ error=true; errors+=' * Correct the 2+3 spam check\n'; } 
		
		if(error) 
		{ 
			alert(errors); 
		}
		else
		{
			$.ajax(
			{
				url: "ad_chapter.php",
				type:"POST",
				data:
				({
					action:'optin',
					email:email,
					check:check
				}),			
				success: function(data)
					{
						if(data!='ok')
						{ 
							alert("There was an error adding your email. Sorry.");
						}
						else
						{
							$('#email').val('');
							$('#check').val('');
							$.post("newsletter_add.php", 
							{
								email:email,
								type:'Book Opt In'
							});
							setTimeout('OptRedirect()', 1000)
						}
						
					}
			});	
		}
	}

