//********************************* FUNCTIONS ******************************************//

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0));
}



// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   // Is s empty?
    return (isEmpty(s) || reWhitespace.test(s));
}

function stripCharsInRE (s, bag)

{       return s.replace(bag, "");
}

// 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;
}



// Removes all characters which do NOT appear in string bag 
// from string s.

function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't in bag.
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}

//
// Removes all redundant characters from string s.
//
function stripRedundancies (s)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character at position i is unique, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't in bag.
        var c = s.charAt(i);
        if (i == (s.length-1)) returnString += c;
		else
		{
			if (s.indexOf(c,i+1) == -1) returnString += c;
		}
    }
    return returnString;
}

function stripWhitespace (s)

{   return stripCharsInBag (s, whitespace);
}

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

function stripInitialWhitespace (s)

{   var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    
    return s.substring (i, s.length);
}



// Returns true if character c is a letter 
// according to current language (default is the value of defaultLanguage variable).
//

function isLetter (c)
{   
	return reLetter.test(c);
}



// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return reDigit.test(c);
}



// Returns true if character c is a letter or digit.

function isLetterOrDigit (c)
{
	return reLetterOrDigit.test(c);
}

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s);
}

function isSignedInteger (s)

{   if (isEmpty(s)) 
	{
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);
	}
    
    else {
       return(reSignedInteger.test(s));
    }
}


function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) > 0) ) );
}

function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) {
       if (isAlphabetic.arguments.length == 1) 
	   		return defaultEmptyOK;
       else 
	   		return (isAlphabetic.arguments[1] == true);
	}
    else {
       return reAlphabetic.test(s);
    }
}

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) {
       if (isAlphanumeric.arguments.length == 1) 
	   {
	   		return defaultEmptyOK;
		}
       else
	   { 
	   		return (isAlphanumeric.arguments[1] == true);
		}
	}
    else {
       return reAlphanumeric.test(s);
    }
}

function isWord (s)

{   var i;

    if (isEmpty(s)) {
       if (isWord.arguments.length == 1) 
	   {
	   		return defaultEmptyOK;
		}
       else
	   { 
	   		return (isWord.arguments[1] == true);
		}
	}
    else {
       return reWord.test(s);
    }
}



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;
}

function isPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInPhoneNumber && rePhoneNumber.test(s));
}

function isInternationalPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isInternationalPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isInternationalPhoneNumber.arguments[1] == true);
    return (isPositiveInteger(s));
}



function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg) && ( (isEmpty(s) && secondArg)  || (parseInt (s,10) >= 0) ) );
}

function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)));
}

function isStateCode(s)
{   if (isEmpty(s)) 
       if (isStateCode.arguments.length == 1) return defaultEmptyOK;
       else return (isStateCode.arguments[1] == true);
	
    return ( (StateCodes.indexOf(s) != -1) &&
             (s.indexOf(StateCodeDelimiter) == -1) );
}

function isPicture (s, possibleExtensions)

{	
	var result;
    if (isEmpty(s)) 
       if (isPicture.arguments.length == 2) return defaultEmptyOK;
       else return (isPicture.arguments[2] == true);
    
    else {
		result = s.match(rePicture);
		if (result != null)
		{
			return (possibleExtensions.indexOf(result[1]) != -1); 
			//return rePicture.test(s)
		}
    }
}

function isEmail (s)

{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    
    else {
       return reEmail.test(s);
    }
}

function isUrl (s)

{   if (isEmpty(s)) 
       if (isUrl.arguments.length == 1) return defaultEmptyOK;
       else return (isUrl.arguments[1] == true);
    
    else {
       return reUrl.test(s);
    }
}

function isHttpUrl (s)

{   if (isEmpty(s)) 
       if (isHttpUrl.arguments.length == 1) return defaultEmptyOK;
       else return (isHttpUrl.arguments[1] == true);
    
    else {
       return reHttpUrl.test(s);
    }
}

function isFieldLenBetween (s, maxL, minL, emptyOK)

{   
	if (isEmpty(s)) 
       if (isFieldLenBetween.arguments.length == 3) return defaultEmptyOK;
       else return (isFieldLenBetween.arguments[3] == true);  
    else {
		var v=s.length;
		if ((maxL==null) && (minL==null))
	       return true;
		else {
			if ((maxL!=null) && (minL!=null))
				return((v <= maxL)&&(v >= miL));
			else {
				if (maxL!=null)
					return (v <= maxL);
				else
					return (v >= minL);
			}
		}
    }
}

function isValidFieldLen(theField,iMax,emptyOK)
{
	var stheField=new String(theField.value);

    if (isValidFieldLen.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if ((isWhitespace(theField.value)) && (emptyOK == false))
    {
		//aMessage="Text";
        //return warnEmpty (theField, aMessage);
        return false;
    }
	else
	{
		if (stheField.length <= iMax)
			return true;
		else
		{
			//aMessage=String(iMax);
			//return warnInvalid (theField,iFieldLenMax,aMessage);
			return false;
		}
	}
}


function checkFieldLen(theField,iMax,iMin,emptyOK)
{
	var stheField=new String(theField.value);

    if (checkFieldLen.arguments.length == 3) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if ((isWhitespace(theField.value)) && (emptyOK == false))
    {
		//aMessage="Text";
        //return warnEmpty (theField, aMessage);
        return false;
    }
	else
	{
		if (!isFieldLenBetween(stheField,iMax,iMin,emptyOK))
		{
			//aMessage="[" + String(iMin) + ", " + String(iMax) + "]";
			//return warnInvalid (theField,iFieldLenBetween,aMessage);
			return false;
		}
		else
			return true;
	}
}



function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}




function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    if (!isInteger(s, false)) return false;
    var num = parseInt (s,10);
    return ((num >= a) && (num <= b));
}

function isIntegerGreaterThan(s, a)
{   if (isEmpty(s)) 
       if (isIntegerGreaterThan.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerGreaterThan.arguments[1] == true);

    if (!isInteger(s, false)) return false;


    var num = parseInt (s,10);
    return (num >= a);
}

function isIntegerLessThan(s, a)
{   if (isEmpty(s)) 
       if (isIntegerLessThan.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerLessThan.arguments[1] == true);

    if (!isInteger(s, false)) return false;

    var num = parseInt (s,10);
    return (num <= a);
}

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}



function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}



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 getDateDelimiter(model)
{
	var sDelimiter;
	
	if (isEmpty(model)) sDelimiter = "";
	else
	{
		sDelimiter = stripCharsNotInBag(model,dateDelimiters);
		sDelimiter = stripRedundancies(sDelimiter);
		// accept only a unique delimiter in a date string
		if ((sDelimiter.length==0) || (sDelimiter.length > 1)) sDelimiter = "";
	}
	return sDelimiter;
		
}
 
function isValidDate (theField,model,emptyOK)
{
	var sDate;
	var aTab;
	var delimiter;
	
    if (isValidDate.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value))
    {
	//alert("isWhitespace(theField.value)");
		//aMessage= "Date : " + theField.name; 
		//return warnEmpty (theField, aMessage);
		return false;
    }
    else
    {
		sDate = theField.value;
		
		delimiter=getDateDelimiter(sDate);
		if (isEmpty(delimiter)) 
		{//alert("isEmpty(delimiter)");
			//return warnInvalid (theField,iDate);
			return false;
		}
		
		aTab = sDate.split(delimiter);
		if (aTab.length!=3)
		{	alert("atab length != 3 ");
			//return warnInvalid (theField,iDate);
			return false;
		}
		else
		{
			if (!isSplitDateOK(aTab,model)) {
				//alert("isSplitDateOK(aTab,model)");
				return false;
			}
			else
			{
				theField.value = reformatDate(sDate,model);
				return true;
			}
		}
    }	
}

function checkAllDate (theField,model,emptyOK)
{
	var sDate;
	var aTab;
	var delimiter;
	
    if (checkAllDate.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value))
    {
		//aMessage= "Date : " + theField.name; 
		//return warnEmpty (theField, aMessage);
		return false;
    }
    else
    {
		sDate = theField.value;
		
		delimiter=getDateDelimiter(sDate);
		if (isEmpty(delimiter)) 
		{
			//return warnInvalid (theField,iDate);
			return false;
		}
		
		aTab = sDate.split(delimiter);
		if (aTab.length!=3)
		{
			//return warnInvalid (theField,iDate);
			return false;
		}
		else
		{
			if (!isSplitDateOK(aTab,StandardDateModel))
			{
				return false;
			}
			else
			{
				theField.value = reformatDate(sDate,model);
				return true;
			}
		}
    }	
}


function isSplitDateOK(aTab, model)
{
			switch (model)
			{
				case "DD/MM/YYYY" :
					if ((aTab[0].length==2) && (aTab[1].length==2) && (aTab[2].length==4))
					{
						if (!isDate(aTab[2], aTab[1], aTab[0]))
						{
							//return warnInvalid (theField,iDate);
							return false;
						}
						else
							return true;
					}
					break;	
				case "DD/MM/YY" :
					if ((aTab[0].length==2) && (aTab[1].length==2) && (aTab[2].length==2))
					{
						if (!isDate(aTab[2], aTab[1], aTab[0]))
						{
							//return warnInvalid (theField,iDate);
							return false;
						}
						else
							return true;
					}
					break;	
				
				case "DD/MM/YY or DD/MM/YYYY" :
					if ((aTab[0].length==2) && (aTab[1].length==2) && ((aTab[2].length==4)||(aTab[2].length==2)))
					{
						if (!isDate(aTab[2], aTab[1], aTab[0]))
						{
							//return warnInvalid (theField,iDate);
							return false;
						}
						else
							return true;
					}
					break;	
				
				case "DD/MM/YYYY or DD/MM/YY" :
					if ((aTab[0].length==2) && (aTab[1].length==2) && ((aTab[2].length==4)||(aTab[2].length==2)))
					{
						if (!isDate(aTab[2], aTab[1], aTab[0]))
						{
							//return warnInvalid (theField,iDate);
							return false;
						}
						else
							return true;
					}
					break;	

				case "MM/DD/YYYY" :
					if ((aTab[0].length==2) && (aTab[1].length==2) && (aTab[2].length==4))
					{
						if (!isDate(aTab[2], aTab[0], aTab[1]))
						{
							//return warnInvalid (theField,iDate);
							return false;
						}
						else
							return true;
					}
					break;	
				
				case "MM/DD/YY" :
					if ((aTab[0].length==2) && (aTab[1].length==2) && (aTab[2].length==2))
					{
						if (!isDate(aTab[2], aTab[0], aTab[1]))
						{
							//return warnInvalid (theField,iDate);
							return false;
						}
						else
							return true;
					}
					break;	
				
				case "MM/DD/YY or MM/DD/YYYY" :
					if ((aTab[0].length==2) && (aTab[1].length==2) && ((aTab[2].length==4)||(aTab[2].length==2)))
					{
						if (!isDate(aTab[2], aTab[0], aTab[1]))
						{
							//return warnInvalid (theField,iDate);
							return false;
						}
						else
							return true;
					}
					break;	
				
				case "MM/DD/YYYY or MM/DD/YY" :
					if ((aTab[0].length==2) && (aTab[1].length==2) && ((aTab[2].length==4)||(aTab[2].length==2)))
					{
						if (!isDate(aTab[2], aTab[0], aTab[1]))
						{
							//return warnInvalid (theField,iDate);
							return false;
						}
						else
							return true;
					}
					break;	

				default :
					//alert("Unable to validate the date for the model argument is not programmed");
					return false;
			}
			//aMessage="\n > Please, pay attention to the model which is "+model
			//return warnInvalid (theField,iDate,aMessage);			
			return false;
}


function isDate (year, month, day)
{   
	// catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year,10);
    var intMonth = parseInt(month,10);
    var intDay = parseInt(day,10);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}


function reformatDate(sDate, model)
{
	var dateDelimiter, modelDelimiter;
	var aDate, aModel;
	
	// à développer suivant le modèle
	dateDelimiter=getDateDelimiter(sDate);
	
	aModel = model.split(" or ");
	if (aModel.length >= 2) // case when model is composed of 2 alternative models
	{
		if (aModel[1].length > aModel[0].length)
			model = aModel[1];
		else
			model = aModel[0];
	}
	else // case when model is simple
		model = aModel[0];
	
	modelDelimiter=getDateDelimiter(model);

	aDate = sDate.split(dateDelimiter);
	aModel = model.split(modelDelimiter);
	
	if (model.length==8) // for instance: "DD/MM/YY"
	{
		if (aDate[2].length == 4)
			aDate[2] = aDate[2].slice(2,4);
		return (aDate.join(modelDelimiter));
		//return (reformat (sDate,"",2,modelDelimiter,2,modelDelimiter,2,""));
	}
	else if (model.length==10) // for instance: "DD/MM/YYYY"
	{
		if (aDate[2].length == 2)
			aDate[2] = new String(DATEBASE+parseInt(aDate[2],10));
		return (aDate.join(modelDelimiter));
		//return (reformat (sDate,"",2,modelDelimiter,2,modelDelimiter,4,""));
	}
	else // other case : ignore 
	{
		return (sDate);
	}
}

/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */


// Display prompt string s in status bar.

function prompt (a)
{   window.status = a;
}



// Display data entry prompt string s in status bar.

function promptEntry (a)
{   window.status = pEntryPrompt + a;
}


function warnEmpty (theField, a)
{   theField.focus();
    alert(mPrefix + a + mSuffix);
    return false;
}

function warnInvalid (theField, a, aOtherText)
{
	var str;
    theField.focus();
    theField.select();
    if (warnInvalid.arguments.length == 2) str="";
    else str=aOtherText;
    alert(a+str);
    return false;
}


function checkString (theField, a, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return false;
       //return warnEmpty (theField, a);
    else return true;
}

function checkStateCode (theField, emptyOK)
{   if (checkStateCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  
		theField.value = theField.value.toUpperCase();
		if (!isStateCode(theField.value, false)) 
			return false;
			//return warnInvalid (theField, iStateCode);
		else return true;
    }
}

function reformatZIPCode (ZIPString)
{   if (ZIPString.length == 5) return ZIPString;
    else return (reformat (ZIPString, "", 5, "-", 4));
}

function checkZIPCode (theField, emptyOK)
{   if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters);
      if (!isZIPCode(normalizedZIP, false)) 
		return false;
         //return warnInvalid (theField, iZIPCode);
      else 
      {  // if you don't want to insert a hyphen, comment next line out
         theField.value = reformatZIPCode(normalizedZIP);
         return true;
      }
    }
}


function reformatPhone (Phone)
{   
	if (LANGUAGE==US)
		return (reformat (Phone, "(", 3, ") ", 3, "-", 4));
	else if (LANGUAGE==FR)
		return (reformat (Phone, "",2, " ", 2, " ", 2, " ", 2, " ", 2));
	else
		return (reformat (Phone, "",2, " ", 2, " ", 2, " ", 2, " ", 2));
}


function checkPhone (theField, emptyOK)
{   if (checkPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters);
       if (!isPhoneNumber(normalizedPhone, false))
       {
			return false;
          //return warnInvalid (theField, iPhone);
       }
       else 
       {  // if you don't want to reformat the phone number, comment next line out
          //theField.value = reformatPhone(normalizedPhone);
          return true;
       }
    }
}



function checkInternationalPhone (theField, emptyOK)
{   if (checkInternationalPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  if (!isInternationalPhoneNumber(theField.value, false)) 
			return false;
          //return warnInvalid (theField, iWorldPhone);
       else return true;
    }
}

function checkPicture (theField, possibleExtensions, emptyOK)
{   if (checkPicture.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else 
    {
		if (!isPicture(theField.value, possibleExtensions, false)) 
		{
			//aMessage=possibleExtensions;
			return false;
			//return warnInvalid (theField, iPicture, aMessage);
		}
		else return true;
	}
}



function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
		return false;
       //return warnInvalid (theField, iEmail);
    else return true;
}



function checkUrl (theField, emptyOK)
{   if (checkUrl.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isUrl(theField.value, false)) 
       return false;
       //return warnInvalid (theField, iUrl);
    else return true;
}



function checkHttpUrl (theField, emptyOK)
{   if (checkHttpUrl.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isHttpUrl(theField.value, false)) 
       return false;
       //return warnInvalid (theField, iUrl);
    else return true;
}


function checkYear (theField, emptyOK)
{   if (checkYear.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isYear(theField.value, false)) 
       return false;
       // return warnInvalid (theField, iYear);
    else return true;
}



function checkMonth (theField, emptyOK)
{   if (checkMonth.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isMonth(theField.value, false)) 
       return false;
       //return warnInvalid (theField, iMonth);
    else return true;
}



function checkDay (theField, emptyOK)
{   if (checkDay.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isDay(theField.value, false)) 
       return false;
       // return warnInvalid (theField, iDay);
    else return true;
}

function checkDate (yearField, monthField, dayField, labelString, OKtoOmitDay)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkDate.arguments.length == 4) OKtoOmitDay = false;
    if (!isYear(yearField.value)) return warnInvalid (yearField, iYear);
    if (!isMonth(monthField.value)) return warnInvalid (monthField, iMonth);
    if ( (OKtoOmitDay == true) && isEmpty(dayField.value) ) return true;
    else if (!isDay(dayField.value))
		return false; 
       //return warnInvalid (dayField, iDay);
    if (isDate (yearField.value, monthField.value, dayField.value))
       return true;
    alert (iDatePrefix + labelString + iDateSuffix);
    return false;
}

function checkCaracteresSpeciaux(str) {
	if (reCaracteresNonSpeciaux.test(str)) {
		return false;
	}
	else {
		return true;
	}
}

// Get checked value from radio button.

function getRadioButtonValue (radio)
{   for (var i = 0; i < radio.length; i++)
    {   if (radio[i].checked) { break; }
    }
    return radio[i].value;
}

// This is the function that performs the verification of the form
// It must be invoked from the onSubmit attribut of the form tag
// The handler of that event should return whatever this function returns
function CheckForm(frm)
{
	var msg;
	var empty_fields="";
	var theFieldName;
	var fieldName;
	var isEmptyField;
	var debut;
	aErrors = "";
	aWarnings = "";
	var optionnel;
	var currentFieldEmpty
	for (var i=0; i < frm.length; i++)
	{
		currentFieldEmpty=false;
		var theField=frm.elements[i];
		
		isEmptyField = false;
		if ((theField.type=="text") || (theField.type=="textarea") || (theField.type=="password"))
		{			
			if (typeof theField.optional == "undefined") theField.optional = defaultEmptyOK;
			if (typeof theField.libelle=="undefined") {
				filedName = theField.name;
			}
			else {
				fieldName = theField.libelle;
			}
			theFieldName=" - " + fieldName + " : ";
			
			if (!theField.optional)
			{
				if (isEmpty(theField.value))
				{
					isEmptyField = true;
					empty_fields += "\n		" + fieldName;
					currentFieldEmpty=true;
					//continue;
				}
			}
			if (!currentFieldEmpty) {
				if (typeof theField.minSize == "undefined")
					theField.minSize=null;
				if (typeof theField.maxSize == "undefined")
					theField.maxSize=theField.maxlength;
				
				if ( (theField.minSize != null) || (theField.maxSize != null) )
				{
					var sTheField=theField.value;
					if (!isFieldLenBetween(sTheField,theField.maxSize, theField.minSize, theField.optional)) {
						aErrors += theFieldName + iFieldLen;
						if ((theField.minSize != null) && (theField.maxSize != null))
							aErrors += iGreaterThan + String(theField.minSize) + iConjonction + iLessThan + String(theField.maxSize) + ".\n";
						else {
							if (theField.minSize != null)
								aErrors += iGreaterThan + String(theField.minSize)+ ".\n";
							else {
								if (theField.maxSize != null)
									aErrors += iLessThan + String(theField.maxSize) + ".\n";
							}
						}
					}
				}		
				if (typeof theField.subType == "undefined")
				{
					aWarnings += theFieldName + iUndefinedSubType;
				}
				else
				{
					switch(theField.subType)
					{
	                    case 3 : //ST_ALPHABETIC:
	                            if (!isAlphabetic(theField.value,theField.optional))
	                                    aErrors += theFieldName + iAlphabetic;
	                            break;
	                    case 4 : //ST_ALPHANUMERIC:
	                            if (!isAlphanumeric(theField.value,theField.optional))                        
	                                    aErrors += theFieldName + iAlphanumeric;
	                            break;
	                    case 9 : //ST_WORD:
	                            if (!isWord(theField.value,theField.optional))                        
	                                    aErrors += theFieldName + iWord;
	                            break;
						case 5 : //ST_EMAIL:
							if (!checkEmail(theField, theField.optional))
								aErrors += theFieldName + iEmail;
							break;
						case 6 : //ST_PHONE:
							if (!checkPhone(theField,theField.optional))
								aErrors += theFieldName + iPhone;					
							break;
						case 7 : //ST_WORLDPHONE:
							if (!checkInternationalPhone(theField,theField.optional))
								aErrors += theFieldName + iWorldPhone;					
							break;
						case 0 : //ST_ALLDATE:
							if (!checkAllDate(theField,theField.favouriteModel,theField.optional))
							{
								aErrors += theFieldName + iDate;
								aErrors += iDateModelMismatch + theField.model + "\n";
							}
							break;
						case 1 : //ST_DATE:
							if (!isValidDate(theField,theField.model,theField.optional))
							{
								aErrors += theFieldName + iDate;
								aErrors += /*iDateModelMismatch +*/ theField.model + "\n";
							}
							break;
						case 8 : //ST_STATECODE:
							if (!checkStateCode(theField,theField.optional))
								aErrors += theFieldName + iStateCode;					
							break;
						case 2 : //ST_ZIPCODE:
							if (!checkZIPCode(theField,theField.optional))
								aErrors += theFieldName + iZIPCode;					
							break;
						case 10 : //ST_URL:
							if (!checkUrl(theField,theField.optional))
								aErrors += theFieldName + iUrl;					
							break;
						case 11 : //ST_HTTPURL:
							if (!checkHttpUrl(theField,theField.optional))
								aErrors += theFieldName + iHttpUrl;					
							break;
						case 12 : //ST_PICTURE:
							if (!checkPicture(theField,theField.possibleExtensions,theField.optional))
								aErrors += theFieldName + iPicture + theField.possibleExtensions;
							break;
						case 14 : //ST_FILE:
							if (!checkPicture(theField,theField.possibleFileExtensions,theField.optional)) {
								aErrors += theFieldName + iPicture + theField.possibleFileExtensions;
							}
							else {
								if (!checkCaracteresSpeciaux(theField.value)) {
									aErrors += theFieldName + iCaracteresSpeciaux;
								}
							}
							break;					
						case 13 : //ST_NUMERIC:
							if (!isEmpty(theField.value))
							{
								var theFieldValue = parseFloat(theField.value);
								if ((theField.min != null) || (theField.max != null))
								{
									if (isNaN(theFieldValue) ||
											((theField.min != null) && (theFieldValue < theField.min)) ||
											((theField.max != null) && (theFieldValue > theField.max)))
									{
										aErrors += theFieldName + iNumber;
										if (theField.min != null)
											aErrors += iGreaterThan + theField.min;
										if ((theField.min != null) && (theField.max != null))
											aErrors += iConjonction + iLessThan + theField.max;
										else if (theField.max != null)
											aErrors += iLessThan + theField.max;
										aErrors +=".\n";
									}
								}
								else {
									if (isNaN(theFieldValue))
									 	aErrors +=  theFieldName + iNumber;
								}
							}
							break;									
						case 15 : //ST_FREE:
							break;
					}
				}
			} //end if
		}
		else 
		{ var sType = theField.type;
			var fieldName;
		// cas boutons radio et checkbox
				if (sType=="radio") 
				{
					var theHiddenField=eval("frm.hBR_"+theField.name);
					if (typeof theHiddenField.libelle=="undefined") {
						fieldName = theField.name;
					}
					else {
						fieldName = theHiddenField.libelle;
					}
					if (typeof theHiddenField.optional=="undefined") theHiddenField.optional = defaultEmptyOK;
					if (!theHiddenField.optional)
					{
						if (isEmpty(theHiddenField.value))
						{
								empty_fields += "\n		" + fieldName //+ " (radio)";
							//continue;
						}
					}
					for (j=i+1;j<frm.length;j++)
					{
						if (frm.elements[j].name!=theField.name)
							break;
					}
					i=j-1;
				}
				if (sType.substring(0,6)=="select") 
				{

					var theHiddenField=eval("frm.h_"+theField.name);
					if (typeof theHiddenField.libelle=="undefined") {
						fieldName = theField.name;
					}
					else {
						fieldName = theHiddenField.libelle;
					}
					if (typeof theHiddenField.optional=="undefined") theHiddenField.optional = defaultEmptyOK;
					if (!theHiddenField.optional)
					{
						if (isEmpty(theHiddenField.value))
						{
								empty_fields += "\n		" + fieldName //+ " (radio)";
							//continue;
						}
				
					}
				} // end sType=select
				if (sType=="checkbox") 
				{
					//alert(theField.name);
					var theHiddenField=eval("frm.hCB_"+theField.name);
					if (typeof theHiddenField != "undefined") {
						if (typeof theHiddenField.libelle=="undefined") {
							fieldName = theField.name;
						}
						else {
							fieldName = theHiddenField.libelle;
						}			
						if (typeof theHiddenField.optional=="undefined") theHiddenField.optional = defaultEmptyOK;
						if (!theHiddenField.optional)
						{
							if (isEmpty(theHiddenField.value))
							{
									empty_fields += "\n		" + fieldName //+ " (radio)";
								//continue;
							}
						}
					}
				} // end sType=checkbox
				
		}	
	} // end for
	if (!empty_fields && !aErrors)
	{
		if (aWarnings)
			alert(iWarningFields + aWarnings);
		//alert("le formulaire sera envoye");
		return true;
	}
	var msg;
	
	msg = iHeadMessage;	
	if (empty_fields) {
		msg += iEmptyFields + empty_fields + "\n\n";
	}
	if (aErrors) {
		msg += aErrors+"\n\n";
	}
	if (aWarnings)
		msg += iWarningFields + aWarnings;
	msg += "\n";
	alert(msg);
	return false;
}
