// BASIC DATA VALIDATION FUNCTIONS:
//
// isCanZIPCode(s [,eok])              True if string s is a valid
//					 Canadian ZIP code.
// isUSZIPCode(s [,eok])               True if string s is a valid
//					 U.S. ZIP code.
// isZIPCode(s [,eok])                 True if string s is a valid
//					 U.S. or Canadian ZIP code.
//
// Note:  If you will be using the functions in this file, you must
//        also include the file "info.js", which has helper functions
//        and variable definitions that these functions need.
//
// 18 Feb 97 created Eric Krock
//
// (c) 1997 Netscape Communications Corporation
// 1998 Supplemented by Dawn Bushong



// non-digit characters which are allowed in ZIP Codes
var ZIPCodeDelimiters = "-";

// our preferred delimiter for reformatting ZIP Codes
var ZIPCodeDelimeter = "-"

// characters which are allowed in Social Security Numbers
var validZIPCodeChars = infodigits + ZIPCodeDelimiters

// U.S. ZIP codes have 5 or 9 digits.
// They are formatted as 12345 or 12345-6789.
var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 9


// isCanZIPCode(STRING s [, BOOLEAN emptyOK])
// 
// isCanZIPCode returns true if string s is a valid 
// Canadian ZIP code.  Must be in the form XNX NXN,
// where X is a character, and N is a digit.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isCanZIPCode (s) {
  if (isEmpty(s)) return emptyReturn(isCanZIPCode.arguments);
  s2 = stripInfoCharsNotInBag(s,infodigits+infoletters);
  return (isInfoLetter(s2.charAt(0)) &&
          isInfoDigit (s2.charAt(1)) &&
          isInfoLetter(s2.charAt(2)) &&
          isInfoDigit (s2.charAt(3)) &&
          isInfoLetter(s2.charAt(4)) &&
          isInfoDigit (s2.charAt(5)));
}


// isUSZIPCode(STRING s [, BOOLEAN emptyOK])
// 
// isZIPCode returns true if string s is a valid 
// U.S. ZIP code.  Must be 5 or 9 digits only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isUSZIPCode (s) {
  if (isEmpty(s)) return emptyReturn(isUSZIPCode.arguments);
  s2 = stripInfoCharsNotInBag(s,infodigits);
  return (isInfoInteger(s2) && 
         ((s2.length == digitsInZIPCode1) || (s2.length == digitsInZIPCode2)));
}


// isZIPCode(STRING s [, BOOLEAN emptyOK])
// 
// isZIPCode returns true if string s is a valid 
// U.S. or Canadian ZIP code.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isZIPCode (s) {
  if (isEmpty(s)) return emptyReturn(isZIPCode.arguments);
  return (isUSZIPCode(s) || isCanZIPCode(s));
}

