jQuery(function($){

	//Ajax Mail function
	//if submit button is clicked
	$('#subscribe-btn').click(function () {		

		//Get the data from all the fields
		var email = $('input[name=email]');

		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (email.val()=='') {
			email.addClass('hightlight');
			alert('Please enter your email address.');		
			return false;
			
		} else if (email.val()=='Add your email') {
			email.addClass('hightlight');
			alert('Please enter your email address.');		
			return false;
			
		} else if(!emailReg.test(email.val())) {
			email.addClass('hightlight');
			alert('Please enter a valid email address.');		
			return false;
			
		} else email.removeClass('hightlight');
		
		//organize the data properly
		var data = 'email=' + email.val();
		
		//show the loading sign
		$('#loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "process.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process.php returned 1/true (send mail success)
				if (html==1) {					
					//hide the form
					$('#subscribe-form').find(':input').each(function() {
						switch(this.type) {
							case 'text':
								$(this).val('Thanks! You are now on the list');
								break;
						}
					});				
					
					$('#loading').fadeOut('Slow');
					
					alert('Thanks! You are now on the list');	
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});
	
});
