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

  $(".buttonRegistra").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
	
	var matricola = $("input#matricola").val();
		if (matricola == "") {
      $("label#matricola_error").show();
      $("input#matricola").focus();
      return false;
    }
   
	var hidenError = $("input#hidenError").val();
		if (hidenError == "") {
      $("label#hidenError_error").show();
      $("input#hidenError").focus();
     return false;
    }
		
	  var nome = $("input#nome").val();
		if (nome == "") {
      $("label#nome_error").show();
      $("input#nome").focus();
      return false;
    }
    
    var cognome = $("input#cognome").val();
		if (cognome == "") {
      $("label#cognome_error").show();
      $("input#cognome").focus();
      return false;
    }
   
   var sede = $("input#sede").val();
		if (sede == "") {
      $("label#sede_error").show();
      $("input#sede").focus();
      return false;
    }
	var email = $("input#email").val();
       if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
		$("label#email_error").show();
        $("input#email").focus();
        return false;
	}
	
	var hidenError = $("input#hidenErrorEmail").val();
		if (hidenError == "") {
      $("label#hidenErrorEmail_error").show();
      $("input#hidenErrorEmail").focus();
     return false;
    }
	
	var passwd1 = $("input#passwd1").val();
		if (passwd1.length <8 ) {
      $("label#passwd1_error").show();
      $("input#passwd1").focus();
      return false;
    }	
		var dataString = 'nome='+ nome + '&cognome='+ cognome + '&matricola=' + matricola + '&sede=' + sede + '&email=' + email + '&passwd='+passwd1;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "bin/registrazione_email.php",
      data: dataString,
      success: function() {
        $('#contact_form').html("<div id='message'></div>");
        $('#message').html("<h2>Registrazione Riuscita!</h2>")
        .append("<p>Riceverete una email di conferma al piu presto!.</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#nome").select().focus();
});
