// FormCheck.js
//
//	Par: Francis Labelle (basée sur le script de Netscape)
//	Date: 11/06/2002
//
//	Lot de fonctions permettant de valider plusieurs types de champs dans
//	un formulaire Web.
//
//	Note: 	[,eok] est un option, lorsque sa valeur est 'true' le champs vérifié
//		sera bon s'il est vide ou s'il correspond au spécification de la procédure
//		propre au champs.
//
//FONCTIONS:
//_______________________________________
//checkDATE(NomDuChamp.value [,eok])					Vérifie que la Date entrée en bonne et que la synthase est JJ/MM/AAAA
//checkANNEE(NomDuChamp.value [,eok, NbreChiffre])		Vérifie que l'année entrée est en format AAAA
//checkNUM(NomDuChamp.value [,eok])						Valide si les informations données sont numériques
//checkNAS(NomDuChamp.value [,eok])						Valide s'il y a bien 9 chiffres et le bon format de NAS: XXX-XXX-XXX
//checkADR(NomDuChamp.value [,eok])						Valide si l'information donnée contient un nombre est des lettres
//checkCP(NomDuChamp.value [,eok])						Valide le format du Code Postal au Canada
//
//isNAS (STRING s [, BOOLEAN emptyOK])
//

// **********************************************************
//			VARIABLES GLOBALES

var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

var defautVideOK = false;
defautNbrChiffreAnnee = 2;
var NASDelimiteur = "-";
var CPDelimiteur = " ";
var NASDelimiteurCherche = "- /";
var DateDelimiteur = "/";
var CaracValideNAS = digits + NASDelimiteur;
var digitsInNAS = 9;

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

// **********************************************************
//			MESSAGES LORSQUE INVALIDE
var iInvalid = " est invalide."
var iCODENOT = "Ce champ doit avoir un code de notaire valide, Ex.: A1235. SVP resaisissez l'information."
var iCP = "Ce champ doit avoir un code postal valide (Ex.: A2A 2W8). SVP resaisissez l'information."
var iNAS = "Ce champ doit être un NAS valide. Ex.:123-455-689. SVP resaisissez l'information."
var iDay1 = "Le champ "
var iDay2 = " doit contenir un no. entre 1 et 31 pour les jours. SVP resaisissez l'information."
var iMonth1 = "Le champ "
var iMonth2 = " doit contenir un no. entre 1 et 12 pour le mois. SVP resaisissez l'information."
var iYear1 = "Ce champ doit contenir une année valide de "
var iYear2 = " chiffres, plus grande ou égale à 1800 et ne pouvant être supérieure à l'année courante. SVP resaisissez l'information."
var iNum1 = "Le champ "
var iNum2 = " ne doit contenir que des chiffres. SVP resaisissez l'information."
var iAdr = "Cette adresse est soit invalide ou incomplète (ex.: no. civique, nom de rue). SVP resaisissez l'information."
var iDatePrefix = "Le champ "
var iDateSuffix = " ne forme pas une date valide. Vérifiez le jour, le mois et l'année et resaisissez l'information."

// **********************************************************
//			STRING CONSTANTE

var sActe = "date de l'acte"
var sFinActe= "date de fin de l'acte";
var sNaissance = "date de naissance du testateur"
var sDateDebut = "date de prise d'effet de la suspension"
var sDateFin = "date de fin de la suspension"

// **********************************************************
//			SOUS-PROCEDURE DE BASE

function makeArray(n)
{
	for (var i = 1; i <= n; i++) {this[i] = 0} 
  	return this
}



// Retourne true si la string 's' est vide.
function isVide(s)
{
	//alert("regarde si vide")
	return ((s == null) || (s.length == 0))
}

// isJour (STRING s [, BOOLEAN emptyOK])
// retourne true si la string est entre 1 et 31
function isJour (s)
{   
	//alert("valide la journee")
	if (isVide(s))
	{ 
       		if (isJour.arguments.length == 1){ return defautVideOK;}
       		else { return (isJour.arguments[1] == true);}   
    	}
    	//alert("valide le range de la journee")
    	return isIntegerDansBorne (s, 1, 31);
}

// isMois (STRING s [, BOOLEAN emptyOK])
// isMois retourne true si la string est entre 1 et 12
function isMois (s)
{
	//alert("valide le mois")
	if (isVide(s)) 
	{
	//	alert("elle est vide")
    	if (isMois.arguments.length == 1) {return defautVideOK;}
       	else {return (isMois.arguments[1] == true);}
    }
    //alert("elle n est pas vide")
    return isIntegerDansBorne (s, 1, 12);
}

// isAnnee retourne true si la string a la bonne dimension
function isAnnee(s)
{   
	//alert("string s"+s)
	if (isVide(s)) 
    	{
    		//alert("string vide")
    		if (isAnnee.arguments.length == 1)
       		{
       			return defautVideOK;
       		}
       		else
       		{
       			return (isAnnee.arguments[1] == true);
       		}
    	}
    	if (!isNonNegatifInteger(s))
    	{
    		return false;
    	}
    	//alert("check si l annee a le bon nombre de chiffres")
    	return (s.length == isAnnee.arguments[2]);
}


// jourInFevrier (INTEGER year)
// retourne le nombre de jour de fevrier de lannee courante
function jourInFevrier (annee)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((annee % 4 == 0) && ( (!(annee % 100 == 0)) || (annee % 400 == 0) ) ) ? 29 : 28 );
}



// isDate (STRING year, STRING month, STRING day)
// retourne true si Annee, mois et jour forme une date valide
function isDate (annee, mois, jour)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    //alert("entre dans isdate")
    if (! (isAnnee(annee, false, 4) && isMois(mois, false) && isJour(jour, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(annee);
    var intMonth = parseFloat(mois);
    var intDay = parseFloat(jour);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > jourInFevrier(intYear))) return false;

    return true;
}

//Vérifie la validation des dates p/r à la date de l'avis (date du jour), date début période et date fin période
function isValidIntervalle (theField, dateEntree, dateDebut, dateFin)
{
    var d = new Date();   
    var anneeCourant = new String(d.getFullYear());
    var moisCourant = new String((d.getMonth() + 1));
    var jourCourant = new String(d.getDate());
    if (moisCourant < 10){
    	moisCourant = "0" + moisCourant;
    }
    if (jourCourant < 10){
    	jourCourant = "0" + jourCourant;
    }
    var dateCourant = anneeCourant + moisCourant + jourCourant;
    var dateDebutString = dateDebut.substr(6,4) + dateDebut.substr(3,2) + dateDebut.substr(0,2);
    var dateFinString = dateFin.substr(6,4) + dateFin.substr(3,2) + dateFin.substr(0,2);
       
    //alert(dateEntree + ", " + dateDebutString + ", " + dateFinString);
    if (theField.name=="DateNaisTesMan" && dateEntree > dateCourant){
    	return "La date de naissance doit être inférieure ou égale à la date d'aujoud'hui. S.V.P ressaisissez l'information.";
    }    
    else if (theField.name=="DateActe" && (dateDebutString == "" || dateFinString == "") && dateEntree > dateCourant){
    	return "La date de l'acte doit être inférieure ou égale à la date de l'avis pour un non-périodique. S.V.P ressaisissez l'information.";
    }    
    else if (theField.name=="DateActe" && dateDebutString != "" && dateFinString != "" && (dateEntree < dateDebutString || dateEntree > dateFinString)){
    	return "La date de l'acte doit être incluse dans la période de l'avis pour un périodique. S.V.P ressaisissez l'information.";
    }
    else if (dateDebutString > dateFinString){
    	return "La date de début de la période ne peut pas être supérieure à la date de fin de la période. S.V.P ressaisissez l'information.";
    }
    else if (dateDebutString > dateCourant){
    	return "La date de début de la période ne peut pas être supérieure à la date de l'avis. S.V.P ressaisissez l'information.";
    }   
    else {return true;}

}

// isIntegerDansBorne (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// Retourne vrai si la string est entre les bornes "a" et "b"
function isIntegerDansBorne (s, a, b)
{   if (isVide(s)) 
       if (isIntegerDansBorne.arguments.length == 1) return defaulVideOK;
       else return (isIntegerDansBorne.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)){
    	 return false;
    	 
    }

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseFloat(s);
    return ((num >= a) && (num <= b));
}


// Retourne true si la string 's' est un nombre positif.
function isNonNegatifInteger (s)
{
	var secondArg = defautVideOK;

    if (isNonNegatifInteger.arguments.length > 1)
        secondArg = isNonNegatifInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSigneInteger(s, secondArg) && ( (isVide(s) && secondArg)  || (parseInt(s) >= 0) ) );
}

// EXEMPLE DE CALL:				RESULTAT:
// isSigneInteger ("5")           true 
// isSigneInteger ("")            defautVideOK
// isSigneInteger ("-5")          true
// isSigneInteger ("+5")          true
// isSigneInteger ("", false)     false
// isSigneInteger ("", true)      true

function isSigneInteger (s)
{
	if (isVide(s)) 
    	if (isSigneInteger.arguments.length == 1) return defautVideOK;
    	else return (isSigneInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defautVideOK;

        if (isSigneInteger.arguments.length > 1)
            secondArg = isSigneInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
        	startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

// EXEMPLE DE CALL:			RESULTAT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)
{   var i;

	if (isVide(s)) 
    	if (isInteger.arguments.length == 1) return defautVideOK;
       	else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
    	// Check that current character is number.
    	var c = s.charAt(i);

    	if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Retourne true si le caractere 'c' est un digit 
// (0 .. 9).
function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}

// Retourne true si le caractere 'c' est une lettre 
// (A .. Z) (a .. z)
function isLettre (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// regarde si la string 's' contient des chiffres et des lettres
function isAlphanumerique(s)
{
	var i;
	var DigitTrouve;
	var	LettreTrouve;
	
    if (isVide(s)) 
       if (isAlphanumerique.arguments.length == 1) return defautVideOK;
       else return (isAlphanumerique.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (isLettre(c))
        {
        	LettreTrouve=1;
        }
        if (isDigit(c))
        {
        	DigitTrouve=1;
        }
        
    }
	
	if (!((DigitTrouve)&&(LettreTrouve)))
	{
		return false;
	}
	else 
	{
    	// la string contient au moins un digit et une lettre
    	return true;
    }
}


// isCP (STRING s [, BOOLEAN emptyOK])
function isCP (s)
{   if (isVide(s)) 
       if (isCP.arguments.length == 1) return defautVideOK;
       else return (isCP.arguments[1] == true);

    return ((isLettre(s.substring(0,1)))&&(isLettre(s.substring(2,3)))&&(isLettre(s.substring(4,5)))&&(isDigit(s.substring(1,2)))&&(isDigit(s.substring(3,4)))&&(isDigit(s.substring(5,6))))
}

//    return (isLettre(s[0]) && isDigit(s[1]) && isDigit(s[2]) && isDigit(s[3]) && isDigit(s[4]) && s.length == 5)
// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function EntreeInvalide (theField, s,ShowAlert)
{	
	if(!ShowAlert){	return s;}
	else{
		theField.focus();
    		theField.select();
    		alert(s);
    		return false;
    	}
}

function EntreeInvalideSelect (theField, s,ShowAlert)
{
	if(!ShowAlert){return s;}
	else{
		theField.focus();
    	alert(s);
    	return false;
    }
}

// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)
{
	var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


//Segment une sting pour la reformater
function reformatDATE(DATEe)
{
	return (reformat (DATEe, "", 2, DateDelimiteur, 2, DateDelimiteur, 4))
}

//Segment une sting pour la reformater
function reformatCP (CP)
{
	return (reformat (CP, "", 3, CPDelimiteur, 3))
}

//Segment une sting pour la reformater
function reformatNAS (NAS)
{
	return (reformat (NAS, "", 3, NASDelimiteur, 3, NASDelimiteur, 3))
}

function reformatPhone (phone)
{
	return (reformat (phone, "", 3, NASDelimiteur, 3, NASDelimiteur, 4))
}

function reformatNoPermisTemp (noPermisTemp)
{
	return (reformat (noPermisTemp, "", 4, NASDelimiteur, 3))
}

//Reformate une string
function reformat (s)
{
	var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}
// **********************************************************
//			PROCEDURE PRINCIPALE DE VERIFICATION

function valid_NAS(tbname,nom,ShowAlert) {
	var text = stripCharsInBag(tbname.value, NASDelimiteurCherche)  
	var s = tbname.value;
	var esum = 0;
    var enumbers = "";
    var checknum = 0;
    var ch_sum = "";
    var checkdigit = 0;
    var sin = "";
    var lastdigit = 0;
    var inStr = text;
    var sin = text;
    var inLen = inStr.length;
    	//else if((inLen > 9) || (inLen < 9)) {
     //    this.focus();
	 //   return EntreeInvalide(tbname,iMonth1 + nom + iInvalid);
        //	alert("Numéro d'assurance sociale invalide. Veuillez entrer un numéro de 9 chiffres.");
		//return false;
     //   }  
              
	if ((s.search(/\d/) >= 0) || (inLen != 9)) {
        this.focus();
	    return EntreeInvalide(tbname,iMonth1 + nom + iInvalid);
	}
	else{

		lastdigit = text.substring(8, 8 + 1);
		// add numbers in odd positions; IE 1, 3, 6, 8          
		var odd = ((text.substring(0,0 + 1)) * (1.0)  + (text.substring(2,2 + 1)) * (1.0) + (text.substring(4, 4+1)) * (1.0) + (text.substring(6,6 + 1)) * (1.0));
                                                
		// form texting of numbers in even positions IE 2, 4, 6, 8
		var enumbers =  (text.substring(1,1 + 1)) + (text.substring(3,3 + 1)) + (text.substring(5,5 + 1)) + (text.substring(7,7 + 1));
                        
		// add together numbers in new text string
		// take numbers in even positions; IE 2, 4, 6, 8
		// and double them to form a new text string
		// EG if numbers are 2,5,1,9 new text string is 410218
		for (var i = 0; i < enumbers.length; i++) {
			var ch = (enumbers.substring(i, i + 1) * 2);
			ch_sum = ch_sum + ch;
		}
                        
                for (var i = 0; i < ch_sum.length; i++) {
			var ch = (ch_sum.substring(i, i + 1));
			esum = ((esum * 1.0) + (ch * 1.0));
		}
                        
		checknum = (odd + esum);
                        
		// subtextact checknum from next highest multiple of 10
                // to give check digit which is last digit in valid SIN
                if (checknum <= 10) {
			(checkdigit = (10 - checknum));
		}
		if (checknum > 10 && checknum <= 20) {
			(checkdigit = (20 - checknum));
		}
		if (checknum > 20 && checknum <= 30) {
                        (checkdigit = (30 - checknum));
                }
                if (checknum > 30 && checknum <= 40) {
                        (checkdigit = (40 - checknum));
		}
                if (checknum > 40 && checknum <= 50) {
                	(checkdigit = (50 - checknum));
                }
                if (checknum > 50 && checknum <= 60) {
                        (checkdigit = (60 - checknum));
                }                              
		if (checkdigit != lastdigit) {
              this.focus();
	    return EntreeInvalide(tbname,iMonth1 + nom + iInvalid,ShowAlert);
                        //alert(checknum + "," + checkdigit + "," + lastdigit);
			//return false;
            } 
            //alert(checknum + "," + checkdigit + "," + lastdigit);
        tbname.value = reformatNAS(text) 
		return true;                                                             
	}
}
/////////////

function valid_phone(tbname,nom,ShowAlert) {
	var text = stripCharsInBag(tbname.value, NASDelimiteurCherche)                
	if ((text.search(/\D/) >= 0) || (text.length != 10)) {
        	this.focus();
	    	return EntreeInvalide(tbname,iMonth1 + nom + iInvalid + " Veuillez entrer l'information dans le format suivant : 999-999-9999.",ShowAlert);
	}
	else{
        	tbname.value = reformatPhone(text) 
		return true;                                                             
	}
}
/////////////

function valid_noPermisTemp(tbname,nom,ShowAlert) {
	var text = stripCharsInBag(tbname.value, NASDelimiteurCherche)                
	if ((text.search(/\D/) >= 0) || (text.length != 7)) {
        	this.focus();
	    	return EntreeInvalide(tbname,iMonth1 + nom + iInvalid + " Veuillez entrer l'information dans le format suivant : 9999-999.",ShowAlert);
	}
	else{
        	tbname.value = reformatNoPermisTemp(text) 
		return true;                                                             
	}
}
/////////////

function valid_email(theField,nom,ShowAlert) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theField.value)){
		return (true)
	}
	return EntreeInvalide(theField,iMonth1 + nom + iInvalid + " Veuillez entrer l'information dans le format suivant : usager@domaine.com",ShowAlert);
}


/////////////
function valid_alpha(theField,nom,min,c,ShowAlert){
	var s = theField.value;
	var l = s.length;
	if(c==1)
	{
		var s1 = s.substring(0,1);
		var s2 = s.substring(1,l);
		s1=s1.toUpperCase();
		theField.value = s = (s1 + s2);
	}
	if(s.search(/\d/) >= 0 || l < min || valide_caractereProscrire(theField,nom,0)!=true)
	{
		this.focus();
	    	return EntreeInvalide(theField,iMonth1 + nom + iInvalid,ShowAlert);
	}
	else {return true;}
}

function valid_minuteLettres(theField,nom,min,c,ShowAlert){
	var s = theField.value;
	var l = s.length;
	if(c==1)
	{
		var s1 = s.substring(0,1);
		var s2 = s.substring(1,l);
		theField.value = s = (s1 + s2);
	}
	if(s.search(/\d/) >= 0 || l < min || valide_caractereProscrire(theField,nom,0)!=true)
	{
		this.focus();
	    	return EntreeInvalide(theField,iMonth1 + nom + iInvalid,ShowAlert);
	}
	else {return true;}
}

/////////////
function valide_caractereProscrire(tbname,nom,ShowAlert){
	if(tbname.value.search(/%/) >= 0)
	{
		this.focus();
	    	return EntreeInvalideSelect(tbname,iMonth1 + nom + iInvalid,ShowAlert);
	}
	else {return true;}
}

/////////////
function valide_province(tbname,nom,ShowAlert){
	if(tbname.value == "")
	{
		this.focus();
	    return EntreeInvalideSelect(tbname,iMonth1 + nom + iInvalid,ShowAlert);
	}
	else {return true;}
}

/////////////
function valide_pays(tbname,nom,ShowAlert){
	if(tbname.value == "")
	{
		this.focus();
	    return EntreeInvalideSelect(tbname,iMonth1 + nom + iInvalid,ShowAlert);
	}
	if(tbname.value == "CA")
	{
		if(document.form1.CPTesMan.value == ""){
			document.form1.CPTesMan.focus();
	    		return EntreeInvalideSelect(document.form1.CPTesMan,"Le code postal est obligatoire pour le Canada.",ShowAlert);	
		}
		else {return true;}
	}
	else {
		document.form1.CPTesMan.value="";
		return true;
	}
}


// Vérifie que NomDuChamp.value est un DATE valide
//function checkDATE (theField, labelString, VideOK, NbrChiffreAnnee)
function checkDate(theField, labelString, NbrChiffreAnnee, OKtoOmitDay, ShowAlert)
{ 
	var taille =  4 + NbrChiffreAnnee;
	if (checkDate.arguments.length == 3){
		OKtoOmitDay = false;
	}
    	else
    	{
    		var DATEnormalise = stripCharsInBag(theField.value, NASDelimiteurCherche);

    		if (DATEnormalise.length!=taille)
    		{
    			this.focus();
    			return EntreeInvalide(theField, iDatePrefix + labelString + iDateSuffix,ShowAlert);
    		}
    		var dayField = DATEnormalise.substring(0,2);
    		var monthField = DATEnormalise.substring(2,4);
    		var yearField = DATEnormalise.substring(4,8);
		//	alert(NbrChiffreAnnee);	
	    	if ((!isAnnee(yearField, true, NbrChiffreAnnee))||(yearField<1800))
	    	{
	    		//alert(yearField);
	    		this.focus();
	    		return EntreeInvalide(theField, iYear1+NbrChiffreAnnee+iYear2,ShowAlert);
	    		//alert("bonjour2");
	    	}
	   	// alert("quelle mois "+monthField);	
	    	if (!isMois(monthField))
	    	{
	    		//alert("le mois est invalide");
	    		this.focus();
	    		return EntreeInvalide(theField, iMonth1 + labelString + iMonth2,ShowAlert );
	    	}
	    	//alert("le mois est bon");
	    	//alert("quelle jour "+dayField);
	    	if ( (OKtoOmitDay == true) && isVide(dayField) ) return true;
	    	else if (!isJour(dayField))
	    	{ 
	    		//alert("je jour est invalide");
	    		this.focus();
	    		return EntreeInvalide (theField, iDay1 + labelString + iDay2,ShowAlert);
	    	}
	    	if (isDate (yearField, monthField, dayField))
	    	{
	    		theField.value = reformatDATE(DATEnormalise);
	    		//var errorMsg = isValidIntervalle(theField, yearField+monthField+dayField, form1.dateDebut.value, form1.dateFin.value);
	    		//alert("--1" + theField.name + ", " + errorMsg);
	    		//if ( errorMsg!= true){
	    			//this.focus();
	    			//alert("--2" + EntreeInvalide(theField, errorMsg, 0));
	    			//return EntreeInvalide(theField, errorMsg, 0);	
	    		//}
	    		//else {
	    			return true;
	    		//}
	    	}
	    	//alert(iDatePrefix + labelString + iDateSuffix)
	    	//return false;
	    	return EntreeInvalide(theField, iDatePrefix + labelString + iDateSuffix, ShowAlert);
	}
}

// Vérifie que NomDuChamp.value est un ANNEE valide
function checkANNEE (theField, VideOK, NbrChiffreAnnee,ShowAlert)
{
	var d = new Date();
	var AnneePresent = d.getFullYear();
	if (checkANNEE.arguments.length == 1) 
	{
		VideOK = defautVideOK;
		NbrChiffreAnnee = defautNbrChiffreAnnee;
	}
	if ((VideOK == true) && (isVide(theField.value))) return true;	
   	if (!isAnnee(theField.value, VideOK, NbrChiffreAnnee) || theField.value < 1800 || (theField.name=="AnneeNaisTesMan" && theField.value > AnneePresent)) 
   	{
   		this.focus();
    		return EntreeInvalide (theField, iYear1+NbrChiffreAnnee+iYear2,ShowAlert);
    	}	   	
    	else {
    		return true;
    	}
}

// Vérifie que NomDuChamp.value est un NOMBRE
function checkNUM (theField, VideOK,nom,ShowAlert)
{
	if (checkNUM.arguments.length == 1) VideOK = defautVideOK;
	if ((VideOK == true) && (isVide(theField.value))) return true;
   	else
   	{
   		if (!isInteger(theField.value)) {
   			this.focus();
   			return EntreeInvalide(theField, iNum1 + nom + iNum2,ShowAlert)
   		}
   		else if (nom == "numéro de minute" && theField.value == 0){
   			this.focus();
   			return EntreeInvalide(theField, "Zéro n'est pas un numéro de minute valide.",ShowAlert)   			
   		}
   		else {return true;}
	}
}

// Vérifie que NomDuChamp.value est un ADRESSE valide
function checkADR (theField, VideOK,ShowAlert)
{
	if (checkADR.arguments.length == 1) VideOK = defautVideOK;
	if ((VideOK == true) && (isVide(theField.value))) return true;
   	else {
   		if (!isAlphanumerique(theField.value) || valide_caractereProscrire(theField,"",0)!=true)
   		{
   			this.focus();
   			return EntreeInvalide(theField, iAdr,ShowAlert)
   		}
   		else {return true;}
	}
}

// Vérifie que NomDuChamp.value est un CODE POSTAL
function checkCP (theField, VideOK, ShowAlert)
{
	if (checkCP.arguments.length == 1){
		VideOK = defautVideOK;
	}	
	if ((VideOK == true) && (isVide(theField.value))){
		return true;
	}
	else
	{
		var CPnormalise = stripCharsInBag(theField.value, NASDelimiteurCherche);
		CPnormalise = CPnormalise.toUpperCase();
		if (!isCP(CPnormalise, false))
		{
			this.focus();
			return EntreeInvalide(theField, iCP,ShowAlert)
		}
		theField.value = reformatCP(CPnormalise);
		return true;
	}
}

// Vérifie que NomDuChamp.value est un CODE notaire
function valide_CodeNot(theField,nom,min,ShowAlert)
{	
	var s = theField.value;
	theField.value=theField.value.toUpperCase();
	if((s.length == min)&&(isLettre(s.substring(0,1)))&&(isDigit(s.substring(1,2)))&&(isDigit(s.substring(2,3)))&&(isDigit(s.substring(3,4)))&&(isDigit(s.substring(4,5))))
	{
		return true;
	}
	else
  	{
		this.focus();
		return false;
	    //return EntreeInvalide(theField,iMonth1 + nom + iInvalid,ShowAlert);
	}
	
}

function valide_CodeVerification(theField,nom,min,max,ShowAlert)
{	
	theField.value=theField.value.toUpperCase();
	var s = theField.value;
	if((!(s.length >= min && s.length <= max))){
		this.focus();
	    	return EntreeInvalide(theField,iMonth1 + nom + iInvalid + " Veuillez entrer une séquence de cinq à huit caractères alpha (A-Z) et/ou numérique (0-9).",ShowAlert);	
	}
	else{
  		for (i = 0; i < s.length; i++)
    		{ 
    			var c = s.charAt(i);
    			if (c.match(/[^A-Z0-9]/)){
    				this.focus();
	    			return EntreeInvalide(theField,iMonth1 + nom + iInvalid + " Veuillez entrer une séquence de cinq à huit caractères alpha (A-Z) et/ou numérique (0-9).",ShowAlert);
	    		}
    		}
		return true;
	}	
}