$(document).ready(function() {
	
	$(".text").click(function () {
	
		this.value= ''; 
		this.style.color = '#000'; 
		this.style.fontStyle = 'normal'; 
		$(this).removeClass('hightlight');	

										  
	});
	
	$("#reset").click(function(){
							   
		$(".text").removeClass('hightlight');	
	});
	
	
	//if submit button is clicked
	$('#submit_btn').click(function () {		
		var filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;

		//Get the data from all the fields
		var name = $('input[name=name]');
		var azienda = $('input[name=azienda]');
		var email = $('input[name=email]');
		var telefono = $('input[name=telefono]');
		var testo = $('textarea[name=testo]');
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (name.val()=='') {
			name.addClass('hightlight');
			return false;
		} else name.removeClass('hightlight');
		
		if (azienda.val()=='') {
			azienda.addClass('hightlight');
			return false;
		} else azienda.removeClass('hightlight');
		

		if (email.val()=='') {
			email.addClass('hightlight');
			return false;
		} else if(filtro.test(email.val())) email.removeClass('hightlight'); else {email.val("Formato email non consentito"); email.addClass('hightlight'); return false;}
		
		if (telefono.val()=='') {
			telefono.addClass('hightlight');
			return false;
		} else telefono.removeClass('hightlight');

		
		if (testo.val()=='') {
			testo.addClass('hightlight');
			return false;
		} else testo.removeClass('hightlight');
		
		//organize the data properly
		var data = 'name=' + name.val() +'&azienda=' + azienda.val()+'&telefono=' + telefono.val()+ '&email=' + email.val() + '&testo='  + encodeURIComponent(testo.val());
		
		//disabled all the text fields
		$('.text').attr('disabled','true');
		
		//show the loading sign
		$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "includes/utility/contact.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
					$('#form').fadeOut('slow');					
					
					//show the success message
					$('#done').fadeIn('slow');
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Ci dispiace, č avvenuto un errore inaspettato. Per favore riprovi pių tardi.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
});	

