//-------------------------------------------------------------------check_1
function check_1 (obj_champ,champ,param){				//check longueur et/ou non-vide
	var err_str="";
	if (param<2){			//il faut entrer au moins un caractère (check non-vide)
		if (obj_champ.value=="") {
			err_str=champ+": "+ErrMsg[1]+"\n";
		}
	}else{				//min length > 1
		if (obj_champ.value.length<param){
			err_str=champ+": "+ErrMsg[6]+param+ErrMsg[5]+"\n";
		}
	}
	return (err_str);
}
//-------------------------------------------------------------------check_2
function check_2 (obj_champ,champ,param){			//check champ numérique
	//alert("check_2");
	var err_str="";
	if (param>0){			//check longueur
		if ((obj_champ.value.length<param)||(isNaN(Number(obj_champ.value)))){  //soit pas un nombre, soit pas assez long
			err_str=champ+": "+ErrMsg[4]+param+ErrMsg[5]+"\n";
		}	
	}else{		//pas de check sur longueur
		if (isNaN(Number(obj_champ.value))){			//si ce n'est pas un nombre
			err_str=champ+": "+ErrMsg[2]+"\n";	
		}
	}
	return (err_str);
}

//-------------------------------------------------------------------emailCheck
function emailCheck (emailStr,err_msg) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
  if (err_msg==true){
		alert("Email address seems incorrect (check @ and .'s)")
	}
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    if (err_msg==true){
    	alert("The username doesn't seem to be valid.")
    }
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	    	if (err_msg==true){
	        alert("Destination IP address is invalid!")
	      }
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	if (err_msg==true){
		alert("The domain name doesn't seem to be valid.")
	}
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
  if (err_msg==true){
	   alert("The address must end in a three-letter domain, or two letter country.")
	}
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   if (err_msg==true){
   	alert(errStr)
   }
   return false
}

// If we've gotten this far, everything's valid!
return true;
}
//-------------------------------------------------------------------check_3
function check_3 (obj_champ,champ,param){			//check champ email
	var err_str="";
	if (emailCheck (obj_champ.value,false)==false){		//appeler fonction
		err_str=champ+": "+ErrMsg[3]+"\n";		
	}
	return (err_str);
}
//-------------------------------------------------------------------check_4
function check_4 (obj_champ,champ,param){			//check au moins un radiobutton sélectionné
	var err_str="";
	var coche=false;
	for (j=0;j<obj_champ.length;j++){			//pour chaque bouton radio
		if (obj_champ[j].checked==true){		//regarder si checked
			coche=true;
		}
	}
	if (coche==false){
		err_str=champ+": "+ErrMsg[7]+"\n";
	}
	return (err_str);
}
//-------------------------------------------------------------------check_5
function check_5 (obj_champ,champ,param){			//combobox - au moins un sélectionné
	var err_str="";
	var valable=false;
	for (j=0;j<obj_champ.length;j++){			//pour chaque ligne du combo
		if ((obj_champ.options[j].selected)&&(obj_champ.options[j].value!=param)){		//si selected et pas une valeur interdite
			valable=true;
		}
	}
	if (valable==false){
		err_str=champ+": "+ErrMsg[11]+"\n";
	}
	return (err_str);
}
//-------------------------------------------------------------------check_6
function check_6 (obj_champ,champ,param){			//checkbox - au moins un sélectionné
	var err_str="";
	var checked_ok=false;
	var leng=param[0];
	if (obj_champ.checked==true){
		checked_ok=true;			//premier champ checked
	}else{						//chercher dans autres champs de la série
		for (k=0;k<param.length;k++){			//pour tous les checkbox listés dans la variable param du tableau
			var num=param[k];
			if ( eval('document.'+obj_champ.form.name+'.'+FormArr[num][0]+'.checked')==true ){		//si checked
				checked_ok=true;
			}else{
				err_str=err_str+FormArr[num][1]+", ";			//ajouter nom du champ au string d'erreur
			}
		}
	}
	if (checked_ok==false){			//si on a toujours pas trouvé de box checked
		err_str=champ+", "+err_str+": "+ErrMsg[10]+"\n";
	}else{
		err_str="";			//si on a trouvé, on remet le string à "" (il y avait une liste des boxes pas checkés dedans)
	}
	return (err_str);
}
//-------------------------------------------------------------------check_7
//	FormArr[7]=["password1","password",7,[1,8]];			//param:indexe de celui avec lequel faire la comparaison

function check_7 (obj_champ,champ,param){			//password et confirmer password
	var err_str="";
	var meme=true;
	var leng=param[0];
	for (k=1;k<param.length;k++){			//pour tous les champs de comparaison
		var num=param[k];
		//si les valeurs diffèrent ou champ pas assez long
		if ( (obj_champ.value!=eval('document.'+obj_champ.form.name+'.'+FormArr[num][0]).value)||(obj_champ.value.length<leng) ){
			
			meme=false;
		}
	}
	if (meme==false){			//on n'a pas trouvé
		err_str=err_str+champ+": "+ErrMsg[8]+" "+ErrMsg[9]+leng+ErrMsg[5]+"\n";
	}else{
		err_str="";			//si on a trouvé, on remet le string à "" (il y avait une liste des champs dedans)
	}
	return (err_str);
}
//-------------------------------------------------------------------divisible
function divisible (num1,num2){
	var is_divisible;
	if (num1%num2==0){
		is_divisible=true;
	}else{
		is_divisible=false;
	}
	return is_divisible;
}

//-------------------------------------------------------------------check_8
function check_8 (obj_champ,champ,param){			//vérifier format d'une date ex. 14/02/2001 ou 14.02.2001
	var err_str="";
	var nombre_jours;
	var bad_date=false;
	var caract="";
	var date_tableau;
	var le_string=String(obj_champ.value);
	var DateSlash=/\d{1,2}\/\d{1,2}\/\d\d\d\d/;		//format 09/08/2001
	var DatePoint=/\d{1,2}\.\d{1,2}\.\d\d\d\d/;		//format 09.08.2001
	results=le_string.match(DateSlash);						//faire le match
	results2=le_string.match(DatePoint);
	if (results!=null){				//si on a trouvé le format avec des /
		caract="/";
	}else if (results2!=null){
		caract=".";							// si on a trouvé le format avec des .
	}else{
		bad_date=true;
		err_str=champ + ": " + ErrMsg[12]+"\n";		//format pas correct
	}
	if (caract!=""){ //on continue!				
		//split string dans un tableau
		date_tableau=le_string.split(caract);
		//faire les checks sur les dates
		//vérifier le mois
		if ((date_tableau[1]<1)||(date_tableau[1]>12)){ 		//dans [1,12]
			bad_date=true;
		}
		//vérifier le jour
		if (date_tableau[1]==02){		//février
			if ((date_tableau[2]%4==0 && date_tableau[2]%100!=0)||(date_tableau[2]%400==0)){  //bissextile
				nombre_jours=29;
			}else{
				nombre_jours=28;
			}
		}else if ((date_tableau[1]==4)||(date_tableau[1]==6)||(date_tableau[1]==9)||(date_tableau[1]==11)){  			//les mois avec 30 jours
			nombre_jours=30;
		}else{  				//les autres mois (31 jours)
			nombre_jours=31;
		}
		if ((date_tableau[0]<1)||(date_tableau[0]>nombre_jours)){		//jours pas dans l'ensemble [1,nombre_jours]
				bad_date=true;
		}	
	}
	if ((bad_date==true)&&(err_str=="")){			//date impossible, et on n'a pas encore imprimé d'erreur string
		err_str=champ + ": " + ErrMsg[13]+"\n";
	}
	return (err_str);
}

//-------------------------------------------------------------------test_me

function test_me (obj_form){	
	var err_msg			
	err_msg="";		
	for (i=0; i<FormArr.length;i++){	//pour tous les champs mentionnés dans le tableau
		var fn;
		fn=FormArr[i][2];			//fn=numéro de la fonction (et du check) dans le tableau
		if (fn!=0) {				//si on veut checker quelque chose
			//on appelle la fonction approprié, qui retourne "" ou une chaîne de caractères contenant l'erreur
			err_msg=err_msg + eval('check_'+fn)(eval('obj_form.'+FormArr[i][0]),FormArr[i][1],FormArr[i][3]);
		}
	}	
	return(err_msg);
}
