$(function () {
	
	$('#FormEmail').click(function () {
		
		if ( $(this).val() == 'e-mail') {
			$(this).val('');
			$(this).css({color:'black'});
		}
		

	});
	
	$('#FormEmail').blur(function () {
		if ( $(this).val() == '') $(this).val('e-mail');
		
	});
	
	
	$('#Form').submit(function (event) {
		
		if ( is_valid_email( $('#FormEmail').val() ) ) {
			
			$.ajax({
				type: "POST",
				url: "/subscribe.php",
				data: $('#Form').serialize(),
				success: function ( msg ) {
					
					if ( msg == 'OK') {
						$('#Form').html("<p>Thank you for signing up.<br><br>We'll contact you when we're ready.</p>");
					} else {
						$('#Form').append("<p>Something went wrong, please try again later.</p>");
					}
					
				},
				error: function () {
					
				}

			});

			
		} else {
			alert('This is not a valid e-mail address.');
			$('#FormEmail').val('').focus();
		}
		
		event.preventDefault();
	});
	
});



function is_valid_email(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);
}
