// VARIABLE DECLARATIONS

var digits = "0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyzéèëêàâcáäùüûîçïæœ"

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÉÈËÊÀÂCÁÄÙÜÛÎÇÏÆŒ"

var whitespace = " \t\n\r";

var decimalPointDelimiter = "."

var phoneNumberDelimiters = "()- ";

var validWorldPhoneChars = digits + phoneNumberDelimiters + "+";

var digitsInUSPhoneNumber = 10;

var ZIPCodeDelimiters = "-";

var ZIPCodeDelimeter = "-"

var validZIPCodeChars = digits + ZIPCodeDelimiters

var digitsInZIPCode1 = 4
var digitsInZIPCode2 = 9

var creditCardDelimiters = " "

var defaultEmptyOK = false

/*function makeArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}*/

function ValEmail(email)
{
var r, re;
var s = email;
re =/\w+((-\w+)|(\.\w+)|(\_\w+))*\@[A-Za-z0-9]+((\.|_|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,5}/;
r = s.search(re);
  
return(r!=-1);
}


var daysInMonth = new Array(13);
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;

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    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;
    }

    // All characters are whitespace.
    return true;
}

function stripCharsInBag (s, bag)

{   var i;
    var 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;
}

function stripCharsNotInBag (s, bag)

{   var i;
    var 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;
}

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);
}

function isLetter (c)
{
if ((c.charCodeAt() >= 192) && (c.charCodeAt()<= 402 ) ) {
	return true;
} else {
return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) && (c == "-") && ( c =="'" ) && (c == ".") && ( c =="’") && (c=="—") )
}
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

function isInteger (s)

{   var i;

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }
	
	if (parseInt (s) < 1) return false;
    // All characters are numbers.
    return true;
}

function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}


function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isNegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNegativeInteger.arguments.length > 1)
        secondArg = isNegativeInteger.arguments[1];

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) );
}


function isNonpositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonpositiveInteger.arguments.length > 1)
        secondArg = isNonpositiveInteger.arguments[1];


    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) <= 0) ) );
}

function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isSignedFloat (s)

{   if (isEmpty(s)) 
       if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedFloat.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedFloat.arguments.length > 1)
            secondArg = isSignedFloat.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isFloat(s.substring(startPos, s.length), secondArg))
    }
}

function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
}

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}


function isZipCodeNew (s)

{   var i;


	if (s.length < 3 || s.length >8) return false;
	
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) || (c=="-") ) )
        return false;
    }
	

    // All characters are numbers or letters.
    return true;
}


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 isInternationalPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isInternationalPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isInternationalPhoneNumber.arguments[1] == true);
    return (isPositiveInteger(s))
}

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) ||
             (s.length == digitsInZIPCode2)))
}

function isStateCode(s)
{   if (isEmpty(s)) 
       if (isStateCode.arguments.length == 1) return defaultEmptyOK;
       else return (isStateCode.arguments[1] == true);
    return ( (USStateCodes.indexOf(s) != -1) &&
             (s.indexOf(USStateCodeDelimiter) == -1) )
}


function isEmail(str){
var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(str.match(emailRegEx)){
return true;
}else{
//alert('Please enter a valid email address.');
return false;
}
}
//function isEmail (s)
//{   if (isEmpty(s)) return false;
//   
//    // is s whitespace?
//    if (isWhitespace(s)) return false;
//    
//    // there must be >= 1 character before @, so we
//    // start looking at character position 1 
//    // (i.e. second character)
//    var i = 1;
//    var sLength = s.length;
//
//    // look for @
//    while ((i < sLength) && (s.charAt(i) != "@"))
//    { i++
//    }
//
//    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
//    else i += 2;
//
//    // look for .
//    while ((i < sLength) && (s.charAt(i) != "."))
//    { i++
//    }
//
//    // there must be at least one character after the .
//    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
//    else return true;
//}

function isYear (s)
{   
	s= parseInt(s);
   	eval("var year = document.forms[0].ServerYear.value");
	//var year = document.forms[0].getServerYear.value; // server year
	//alert(year +  " " + s);
	if ((s < 1850) || (s > parseInt(year))) return false;
	return true;
}

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);
    return ((num >= a) && (num <= b));
}


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 );
}


// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate(str){
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var curdate = new Date();
	var result = str.match(re);
	var d = parseInt(result[1],10);
	var m = parseInt(result[2],10);
	var y = parseInt(result[3]);

	eval("var year = document.forms[0].ServerYear.value");
		//alert(str +" d " + d + " m " + m + " y " + y + " year " + year);
	//var year = document.forms[0].getServerYear.value; // server year
		//alert(year);
	//alert(year);

	if(m == 2){
		var days = ((y % 4) == 0) ? 29 : 28;
	}else if(m == 4 || m == 6 || m == 9 || m == 11){
		var days = 30;
	}else{
		var days = 31;
	}
	if (d < 1 || d > days) return false;
	if(m < 1 || m > 12 || y < 1850 || y > parseInt(year)) return false;

return true;
}


//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) && (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;
//}




/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */


// Display prompt string s in status bar.

function prompt (s)
{   window.status = s
}



// Display data entry prompt string s in status bar.

function promptEntry (s)
{   window.status = pEntryPrompt + s
}




// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}



// 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 warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}




/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */

function checkString (theField, s, 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 warnEmpty (theField, s);
    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 warnInvalid (theField, iZIPCode);
      else 
      {  // if you don't want to insert a hyphen, comment next line out
         theField.value = reformatZIPCode(normalizedZIP)
         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 warnInvalid (theField, iWorldPhone);
       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 warnInvalid (theField, iEmail);
    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 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 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 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 warnInvalid (dayField, iDay);
    if (isDate (yearField.value, monthField.value, dayField.value))
       return true;
    alert (iDatePrefix + labelString + iDateSuffix)
    return false
}

function getRadioButtonValue (radio)
{   for (var i = 0; i < radio.length; i++)
    {   if (radio[i].checked) { break }
    }
    return radio[i].value
}

function checkCreditCard (radio, theField)
{   var cardType = getRadioButtonValue (radio)
    var normalizedCCN = stripCharsInBag(theField.value, creditCardDelimiters)
    if (!isCardMatch(cardType, normalizedCCN)) 
       return warnInvalid (theField, iCreditCardPrefix + cardType + iCreditCardSuffix);
    else 
    {  theField.value = normalizedCCN
       return true
    }
}


function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()


function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()


function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()


function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isAmericanExpress()


function isDinersClub(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return isCreditCard(cc);
  return false;
}

function isCarteBlanche(cc)
{
  return isDinersClub(cc);
}

function isDiscover(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isDiscover()


function isEnRoute(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 15) &&
      ((first4digs == "2014") ||
       (first4digs == "2149")))
    return isCreditCard(cc);
  return false;
}


function isJCB(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) &&
      ((first4digs == "3088") ||
       (first4digs == "3096") ||
       (first4digs == "3112") ||
       (first4digs == "3158") ||
       (first4digs == "3337") ||
       (first4digs == "3528")))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isJCB()


function isAnyCard(cc)
{
  if (!isCreditCard(cc))
    return false;
  if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) &&
      !isDiscover(cc) && !isEnRoute(cc) && !isJCB(cc)) {
    return false;
  }
  return true;

} // END FUNCTION isAnyCard()


function isCardMatch (cardType, cardNumber)
{

	cardType = cardType.toUpperCase();
	var doesMatch = true;

	if ((cardType == "VISA") && (!isVisa(cardNumber)))
		doesMatch = false;
	if ((cardType == "MASTERCARD") && (!isMasterCard(cardNumber)))
		doesMatch = false;
	if ( ( (cardType == "AMERICANEXPRESS") || (cardType == "AMEX") )
                && (!isAmericanExpress(cardNumber))) doesMatch = false;
	if ((cardType == "DISCOVER") && (!isDiscover(cardNumber)))
		doesMatch = false;
	if ((cardType == "JCB") && (!isJCB(cardNumber)))
		doesMatch = false;
	if ((cardType == "DINERS") && (!isDinersClub(cardNumber)))
		doesMatch = false;
	if ((cardType == "CARTEBLANCHE") && (!isCarteBlanche(cardNumber)))
		doesMatch = false;
	if ((cardType == "ENROUTE") && (!isEnRoute(cardNumber)))
		doesMatch = false;
	return doesMatch;

}  // END FUNCTION CardMatch()

function isIntegerLengthMore10(f){
	if (f.length <= 9) return false;
	return true;
}

function isLengthMore3(f){
	//alert(f.length);
	if (f.length <= 2) return false;
	return true;
}
function isIntegerMore3(f){
	//alert(f.length);
	if (f.length <= 2) return false;
	return true;
}

function isPeriod(s){   
	if (parseInt(s,10) > 60 || parseInt(s,10) < 1 ) return false;
    // period between 1 and 60
    return true;
}

function isCheck(s){
	//alert(s);
	//alert(document.forms[0].var_conditionGenerale.checked);
	if (document.forms[0].checkboxJaccepte.checked){
		return true;
		}
		return false;
}
// return true 
function isFirstSelect(str){
	return eval("document.forms[0]." + str + ".options[0].selected");
}
// end dunc


function ValidateField2(FieldName1,FieldName2,functionName){
    f = null;
	FieldName1 = FieldName1.toString();
	FieldName2 = FieldName2.toString();
	eval("f1 = document.forms[0]." + FieldName1 + ".value"); //the form must be set here
	eval("f2 = document.forms[0]." + FieldName2 + ".value"); //the form must be set here

	var testVariable = false;
	
	switch (functionName){
			case "isEqual": 
			if ((f1.toLowerCase == f2.toLowerCase)){
			testVariable = true;
			testVariable = !isEmpty(f2) && isEmail(f2);
			} else
			{
			 testVariable = false;
			}
			break;
			default:
			testVariable = false;
	}
	
	
	
	if (testVariable == true){
	document.getElementById('span_' + FieldName2).innerHTML = "<img src='images/valid.gif' />";
	//alert("valid > " + document.getElementById('s_' + FieldName));
	return true;
	}
	else {
	document.getElementById('span_' + FieldName2).innerHTML = "<img src='images/invalid.gif' />";
	//alert("invalid > " + document.getElementById('s_' + FieldName));
	return false;
		}
	
}


function ValidateField(FieldName,functionName){
    f = null;
	FieldName = FieldName.toString();
	eval("f = document.forms[0]." + FieldName + ".value"); //the form must be set here
	var testVariable = false;
	
	switch (functionName){
			case "isEmpty": 
			testVariable = !isEmpty(f);
			break;
			case "isAlpha":
			testVariable = !isEmpty(f) && isAlpha(stripWhitespace(f));
			break;
			case "isInteger":
			testVariable = !isEmpty(f) && isInteger(stripWhitespace(f));
			break;
			case "isIntegerLengthMore10":
			testVariable = !isEmpty(f) && isIntegerLengthMore10(stripWhitespace(f));
			break;
			case "isLengthMore3":
			testVariable = !isEmpty(f) && isLengthMore3(stripWhitespace(f));
			break;
			case "isIntegerMore3":
			testVariable = !isEmpty(f) && isInteger(stripWhitespace(f)) && isIntegerMore3(stripWhitespace(f));
			break;
			case "isPeriod":
			testVariable = !isEmpty(f) && isPeriod(stripWhitespace(f)) && isInteger(stripWhitespace(f));
			break;
			case "isCheck":
			testVariable = isCheck(FieldName);
			break;
			case "isAlphanumeric":
			testVariable= !isEmpty(f) && isAlphanumeric(stripWhitespace(f));
			break;
			case "isZipCode2":
			testVariable= !isEmpty(f) && isZipCodeNew(stripWhitespace(f));
			break;
			case "isFirstSelect":
			testVariable= !isFirstSelect(FieldName);
			break;
			case "isLength":
			testVariable = isLength(f);
			break;
			case "isLenghtBetween":
			testVariable = isLengthBetween(f);
			break;
			case "isDate":
			testVariable = isDate(f);
			break;
			case "isDay":
			testVariable = isDay(f);
			break;
			case "isMonth":
			testVariable = isMonth(f);
			break;
			case "isYear":
			testVariable = isYear(f) && isInteger(stripWhitespace(f));
			break;
			case "isNotMatch":
			testVariable = !isMatch(f);
			break;
			case "isEmail":
			testVariable = !isEmpty(f) && isEmail(f);
			break;
			default:
			testVariable = false;
	}
	//testVariable = functionName(f.value);
	if (testVariable == true){
	document.getElementById('span_' + FieldName).innerHTML = "<img src='images/valid.gif' />";
	//alert("valid > " + document.getElementById('s_' + FieldName));
	return true;
	}
	else {
	document.getElementById('span_' + FieldName).innerHTML = "<img src='images/invalid.gif' />";
	//alert("invalid > " + document.getElementById('s_' + FieldName));
	return false;
		}
}





function ValidateFieldPages(FieldName,functionName){
    f = null;
	FieldName = FieldName.toString();
	eval("f = document.forms[0]." + FieldName + ".value"); //the form must be set here
	var testVariable = false;
	
	switch (functionName){
			case "isEmpty": 
			testVariable = !isEmpty(f);
			break;
			case "isAlpha":
			testVariable = !isEmpty(f) && isAlpha(stripWhitespace(f));
			break;
			case "isInteger":
			testVariable = !isEmpty(f) && isInteger(stripWhitespace(f));
			break;
			case "isIntegerLengthMore10":
			testVariable = !isEmpty(f) && isIntegerLengthMore10(stripWhitespace(f));
			break;
			case "isPeriod":
			testVariable = !isEmpty(f) && isPeriod(stripWhitespace(f)) && isInteger(stripWhitespace(f));
			break;
			case "isCheck":
			testVariable = isCheck(FieldName);
			break;
			case "isAlphanumeric":
			testVariable= !isEmpty(f) && isAlphanumeric(stripWhitespace(f));
			break;
			case "isZipCode2":
			testVariable= !isEmpty(f) && isZipCodeNew(stripWhitespace(f));
			break;
			
			case "isFirstSelect":
			testVariable= !isFirstSelect(FieldName);
			break;
			case "isLength":
			testVariable = isLength(f);
			break;
			case "isLenghtBetween":
			testVariable = isLengthBetween(f);
			break;
			case "isDate":
			testVariable = isDate(f);
			break;
			case "isDay":
			testVariable = isDay(f);
			break;
			case "isMonth":
			testVariable = isMonth(f);
			break;
			case "isYear":
			testVariable = isYear(f) && isInteger(stripWhitespace(f));
			break;
			case "isNotMatch":
			testVariable = !isMatch(f);
			break;
			case "isEmail":
			testVariable = !isEmpty(f) && isEmail(f);
			break;
			default:
			testVariable = false;
	}
	//testVariable = functionName(f.value);
	if (testVariable == true){
	document.getElementById('span_' + FieldName).innerHTML = "<img src='../images/valid.gif' />";
	//alert("valid > " + document.getElementById('s_' + FieldName));
	}
	else {
	document.getElementById('span_' + FieldName).innerHTML = "<img src='../images/invalid.gif' />";
	//alert("invalid > " + document.getElementById('s_' + FieldName));
		}
}



function validform(){
	var valid = true;
	var str = ""; 
	//var elements = document.getElementsByTagName('input'); 
	eval("var elements = document." + arguments[0] + ".elements")

 	for(var i = 0; i < elements.length; i++) { 
		if (elements.item(i).name.indexOf("_") > 0){
			valtype = elements.item(i).name.split("_")[1]
			switch (valtype) {
				case "cn" :
					if (!isAlphanumeric(stripWhitespace(elements.item(i).value))){
						elements.item(i).style.background = "#FF99FF";
						valid = valid && false;
					}
					break
				case "zip" :
					if (!isZIPCode(stripWhitespace(elements.item(i).value))){
						elements.item(i).style.background = "#FF99FF";
						valid = valid && false;
					}
					break
				case "email" :
					if (!ValEmail(stripWhitespace(elements.item(i).value))){
						elements.item(i).style.background = "#FF99FF";
						valid = valid && false;
					}
					break	
				case "int" :
					if (!isInteger(stripWhitespace(elements.item(i).value))){
						elements.item(i).style.background = "#FF99FF";
						valid = valid && false;
					}
					break	
				case "fl" :
					if (!isFloat(stripWhitespace(elements.item(i).value))){
						elements.item(i).style.background = "#FF99FF";
						valid = valid && false;
					}
					break
				case "ne" :
					if (stripWhitespace(elements.item(i).value) == "") {
						elements.item(i).style.background = "#FF99FF";
						valid = valid && false;
					}
					break
			}
		}
	}
	if (!valid) { alert("The red fields have invalid values!") }
	return valid;
}
