$(function() {

  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
	
	var indirizzo = $("select#dest_box").val();
	if (indirizzo == "") {
		$("label#dest_error").show();
		$("select#dest_box").focus();
		return false;
    }
	
	var name = $("input#name_box").val();
	if (name == "") {
		$("label#name_error").show();
		$("input#name_box").focus();
		return false;
    }
	
	var company = $("input#company_box").val();
	
	var email = $("input#email_box").val();
	
	var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/
	
	if (!(re.test(email))){
		$("label#email_error").show();
		$("input#email_box").focus();
		return false;
	}
	
	if (email == "") {
		$("label#email_error").show();
		$("input#email_box").focus();
		return false;
    }
	
	var phone = $("input#phone_box").val();
	/*if (phone == "") {
		$("label#phone_error").show();
		$("input#phone").focus();
		return false;
    }*/
	
	var servizi = $("select#servizi_box").val();
	if (servizi == "") {
		$("label#servizi_error").show();
		$("select#servizi_box").focus();
		return false;
    }
	
	var note = $("textarea#note_box").val();
		
	var dataString = 'indirizzo=' + indirizzo + '&name='+ name + '&azienda=' + company + '&email=' + email + '&phone=' + phone + '&servizio=' + servizi + '&note=' + note;
		
		
		
		
		
		//alert (dataString);return false;






	
	$.ajax({
		type: "POST",
		url: "bin/process.php",
		data: dataString,
		success: function() {
			$('#contact_form').html("<div id='message'></div>");
			$('#message').html("<h2>La Sua richiesta &egrave stata inviata!</h2>")
			.append("<p>La ricontatteremo al pi&ugrave presto.</p>")
			.hide()
			.fadeIn(1500, function() {
				$('#message').append("<img id='checkmark' src='images/conferma.png' />");
			});
		}
    });
	
    return false;
	});
});

runOnLoad(function(){
  $("input#name_box").select().focus();
});