$(document).ready(function(){
	
	var options = {
		beforeSubmit: validate,
		url: '/mailList',
		type: 'post',

		success: completed,
		resetForm: true
	};
	
	
	//Clear text field when clicked
	$("#addemail").click(function(){
		$("#addemail")[0].value = "";
	});
	

	$('#mailList').submit(function() { 
		$(this).ajaxSubmit(options); 
		return false;
	});
});		
	
	
	/**
	 * Form validation
	 */
	function validate(formData, jqForm, options){
		//Get the variable
		var email = $('#mailList input[@name=addemail]').fieldValue();
				
		//Validate
		if(checkemail(email) == false){
			return false;
		}
		
		//
		blockUser();
		return true;
	}
	
	
	
	
	/**
	 * Call when ajax finished.
	 */
	function completed(responseText, statusText){
	//no message
		if(responseText == "true"){
				$.blockUI( '<table cellpadding="0" cellspacing="0" border="0" width="280"><tr><td><img src="/assets/images/4hp_load_logo_3_36.gif" /><\/td><td valign="center" align="left"><span style="font-size:15px;">Thank you.</span><br /><br /><span style="font-size:12px;"> You have been added to the mailing list.</span><\/td><\/tr><\/table>' );
		}else{
				$.blockUI( '<table cellpadding="0" cellspacing="0" border="0" width="280"><tr><td><img src="/assets/images/4hp_load_logo_3_36.gif" /><\/td><td valign="center" align="left">You are already on our mailing list.</span><\/td><\/tr><\/table>' );
		}

		var unblock = function(){
			$.unblockUI();
		}
		
		setTimeout(unblock, 3500); 
	}
	
	
	/**
	 * Block the UI
	 */
	function blockUser(){
		$.blockUI( { backgroundColor: '#231b1a', color: '#ffffff', height:'100px', padding:'20px' ,width:'300px'} );
		$.blockUI( '<table cellpadding="0" cellspacing="0" border="0" width="280"><tr><td><img src="/assets/images/4hp_load_logo_3_36.gif" /><\/td><td>Please Wait...<\/td><\/tr><\/table>' );
	}