$(function()
{
  for (i = 0; i < 40; i++)
  {
    image = new Image();
    $(image).attr("src", "ajax.php?p=49:1:"+langid+":"+i);
    $(image).attr("id", i);
    $("#kb"+i).append(image);
  }

  $(".keyboard", this).click(function(e){
    var username = $("#username").val();
    var pwd = $("#pwd").val();

    $.post("ajax.php", {p:"49:2:"+langid+":"+e.target.id, username:username, pwd:pwd}, function(xml) {
      if (xml == 0)
      {
        document.location = $("#login_url").val();
      }
      else
      {
        if (xml == "y")
        {
          alert("Non hai premuto il pulsante giallo!");
        }
        else if (xml == "w")
        {
          alert("Utente non ancora abilitato:\nsi attende conferma della registrazione.");
        }
        else if (xml == "b")
        {
          alert("Utente sospeso.");
        }
        else
          alert("Autenticazione non riuscita, nome utente o password errati.");
  
        window.location.reload(true);
      }
    });
  });

  var validator = $("#precform").validate({
    rules: {
      prec_email: {
        required: true,
        email: true
      }
    },
    messages: {
      prec_email: {
        required: "&nbsp; Inserire un indirizzo valido",
        email: "&nbsp; Inserire un indirizzo valido"
      }
    },
    errorPlacement: function(error, element) {
      error.appendTo( element.parent().next() );
    },
    submitHandler: function() {
      recoverPwd();
    },
    success: function(label) {
      // set &nbsp; as text for IE
      label.html("&nbsp;").addClass("checked");
    }
  });
});

function recoverPwd()
{
  $.post(
    "ajax.php", 
    {
      p:modid+":16:"+langid,
      prec_email: $("#prec_email").val()
    },
    function(xml) {
      $("#prec_msg").html(xml);
    }
  );
}
