/****************************************************************************
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 = $("#catform").validate({
    rules: {
      req_name: {
        required: true,
        namechars: true
      },
      req_surname: {
        required: true,
        namechars: true
      },
      req_company: {
        required: function(element){
          return $("#req_type").val() != "Privato"
        }
      },
      req_cf: {
        required: true,
        iva: true,
        cf: true
      },
      req_city: {
        required: true,
        namechars: true
      },
      req_phone: {
        required: true,
        digits: true
      },
      req_fax: {
        digits: true
      },
      req_email: {
        required: true,
        email: true
      },
      req_rules: "required",
      req_pdata: "required"
    },
    messages: {
      req_name: {
        required: "&nbsp; Inserire il nome"
      },
      req_surname: {
        required: "&nbsp; Inserire il cognome"
      },
      req_company: {
        required: "&nbsp; Inserire la ragione sociale"
      },
      req_cf: {
        required: "&nbsp; P.IVA o Codice Fiscale"
      },
      req_city: {
        required: "&nbsp; Inserire l'indirizzo"
      },
      req_phone: {
        required: "&nbsp; Inserire il telefono",
        digits: "&nbsp; Iserire solo numeri"
      },
      req_fax: {
        digits: "&nbsp; Iserire solo numeri"
      },
      req_email: {
        required: "&nbsp; Inserire un inidrizzo valido",
        email: "&nbsp; Inserire un inidrizzo valido"
      },
      req_rules: " ",
      req_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();
    },
    // 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);
  }, "&nbsp; Carattere non valido");  
  jQuery.validator.addMethod("usernamechars", function(value, element) {
    return this.optional(element) || /^[a-z-0-9-.]+$/i.test(value);
  }, "&nbsp; Carattere non valido");
  jQuery.validator.addMethod("pwdchars", function(value, element) {
    return this.optional(element) || /^[a-z-0-9-!$?@*]+$/i.test(value);
  }, "&nbsp; Carattere non valido");
  jQuery.validator.addMethod("cf", function(cf, element) {
    var req_type = $("#req_type").val();
    if (req_type == "Privato")
      return this.optional(element) || cf.length == 16 && cf.match(/^[a-zA-Z]{6}\d{2}[a-zA-Z]{1}\d{2}[a-zA-Z]{1}\d{3}[a-zA-Z]{1}$/);
      else
        return true;
  }, "&nbsp; Codice fiscale non valido");
  jQuery.validator.addMethod("iva", function(pi, element) {
    var req_type = $("#req_type").val();
    if (req_type == "Privato")
      return true;
    else
      return this.optional(element) || pi.length == 11 && pi.match(/^\d{5}\d{6}$/);
  }, "&nbsp; Partita IVA non valida");
});

function register()
{
  $.post(
    "ajax.php", 
    {
      mod:"blabla", 
      myaction:"request_catalogue", 
      req_type: $("#req_type").val(),
      req_name: $("#req_name").val(),
      req_surname: $("#req_surname").val(),
      req_company: $("#req_company").val(),
      req_cf: $("#req_cf").val(),
      req_phone: $("#req_phone").val(),
      req_fax: $("#req_fax").val(),
      req_username: $("#req_username").val(),
      req_email: $("#req_email").val(),
      req_city: $("#req_city").val(),
      req_note: $("#req_note").val(),
      req_newsletter: $("#req_newsletter:checked").val(),
      req_vector: $("input[@name='req_vector']:checked").val()
    }, 
    function(xml) {
      $("#req_msg").html(xml);
    }
  );
}
