$(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 name = $("input#nome").val();
		if (name == "") {
      $("label#nome_error").show();
      $("input#nome").focus();
      return false;
    }
	  var name = $("input#cognome").val();
		if (name == "") {
      $("label#cognome_error").show();
      $("input#cognome").focus();
      return false;
    }
	  var name = $("input#ragione").val();
		if (name == "") {
      $("label#ragione_error").show();
      $("input#ragione").focus();
      return false;
    }
		  var name = $("input#indirizzo").val();
		if (name == "") {
      $("label#indirizzo_error").show();
      $("input#indirizzo").focus();
      return false;
    }
		  var name = $("input#citta").val();
		if (name == "") {
      $("label#citta_error").show();
      $("input#citta").focus();
      return false;
    }
		  var name = $("input#provincia").val();
		if (name == "") {
      $("label#provincia_error").show();
      $("input#provincia").focus();
      return false;
    }
		  var name = $("input#cap").val();
		if (name == "") {
      $("label#cap_error").show();
      $("input#cap").focus();
      return false;
    }
		  var name = $("input#telefono").val();
		if (name == "") {
      $("label#telefono_error").show();
      $("input#telefono").focus();
      return false;
    }
		  var name = $("input#fax").val();
		if (name == "") {
      $("label#fax_error").show();
      $("input#fax").focus();
      return false;
    }
		  var name = $("input#email").val();
		if (name == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    }
		var email = $("textarea#messaggio").val();
		if (email == "") {
      $("label#messaggio_error").show();
      $("textarea#messaggio").focus();
      return false;
    }

		
		var dataString = 'nome='+ nome + 'cognome='+ cognome + 'ragione='+ ragione + 'indirizzo='+ indirizzo + 'citta='+ citta + 'cap='+ cap + 'telefono='+ telefono + 'fax='+ fax + '&email=' + email + '&messaggio=' + messaggio;
		//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>Il messaggio è stato inviato correttamente.</h2>")
        .append("<p>Sarete contattati quanto prima.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#nome").select().focus();
});
