<!--//

function emailForm() {
	
	// add the onfocus/onblur bindings
	$('#yourEmail').focus(function() {
		if(this.value == 'Your email'){
			this.value = '';
		};
	});

	$('#yourEmail').blur(function() {
		if(this.value == '') {
			this.value = 'Your email';
		};
	});

	// form validation
	$("#emailAlertForm").validate({
		wrapper: 'span',
		rules: {
			yourEmail: {
				required: true, 
				email: true
			}
		},
		messages: {
			yourEmail: 'Please enter a valid email'
		}, 
		errorPlacement: function (error, element) {
			error.appendTo('#emailAlertError'); 
		}, 
		submitHandler: function() {

			// hide the submit btn
			$('#emailSubmitBtn').hide();

			// show the spinner
			$('#emailAlertSubmitSpinner').show();

			// submit the form
			$.ajax({
				type: 'POST', 
				url: '/newsletter/index.html', 
				dataType: 'json', 
				data: {
					fa: 'ajax_signup', 
					email: $('#yourEmail').val()
				}, 
				success: function(data) {

					// hide the spinner
					$('#emailAlertSubmitSpinner').hide();

					if (data.status == 'OK') {
						$('#emailAlertError').hide();
						$('#yourEmail').hide();
						$('#emailAlertSuccess').show();
					} else if (data.status == 'DUPLICATE') {
						$('#emailAlertError').html('Already subscribed');
						$('#emailSubmitBtn').show();
						$('#yourEmail').addClass('error');
					} else {
						$('#emailAlertError').html('An error occured');
						$('#emailSubmitBtn').show();
						$('#yourEmail').addClass('error');
					}
				}, 
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					// hide the spinner
					$('#emailAlertSubmitSpinner').hide();

					$('#emailAlertError').html('An error occured');
					$('#emailAlertSubmitBtn').show();
					$('#yourEmail').addClass('error');
				}
			});
		}
	});
}

$(document).ready(function() {
	emailForm();
});
	
//-->

