function nospecials(myfield, e, dec) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) || (key==37) || (key==39) || (key==46) )
    return true;

  // caratteri
  else if ((("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_").indexOf(keychar) > -1))
    return true;

  else
    return false;
}

function solonumeri(myfield, e, dec) {
  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) || (key==37) || (key==39) || (key==46) )
    return true;

  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
    return true;

  else
    return false;
}

function trim(stringa){
    while (stringa.substring(0,1) == ' '){
            stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }    
return stringa;
}

function validateEmail(elementValue){   
   var emailPattern = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;   
   return emailPattern.test(elementValue);   
}  

function controlla_corsoestivo() {
    if (trim(document.formestivo.genitore.value) == "" || trim(document.formestivo.bambino.value) == "" || trim(document.formestivo.anni.value) == "" || trim(document.formestivo.telefono.value) == "" || trim(document.formestivo.indirizzomail.value) == "") {
        alert("Tutti i campi sono obbligatori!");
        return false;
    }
    if (!validateEmail(document.formestivo.indirizzomail.value)) {
        alert("Indirizzo email non valido!");
        return false;    
    }
    if (!(document.formestivo.primasettimana.checked || document.formestivo.secondasettimana.checked || document.formestivo.terzasettimana.checked || document.formestivo.quartasettimana.checked)) {
        alert("Selezionare almeno una settimana!");
        return false;
    }
    return true;
}

function controlla_campob_applicant_en() {
    if (trim(document.campob_applicant.applcityform.value) == "") {
        alert("Fill out the field \"City\"");
        return false;
    }
    if (trim(document.campob_applicant.applcountryform.value) == "") {
        alert("Fill out the field \"Country\"");
        return false;
    }
    if (trim(document.campob_applicant.applnameform.value) == "") {
        alert("Fill out the field \"Name\"");
        return false;
    }
    if (trim(document.campob_applicant.appllastnameform.value) == "") {
        alert("Fill out the field \"Last Name\"");
        return false;
    }
    if (trim(document.campob_applicant.applemailform.value) == "") {
        alert("Fill out the field \"Email Address\"");
        return false;
    }
    if (!validateEmail(document.campob_applicant.applemailform.value)) {
        alert("Invalid email address");
        return false;    
    }
    if (trim(document.campob_applicant.applphoneform.value) == "") {
        alert("Fill out the field \"Phone Number\"");
        return false;
    }
    if (trim(document.campob_applicant.applmobileform.value) == "") {
        alert("Fill out the field \"Mobile Number\"");
        return false;
    }
    return true;
}

function controlla_campob_applicant_it() {
    if (trim(document.campob_applicant.applcityform.value) == "") {
        alert("Compilare il campo \"Cittą\"");
        return false;
    }
    if (trim(document.campob_applicant.applnameform.value) == "") {
        alert("Compilare il campo \"Nome\"");
        return false;
    }
    if (trim(document.campob_applicant.appllastnameform.value) == "") {
        alert("Compilare il campo \"Cognome\"");
        return false;
    }
    if (trim(document.campob_applicant.applemailform.value) == "") {
        alert("Compilare il campo \"Indirizzo e-mail\"");
        return false;
    }
    if (!validateEmail(document.campob_applicant.applemailform.value)) {
        alert("Indirizzo e-mail non valido");
        return false;    
    }
    if (trim(document.campob_applicant.applphoneform.value) == "") {
        alert("Compilare il campo \"Telefono\"");
        return false;
    }
    if (trim(document.campob_applicant.applmobileform.value) == "") {
        alert("Compilare il campo \"Cellulare\"");
        return false;
    }
    return true;
}

function seleziona_anno_news() {
  var select_id = document.myform.archivionews;
  var selected_id = select_id.options[select_id.selectedIndex].value;
  if (selected_id != "noselect")
    location.href = "news.php?anno="+selected_id;
}

function seleziona_anno_stampa() {
  var select_id = document.myform.archiviostampa;
  var selected_id = select_id.options[select_id.selectedIndex].value;
  if (selected_id == "recenti") 
    location.href = "stampa.php";
  else if (selected_id != "noselect")
    location.href = "stampa.php?anno="+selected_id;
}

function seleziona_anno_circolari() {
  var select_id = document.myform.archiviocircolari;
  var selected_id = select_id.options[select_id.selectedIndex].value;
  if (selected_id == "recenti") 
    location.href = "circolari.php";
  else if (selected_id != "noselect")
    location.href = "circolari.php?anno="+selected_id;
}

// Calcola anno corrente per Copyright
function ins_anno_corr() {
    today=new Date();
    y0=today.getFullYear();
    document.write(y0);
}


function controlladata() {
   gg=document.campob_form.giorno.value;
   mm=document.campob_form.mese.value;
   aa=document.campob_form.anno.value;
   
   strdata=gg+"/"+mm+"/"+aa;

   data = new Date(aa,mm-1,gg);
   daa=data.getFullYear().toString();
   dmm=(data.getMonth()+1).toString();
   dmm=dmm.length==1?"0"+dmm:dmm
   dgg=data.getDate().toString();
   dgg=dgg.length==1?"0"+dgg:dgg
   dddata=dgg+"/"+dmm+"/"+daa;
   if (dddata!=strdata) {
      return false;
   } else {
      return true;
   }
}

function isNumeric(string) {  
  var numericExpression = /^[0-9]+$/;  
  if(string.match(numericExpression)) {  
     return true;  
  } else {  
     return false;  
  }  
}  

function controlla_campob_form_en() {
    validadata = controlladata();
    if (!(document.campob_form.boatform[0].checked || document.campob_form.boatform[1].checked)) {
        alert("Flag \"Will you use your own Boat?\"");
        return false;
    }
    if (document.campob_form.boatform[1].checked && !(document.campob_form.equipementform[0].checked || document.campob_form.equipementform[1].checked)) {
        alert("Flag \"Will you use your own Equipement?\"");
        return false;
    }
    if (trim(document.campob_form.sailnazform.value) == "") {
        alert("Select Nation");
        return false;
    }
    if (trim(document.campob_form.sailnumberform.value) != "") {
        if (!isNumeric(trim(document.campob_form.sailnumberform.value))) {
            alert("Enter only numeric values in field \"Sail Number\"");
            return false;
        }
    }
    if (trim(document.campob_form.atlnameform.value) == "") {
        alert("Fill out the field \"Name\"");
        return false;
    }
    if (trim(document.campob_form.atllastnameform.value) == "") {
        alert("Fill out the field \"Last Name\"");
        return false;
    }
    if (!(document.campob_form.atlsexform[0].checked || document.campob_form.atlsexform[1].checked)) {
        alert("Flag \"Sex\"");
        return false;
    }
    if (!validadata) {
        alert("Invalid Date of Birth");
        return false;
    }
    if (trim(document.campob_form.atladdressform.value) == "") {
        alert("Fill out the field \"Helmsman Home Address\"");
        return false;
    }
    if (trim(document.campob_form.atlcityform.value) == "") {
        alert("Fill out the field \"Helmsman City\"");
        return false;
    }
    if (trim(document.campob_form.atlclubform.value) == "") {
        alert("Fill out the field \"Yacht Club Membership\"");
        return false;
    }
    if (trim(document.campob_form.atlclubaddressform.value) == "") {
        alert("Fill out the field \"Yacht Club Address\"");
        return false;
    }
    return true;  
}

function controlla_campob_form_it() {
    validadata = controlladata();
    if (trim(document.campob_form.sailnumberform.value) != "") {
        if (!isNumeric(trim(document.campob_form.sailnumberform.value))) {
            alert("Immettere solo numeri nel campo \"Numero velico\"");
            return false;
        }
    }
    if (trim(document.campob_form.atlnameform.value) == "") {
        alert("Compilare il campo \"Nome\"");
        return false;
    }
    if (trim(document.campob_form.atllastnameform.value) == "") {
        alert("Compilare il campo \"Cognome\"");
        return false;
    }
    if (!(document.campob_form.atlsexform[0].checked || document.campob_form.atlsexform[1].checked)) {
        alert("Selezionare \"Sesso\"");
        return false;
    }
    if (!validadata) {
        alert("Data di Nascita non valida");
        return false;
    }
    if (trim(document.campob_form.atlfivform.value) != "") {
        if (!isNumeric(trim(document.campob_form.atlfivform.value))) {
            alert("Immettere solo numeri nel campo \"Tessera FIV\"");
            return false;
        }
    }
    if (trim(document.campob_form.atlcityform.value) == "") {
        alert("Compilare il campo \"Cittą Timoniere\"");
        return false;
    }
    if (trim(document.campob_form.atlclubform.value) == "") {
        alert("Compilare il campo \"Yacht Club di appartenenza\"");
        return false;
    }
    return true;  
}

function vai_alla_pagina(key) {
  var select_id = document.vaiapagina.paginaform;
  var selected_id = select_id.options[select_id.selectedIndex].value;
  if (key) {
      location.href = "corposociale.php?pag="+selected_id+"&chiave="+key;
  } else {
      location.href = "corposociale.php?pag="+selected_id;
  }
}

function apri_fin(link) {
larfin = Math.floor(screen.width - 70);
altfin = Math.floor(screen.height - 160);
var poslef = Math.floor(((screen.width - larfin) / 2)- 5);
var postop = 5;
window.open(link,'','width='+larfin+', height='+altfin+', left='+poslef+', top='+postop);
}

function controlla_mail_areasoci() {
    if (!validateEmail(document.richiestapass.insemail.value)) {
        alert("Indirizzo email non valido!");
        return false;    
    }
}

function controlla_mail_loginsoci() {
    if (!validateEmail(document.loginsoci.user_soci.value)) {
        alert("Indirizzo email non valido!");
        return false;    
    }
}




