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

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  var name = $("input#name").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }

	var email = $("input#email").val();
	if (!email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/)) {
      //alert(email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/));
	  $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
	var phone = $("input#phone").val();
	if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }
	
	var testo = $("#testo").val();
	
	if (testo == "") {
      $("label#testo_error").show();
      $("input#testo").focus();
      return false;
    }
		
		var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&testo=' + testo;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "form/contattimail.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>La tua richiesta &egrave; stata inviata!</h2>")
        .append("<p>verrai contattato al pił presto da un nostro consulemte</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});

