/****************************************************************************
eBIT-Site
-------------------
Copyright 2006, 2007, 2008 Davide Zaccaria, Orazio Langenbach

This file is part of eBIT-Site, a module of the framework POMO.

eBIT-Site is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

eBIT-Site is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with eBIT-Site.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

$(function() {
  var validator = $("#signupform").validate({
    rules: {
      reg_name: {
        required: true,
        namechars: true
      },
      reg_surname: {
        required: true,
        namechars: true
      },
      reg_city: {
        required: true,
        namechars: true
      },
      reg_username: {
        required: true,
        minlength: 3,
        usernamechars: true,
        remote: "ajax.php?p="+modid+":14"
      },
      reg_pwd: {
        required: true,
        minlength: 5,
        pwdchars: true
      },
      reg_pwd2: {
        required: true,
        minlength: 5,
        equalTo: "#reg_pwd"
      },
      reg_email: {
        required: true,
        email: true,
        remote: "ajax.php?p="+modid+":13"
      },
      reg_rules: "required",
      reg_pdata: "required"
    },
    messages: {
      reg_name: {
        required: "&nbsp; Inserire il nome"
      },
      reg_surname: {
        required: "&nbsp; Inserire il cognome"
      },
      reg_city: {
        required: "&nbsp; Inserire la citt&agrave;"
      },
      reg_username: {
        required: "&nbsp; Inserire un nome utente",
        minlength: jQuery.format("&nbsp; Inserire almeno {0} caratteri"),
        maxlength: jQuery.format("&nbsp; Inserire meno di {0} caratteri"),
        remote: jQuery.format("&nbsp; {0} gi&agrave; in uso")
      },
      reg_pwd: {
        required: "&nbsp; Inserire una password",
        minlength: jQuery.format("&nbsp; Inserire almeno {0} caratteri")
      },
      reg_pwd2: {
        required: "&nbsp; Ripetere la password",
        minlength: jQuery.format("&nbsp; Inserire almeno {0} caratteri"),
        equalTo: "&nbsp; Le password devono essere uguali"
      },
      reg_email: {
        required: "&nbsp; Inserire un inidrizzo valido",
        email: "&nbsp; Inserire un inidrizzo valido",
        remote: jQuery.format("&nbsp; {0} gi&agrave; in uso")
      },
      reg_rules: " ",
      reg_pdata: " "
    },
    // the errorPlacement has to take the table layout into account
    errorPlacement: function(error, element) {
      if ( element.is(":radio") )
        error.appendTo( element.parent().next().next() );
      else if ( element.is(":checkbox") )
        error.appendTo ( element.next() );
      else
        error.appendTo( element.parent().next() );
    },
    // specifying a submitHandler prevents the default submit, good for the demo
    submitHandler: function() {
      //register();
      document.signupform.submit();
    },
    // set this class to error-labels to indicate valid fields
    success: function(label) {
      // set &nbsp; as text for IE
      label.html("&nbsp;").addClass("checked");
    }
  });

  jQuery.validator.addMethod("namechars", function(value, element) {
    return this.optional(element) || /^[a-z-àâãäèëêéìïîòôöõøùûü'\s]+$/i.test(value);
  }, "Carattere non valido");  
  jQuery.validator.addMethod("usernamechars", function(value, element) {
    return this.optional(element) || /^[a-z-0-9-.]+$/i.test(value);
  }, "Carattere non valido");
  jQuery.validator.addMethod("pwdchars", function(value, element) {
    return this.optional(element) || /^[a-z-0-9-!$?@*]+$/i.test(value);
  }, "Carattere non valido");
});

function register()
{
  $.post(
    "ajax.php", 
    {
      mod:"register", 
      myaction:"register", 
      reg_photo: $("#reg_photo").val(),
      reg_name: $("#reg_name").val(),
      reg_surname: $("#reg_surname").val(),
      reg_username: $("#reg_username").val(),
      reg_email: $("#reg_email").val(),
      reg_city: $("#reg_city").val(),
      reg_pwd: $("#reg_pwd").val(),
      reg_newsletter: $("#reg_newsletter:checked").val()
    }, 
    function(xml) {
      $("#reg_msg").html(xml);
    }
  );
}
