var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz' "
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ' "

var whitespace = " \t\n\r";

var decimalPointDelimiter = ".";
var signNumberDelimiter = "+-";

var phoneNumberDelimiters = "()- ";
var SSNDelimiters = "- ";
var ZIPCodeDelimiters = "-";
var CAZIPCodeDelimiters = " ";
var creditCardDelimiters = " ";

var validUSPhoneChars = digits + phoneNumberDelimiters;
var validWorldPhoneChars = digits + phoneNumberDelimiters + "+";
var validSSNChars = digits + SSNDelimiters;
var validZIPCodeChars = digits + ZIPCodeDelimiters;

var digitsInSocialSecurityNumber = 9;
var digitsInUSPhoneNumber = 10;
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
var digitsInCAZIPCode = 6;

// m is an abbreviation for "missing"
var mPrefix = "Please enter your ";
var mSuffix = ".\n";

// s is an abbreviation for "string"
var sUSLastName = "Last Name";
var sUSFirstName = "First Name";
var sWorldLastName = "Family Name";
var sWorldFirstName = "Given Name";
var sTitle = "Title";
var sCompanyName = "Company Name";
var sUSAddress = "Street Address";
var sWorldAddress = "Address";
var sCity = "City";
var sStateCode = "State Code";
var sWorldState = "State, Province, or Prefecture";
var sCountry = "Country";
var sZIPCode = "ZIP Code";
var sWorldPostalCode = "Postal Code";
var sPhone = "Phone Number";
var sFax = "Fax Number";
var sDateOfBirth = "Date of Birth";
var sExpirationDate = "Expiration Date";
var sEmail = "Email";
var sSSN = "Social Security Number";
var sCreditCardNumber = "Credit Card Number";
var sOtherInfo = "Other Information";

// i is an abbreviation for "invalid"
var iReenter = "Please reenter it now.\n";
var iThe = "The ";
var iStateCode = " field must be a valid two character U.S. state abbreviation (like CA for California).  " + iReenter;
var iZIPCode = " field must be a 5 or 9 digit U.S. ZIP Code (like 94043).  " + iReenter;
var iCAZIPCode = " field must be a 7 character Canadian ZIP Code (like G0L 4A0).  " + iReenter;
var iPostalCode = " field must be alphanumeric.  " + iReenter;
var iUSPhone = " field must be a 10 digit U.S. phone number (like 415 555 1212).  " + iReenter;
var iWorldPhone = " field must be a valid international phone number.  " + iReenter;
var iSSN = " field must be a 9 digit U.S. social security number (like 123 45 6789).  " + iReenter;
var iEmail = " field must be a valid email address (like email@isp.com).  " + iReenter;
var iCreditCardPrefix = "This is not a valid ";
var iCreditCardSuffix = " credit card number. (Click the link on this form to see a list of sample numbers.)  " + iReenter;
var iDay = " field must be a day number between 1 and 31.  " + iReenter;
var iMonth = " field must be a month number between 1 and 12.  " + iReenter;
var iYear = " field must be a 2 or 4 digit year number.  " + iReenter;
var iDatePrefix = "The Day, Month, and Year for ";
var iDateSuffix = " do not form a valid date.  " + iReenter;

// p is an abbreviation for "prompt"
var pEntryPrompt = "Please enter a ";
var pStateCode = "2 character code (like CA).";
var pZIPCode = "5 or 9 digit U.S. ZIP Code (like 94043).";
var pCAZIPCode = "7 character Canadian ZIP Code (like G0L 4A0).";
var pUSPhone = "10 digit U.S. phone number (like 415 555 1212).";
var pWorldPhone = "international phone number.";
var pSSN = "9 digit U.S. social security number (like 123 45 6789).";
var pEmail = "valid email address (like foo@bar.com).";
var pCreditCard = "valid credit card number.";
var pDay = "day number between 1 and 31.";
var pMonth = "month number between 1 and 12.";
var pYear = "2 or 4 digit year number.";

var USStateCodeDelimiter = "|";
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP";

var defaultEmptyOK = false;  //make info required by default

/*  ================================================================
    FUNCTIONS: Common Functions
    
      defaultEmptyOKCheck(f, c)  Check whether an empty field is ok
      makeArray(n)               Creates an array with length n
      checkSign(s)               Checks the sign of the input string 
    ================================================================

function defaultEmptyOKCheck(f, c) {
  if (f.length == c) //f.arguments.length
    return defaultEmptyOK;
  else
    return (f.arguments[c]);
}
*/
function makeArray(n) {
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0;
   } 
   return this;
}
function checkSign(s) {
  var c = s.charAt(0);
  if (signNumberDelimiter.indexOf(c) != -1)
    return 1;
  else
    return 0;    
}
/*  ================================================================ */

var daysInMonth = makeArray(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;

/*  ================================================================
    FUNCTIONS: Functions that check the validity of the inputs
    
      isEmpty (s)                Check whether value is empty
      isLetter (c)               Check whether character c is an English letter 
      isDigit (c)                Check whether character c is a digit 
      isLetterOrDigit (c)        Check whether character c is a letter or digit.
      isWhitespace (s)           Check whether string s is empty or whitespace.
*     isInteger (s)              True if all characters in string s are numbers.
*     isSignedInteger (s)        True if all characters in string s are numbers; leading + or - allowed.
*     isPositiveInteger (s)      True if string s is an integer > 0.
*     isNonnegativeInteger (s)   True if string s is an integer >= 0.
*     isNegativeInteger (s)      True if string s is an integer < 0.
*     isNonpositiveInteger (s)   True if string s is an integer <= 0.
*     isFloat (s)                True if string s is an unsigned floating point (real) number. (Integers also OK.)
*     isSignedFloat (s)          True if string s is a floating point number; leading + or - allowed. (Integers also OK.)

*     isAlphabetic (s)           True if string s is English letters 
*     isAlphanumeric (s)         True if string s is English letters and numbers only.
*     isSSN (s)                  True if string s is a valid U.S. Social Security Number.
*     isUSPhoneNumber (s)        True if string s is a valid U.S. Phone Number.
*     isInternationalPhoneNumber (s) True if string s is a valid international phone number.
*     isZIPCode (s)              True if string s is a valid U.S. ZIP code.
*     isStateCode (s)            True if string s is a valid U.S. Postal Code
*     isEmail (s)                True if string s is a valid email address.
*     isIntegerInRange (s, a, b) True if string s is an integer between a and b, inclusive.
*     isYear (s)                 True if string s is a valid Year number.
*     isMonth (s)                True if string s is a valid month between 1 and 12.
*     isDay (s)                  True if string s is a valid day between 1 and 31.
      daysInFebruary (year)      Returns number of days in February of that year.
      isDate (year, month, day)  True if string arguments form a valid date.

      isCreditCard(st)           True if credit card number passes the Luhn Mod-10 test.
      isVisa(cc)                 True if string cc is a valid VISA number.             4111 1111 1111 1111 (16 digits)
      isMasterCard(cc)           True if string cc is a valid MasterCard number.       5500 0000 0000 0004 (16 digits)
      isAmericanExpress(cc)      True if string cc is a valid American Express number. 340000000000009 (15 digits)
      isDinersClub(cc)           True if string cc is a valid Diner's Club number.     30000000000004 (14 digits)
      isCarteBlanche(cc)         True if string cc is a valid Carte Blanche number.
      isDiscover(cc)             True if string cc is a valid Discover card number.    6011000000000004 (16 digits)
      isEnRoute(cc)              True if string cc is a valid enRoute card number.     201400000000009 (15 digits)
      isJCB(cc)                  True if string cc is a valid JCB card number.
      isAnyCard(cc)              True if string cc is a valid card number for any of the accepted types.
      isCardMatch (t, n) True if Number is valid for credic card of type Type.
  
    INPUT:     s

    RETURNS:   true, if valid:  false, otherwise
    ================================================================ */
function isEmpty (s)         { return ((s == null) || (s.length == 0)); }
function isLetter (c)        { return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || (c == ' ') ); }
function isDigit (c)         { return ((c >= "0") && (c <= "9")); }
function isLetterOrDigit (c) { return (isLetter(c) || isDigit(c)); }
function isWhitespace (s) {
  if (isEmpty(s)) return true;
  for (var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) return false;
  }
  return true;
}
function isInteger (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  
  for (var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (!isDigit(c)) return false;
  }
  return true;
}
function isSignedInteger (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return (isInteger(s.substring(checkSign(s), s.length), r) );
}
function isPositiveInteger (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r
  return (isSignedInteger(s, r) && (parseInt (s) > 0) );
}
function isNonnegativeInteger (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return (isSignedInteger(s, r) && (parseInt (s) >= 0) );
}
function isNegativeInteger (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return (isSignedInteger(s, r) && (parseInt (s) < 0) );
}
function isNonpositiveInteger (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return (isSignedInteger(s, r) && (parseInt (s) <= 0) );
}
function isFloat (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r
  if (s == decimalPointDelimiter) return false;

  var seenDecimalPoint = false;
  for (var i = 0; i < s.length; i++) {   
    var c = s.charAt(i);

    if ((c == decimalPointDelimiter) && !seenDecimalPoint) 
      seenDecimalPoint = true;
    else if (!isDigit(c)) 
      return false;
  }
  return true;
}
function isSignedFloat (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return (isFloat(s.substring(checkSign(s), s.length), r))
}
function isAlphabetic (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;

  for (var i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if (!isLetter(c))
      return false;
  }
  return true;
}
function isAlphanumeric (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;

  for (var i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if (! isLetterOrDigit(c) )
      return false;
  }
  return true;
}
function isSSN (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return (isInteger(s) && s.length == digitsInSocialSecurityNumber)
}
function isUSPhoneNumber (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}
function isInternationalPhoneNumber (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return (isPositiveInteger(s))
}
function isZIPCode (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return (isInteger(s) && ((s.length == digitsInZIPCode1) || (s.length == digitsInZIPCode2)))
}
function isCAZIPCode (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;

  var b = true;
  for (var i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if (b) {
      if (! isLetter(c))
        return false;
    } else {
      if (! isDigit(c))
        return false;
    }
    b = !b;
  }

  return (s.length == digitsInCAZIPCode)
}
function isStateCode(s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return ( (USStateCodes.indexOf(s) != -1) && (s.indexOf(USStateCodeDelimiter) == -1) )
}
function isEmail (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  if (isWhitespace(s)) return false;
    
  // there must be >= 1 character before @, so we start looking at character position 1
  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 isIntegerInRange (s, a, b, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  if (!isInteger(s, false)) return false;

  var num = parseInt (s);
  return ((num >= a) && (num <= b));
}
function isYear (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  if (!isNonnegativeInteger(s)) return false;
  return ((s.length == 2) || (s.length == 4));
}
function isMonth (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  return isIntegerInRange (s, 1, 12);
}
function isDay (s, r) {
  if (r == null) { r = defaultEmptyOK; }
  if (isEmpty(s)) return r;
  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 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;
}
function isCreditCard(st) {
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (var 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);
}
function isVisa(cc) { 
  if (((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}
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;
}
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;
}
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;
}
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;
} 
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;
}
function isCardMatch (t, n)
{
  t = t.toUpperCase();

  if ((t == "VISA") && (!isVisa(n))) return false;
  if ((t == "MASTERCARD") && (!isMasterCard(n))) return false;
  if (((t == "AMERICANEXPRESS") || (t == "AMEX")) && (!isAmericanExpress(n))) return false;
  if ((t == "DISCOVER") && (!isDiscover(n))) return false;
  if ((t == "JCB") && (!isJCB(n))) return false;
  if ((t == "DINERS") && (!isDinersClub(n))) return false;
  if ((t == "CARTEBLANCHE") && (!isCarteBlanche(n))) return false;
  if ((t == "ENROUTE") && (!isEnRoute(n))) return false;
  return true;
}
/*  ================================================================  */
/*  ================================================================
    FUNCTIONS: Functions that perform data formating
    
      stripCharsInBag (s, bag)   Removes all characters which appear in string bag from string s.
      stripCharsNotInBag (s, bag)Removes all characters which do NOT appear in string bag from string s.
      stripWhitespace (s)        Removes all whitespace characters from s.
      stripInitialWhitespace (s) Removes initial (leading) whitespace characters from s.
      reformat (s)               Inserts formatting characters or delimiters into TARGETSTRING.
      reformatZIPCode (s)        If 9 digits, inserts separator hyphen.
      reformatUSPhone (s)        Reformats as (123) 456-789.
      reformatSSN (s)            Reformats as 123-45-6789.
               
    INPUT:     s

    RETURNS:   formated string
    ================================================================ */
function stripCharsInBag (s, bag) {
  var returnString = "";
  for (var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}
function stripCharsNotInBag (s, bag) {
  var returnString = "";
  for (var i = 0; i < s.length; i++) {
    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) {
  for (var i = 0; i < s.length; i++) { if (whitespace.indexOf(s.charAt(i)) != -1) break; }
  return s.substring (i, s.length);
}
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 {
      if (sPos <= s.length) {
        var l = sPos + arg
        if (l > s.length) {
          resultString += s.substring(sPos, s.length);
          for (var j = 1; j <= arg - (s.length - sPos); j++) {
            resultString += ' ';
          }
        } else {
          resultString += s.substring(sPos, sPos + arg);
        } 
        sPos = l; //+= arg;
      } else {
        for (var j = 1; j <= arg; j++) {
          resultString += ' ';
        }
      }
    }
  }
  return resultString;
}
function reformatZIPCode (s) {
  if (s.length == 5) 
    return s;
  else 
    return (reformat (s, "", 5, "-", 4));
}
function reformatCAZIPCode (s) { return (reformat (s, "", 3, " ", 3)) }
function reformatUSPhone (s) { return (reformat (s, "", 3, "-", 3, "-", 4)) }
function reformatSSN (s)     { return (reformat (s, "", 3, "-", 2, "-", 4)) }
/*  ================================================================  */
/*  ================================================================
    FUNCTIONS: Functions that prompt users
    
      prompt (s)                 Display prompt string s in status bar.
      promptEntry (s)            Display data entry prompt string s in status bar.
      warnError (f, s)           Notify user that there is an in the field f.
               
    INPUT:     s

    RETURNS:   formated string
    ================================================================ */
function prompt (s)          { window.status = s; }
function promptEntry (s)     { window.status = pEntryPrompt + s; }
function warnError (f, s, m) {   
  f.focus();
  alert(m); //mPrefix + s + mSuffix);
  return false;
}
/*  ================================================================  */
/*  ================================================================
    FUNCTIONS: Functions that nofity users of mistakes
    
*     checkAndWarn (f, n)        Check that f.value is corret and warn if not.
*     checkAlphabetic (f, n)     Check that f.value is a letter.
*     checkNumeric (f, n)        Check that f.value is a number.
*     checkString (f, n)         Check that f.value is not empty or all whitespace.
*     checkStateCode (f)         Check that f.value is a valid U.S. state code.
*     checkZIPCode (f)           Check that f.value is a valid ZIP code.
*     checkUSPhone (f)           Check that f.value is a valid US Phone.
*     checkInternationalPhone (f)Check that f.value is a valid International Phone.
*     checkEmail (f)             Check that f.value is a valid Email.
*     checkSSN (f)               Check that f.value is a valid SSN.
*     checkYear (f)              Check that f.value is a valid Year.
*     checkMonth (f)             Check that f.value is a valid Month.
*     checkDay (f)               Check that f.value is a valid Day.
*     checkDate (y, m, d, l, k)  Check that field values form a valid date.
      getRadioButtonValue (r)    Get checked value from radio button.
*     checkCreditCard (r, f)     Validate credit card info.
               
    INPUT:     s

    RETURNS:   formated string
    ================================================================ */

/*  ================================================================ */
function checkAndWarn (f, n, func, r) {
  if (r == null) { r = defaultEmptyOK; }
  var res = func(f, n);
  if (res != '') { 
    warnError (f, n, res);
    return false;
  }
  return true;
}
function checkNumeric (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  if (!isInteger(f.value)) return mPrefix + n + mSuffix;
  return '';
}
function checkAlphabetic (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  if (!isAlphabetic(f.value)) return mPrefix + n + mSuffix;
  return '';
}
function checkString (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  if (isWhitespace(f.value)) return mPrefix + n + mSuffix;
  return '';
}
function checkStateCode (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  s = s.toUpperCase();
  if (!isStateCode(s)) return iThe + n + iStateCode;
  return '';
}
function checkZIPCode (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  s = stripCharsInBag(s, ZIPCodeDelimiters)
  if (!isZIPCode(s)) return iThe + n + iZIPCode;
  f.value = reformatZIPCode(s)
  return '';
}
function checkCAZIPCode (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  s = stripCharsInBag(s, CAZIPCodeDelimiters)
  if (!isCAZIPCode(s)) return iThe + n + iCAZIPCode;
  f.value = reformatCAZIPCode(s)
  return '';
}
function checkPostalCode (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  if (!isAlphanumeric(s)) return iThe + n + iPostalCode;
  return '';
}
function checkUSPhone (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  var s = stripCharsInBag(s, phoneNumberDelimiters)
  if (!isUSPhoneNumber(s)) return iThe + n + iUSPhone;
  f.value = reformatUSPhone(s)
  return '';
}
function checkInternationalPhone (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  var s = stripCharsInBag(s, phoneNumberDelimiters)
  if (!isInternationalPhoneNumber(s)) return iThe + n + iWorldPhone;
  return '';
}
function checkEmail (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  if (!isEmail(s)) return iThe + n + iEmail;
  return '';
}
function checkSSN (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  s = stripCharsInBag(s, SSNDelimiters)
  if (!isSSN(s)) return iThe + n + iSSN;
  f.value = reformatSSN(s)
  return '';
}
function checkYear (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  if (!isYear(s)) return iThe + n + iYear;
  return '';
}
function checkMonth (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  if (!isMonth(s)) return iThe + n + iMonth;
  return '';
}
function checkDay (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  if (!isDay(s)) return iThe + n + iDay;
  return '';
}
function checkDate (y, m, d, l, o) {
  var s = f.value;
  if (checkDate.length == 4) OKtoOmitDay = false;
  if (!isYear(y.value)) return iThe + n + iYear;
  if (!isMonth(m.value)) return iThe + n + iMonth;
  if ( (OKtoOmitDay == true) && isEmpty(d.value) ) 
    return '';
  else if (!isDay(d.value)) 
    return iThe + n + iDay;
  
  if (isDate (y.value, m.value, d.value))
    return '';
  
  return iDatePrefix + l + iDateSuffix;
}
function checkCreditCard (d, f, r) {
  if (r == null) { r = defaultEmptyOK; }
  var s = f.value;
  if (isEmpty(s)) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  var c = getRadioButtonValue (d)
  var s = stripCharsInBag(s, creditCardDelimiters)
  if (!isCardMatch(c, s)) 
    return iCreditCardPrefix + c + iCreditCardSuffix;
  f.value = s;
  return '';
}
function checkSelectedIndex (f, n, r) {
  if (r == null) { r = defaultEmptyOK; }
//  var i = f.options[f.selectedIndex].value;
  var i = f.selectedIndex;
  if (i == 0) {
    if (r)
      return '';
    else
      return mPrefix + n + mSuffix;
  }
  return '';
}
/*  ================================================================ */
/*  ================================================================ */
function checkQuestions (q, m, r) {
  var s = '';
  var count = 0;
  var n ;

  for (var i = 3; i < checkQuestions.arguments.length; i++) {
    var item = checkQuestions.arguments[i];

    n = (item.name + '*').substring(0, 1);

    if (n == 'c') { if (item.checked) count += 1; }
    if (n == 'r') { if (item.checked) count += 1; }
    if ((n == 'u') || (n == '*')) {
      for (var j = 0; j < item.length; j++) {
        if (item[j].checked) { 
          count += 1;
          break; 
        }
      }
    }
    if ((n == 'x') || (n == 't')) { if (!isWhitespace(item.value)) count += 1; }
    
  }
  if (r && (count == 0))
    s += 'You must answer ' + q + '.\n';
  if (count > m)
    s += 'You may only have ' + m + ' answer(s) for ' + q + '.\n';
  return s
}
function checkQuestionAnswered () {
  var n ;

  for (var i = 0; i < checkQuestionAnswered.arguments.length; i++) {
    var item = checkQuestionAnswered.arguments[i];

    n = (item.name + '*').substring(0, 1);

    if (n == 'c') { if (item.checked) return true; }
    if (n == 'r') { if (item.checked) return true; }
    if ((n == 'u') || (n == '*')) {
      for (var j = 0; j < item.length; j++) {
        if (item[j].checked) { return true; }
      }
    }
    if ((n == 'x') || (n == 't')) { if (!isWhitespace(item.value)) return true; }
  }
  return false;
}
function getRadioButtonValue (r) {
  for (var i = 0; i < r.length; i++) { 
    if (r[i].checked) { break; }
  }
  return r[i].value;
}

