$(function() {

		//if submit button is clicked
	$('#sub_submit').click(function () {		

		//Get the data from all the fields
		var email = $('input[name=sub_email]');
		var tok1 = $('input[name=sub_token1]');
		var tok2 = $('input[name=sub_token2]');		
		var error = 0;


if (email.val()=='') { 
email.addClass('hightlight'); 
error=1;
} else{
	var filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	if(filter.test(email.val())){
		email.removeClass('hightlight'); 
	}else{
		email.addClass('hightlight');
		error=1;
	}

}

if (error) return false;	
		
		$('#sub_submit').attr('disabled', true);
		//organize the data properly
		var data = {email : email.val(), token1 : tok1.val(), token2 : tok2.val()};
		
		//start the ajax
		$.ajax({

			//this is the php file that processes the data and send mail
			url: "subscribe.php",	
			
			type: "POST",

			//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=='Email added!') {					
					//hide the form
					email.val('Email został zapisany').addClass('sub-sent');
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
	
});	
