var defaultEmptyOK = false
var digits = "1234567890";
var SSNDelimiters = "- ";  // Non-digit characters which are allowed in Social Security Numbers
var validSSNChars = digits + SSNDelimiters;  // characters which are allowed in Social Security Numbers
var digitsInSocialSecurityNumber = 9;  // U.S. Social Security Numbers have 9 digits. They are formatted as 123-45-6789.
var sSSN = "Social Security Number"
var pSSN = "9 digit U.S. social security number (like 123 45 6789)."

// Check whether string s is empty.

function isSSNEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit(c)
{   return ((c >= "0") && (c <= "9"))
}

// Removes all characters which appear in string bag from string s.

function stripSSNCharsInBag (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;
}

// isSSNInteger (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:
// isSSNInteger ("5")            true 
// isSSNInteger ("")             defaultEmptyOK
// isSSNInteger ("-5")           false
// isSSNInteger ("", true)       true
// isSSNInteger ("", false)      false
// isSSNInteger ("5", false)     true

function isSSNInteger(s)

{   var i;

    if (isSSNEmpty(s)) 
       if (isSSNInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSSNInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// isSSN (STRING s [, BOOLEAN emptyOK])
// 
// isSSN returns true if string s is a valid U.S. Social
// Security Number.  Must be 9 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isSSNInteger.

function isSSN(s)
{   if (isSSNEmpty(s)) 
       if (isSSN.arguments.length == 1) return defaultEmptyOK;
       else return (isSSN.arguments[1] == true);
    return (isSSNInteger(s) && s.length == digitsInSocialSecurityNumber)
}

// 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
}

// Check that string theField.value is a valid SSN.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkSSN (theField, emptyOK)
{   
    var iSSN = 'This field must be a 9 digit U.S. social security number (like 123 45 6789). Please reenter it now.';

    if (checkSSN.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isSSNEmpty(theField.value))) return true;
    else
    {  var normalizedSSN = stripSSNCharsInBag(theField.value, SSNDelimiters)
       if (!isSSN(normalizedSSN, false)) 
          return warnInvalid (theField, iSSN);
       else 
       {  
          return true;
       }
    }
}

