$(function() {

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

		//Get the data from all the fields
		var email = $('input[name=email]');
		var name = $('input[name=name]');
		var area = $("input[name='area[]']").serialize().replace(/%5B%5D/g, '[]');
		var tel = $('input[name=tel]');
		var msg = $('textarea[name=msg]').val();
		var tok1 = $('input[name=token1]');
		var tok2 = $('input[name=token2]');		
		var error = 0;


if (name.val()=='') { 
name.prev().addClass('hightlight'); 
error=1;
} else name.prev().removeClass('hightlight'); 

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

}

if (msg == 'Zadaj dowolne pytanie związane z pozycjonowaniem i tworzeniem stron, sklepów, blogów, serwisów internetowych...') msg='';

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

			//this is the php file that processes the data and send mail
			url: "sendmsg-page.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=='Thank you! We have received your message.') {					
					//hide the form
					$('#form').fadeOut('fast', function() {
									$('#form-sent').fadeIn('fast');
									$('#submit').attr('disabled', false);
								});					
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
	
});	
