var defaultEmptyOK = false
var is = new Is();

//****************************************************************************************
// BOI, followed by one or more digits, followed by EOI.
var reInteger = /^\d+$/
var reSignedInteger = /^[+|-]?\d+$/
//****************************************************************************************

//****************************************************************************************
// BOI, followed by one or more characters, followed by @,
// followed by one or more characters, followed by ., 
// followed by one or more characters, followed by EOI.
var reEmail = /^.+\@.+\..+$/
//****************************************************************************************

//****************************************************************************************
// U.S. phone numbers have 10 digits.
// They are formatted as 123 456 7890 or (123) 456-7890.
var digitsInUSPhoneNumber = 10;
//****************************************************************************************

//****************************************************************************************
// Postal Code have 6 characters.
// They are formatted as J7E3X1.
var ncharsInPostalCode = 6;
//****************************************************************************************

//****************************************************************************************
// Characters allowed in a U.S. phone numbers
var allowedInUSPhoneNumber = "()- ";
//****************************************************************************************

//****************************************************************************************
// Characters allowed in a Postal Code
var allowedInPostalCode = " ";
//****************************************************************************************

//****************************************************************************************
// Days in each month of the year
var daysInMonth = new Array(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
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;
//****************************************************************************************

//****************************************************************************************
// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}
//****************************************************************************************

// whitespace characters
var whitespace = " \t\n\r";

// Returns true if string s is empty or whitespace characters only.
function isWhitespace (s) {
	var i;
	// Is s empty?
	if (isEmpty(s)) return true;
	 // Check whitespaces
	for (i = 0; i < s.length; i++) {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}

//****************************************************************************************
// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true
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)
}
//****************************************************************************************

//****************************************************************************************
// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true 
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      true
function isSignedInteger (s)
{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    
    else {
       return reSignedInteger.test(s)
    }
}
//****************************************************************************************

//****************************************************************************************
// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer >= 0.
function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.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 (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}
//****************************************************************************************

//****************************************************************************************
// isUSPhoneNumber (STRING s [, BOOLEAN emptyOK])
// 
// isUSPhoneNumber returns true if string s is a valid U.S. Phone
// Number.  Must be 10 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, parentheses, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isUSPhoneNumber (s)
{
   s = stripCharsInBag(s, allowedInUSPhoneNumber)
   if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}
//****************************************************************************************


//****************************************************************************************
// Fonction qui enlève certain(s) caractère(s) d'une chaine de caractères
//
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;
}
//****************************************************************************************

//****************************************************************************************
// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    
    else {
       return reEmail.test(s)
    }
}
//****************************************************************************************

//****************************************************************************************
// isYear (STRING s [, BOOLEAN emptyOK])
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 2 or 4 digits only.
// 
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but 
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
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));   
}
//****************************************************************************************

//****************************************************************************************
// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// 
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.
function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.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 = parseInt (s);    
    return ((num >= a) && (num <= b));    
}
//****************************************************************************************

//****************************************************************************************
// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
function isMonth (s)
{   		
	if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}
//****************************************************************************************

//****************************************************************************************
// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}
//****************************************************************************************

//****************************************************************************************
// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.
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 );
}
//****************************************************************************************

//****************************************************************************************
// isDate (STRING year, STRING month, STRING day)
//
// isDate returns true if string arguments year, month, and day 
// form a valid date.
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);
    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;
}
//****************************************************************************************

//****************************************************************************************
// DisabledField (INPUTOBJECT objElem, BOOLEAN disabledElem)
// check the Browser and disabled the field
function DisabledField(objElem,disabledElem)
{
	if (is.ie || is.ns6){
		objElem.disabled = disabledElem;	
	}
	else{
		if(disabledElem == false){
			objElem.onfocus = "";
		}
		else{
			objElem.onfocus = BlurElement;
		}
	}
}

//****************************************************************************************
// BlurElement (WINDOWOBJECT e)
// get the currently focus field and unfocus it
function BlurElement (e)
{	
 e.target.blur();
}
//****************************************************************************************

//****************************************************************************************
// Is()
// BROWSER SNIFFER (Sniff out the good and bad browsers)
function Is() {
    var agent = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.ns  = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
    this.ns2 = (this.ns && (this.major == 2));
    this.ns3 = (this.ns && (this.major == 3));
    this.ns4b = (this.ns && (this.minor < 4.04));
    this.ns4 = (this.ns && (this.major >= 4 && this.major < 5));
    this.ns6 = (this.ns && (this.major >= 5));
    this.ie   = (agent.indexOf("msie") != -1);
    this.ie3  = (this.ie && (this.major == 2));
    this.ie4  = (this.ie && (this.major >= 4));
    this.op3 = (agent.indexOf("opera") != -1);
    this.win   = (agent.indexOf("win")!=-1);
    this.mac   = (agent.indexOf("mac")!=-1);
    this.unix  = (agent.indexOf("x11")!=-1);
}
//****************************************************************************************

function isPostalCode(s)
{
	
	var part1 = false;
	
	s = stripCharsInBag(s, allowedInPostalCode)
	
	if (s.length == 6)
	{
		//Validation du code postal
		strcode = s.toUpperCase()
		car = strcode.charAt(0);
			
		if (car >= "A" && car <= "Z")
		{
			part1 = true;
		}else
		{
			part1 = false;
		}

		car = strcode.charAt(1);
		if ((part1 == true) && (car >= "0" && car <= "9"))
		{
			part1 = true;
		}else
		{
			part1 = false;
		}

		car = strcode.charAt(2);
		if ((part1 == true) && (car >= "A" && car <= "Z"))
		{
			part1 = true;
		}else
		{
			part1 = false;
		}

		car = strcode.charAt(3);
			
		if ((part1 == true) && (car >= "0" && car <= "9"))
		{
			part1 = true;
		}else
		{
			part1 = false;
		}

		car = strcode.charAt(4);
		if ((part1 == true) && (car >= "A" && car <= "Z"))
		{
			part1 = true;
		}else
		{
			part1 = false;
		}

		car = strcode.charAt(5);
		if ((part1 == true) && (car >= "0" && car <= "9"))
		{
			part1 = true;
		}else
		{
			part1 = false;
		}
			
		//Ensemble du code postal
		if (part1 == true)
		{
			return true;
		}else
		{
			return false;
		}
			
	}else
	{
		return false;
	}

}

function LeapYear(yr)
{
/* Is it a leap year?
   1.Years divisible by 4 are leap years, but
   2.Years divisible by 100 are not leap years, but
   3.Years divisible by 400 are leap years. */

if (((yr % 4 == 0) && yr % 100 != 0) || yr % 400 == 0)
   return true;
else
   return false;
}

function IsIntlDate(InString)
{
/* Note: Date must be in the format YYYY/MM/DD*/

/* Allow empty fields as dates. */

   if (InString.value.length >0)
   {
      Slashes   = 0;
      Month     = 0;
      Day       = 0;
      Year      = 0;
      RefString = "01234567890-/";

      for (i=0; i<InString.value.length; i++)
      {
         TempChar = InString.value.substring(i, i +1);

         /* Invalid character? */

         if (RefString.indexOf(TempChar,0) == -1) {  return (false); }

         /* Must have two slashes */

         if ( TempChar == "-" || TempChar == "/") {Slashes++; }

      } /* end for */

      if ( Slashes != 2 ) { return (false); }


      /* Parse out the date pieces */

      i =  0;
      x = "";

      /* Year */
      x = "";
      while ((InString.value.charAt(i) != "-") && (InString.value.charAt(i) != "/") && (i <= InString.value.length))
      {
        x = x +  InString.value.charAt(i);
        i++;
      }
      Year = x;
	  if (x.length != 4) {return(false);}
      if (( Year < 1900 ) || ( Year > 3000 )) { return (false); }


      i++; // Skip the slash
      /* Month */
	  x="";
      while ((InString.value.charAt(i) != "-") && (InString.value.charAt(i) != "/") && (i <= InString.value.length))
      {
        x = x +  InString.value.charAt(i);
        i++;
      }
      Month = x;  // Rely on implicit conversion of char string x to a number

	  if (x.length != 2) {return(false);}

      if (( Month < 1 ) || ( Month > 12 )) { return (false); }


      /* Day */
      i++; // Skip the slash
      x = "";
      while ((InString.value.charAt(i) != "-") && (InString.value.charAt(i) != "/") && (i <= InString.value.length))
      {
        x = x +  InString.value.charAt(i);
        i++;
      }
      Day = x;
	  if (x.length != 2) {return(false);}
      if (( Day < 1 ) || ( Day > 31 )) {return (false); }

      /* Check Day a bit more closely */
      if (( Month == 4 || Month == 6 || Month == 9 || Month == 11 ) && ( Day > 30 ))
      {
         return ( false );
      }



      if ( Month == 2)
      /* Check leap year */
      {
         /* Is it a leap year?
            1.Years divisible by 4 are leap years, but
            2.Years divisible by 100 are not leap years, but
            3.Years divisible by 400 are leap years. */

         if ( LeapYear(Year) )
         {
            if ( Day > 29 ) { return ( false ); }
         }
         else
         {
            if ( Day > 28 ) { return ( false ); }
         }

      }
   }

   return ( true );
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
