/* =====================================================================================================
'  FONCTION  limiteTextarea - JSCRIPT
'   LIMITE LA TAILLE D'UN TEXTAREA
' ===================================================================================================== */

function limite(zone,max,affichage){
  if(zone.value.length>=max){zone.value=zone.value.substring(0,max);}
	var nb = max - zone.value.length
  affichage.value = nb;
  
}

/* =====================================================================================================
'   FONCTION  verifTextarea - JSCRIPT
'   LIMITE LA TAILLE D'UN TEXTAREA
' ===================================================================================================== */

function verifTextarea(tarea, info, tailleMax){
            var texte = tarea.value;
            var taille = tarea.value.length;
            if(taille > tailleMax){
               var texte_ok = texte.substring(0,tailleMax);
               tarea.value = texte_ok;
            } else {
                info.value = (tailleMax - taille);
            }
         }

/* =====================================================================================================
'   FONCTION  verifPseudo - JSCRIPT
'   VERIFIE LE FORMAT D'UN PSEUDO OU MOT DE PASSE
' ===================================================================================================== */

  function verifPseudo(donnee) {
  /* renvoit une valeur juste si la chaîne est composée uniquement de characters dont
  le code ascii est :45(-), 48-57(0-9), 65-90(A-Z), 97-122(a-z)    */

  chaine = new String(donnee);
  longueur  = chaine.length;

    for (var i=0; i< longueur; i++) {

      code = chaine.charCodeAt(i);

      if ( ( code == 45 ) || ( (code >= 48) && (code<= 57) ) || ( (code >= 65) && (code<= 90) ) || ( (code>= 97) && (code<= 122) ) )  {
      }
      else {
        return("Character '" + chaine.charAt(i) + "' is forbidden");
      }
    }

    return "";
  }

/* =====================================================================================================
'   FONCTION  verifSMS - JSCRIPT
'   VERIFIE LE FORMAT D'UN SMS
' ===================================================================================================== */

  function verifSMS(donnee) {
  /* renvoit une valeur juste si la chaîne est composée uniquement de characters dont
  le code ascii est : 32-126, 192(À), 194(Â), 199(Ç), 200-203(È,È,Ê,Ë), 206(Î), 207(Ï), 212(Ô), 214(Ö), 217(Ù), 219(Û), 220(Ü),  224(à), 226(â), 231-235(ç,è,é,ê,ë), 238(î), 239(ï), 244(ô), 246(ö), 249(ù), 251(û), 252(ü)   */

  chaine = new String(donnee);
  longueur  = chaine.length;

    for (var i=0; i< longueur; i++) {

      code = chaine.charCodeAt(i);

      if ( ( (code>= 32) && (code<= 126) ) ||  ( code == 192 ) || ( code == 194 ) || ( code == 199 ) || ( code == 206 ) || ( code == 207 ) || ( code == 212 ) || ( code == 214 ) || ( code == 217 ) || ( code == 219 ) || ( code == 220 ) || ( code == 224 ) || ( code == 226 ) || ( code == 238 ) || ( code == 239 ) || ( code == 244 ) || ( code == 246 ) || ( code == 249 ) || ( code == 251 ) || ( code == 252 )  || ( (code >= 65) && (code<= 90) ) || ( (code>= 97) && (code<= 122) ) || ( (code >= 200) && (code<= 203) ) || ( (code>= 231) && (code<= 235) ) )  {
      }
      else {
        return("Character '" + chaine.charAt(i) + "' is forbidden");
      }
    }

    return "";
  }

  
/* =====================================================================================================
'   FONCTION  verifTexte - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE TEXTE (SANS CHIFFRES)
' ===================================================================================================== */

  function verifTexte(donnee) {
  /* renvoit une valeur juste si la chaîne est composée uniquement de characters dont
  le code ascii est : 32( ), 39 ('), 180(´), 44 (,), 45(-), 46(.), 65-90(A-Z), 97-122(a-z), 192(À), 194(Â), 199(Ç), 200-203(È,È,Ê,Ë), 206(Î), 207(Ï), 212(Ô), 214(Ö), 217(Ù), 219(Û), 220(Ü),  224(à), 226(â), 231-235(ç,è,é,ê,ë), 238(î), 239(ï), 244(ô), 246(ö), 249(ù), 251(û), 252(ü)   */

  chaine = new String(donnee);
  longueur  = chaine.length;

    for (var i=0; i< longueur; i++) {

      code = chaine.charCodeAt(i);

      if ( ( code == 32 ) || ( code == 39 ) || ( code == 44 ) || ( code == 45 ) || ( code == 46 ) || ( code == 180 ) || ( code == 192 ) || ( code == 194 ) || ( code == 199 ) || ( code == 206 ) || ( code == 207 ) || ( code == 212 ) || ( code == 214 ) || ( code == 217 ) || ( code == 219 ) || ( code == 220 ) || ( code == 224 ) || ( code == 226 ) || ( code == 238 ) || ( code == 239 ) || ( code == 244 ) || ( code == 246 ) || ( code == 249 ) || ( code == 251 ) || ( code == 252 )  || ( (code >= 65) && (code<= 90) ) || ( (code>= 97) && (code<= 122) ) || ( (code >= 200) && (code<= 203) ) || ( (code>= 231) && (code<= 235) ) )  {
      }
      else {
        return("Character '" + chaine.charAt(i) + "' is forbidden");
      }
    }

    return "";
  }

/* =====================================================================================================
'   FONCTION  verifAlphaNum - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE ALPHA NUMERIQUE
' ===================================================================================================== */

  function verifAlphaNum(donnee) {
  /* renvoit une valeur juste si la chaîne est composée uniquement de characters dont
  le code ascii est : 32( ), 39 ('), 180(´), 44 (,), 45(-), 46(.), 48-57(0-9), 65-90(A-Z), 97-122(a-z), 192(À), 194(Â), 199(Ç), 200-203(È,È,Ê,Ë), 206(Î), 207(Ï), 212(Ô), 214(Ö), 217(Ù), 219(Û), 220(Ü),  224(à), 226(â), 231-235(ç,è,é,ê,ë), 238(î), 239(ï), 244(ô), 246(ö), 249(ù), 251(û), 252(ü)     */

  chaine = new String(donnee);
  longueur  = chaine.length;

    for (var i=0; i< longueur; i++) {

      code = chaine.charCodeAt(i);

      if ( ( code == 32 ) || ( code == 39 ) || ( code == 44 ) || ( code == 45 ) || ( code == 46 ) || ( code == 180 ) || ( code == 192 ) || ( code == 194 ) || ( code == 199 ) || ( code == 206 ) || ( code == 207 ) || ( code == 212 ) || ( code == 214 ) || ( code == 217 ) || ( code == 219 ) || ( code == 220 ) || ( code == 224 ) || ( code == 226 ) || ( code == 238 ) || ( code == 239 ) || ( code == 244 ) || ( code == 246 ) || ( code == 249 ) || ( code == 251 ) || ( code == 252 ) || ( (code >= 48) && (code<= 57) ) || ( (code >= 65) && (code<= 90) ) || ( (code>= 97) && (code<= 122) ) || ( (code >= 200) && (code<= 203) ) || ( (code>= 231) && (code<= 235) ) )  {
      }
      else {
        return("Character '" + chaine.charAt(i) + "' is forbidden");
      }
    }

    return "";
  }


/* =====================================================================================================
'   FONCTION  verifAlphaNumSouple - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE ALPHA NUMERIQUE assouplie
'   y compris doubles guillemets, points d'exclamation, interrogation,:, %, &, (, ), *, +, /
' ===================================================================================================== */

  function verifAlphaNumSouple(donnee) {
  /* renvoit une valeur juste si la chaîne est composée uniquement de characters dont
  le code ascii est : 10(saut de ligne), 13,  32( ), 33(!),  39 ('), 40((), 41()), 42(*), 43(+), 44 (,), 45(-), 46(.), 48-58(0-9 et :), 63(?), 65-90(A-Z), 180(´), 97-122(a-z), 192(À), 194(Â), 199(Ç), 200-203(È,È,Ê,Ë), 206(Î), 207(Ï), 212(Ô), 214(Ö), 217(Ù), 219(Û), 220(Ü),  224(à), 226(â), 231-235(ç,è,é,ê,ë), 238(î), 239(ï), 244(ô), 246(ö), 249(ù), 251(û), 252(ü)   */

  chaine = new String(donnee);
  longueur  = chaine.length;

    for (var i=0; i< longueur; i++) {

      code = chaine.charCodeAt(i);

      if ( ( code == 10 ) || ( code == 13 ) || ( code == 32 ) || ( code == 33 ) || ( code == 63 ) || ( code == 180 ) || ( code == 192 ) || ( code == 194 ) || ( code == 199 ) || ( code == 206 ) || ( code == 207 ) || ( code == 212 ) || ( code == 214 ) || ( code == 217 ) || ( code == 219 ) || ( code == 220 ) || ( code == 224 ) || ( code == 226 ) || ( code == 238 ) || ( code == 239 ) || ( code == 244 ) || ( code == 246 ) || ( code == 249 )|| ( code == 251 ) || ( code == 252 ) || ( (code >= 65) && (code<= 90) )|| ( (code >= 39) && (code<= 46) ) || ( (code >= 48) && (code<= 58) ) || ( (code >= 65) && (code<= 90) ) || ( (code>= 97) && (code<= 122) ) || ( (code >= 200) && (code<= 203) ) || ( (code>= 231) && (code<= 235) ) )  {
      }
      else {

        return("Character '" + chaine.charAt(i) + "' is forbidden");
      }
    }

    return "";
  }

  
/* =====================================================================================================
'   FONCTION  verifEntier - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE NUMERIQUE ENTIER
' ===================================================================================================== */

  function verifEntier(donnee) {
  /* renvoit une valeur juste si la chaîne est composée uniquement de characters dont
  le code ascii est : 48-57(0-9)  */

  chaine = new String(donnee);
  longueur  = chaine.length;

    for (var i=0; i< longueur; i++) {

      code = chaine.charCodeAt(i);

      if ( ( (code >= 48) && (code<= 57) ) )  {
      }
      else {
        return("Character '" + chaine.charAt(i) + "' is forbidden");
      }
    }

  return "";

  }


/* =====================================================================================================
'   FONCTION  verifReel - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE NUMERIQUE REELLE
' ===================================================================================================== */

  function verifReel(donnee) {
  /* renvoit une valeur juste si la chaîne est composée uniquement de characters dont
  le code ascii est : 48-57(0-9), 44(,), 46(.)
  et ne comportant que des chiffres dans la partie décimale */

  chaine        = new String(donnee);
  longueur      = chaine.length;
  partieEntiere = true;

    for (var i=0; i< longueur; i++) {

      code = chaine.charCodeAt(i);

      if ( ( (code >= 48) && (code<= 57) ) || ((code == 44) && partieEntiere) || ((code == 46) && partieEntiere) )  {

        if ((code == 44) || (code == 46)) {
          partieEntiere = false;
        }

      }
      else {
        return("Character '" + chaine.charAt(i) + "' is forbidden");
      }
    }
  return "";

  }

/* =====================================================================================================
'   FONCTION  verifURL - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE URL
' ===================================================================================================== */

  function verifURL(donnee) {
  /* renvoit une valeur juste si la chaîne est une url valide  */

  	chaine 	= new String(donnee);
 //	expreg	= new RegExp("^[a-zA-Z0-9\-\._]+(\.[a-zA-Z0-9\-\._]+){2,}(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$");

 	expreg	= new RegExp("^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");

		if (!expreg.test(chaine)) {
  		return("Url is not valid");
  		}   
  	return "";
  }

/* =====================================================================================================
'   FONCTION  verifEmail - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE EMAIL
' ===================================================================================================== */

  function verifEmail(donnee) {
  /* renvoit une valeur juste si la chaîne est une adresse email valide  */

  chaine 	= new String(donnee);
  expreg	= new RegExp("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+.[a-zA-Z0-9]{2,4}$");

  if (!expreg.test(chaine)) {
   return("Email address is not valid");
  }

  	return "";

  }


/* =====================================================================================================
'   FONCTION  verifDate - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE DATE
' ===================================================================================================== */

  function verifDate(donnee) {
  /*  renvoit une valeur juste si la chaine
      est formatée en date  */

  	if (donnee.length < 10){return("Format non valide") }

 		var iDay 		= donnee.substring(0,2)
 		var iMonth 	= donnee.substring(3,5)
  	var iYear 	= donnee.substring(6)

 		iDay 		= iDay*1
 		iMonth 	= iMonth*1
 		iYear 	= iYear*1
 		var rDate = new Date(iYear,iMonth,iDay)

 		if(rDate.toString() == "NaN"){return("Not valid") }

		/* 	Définition du dernier jour de février
	  		Année bissextile si annnée divisible par 4 et que ce n'est pas un siècle, ou bien si divisible par 400 */

	  if (iYear%4 == 0 && iYear%100 !=0 || iYear%400 == 0){
	  	fev = 29;
	  } else {
	  	fev = 28;
	  }

	  /* Nombre de jours pour chaque mois */
	  nbJours = new Array(31,fev,31,30,31,30,31,31,30,31,30,31);

	  /* Enfin, retourne vrai si le jour est bien entre 1 et le bon nombre de jours, idem pour les mois, sinon retourn faux */
	 	if ((iMonth < 1) || (iMonth >12) || (iDay < 1) || (iDay > nbJours[iMonth-1]) ) {
	 		return("ot valid");
	 	}

 		return "";

}


/* =====================================================================================================
'   FONCTION  verifDateN - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE DATE DE NAISSANCE DE MAJEUR
' ===================================================================================================== */

  function verifDateN(donnee) {
  /*  renvoit une valeur juste si la chaine
      est formatée en date et majorite */

    var majorite = new Number(18)

  	if (donnee.length < 10){return("Format non valide") }

 		var iDay 		= donnee.substring(0,2)
 		var iMonth 	= donnee.substring(3,5)
  	var iYear 	= donnee.substring(6)

 		iDay 		= iDay*1
 		iMonth 	= iMonth*1
 		iYear 	= iYear*1
 		var rDate = new Date(iYear,iMonth,iDay)

 		if(rDate.toString() == "NaN"){return("Not valid") }

		/* 	Définition du dernier jour de février
	  		Année bissextile si annnée divisible par 4 et que ce n'est pas un siècle, ou bien si divisible par 400 */

	  if (iYear%4 == 0 && iYear%100 !=0 || iYear%400 == 0){
	  	fev = 29;
	  } else {
	  	fev = 28;
	  }

	  /* Nombre de jours pour chaque mois */
	  nbJours = new Array(31,fev,31,30,31,30,31,31,30,31,30,31);

	  /* Enfin, retourne vrai si le jour est bien entre 1 et le bon nombre de jours, idem pour les mois, sinon retourn faux */
	 	if ((iMonth < 1) || (iMonth >12) || (iDay < 1) || (iDay > nbJours[iMonth-1]) ) {
	 		return("Format non valide");
	 	}

		/* vérifie la majorité */
 		var dateCourante 	= new Date()
		var anneeCourante = dateCourante.getFullYear() *1
		var moisCourant 	= (dateCourante.getMonth() *1) +1
		var joursCourant 	= dateCourante.getDate() * 1

		if ( ((anneeCourante - iYear) < majorite ) || (((anneeCourante - iYear) == majorite) && (moisCourant < iMonth)) || (((anneeCourante - iYear) == majorite) && (moisCourant = iMonth) && (joursCourant < iDay)) ) {
			return("Not valid (you must be above 18 years old)");
		}

 		return "";

}


/* =====================================================================================================
'   FONCTION  VERIFFORMAT - JSCRIPT
'   VERIFIE LE FORMAT D'UNE DONNEE
' ===================================================================================================== */

function verifFormat( nomChamp, champ, nbMin, nbMax, typeDonnee, champAffichage) {
 

  var regExpBeginning = /^\s+/;
  var regExpEnd       = /\s+$/;


  ch    = new String(champ.value);
  ch 	= ch.replace(regExpBeginning, "").replace(regExpEnd, "");
  
  lng   = ch.length;

  if ((champ.value == null ) && (nbMin == 0)) {
    return true;
  }
  
  if ((champ.value == "" ) && (nbMin == 0)) {
    return true;
  }
  
    switch (typeDonnee) {

      case "pseudo":
      ret = verifPseudo(ch);
      break;

      case "texte":
      ret = verifTexte(ch);
      break;

      case "alphaNum":
      ret = verifAlphaNum(ch);
      break;

      case "alphaNumSouple":
      ret = verifAlphaNumSouple(ch);
      break;

      case "entier":
      ret = verifEntier(ch);
      break;

      case "reel":
      ret = verifReel(ch);
      break;

      case "email":
      ret = verifEmail(ch);
      break;

      case "sms":
      ret = verifSMS(ch);
      break;

      case "url":
      ret = verifURL(ch);
      break;

      case "date":
      ret = verifDate(ch);
      break;

      case "dateN":
      ret = verifDateN(ch);
      break;

      case "libre":
      ret = "";
      break;
      
    }

  if (ret != "") {

  	champAffichage.innerHTML     = ret + " in the field " + nomChamp;
    try {
    champ.focus();
    } catch(e) {
    }
    return false;
  }

  if (lng < nbMin) {

    champAffichage.innerHTML      =  nomChamp + " length must be " + nbMin + " characters at least.";
    try {
    champ.focus();
    } catch(e) {
    }
    return false;
  }

  if (lng > nbMax) {

    champAffichage.innerHTML      =  nomChamp + " length must be less than " + nbMax + " characters.";
    try {
    champ.focus();
    } catch(e) {
    }
    return false;
  }

 	champAffichage.innerHTML  = " ";

  return true;
}


