// 2005-05-23 OD : Change message
//var  strMessageWhenNoValue = "Saisie obligatoire dans ce champs.";
var  strMessageWhenNoValue = "Merci de bien vouloir renseigner les champs annotés d\'une astérisque.";
var  strMessageWhenNoPassword = "Merci de bien vouloir renseigner votre mot de passe.";

// trim - retire les espaces-----------------------------------------------------------------------------------------------------------------------
function trim(value) {
	startposn=0;
	while((value.charAt(startposn)==" ")&&(startposn<value.length)) {
		startposn++;
	}
	if(startposn==value.length) {
		value="";
	} else {
		value=value.substring(startposn,value.length);
		endposn=(value.length)-1;
		while(value.charAt(endposn)==" ") {
			endposn--;
		}
		value=value.substring(0,endposn+1);
	}
	return(value);
}

// test sur 2 champs dépendant-----------------------------------------------------------------------------------------------------------------------
function Choix(champ1,champ2)
{
if(( champ1.options[champ1.selectedIndex].value == "0") && (trim(champ2.value)== ""))
  {alert("Merci de bien vouloir selectionner un des choix proposes.");
   champ1.focus();
   return false;}
   return true;
}

// test réponse à 2 niveau, radio bouton + champ-----------------------------------------------------------------------------------------------------------------------
function Niveau(champ1,champ2)
{
if((champ1[0].checked))
  { return InputValidation(champ2,true,true,false,0);}
   return true;
}

// test password--------------------------------------------------------------------------------------------------------------------
function Passe(champ1,champ2,VMax){
      longueur=0
      chaine=trim(champ1.value)
			 longueur=chaine.length
			 if(longueur <VMax) {
     alert("Votre mot de passe doit comporter un minimum de " + VMax + " caractere(s).");
     champ1.value="";
     champ1.focus();
	 return false;}
	 
	 if (trim(champ1.value)!=trim(champ2.value)) {
     alert("Confirmation de mot de passe incorrecte");
     champ2.value="";
     champ2.focus();
	 return false;}
	 return true;
}
	                
	                

// teste la validité de l'adresse email -----------------------------------------------------------------------------------------------------------------------
function CheckEmail(lemail)  
{  
	// si la valeur est nulle
	if (trim(lemail.value)=="")
	{
	        //alert("Email field not filled in.");
					alert("Merci de bien vouloir renseigner l\'adresse email.");
	        lemail.focus();
	        return false;
	}
	else
	{
	// si la valeur ne contient pas @
	        if (lemail.value.indexOf("@")==-1)        
			{
	                //alert("You must enter a valid address email.");
									alert("L\'adresse email saisie n\'est pas valide.");
	                lemail.focus();
	                return false;
	        }
	        else
	        {
	                longueur = lemail.value.length;
	                position = lemail.value.indexOf("@");
	                left_str = lemail.value.substring(0,position);
	                right_str = lemail.value.substring(position+1,longueur);
	                left_len = left_str.length;
	                right_len = right_str.length;
	
	// si la valeur gauche ou droite du @ est vide
	                if ((left_len==0) || (right_len==0)){
									//alert("You must enter a valid address email.");
									alert("Merci de bien vouloir renseigner l\'adresse email.");
	                lemail.focus();
	                return false;
	                }
	                else
	                {
	// s'il n'y a pas de point
	                        if (right_str.indexOf(".")==-1)
	                        {
	                        //alert("You must enter a valid address email.");
													alert("L\'adresse email saisie n\'est pas valide.");
	                        lemail.focus();
	                        return false;
	                        }
	                        else
	                        {
	                                right_longueur = right_str.length;
	                                right_position = right_str.lastIndexOf(".");
	                                l_right_str = right_str.substring(0,right_position);
	                                r_right_str = right_str.substring(right_position+1,right_longueur);
	                                l_right_len = l_right_str.length;
	                                r_right_len = r_right_str.length;
	
	// s'il y a au moins 2 lettres après le point
	                                if ((r_right_len < 2)||(r_right_len > 4))
	                                {
	                                //alert("You must enter a valid address email.");
																	alert("L\'adresse email saisie n\'est pas valide.");
	                                lemail.focus();
	                                return false;
	                                }
	                                else
	                                {
	// s'il y a au moins 1 lettre avant le point
	                                        if (l_right_len==0)
	                                        {
	                                        //alert("You must enter a valid address email.");
																					alert("L\'adresse email saisie n\'est pas valide.");
	                                        lemail.focus();
	                                        return false;
	                                        }
	                                }
	                        }
	                }
	        }
	}
	return true;
}


//Test champ exist ou vide

function InputValidation(champ,exist,isNumber,VMax,VMin){
	// exist	boolean 	pour verifier l'existence d"une valeur
	//VMax 	int		optionnel : valeur max à tester
	//VMin 	int		optionnel : valeur min à tester
	var ok = true;
	if(champ == null){
		return true;
	}
	switch(champ.type){
	case 'text':
	case 'textarea':
	if(exist && trim(champ.value)== "") {
			alert(strMessageWhenNoValue);
			champ.value = "";
			champ.focus();
			return false;
		}
		if(isNumber && isNaN(champ.value)){
			alert("Vous devez saisir un nombre");
			champ.value = "";
			champ.focus();
			return false;
		}
		if((VMax != "") && (champ.value > VMax)){
			alert("Valeur incorrecte");
			champ.value = "";
			champ.focus();
			return false;
		}
		if((VMin != "") && (champ.value < VMin)){
			alert("Valeur incorrecte");
			champ.value = "";
			champ.focus();
			return false;
		}
		break;
	case 'password':
	if(exist && trim(champ.value)== "") {
			alert(strMessageWhenNoPassword);
			champ.value = "";
			champ.focus();
			return false;
		}
		break;
	case 'checkbox':
	if(exist && ! champ.checked) {
			alert(strMessageWhenNoValue);
			champ.value = "";
			champ.focus();
			return false;
		}
		break;
	case 'select-one':
	if(exist && nbSelected(champ) == "0"){
			alert(strMessageWhenNoValue);
			champ.focus();
			return false;
		}
			break;
	default :
	}
	return true;
}


// gets the number of non empty elements in a field
// of  type select-one or select-multiple
function nbSelected(Field) {
	var i, nbSelected = 0;
	for (i = 0; i < Field.options.length; i++) {
		if (Field.options[i].selected
		  && i != 0 && Field.options[i].text != '' && Field.options[i].text != ' ') {nbSelected++;};
	};
	return nbSelected;
};



// gestion de la date----------------------------------------------------------------------------------------------------------------------------
function DateValidation(champ)
{
	var ok = false;
	if(champ == null){return true;};
	if(champ.value == ""){return true;};
	ok = isDate(champ);
	if(! ok)
	{
	alert("You must enter a valid date in this format : dd/mm/yyyy");
	}
	else
	{return ok;}
}

function isDate (obj)
{   
	var theString = new String(obj.value);
	var theTokens = theString.split('/');
	if ( theTokens.length < 3 || theTokens.length > 3 ){return false;};
	var tokenIndex;
	for ( tokenIndex = 0; tokenIndex < theTokens.length; tokenIndex++ ) {
		theTokens[tokenIndex] = new String(theTokens[tokenIndex]) 
		if ( theTokens[tokenIndex].charAt(0) == '0' )
			theTokens[tokenIndex] = theTokens[tokenIndex].substring(1, theTokens[tokenIndex].length);
	}
	var day = theTokens[0]; 
	var month = theTokens[1];
	var year = theTokens[2];
	// catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year) && isMonth(month, false) && isDay(day, false))) return false;

    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}
var daysInMonth = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31);

function isYear (s)
{  
	if(isNaN(s)){return false};
    	if ((s.length == 2) || (s.length == 4)){	return true;};
	return false;
}
function isMonth (s)
{  
     if(!isNaN(s)){ return isIntegerInRange (s, 1, 12);};
     return false;
}
function isDay (s)
{        
	if( !isNaN(s)){ return isIntegerInRange (s, 1, 31);};
     return false
}
function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isIntegerInRange (s, a, b)
{   
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}