﻿
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Mettre une adresse e-mail.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Mettre une adresse e-mail correcte.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "L’adresse e-mail contient des caractères non valables.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Info demandée non remplie: "+fld.id+"\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateCheck(fld) {
    var error = "";
 //alert(fld.checked);
    if (fld.checked == 0) {
        fld.style.background = 'Yellow'; 
        error = "Confirmer: "+fld.id+"\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}


function valButton(btn) {

if (form1.periodo.checked=true){
	//alert(form1.periodo.checked);
	return 1
}
    var cnt = -1;
	//alert(btn.length);
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
	
}

function validateFormOnSubmit(theForm) {
var reason = "";


  reason += validateEmpty(theForm.nome);
  reason += validateEmpty(theForm.cognome);
   reason += validateEmail(theForm.email);


    reason += validateCheck(theForm.Consenso);



      
  if (reason != "") 
  {
    alert("Certaines infos doivent être corrigées:\n" + reason);
    return false;
  }
  



  return true;
}


