//******************************************************
//*** La class pour valider le formulaire
//******************************************************
function Champ(id, msg, type, err) {
    this.id=id;
    this.msg=msg;
    this.type=type;
    this.err=err;
}

//***** La fonction verifier
Champ.prototype.verifier=function() {

    if(this.type != "radio" && this.type != "select" && this.type != "file") {
        var txt = document.getElementById(this.id).value;
        txt = txt.replace(/  /g, ' ');
        document.getElementById(this.id).value = txt;
    }
    switch (this.type) {

        case "num" :
            txt = txt.replace(/ /g, '');
            if( txt == "" ) return null;
            var reg = new RegExp("^[0-9]{1,}$", "g");
            return ( !reg.test(txt) ) ? this : null;

        case "text" :
            //alert(this.id);
            //alert("test1");
            txt = txt.replace(/ /g, '');
            if(txt == "") document.getElementById(this.id).value = txt;
            return (txt == "") ? this : null;

        case "email" :
            //alert("email");
            var reg = new RegExp("^[0-9A-Za-z]{1}[0-9A-Za-z._-]+@{1}[0-9A-Za-z]{1}[0-9A-Za-z.-]{1,}[.]{1}[A-Za-z]{2,5}$", "g");
            return ( !reg.test(txt) ) ? this : null;

        case "tel" :
            //alert("testtel");
            txt = txt.replace(/[ //.;,_-]/g, '');
            if (txt == "")
            {
                return this;
            }
            var reg = new RegExp("^0{1}[1-68-9]{1}[0-9]{8}$", "g");
            document.getElementById(this.id).value = txt;
            if( !reg.test(txt) ) {
                this.msg = "Le numéro de téléphone est invalide";
                return this;
            } else {
                return null;
            }

        case "phone" :
            txt = txt.replace(/[ //.;,_-]/g, '');
            var reg = new RegExp("^0{1}[1-58-9]{1}[0-9]{8}$", "g");
            document.getElementById(this.id).value = txt;
            return ( !reg.test(txt) ) ? this : null;

        case "portable" :
            txt = txt.replace(/[ //.;,_-]/g, '');
            var reg = new RegExp("^0{1}[6-7]{1}[0-9]{8}$", "g");
            document.getElementById(this.id).value = txt;
            return ( !reg.test(txt) ) ? this : null;

        case "cp" :
            // alert("testcp");
            txt = txt.replace(/ /g, '');
            var reg = new RegExp("^[0-9]{5}$", "g");
            document.getElementById(this.id).value = txt;
            return ( !reg.test(txt) ) ? this : null;

        case "radio" :
            var objs=document.getElementsByName(this.id);
            for(i=0;i<objs.length;i++) {
                if (objs[i].checked) return null;
            }
            return this;
        case "select" :
            var obj = document.getElementById(this.id);
            var text_value = obj.options[obj.selectedIndex].value;
            //alert(text_value);
            return ( text_value == "noselect" ) ? this : null;
        case "date" :
            var obj = document.getElementById(this.id).value;
            if( obj == "1900-01-01" ) return this;
            var ladate = (obj).split("-");
            var unedate = new Date(eval(ladate[0]),eval(ladate[1])-1,eval(ladate[2]));
            return ((unedate.getDate() == eval(ladate[2])) && (unedate.getMonth() == (eval(ladate[1])-1) ) && (unedate.getFullYear() == eval(ladate[0]))) ? null : this;
        case "file" :
            var txt = document.getElementById(this.id).value;
            if (txt == "")
            {
                return this;
            }
            dotPos = txt.lastIndexOf(".");
            if( dotPos==-1 )
                {
                    this.msg = "Le format de fichier est invalide (formats acceptés : doc, docx, pdf et txt)";
                    return this;
                }
            extension = txt.substr(dotPos,txt.length).toLowerCase();
            if(extension == ".doc" || extension == ".docx" || extension == ".pdf" || extension == ".txt")
            {
                return null;
            }
            else
            {
                this.msg = "Le format de fichier est invalide (formats acceptés : doc, docx, pdf et txt)";
                return this;
            }
        default:
            return (txt == "") ? this : null;
    }

}
//***** La fonction setFocus
Champ.prototype.setFocus=function() {
    document.getElementById(this.id).focus();
}
//***** La fonction ChampList
function ChampListe() {
    this.items = new Array();
}
//***** La fonction add
ChampListe.prototype.add=function(id,msg,type,err) {
    this.items.push(new Champ(id,msg,type,err));
}
//***** La fonction VerifierTout
ChampListe.prototype.verifierTout=function() {

    var result = null;
    var result_first = null;
    var erreur = false;

    for (var i=0; i < this.items.length; i++)
    {
        
        //alert(this.items[i].err);
        document.getElementById(this.items[i].err).innerHTML = "";
        result = this.items[i].verifier();
        
        $("#"+this.items[i].err).removeClass("error-border");

        //alert(result);
        if (result) {
            document.getElementById(this.items[i].err).innerHTML = this.items[i].msg;
            if(this.items[i].err.indexOf("") != -1 ) $("#"+this.items[i].err).addClass('error-border');
            if(!erreur) {
                erreur = true;
                result_first = result;
            }
        }
        
    }

    return result_first;
}



function verification_email(){
    var champs = new ChampListe();
    champs.add("email", "veuillez renseigner un email", "email", "validation");
    
    var champ = champs.verifierTout();

    if (champ) {
        //alert(champ);
        if(champ.type!='radio')
            champ.setFocus();
        return false;
    }
    else {
        return true;
    }

}


function verification_demande_rdv(){

    var champs = new ChampListe();
   
    //champs.add("date","Champs vide","text","alerte_date");
    champs.add("prenom","Champs vide","text","alerte_prenom");
    champs.add("nom","Champs vide","text","alerte_nom");
    champs.add("email", "Champs vide", "email", "alerte_email");
    champs.add("tel","Champs vide","tel","alerte_tel");
    champs.add("raisonS","Champs vide","text","alerte_raisonS");
    champs.add("ville","Champs vide","text","alerte_ville");
    champs.add("fonction","Champs vide","text","alerte_fonction");
    champs.add("comments","Champs vide","text","alerte_comments");

    var champ = champs.verifierTout();

    if (champ) {
       //alert('titit');
        if(champ.type!='radio')
            champ.setFocus();
        return false;
    }
    else {
        return true;
    }

}
function verification_demande_info(){

    var champs = new ChampListe();
    champs.add("prenom","Champs vide","text","alerte_prenom");
    champs.add("nom","Champs vide","text","alerte_nom");  
    champs.add("email", "Champs vide", "email", "alerte_email");
    champs.add("tel","Champs vide","tel","alerte_tel");
    champs.add("raisonS","Champs vide","text","alerte_raisonS");
    champs.add("ville","Champs vide","text","alerte_ville");
    champs.add("fonction","Champs vide","text","alerte_fonction");
    champs.add("comments","Champs vide","text","alerte_comments");

    var champ = champs.verifierTout();
    //alert(champ);
    if (champ) {
        //alert(champ);
        if(champ.type!='radio')
            champ.setFocus();
        return false;
    }
    else {
        return true;
    }

}

// Vérifie les champs du formulaire de WebCallBack
function checkFormWebCallBack(){

    var champs = new ChampListe();
    champs.add("nom","Champs vide","text","alerte_nom_");
    champs.add("raisonS","Champs vide","text","alerte_raisonS_");
    champs.add("fonction","Champs vide","text","alerte_fonction_");
    champs.add("ville","Champs vide","text","alerte_ville_");
    champs.add("tel","Champs vide","tel","alerte_tel_");

    var champ = champs.verifierTout();
    //alert(champ);
    if (champ) {
        //alert(champ);
        if(champ.type!='radio')
            champ.setFocus();
        return false;
    }
    else {
        return true;
    }

}


function verification_candidature(){
    var champs = new ChampListe();
    champs.add("prenom","Champs vide","text","alerte_prenom");
    champs.add("nom","Champs vide","text","alerte_nom");
    champs.add("email", "Champs vide", "email", "alerte_email");
    champs.add("tel","Champs vide","tel","alerte_tel");
    champs.add("lettre_motiv","Champs vide","file","alerte_lettre_motiv");
    champs.add("cv","Champs vide","file","alerte_cv");



    var champ = champs.verifierTout();
    //alert(champ);
    if (champ) {
        //alert(champ);
        if(champ.type!='radio')
            champ.setFocus();
        return false;
    }
    else {
        return true;
    }

}

// Vérifie les champs du formulaire d'envoi de page
function checkFormSendPage()
{
    var champs = new ChampListe();
   
    champs.add("expName","Champs vide","text","errorExpName");
    champs.add("expEmail", "Champs vide", "email", "errorExpEmail");
    champs.add("destName","Champs vide","text","errorDestName");
    champs.add("destEmail","Champs vide","email","errorDestEmail");

    var champ = champs.verifierTout();
    //alert(champ);
    if (champ)
    {
        //alert(champ);
        if(champ.type!='radio')
        {
            champ.setFocus();
        }
        return false;
    }
    return true;

}

