function erreurCheck(champ, message, activefocus) {
    if (message) {                              // Si on ne donne pas de message, on reste silencieux...
        alert(message);
		if (activefocus==1) champ.focus();     // Les boutons radios sont multiples : on ne peut pas leur apporter le focus globalenent
    }
    return false;
}

// checkTexte : verifie si le champ donne est rempli ou pas
function checkTexte(champ, message, activefocus) {
  
    if (champ.value.length != 0 && champ.value != "<P>&nbsp;</P>"){
    	return true;
    }
    else {
      	return erreurCheck(champ, message, activefocus);
    }
}

// checkRichtext : verifie si le champ donne est rempli ou pas
function checkRichtext(champ_contenu, message, activefocus)
{
 if ( window.frames[champ_contenu].objContent.DOM.body.innerText.length == 0 ) 
 {
  alert(message) ;
  return false ;
 }
 return true ;
}

function checkdate(champ, message1, message2 ,obligatoire, activefocus)
{
 var d = champ.value;
 var erreur = 1;
 
 if(d=="" && obligatoire==1)
 {
  return erreurCheck(champ, message1, activefocus);
  erreur = 0;
 }
 
 if(d!="" && erreur==1)
 {
  // Cette fonction vérifie le format JJ/MM/AAAA saisi et la validité de la date.
  // Le séparateur est défini dans la variable separateur
  var amin=2000; // année mini
  var amax=2060; // année maxi
  var separateur="/"; // separateur entre jour/mois/annee
  var j=(d.substring(0,2));
  var m=(d.substring(3,5));
  var a=(d.substring(6));
  var ok=1;
  if ( ((isNaN(j))||(j<1)||(j>31)) && (ok==1) )
  {
   //alert("Le jour n'est pas correct.");
   ok=0;
  }
  if ( ((isNaN(m))||(m<1)||(m>12)) && (ok==1) )
  {
   //alert("Le mois n'est pas correct.");
   ok=0;
  }
  if ( ((isNaN(a))||(a<amin)||(a>amax)) && (ok==1) )
  {
   //alert("L'année n'est pas correcte.");
   ok=0;
  }
  if ( ((d.substring(2,3)!=separateur)||(d.substring(5,6)!=separateur)) && (ok==1) )
  {
   //alert("Les séparateurs doivent être des "+separateur);
   ok=0;
  }
  if (ok==1)
  {
   var d2=new Date(a,m-1,j);
   j2=d2.getDate();
   m2=d2.getMonth()+1;
   a2=d2.getYear();
   if (a2<=100) {a2=1900+a2}
   if ( (j!=j2)||(m!=m2)||(a!=a2) )
   {
    //alert("La date "+d+" n'existe pas !");
    ok=0;
   }
  }
 }
 else
 {
  var ok = 1;
 }
   
    if (ok==1)
	{
     return true;
    } else {
     return erreurCheck(champ, message2, activefocus);
    }
}

// checkEmail : verifie si le champ donne est une adresse e-mail
function checkEmail(champ, message1, message2, obligatoire, activefocus) {
    var checkStr = champ.value.toLowerCase();
    var checkOK = "0123456789abcdefghijklmnopqrstuvwxyz-_/.@";
    var pos, car, valide = true, posAt = -1, posDot = -1;
	var erreur = 0;
    
	if(obligatoire==1 && checkStr.length==0)
	{
	 return erreurCheck(champ, message1, activefocus);
	 erreur = 1;
	}

    if(erreur==0 && checkStr.length!=0)
	{
	 for (pos = 0; pos < checkStr.length; pos++)
	 {
        if (checkStr.charAt(pos) == '@') {
            posAt = pos;
            continue;
        }
        if (checkStr.charAt(pos) == '.') {
            posDot = pos;
            continue;
        }
        if (checkOK.indexOf(checkStr.charAt(pos)) == -1) {
            valide = false;
            break;
        }
     }
	}
	else
	{
	 checkStr = "chainedecaractere";
	 posDot = 5;
	 posAt = 1;
	}

    if (valide && (posAt > -1) && (posDot > posAt + 1) && (posDot < checkStr.length - 1)) {
        return true;
    } else {
        return erreurCheck(champ, message2, activefocus);
    }

}

// checkRadio : verifie si le groupe de cases a bien au moins une coche
function checkRadio(champ, message, activefocus) {
    var pos, valide = false;
    if (champ.length) {
        for (pos = 0; pos < champ.length; pos++) {
            if (champ[pos].checked) {
                valide = true;
                break;
            }
        }
    } else {
        if (champ.checked) {
            valide = true;
        }
    }

    if (valide) {
        return true;
    } else {
        return erreurCheck(champ, message, activefocus);
    }
}

// checkListe : verifie si la liste deroulante a bien une selection non vide
function checkListe(champ, message, activefocus) {
    if ((champ.multiple) && (champ.selectedIndex == -1)) {
        return erreurCheck(champ, message);
    }
    else if (champ.options[champ.selectedIndex].value > "") {
        return true;
    } else {
        return erreurCheck(champ, message, activefocus);
    }
}

// checkListeAjout : verifie si la liste deroulante a bien une selection non vide
function checkListeAjout(champ1,champ2, message, activefocus) {
    if ((champ1.multiple) && (champ1.selectedIndex == -1) && champ2.value.length == 0) {
        return erreurCheck(champ1, message, activefocus);
    }
    else if (champ1.options[champ1.selectedIndex].value > "" || champ2.value.length != 0) {
        return true;
    } else {
        return erreurCheck(champ1, message, activefocus);
    }
}

function checkNum(champ, message1, message2 , obligatoire, activefocus, mini, maxi) {
	var checkStr = champ.value;
	var erreur = 0;
    var pos;

 if(checkStr.length==0 && obligatoire==1)
 {
  erreur = 1;
  return erreurCheck(champ, message1, activefocus);
 }   

 if(checkStr.lenght!=0 && erreur==0)
 {
  // Remplace les ',' par des '.', de maniere a utiliser parseFloat
  while ((pos = checkStr.indexOf(',')) > -1)
  {
   checkStr = checkStr.substring(0, pos) + '.' + checkStr.substring(pos + 1);
  }
  var checkFloat = parseFloat(checkStr); 
  
  //le champ n'est pas au format numérique
  if(isNaN(checkStr))
  {
   return erreurCheck(champ, message2, activefocus);
  }
  else
  {
   if(mini!="" && checkFloat < mini)
   {
    return erreurCheck(champ, message2, activefocus);
   }
   else
   {
    if(maxi!="" && checkFloat > maxi)
    {
     return erreurCheck(champ, message2, activefocus);
    }
    else
    {
     return true;
    }
   }
  }
 }
 else
 {
  return true;
 }
}

function checkFichier(champ,champold,champdel,message1,message2,obligatoire,activefocus,extensionautorise)
{
 var fichierString = new String(champ.value);
 fichierString.toLowerCase();
 //alert(champold+" : "+champold.value+" -> "+champdel+" : "+champdel.checked);
 if(champ.value.length == 0 && obligatoire == 1 && (champold.value.length==0 || champdel.checked==true))
 {
  return erreurCheck(champ, message1, activefocus);
 }
 else
 {
  if(extensionautorise!="")
  {
   var extension = fichierString.substring(fichierString.lastIndexOf('.'),fichierString.length);
   var reg = new RegExp(extension,"gi");
   if(extensionautorise.match(reg))
   {
    return true;
   }
   else
   {
    return erreurCheck(champ, message2, activefocus);
   }
  }
  else
  {
   return true;
  }
 }
}


function checkUrl(champ, message, obligatoire, activefocus)
{
 if ((champ.value.length==0 || champ.value=="http://" || champ.value=="https://" || champ.value=="ftp://") && obligatoire==1)
 {
  return erreurCheck(champ, message, activefocus);
 }
 else
 {
  if(champ.value.length!=0)
  {
   var strUrl = new String(champ.value);
   if (strUrl.substr(0, 7)!='http://' && strUrl.substr(0, 6)!='ftp://' && strUrl.substr(0, 8)!='https://')
   {
    var reponse = window.confirm('Le lien saisi ne commence ni par \'http://\' ni par \'https://\' ni par \'ftp://\'. Etes-vous sur de sa syntaxe ?');
    return reponse;
   }
   else
   {
    return true;
   }
  }
  else
  {
   return true;
  }
 }
}
