/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function foto_allegato_validation() {
  if ($("#upload_foto_submit").size() > 0) {
    $("#upload_foto_submit").click(function() {
      if (!$("#fotofile").valid())
        return false;
      return true;
    });
  }
  if ($("#upload_allegato_submit").size() > 0) {
    $("#upload_allegato_submit").click(function() {
      if (!$("#allegatofile").valid())
        return false;
      return true;
    });
  }
  return;
}

function filter_reset() {
  $("input[name$=filter_reset]").click(function() {
      $("input[type=text].filter").clearFields();
      $("select.filter").each(function() {
        $("option",$(this))[0].selected = true;
      });
      return false;
    }
  );
}

function checkbox_all() {
  if ($('.check_all').size() == 1) {
    $(".check_all").click(function() {
      var checked = this.checked;
      $("input[type=checkbox].index_checkbox").attr("checked",checked);
      if (!checked) $("input.delete").attr("disabled",true);
      else $("input.delete").attr("disabled",false);
    });
    $("input[type=checkbox].index_checkbox").click(function() {
      if (!this.checked) {
        $("input[type=checkbox].check_all").attr("checked",false);
        if ($("input[type=checkbox].index_checkbox:checked").size() == 0) {
          $("input.delete").attr("disabled",true);
        }
      } else {
        $("input.delete").attr("disabled",false);
      }
    });
    if ($("input[type=checkbox].index_checkbox:checked").size() == 0)
      $("input.delete").attr("disabled",true);
    else
      $("input.delete").attr("disabled",false);
  }
  return;

}

function js_validation(form_id) {
  switch (form_id) {
    case 'ins_mod_user':
      $("#ins_mod_user").validate({
              rules: {
                  nome: {
                      required: true,
                      maxlength: 45
                  },
                  cognome: {
                      required: true,
                      maxlength: 45
                  },
                  username: {
                      required: true,
                      minlength: 2,
                      maxlength: 45
                  },
                  password: {
                      //required: true,
                      minlength: 5,
                      maxlength: 45
                  },
                  password_confirm: {
                      //required: true,
                      minlength: 5,
                      maxlength: 45,
                      equalTo: "#password"
                  },
                  email: {
                      required: true,
                      email: true,
                      maxlength: 100
                  },
                  fotofile: {
                      accept: "jpg|png|gif"
                  },
                  allegatofile: {
                      accept: "pdf|zip|xls|doc"
                  }
              },
              messages: {
                  nome: "Inserisci il nome",
                  cognome: "Inserisci il cognome",
                  username: {
                      required: "Inserisci un username",
                      minlength: "L'username deve avere almeno 2 caratteri",
                      maxlength: "L'username deve avere al massimo 45 caratteri"
                  },
                  password: {
                      required: "Inserisci una password",
                      minlength: "La password deve avere almeno 5 caratteri",
                      maxlength: "La password deve avere al massimo 45 caratteri"
                  },
                  password_confirm: {
                      required: "Inserisci una password",
                      minlength: "La password deve avere almeno 5 caratteri",
                      maxlength: "La password deve avere al massimo 45 caratteri",
                      equalTo: "Inserisci la stessa password inserita sopra"
                  },
                  email: "Inserisci un valido indirizzo email",
                  fotofile: "Solo files jpg, png, gif",
                  allegatofile: "Solo files pdf, zip, doc, xls"
              }
        });
        foto_allegato_validation();
      break;
    case 'ins_mod_link':
      $("#ins_mod_link").validate({
              rules: {
                  nome: {
                      required: true,
                      maxlength: 255
                  },
                  url: {
                      required: true,
                      maxlength: 255
                  },
                  fotofile: {
                      accept: "jpg|png|gif"
                  },
                  allegatofile: {
                      accept: "pdf|zip|xls|doc"
                  }
              },
              messages: {
                  nome: "Inserisci il nome",
                  url: "Inserisci l'URL",
                  fotofile: "Solo files jpg, png, gif",
                   allegatofile: "Solo files pdf, zip, doc, xls"
              }
        });
        foto_allegato_validation();
      break;
    case 'ins_mod_generic':
      $("#ins_mod_generic").validate({
              rules: {
                  nome: {
                      required: true,
                      maxlength: 255
                  },
                  fotofile: {
                      accept: "jpg|png|gif"
                  },
                  allegatofile: {
                      accept: "pdf|zip|xls|doc"
                  }
              },
              messages: {
                  nome: "Inserisci il nome",
                  fotofile: "Solo files jpg, png",
                   allegatofile: "Solo files pdf, zip, doc, xls"
              }
        });
        foto_allegato_validation();
      break;
    case 'ins_mod_sponsor':
      $("#ins_mod_sponsor").validate({
              rules: {
                  nome: {
                      required: true,
                      maxlength: 255
                  },
                  url: {
                      required: false,
                      maxlength: 255
                  },
                  fotofile: {
                      accept: "jpg|png|gif"
                  },
                  allegatofile: {
                      accept: "pdf|zip|xls|doc"
                  }
              },
              messages: {
                  nome: "Inserisci il nome",
                  url: "Inserisci l'URL",
                  fotofile: "Solo files jpg, png, gif",
                   allegatofile: "Solo files pdf, zip, doc, xls"
              }
        });
        foto_allegato_validation();
      break;
    case 'ins_mod_fotogallery':
      $("#ins_mod_fotogallery").validate({
              rules: {
                  nome: {
                      required: true,
                      maxlength: 255
                  },
                  fotofile: {
                      accept: "jpg|png|gif"
                  },
                  allegatofile: {
                      accept: "zip"
                  }
              },
              messages: {
                  nome: "Inserisci il nome",
                  fotofile: "Solo files jpg, png, gif",
                  allegatofile: "Solo files zip"
              }
        });
        foto_allegato_validation();
      break;
    case 'ins_mod_evento':
      $("#ins_mod_evento").validate({
              rules: {
                  titolo: {
                      required: true,
                      maxlength: 255
                  },
                  descrizione: {
                      required: false
                  },
                  fotofile: {
                      accept: "jpg|png|gif"
                  },
                  allegatofile: {
                      accept: "pdf|doc|zip"
                  }
              },
              messages: {
                  titolo: "Inserisci il titolo",
                  descrizione: "Inserisci la descrizione",
                  fotofile: "Solo files jpg, png, gif",
                  allegatofile: "Solo files pdf, doc, zip"
              }
        });
        foto_allegato_validation();
        date_binder_start_date('#data_inizio','#data_fine');
      break;
    case 'ins_mod_news':
      $("#ins_mod_news").validate({
              rules: {
                  titolo: {
                      required: true,
                      maxlength: 255
                  },
                  descrizione: {
                      required: false
                  },
                  fotofile: {
                      accept: "jpg|png|gif"
                  },
                  allegatofile: {
                      accept: "pdf|doc|zip"
                  }
              },
              messages: {
                  titolo: "Inserisci il titolo",
                  descrizione: "Inserisci la descrizione",
                  fotofile: "Solo files jpg, png, gif",
                  allegatofile: "Solo files pdf, doc, zip"
              }
        });
        foto_allegato_validation();
      break;
    case 'ins_mod_spettacolo':
      $("#ins_mod_spettacolo").validate({
              rules: {
                  nome: {
                      required: true,
                      maxlength: 255
                  },
                  descrizione: {
                      required: false
                  },
                  fotofile: {
                      accept: "jpg|png|gif"
                  },
                  allegatofile: {
                      accept: "pdf|doc|zip"
                  }
              },
              messages: {
                  nome: "Inserisci il nome",
                  descrizione: "Inserisci la descrizione",
                  fotofile: "Solo files jpg, png, gif",
                  allegatofile: "Solo files pdf, doc, zip"
              }
        });
        foto_allegato_validation();
        init_personaggio_rows();
      break;
     case 'ins_mod_appuntamento':
      $("#ins_mod_appuntamento").validate({
//              rules: {
//                  nome: {
//                      required: true,
//                      maxlength: 255
//                  },
//                  descrizione: {
//                      required: false
//                  },
//                  fotofile: {
//                      accept: "jpg|png|gif"
//                  },
//                  allegatofile: {
//                      accept: "pdf|doc|zip"
//                  }
//              },
//              messages: {
//                  nome: "Inserisci il nome",
//                  descrizione: "Inserisci la descrizione",
//                  fotofile: "Solo files jpg, png, gif",
//                  allegatofile: "Solo files pdf, doc, zip"
//              }
        });
        foto_allegato_validation();
        $.ajaxSetup({type: "POST", timeout: 20000, dataType: "json"});
        init_appuntamento_rows();
      break;
    case 'ins_mod_via':
      $("#ins_mod_via").validate({
              rules: {
                  nome: {
                      required: true,
                      maxlength: 255
                  },
                  tipo: {
                      required: true,
                      maxlength: 255
                  },
                  note: {
                      required: false,
                      maxlength: 255
                  }
              },
              messages: {
                  nome: "Inserisci il nome della via/piazza/...",
                  tipo: "Inserisci il tipo: via/piazza/piazzale..."
              }
        });
        ajax_comune_change('comune_id','provincia_id');
      break;
    case 'ins_mod_socio':
      $("#ins_mod_socio").validate({
              rules: {
                  cognome: {
                      required: true,
                      maxlength: 255
                  },
                  nome: {
                      required: true,
                      maxlength: 255
                  }
              },
              messages: {
                  nome: "Inserisci il nome del socio",
                  cognome: "Inserisci il cognome del socio"
              }
        });
        $.ajaxSetup({type: "POST", timeout: 20000});
        ajax_comune_change('comune_id','provincia_id',true);
        socio_via_suggest();
        socio_comune_cap_change();
        $('div[id^=man_date_]').each(function() {
//          var index = this.id.substr(this.id.lastIndexOf("_") + 1);
//          var anno = $('#tessera_socio_anno_' + index).val();
          man_date_init(this,2003,Number(curr_year) + 1);
        });
        $('input[id^=tessera_socio_anno_], input[id^=tessera_socio_numero_]').change(function() {
          check_tessera('ins_mod_socio_submit');
        });
        $('#cognome, #numero_civico, #indirizzo, #nome').change(function() {
          check_omonimi();
        });
        init_tessere_rows();

      break;
    case 'socio_rinnovo_index':
//      $("#soci_rinnovo_index").validate({
//              rules: {
//                  cognome: {
//                      required: true,
//                      maxlength: 255
//                  },
//                  nome: {
//                      required: true,
//                      maxlength: 255
//                  }
//              },
//              messages: {
//                  nome: "Inserisci il nome del socio",
//                  cognome: "Inserisci il cognome del socio"
//              }
//        });
        $.ajaxSetup({type: "POST", timeout: 20000});
        $('input[id^=tessera_socio_numero_]').change(function() {
          check_tessera_rinnovo('rinnova_socio_submit');
        });
        $('div[id^=man_date_]').each(function() {
//          var index = this.id.substr(this.id.lastIndexOf("_") + 1);
//          var anno = $('#tessera_socio_anno_' + index).val();
          man_date_init(this,2003,Number(curr_year) + 1);
        });

      break;
     case 'ins_mod_area_tematica':
      $("#ins_mod_azienda_laboratorio").validate({
              rules: {
                  nome: {
                      required: true,
                      maxlength: 255
                  },
                  descrizione: {
                      required: false
                  },
                  fotofile: {
                      accept: "jpg|png|gif"
                  },
                  allegatofile: {
                      accept: "pdf|doc|zip"
                  }
              },
              messages: {
                  nome: "Inserisci il nome",
                  descrizione: "Inserisci la descrizione",
                  fotofile: "Solo files jpg, png, gif",
                  allegatofile: "Solo files pdf, doc, zip"
              }
        });
        foto_allegato_validation();
      break;
  default:
    break;
}
  return;
}