$(function() {

		//if submit button is clicked
		
		$("#step2 input[type='button']").click(function () {
			var email = $('input[name=email]');			
			var error = 0;
			
			if (email.val()=='') { 
				email.addClass('ekspert-email');
				error=1;
			}else{
				var filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
				if(filter.test(email.val())){
					email.removeClass('ekspert-email'); 
				}else{
					email.addClass('ekspert-email');
					error=1;
				}
			}
				
			if (error) return false;
			else{
				$('#step2').hide();
				$('#step3').show();
			};
		});
		

		$("#step3 input[type='submit']").click(function () {		

		//Get the data from all the fields
		var pytanie = $('textarea[name=pytanie]');
		var www = $('input[name=www]');
		var frazy = $('input[name=frazy]');
		var jestpozycj = $('input[name=jestpozycj]');
		var imie = $('input[name=imie]');
		var email = $('input[name=email]');
		var uzupelnienie = $('textarea[name=uzupelnienie]');
		
		var tok1 = $('input[name=sub_token1]');
		var tok2 = $('input[name=sub_token2]');		
				
		$("#step3 input[type='submit']").attr('disabled', true);
		//organize the data properly
		var data = {pytanie : pytanie.val(), www : www.val(), frazy : frazy.val(), jestpozycj : jestpozycj.val(), imie : imie.val(), uzupelnienie : uzupelnienie.val(), 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: "sendmsg-ekspert.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
					$('#step3').hide();
					$('#step4').show();
					$('#submit').attr('disabled', false);					
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
	
});	
