﻿    function docGetElmt(a)
    {
        return document.getElementById(a);
    }
    //-----------------Validation des champs [Obligatoire]--------------------------
    //------------------------------------------------------------------------------
    //------------------------------------------------------------------------------
    //------------------------------------------------------------------------------
    function Validation_Obligation_Champs_Avis(Nom,lblnom,Prenom,lblprenom,Mail,lblmail,Com,lblCom,CIN,lblcin,Tel,lbltel,label1,cbCat,lblcbCat)
    {
        var bok = true;
        //-----------------CATEGORIE--------------
        if (cbCat.selectedIndex == 0 && lblcbCat.innerHTML == "") {
            lblcbCat.innerHTML = "Merci de préciser la nature de votre demande";
            lblcbCat.style.color = "Red";
            bok = false;
        }
        else if (cbCat.selectedIndex == 0 && lblcbCat.innerHTML != "") {
            lblcbCat.style.color = "Red";
            bok = false;
        }
        else if (cbCat.selectedIndex != 0 && lblcbCat.innerHTML != "") {
            lblcbCat.style.color = "Red";
            bok = false;
        }
        else if (cbCat.selectedIndex != 0 && lblcbCat.innerHTML == "") {
            lblcbCat.innerHTML = "";
            lblcbCat.style.color = "White";
        }       
        //-----------------NOM--------------
        if(Nom.value=="" && lblnom.innerHTML=="")
        {
            lblnom.innerHTML ="Veuillez saisir votre Nom";
            lblnom.style.color = "Red";
            bok = false;
        }
        else if(Nom.value=="" && lblnom.innerHTML!="")
        {
            lblnom.style.color = "Red";
            bok = false;
        }
        else if(Nom.value!="" && lblnom.innerHTML!="")
        {
            lblnom.style.color = "Red";
            bok = false;
        }
        else if(Nom.value!="" && lblnom.innerHTML=="")
        {
            lblnom.innerHTML ="";
            lblnom.style.color = "White";
        }
        //--------------PRENOM--------------
        if(Prenom.value=="" && lblprenom.innerHTML=="")
        {
            lblprenom.innerHTML ="Veuillez saisir votre Prénom";
            lblprenom.style.color = "Red";
            bok = false;
        }
        else if(Prenom.value=="" && lblprenom.innerHTML!="")
        {
            lblprenom.style.color = "Red";
            bok = false;
        }
        else if(Prenom.value!="" && lblprenom.innerHTML!="")
        {
            lblprenom.style.color = "Red";
            bok = false;
        }
        else if(Prenom.value!="" && lblprenom.innerHTML=="")
        {
            lblprenom.innerHTML ="";
            lblprenom.style.color = "White";
        }
        //--------------EMAIL---------------
        if(Mail.value=="" && lblmail.innerHTML=="")
        {
            lblmail.innerHTML ="Veuillez saisir votre Adresse Mail";
            lblmail.style.color = "Red";
            bok = false;
        }
        else if(Mail.value=="" && lblmail.innerHTML!="")
        {
            lblmail.style.color = "Red";
            bok = false;
        }
        else if(Mail.value!="" && lblmail.innerHTML!="")
        {
            lblmail.style.color = "Red";
            bok = false;
        }
        else if(Mail.value!="" && lblmail.innerHTML=="")
        {
            lblmail.innerHTML ="";
            lblmail.style.color = "White";
        }
        //--------------Commentaire-----------------
        if(Com.value=="" && lblCom.innerHTML=="")
        {
            lblCom.innerHTML ="Veuillez saisir votre Commentaire";
            lblCom.style.color = "Red";
            bok = false;
        }
        else if(Com.value=="" && lblCom.innerHTML!="")
        {
            lblCom.style.color = "Red";
            bok = false;
        }
        else if(Com.value!="" && lblCom.innerHTML!="")
        {
            lblCom.style.color = "Red";
            bok = false;
        }
        else if(Com.value!="" && lblCom.innerHTML=="")
        {
            lblCom.innerHTML ="";
            lblCom.style.color = "White";
        }
        //--------------CIN-----------------
//        if(CIN.value=="" && lblcin.innerHTML=="")
//        {
//            lblcin.innerHTML ="Veuillez saisir votre CIN";
//            lblcin.style.color = "Red";
//            bok = false;
//        }
//        else if(CIN.value=="" && lblcin.innerHTML!="")
//        {
//            lblcin.style.color = "Red";
//            bok = false;
//        }
//        else if(CIN.value!="" && lblcin.innerHTML!="")
//        {
//            lblcin.style.color = "Red";
//            bok = false;
//        }
//        else if(CIN.value!="" && lblcin.innerHTML=="")
//        {
//            lblcin.innerHTML ="";
//            lblcin.style.color = "White";
//        }
        //--------------Tél.-----
        if(Tel.value=="" && lbltel.innerHTML=="")
        {
            lbltel.innerHTML ="Veuillez saisir votre Numéro de Téléphone";
            lbltel.style.color = "Red";
            bok = false;
        }
        else if(Tel.value=="" && lbltel.innerHTML!="")
        {
            lbltel.style.color = "Red";
            bok = false;
        }
        else if(Tel.value!="" && lbltel.innerHTML!="")
        {
            lbltel.style.color = "Red";
            bok = false;
        }
        else if(Tel.value!="" && lbltel.innerHTML=="")
        {
            lbltel.innerHTML ="";
            lbltel.style.color = "White";
        }
        
        return bok;
    }    
    //------------------------------------------------------------------------------
    //------------------------------------------------------------------------------
    //------------------------------------------------------------------------------
    //-----------------Validation Du Nom et Prénom----------------------------------
    function ValidateNom_AVIS(Nom,lblnom)
    {
     var bok = true;
     var alphaPat = /^[A-Za-z ]*$/;
     var nomid = Nom.value;
     if(nomid!="")
     {
         var matchArray = nomid.match(alphaPat);
         if (matchArray == null && alphaPat != "" || nomid.length < 3)
         {
               lblnom.innerHTML = "Nom invalide ";
               bok = false;
         }
	    else 
	     {
		       lblnom.innerHTML ="";
	     }
     }
     else
     {
            lblnom.innerHTML ="";
            bok=false;
     }
	 return bok;
     }
 
    function ValidatePrenom_AVIS(Prenom,lblprenom)
    {
     var bok = true;
     var alphaPat = /^[A-Za-z ]*$/;
     var prenomid = Prenom.value;
     if(prenomid!="")
     {
         var matchArray = prenomid.match(alphaPat);
         if (matchArray == null && alphaPat != "" || prenomid.length < 3)
         {
               lblprenom.innerHTML = "Prénom invalide ";
               bok = false;
         }
	    else 
	    {
		    lblprenom.innerHTML ="";
	    }
	  }
	  else
	  {
	        lblprenom.innerHTML ="";
	        bok=false;
	  }
	 return bok;
    }
    //------------------------------------------------------------------------------
    //------------------------Validation de la CIN----------------------------------
    //------------------------------------------------------------------------------
    
    function ValidateCIN_AVIS(CIN,lblcin)
    {
         var bok = true;
         var cinPat = /^[a-zA-Z]{1,2}[0-9]{4,6}$/;
         var cinid = CIN.value;
         if(cinid!="")
         {
             var matchArray = cinid.match(cinPat);
             if (matchArray == null && cinid != "")
             {
                   lblcin.innerHTML = "CIN invalide ";
                   bok = false;
             }
             else 
             {
	             lblcin.innerHTML ="";
             }
         }
         else
         {
            lblcin.innerHTML ="";
            bok = true;
         }
         return bok;
    }
    
    
    //------------------------------------------------------------------------------
    //----------------------------Validation de l'Adresse Mail----------------------
    //------------------------------------------------------------------------------
    
    function ValidateEmail_AVIS(Mail,lblmail)
    {
         var bok = true;
         var emailPat = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
         var emailid=Mail.value;
         if(emailid!="")
         {
             var matchArray = emailid.match(emailPat);
             if (matchArray == null && emailid != "")
             {
                   lblmail.innerHTML = "Adresse Mail invalide ";
                   bok = false;
             }
	         else 
	         {
	             lblmail.innerHTML ="";
	         }
	     }
	     else
	     {
	        lblmail.innerHTML ="";
	        bok = false;
	     }
    	    
	     return bok;
     }
    
    //------------------------------------------------------------------------------
    //------------------------Validation du Téléphone ------------------------------
    //------------------------------------------------------------------------------
    
    function ValidatePhone_AVIS(tel,lbltel) 
    {
	    var bok = true;
	    if(tel.value!="")
	    {
	    var Tel = new String(tel.value);
	       
        numChars = "0123456789";
        var isNum = true;
        var index = 0;
        var Data = tel.value;
        while ((index < Data.length) && (isNum))
        {
            isNum = (numChars.indexOf(Data.charAt(index)) != -1);
            index ++;
        }
             
        if (!isNum)
	    {
	        lbltel.innerHTML ="Erreur ! Saisissez un nombre entier";
            lbltel.style.color = "Red";
            bok = false;
        }
        else
        {
            if (Tel.length != 0 && Tel.length!= 10) 
            {
                lbltel.innerHTML ="Num. Téléphone Invalide ";
                lbltel.style.color = "Red";
                bok = false;
            } 
            else 
            {
                lbltel.innerHTML ="";
            }
	   }
	   }
	   else
	   {
	        lbltel.innerHTML ="";
	        bok = false;
	   }
	   return bok;
    }
    //------------------------------------------------------------------------------
    //------------------------Validation du Commentaire-----------------------------
    //------------------------------------------------------------------------------
    
    function ValidateCommentaire_AVIS(com,lblcom,lblcom2)
    {
        var bok = true;
        //alert(com.innerHTML.charAt(10).toString());
        if(com.innerHTML.length > 250)
        {
            /*lblcom2.style.Color = "Red";
            lblcom.innerHTML = "Longueur maximum atteinte !!!";*/
            bok = true;
        }
        else
        {
            lblcom2.style.Color = "#00A1AE";
            lblcom.innerHTML = "";
            bok = true;
        }
        return bok;
    }
    
    //************************************************************************************************
    //************************************************************************************************
    //************************************** VALIDATION **********************************************
    //**************************************** FINALE ************************************************
    //************************************************************************************************
    function Validation_AVIS(c)
    {
    //************************************************************************************************
    //************************************************************************************************
    //********************************************   ETAPE 1   ***************************************
    //************************************************************************************************
    //************************************************************************************************
    var bok = true;
    try{
    // -------------------- COMMENTAIRE ------------------------- //
    /*
    t[0]= Nom                t[1]= Message Nom
    t[2]= Prenom              t[3]= Message Prenom
    t[4]= CIN                 t[5]= Message CIN
    t[6]= Commentaire         t[7]= Message Commentaire
    t[8]= Email               t[9]= Message Email
    t[10]= Tel                t[11]= Message Tel
    t[12]= Label facultatif Commentaire
    */
    // --------------------- FIN -----------------------//
    var tab = new Array(100);
    var t = c.split(",");
    tab[0] = ValidateNom_AVIS(docGetElmt(t[0]),docGetElmt(t[1]));
    tab[1] = ValidatePrenom_AVIS(docGetElmt(t[2]),docGetElmt(t[3]));
    tab[2] = ValidateEmail_AVIS(docGetElmt(t[4]),docGetElmt(t[5]));
    tab[3] = ValidateCommentaire_AVIS(docGetElmt(t[6]),docGetElmt(t[7]),docGetElmt(t[12]));//Validation Commentaire
    tab[4] = ValidateCIN_AVIS(docGetElmt(t[8]),docGetElmt(t[9]));
    tab[5] = ValidatePhone_AVIS(docGetElmt(t[10]),docGetElmt(t[11]));//Validation Tel Résidentiel
    tab[6] = Validation_Obligation_Champs_Avis(docGetElmt(t[0]),docGetElmt(t[1]),docGetElmt(t[2]),
                                          docGetElmt(t[3]),docGetElmt(t[4]),docGetElmt(t[5]),
                                          docGetElmt(t[6]),docGetElmt(t[7]),docGetElmt(t[8]),
                                          docGetElmt(t[9]), docGetElmt(t[10]), docGetElmt(t[11]), docGetElmt(t[12]),
                                          docGetElmt(t[13]), docGetElmt(t[14]), docGetElmt(t[15]));                                         //Validation des champs pour AVIS
    for(var i=0;i<=tab.length-1;i++)
    {
        if(tab[i] == false)
        {
            bok =  false;
            break;
        }
    }
    
    }catch(e)
    {
        alert("Validation : " + e);
    }
    return bok;
    }
    //************************************************************************************************
    //******************************************FIN***************************************************
    //************************************** VALIDATION **********************************************
    //**************************************** FINALE ************************************************
    //************************************************************************************************
    