window.onload=function(){
if(!document.getElementById || !document.createElement) return;
document.forms[0].onsubmit=function(){
    if(Validate())
       document.forms[0].submit();
    else{
        alert("Spiacente, alcuni campi non sono stati inseriti correttamente");
        return(false);
        }
    }
}

function Validate(){
var labels=document.getElementsByTagName("label");
var validationOK=true;
for(var i=0;i<labels.length;i++){
    var lab=labels[i];
    if(lab.className=="req2") lab.className="req";
    if(lab.className=="req"){
        var inp=lab.getElementsByTagName("input");
        if(inp.length>0 && inp[0].value==""){  //input vuoto
            lab.className="req2";
            validationOK=false;
            }
        if(inp.length>0 && inp[0].id=="email"){ //email non valida
        if(!isEmail(inp[0].value)){
            lab.className="req2";
            validationOK=false;
            }
        }
        if(inp.length>0 && inp[0].type=="checkbox" && inp[0].checked==false){
            lab.className="req2";  //checkbox non selezionata
            validationOK=false;
            }            
        var sel=lab.getElementsByTagName("select"); //select con indice zero
        if(sel.length>0 && sel[0].selectedIndex==0){
            lab.className="req2";
            validationOK=false;
            }
        }
    }
return(validationOK);
}

function isEmail(str){
var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
return (!r1.test(str) && r2.test(str));
}
