<!--

//
// THIS FILE CONTAINS ROUTINES SPECIFIC TO THE VALIDATION OF FIELDS
//
// FUNCTIONS:
//      checkString(theField, emptyOK)
//      isEmpty(s)
//      isWhitespace(s)
//      checkEmail(theField, emptyOK)
//      isEmail(s)
//      checkPhoneSuffix(theField, emptyOK)
//      checkPhonePrefix(theField, emptyOK)
//      checkPhoneAreaCode(theField, emptyOK)
//      check5DigitZIP(theField, emptyOK)
//      stripCharsInBag(s, bag)
//      stripCharsNotInBag(s, bag)
//      isDigit(c)
//      isInteger(s)
//      checkDate(strYear, strMonth, strDay)
//      isLeapYear(intYear)
//      isDateNotInFuture(strYear, strMonth, strDay)
//

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

var defaultEmptyOK = false;

/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */

// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkString (theField, emptyOK) {
    // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) {
        return false;
    } else return true;
}

// Check whether string s is empty.

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

// Returns true if string s is empty or
// whitespace characters only.

function isWhitespace (s) {
    var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, 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;
}

// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkEmail (theField, emptyOK) {
    if (checkEmail.arguments.length == 1) {
        emptyOK = defaultEmptyOK;
    }   
    if ((emptyOK == true) && (isEmpty(theField.value))) {
        return true;
    } else if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theField.value))) {
        return false;
    } else {
        return true;
    }
}

// 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);

    // 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 checkPhoneSuffix(theField, emptyOK) {

    if (checkPhoneSuffix.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) {
        return true;
    } else {
        if (!isInteger(theField.value) || theField.value.length != 4) {
            return false;
        }
    }

    return true;
}

function checkPhonePrefix(theField, emptyOK) {

    if (checkPhonePrefix.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) {
        return true;
    } else {
        if (!isInteger(theField.value) || theField.value.length != 3) {
            return false;
        }
    }

    return true;
}

function checkPhoneAreaCode(theField, emptyOK) {

    if (checkPhoneAreaCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) {
        return true;
    } else {
        if (!isInteger(theField.value) || theField.value.length != 3) {
            return false;
        }
    }

    return true;
}

// check5DigitZIP(TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid ZIP code.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function check5DigitZIP(theField, emptyOK) {
    if (check5DigitZIP.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) {
        return true;
    } else {
        if (!isInteger(theField.value) || theField.value.length != 5) {
            return false;
        }
    }

    return true;
}

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

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



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

function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is 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;
}

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

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

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

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



function checkDate(strYear, strMonth, strDay) {

    var intday;
    var intMonth;
    var intYear;

    intday = parseInt(strDay, 10);
    if (isNaN(intday)) {
        return false;
    }

    intMonth = parseInt(strMonth, 10);
    if (isNaN(intMonth)) {
        return false;
    }

    intYear = parseInt(strYear, 10);
    if (isNaN(intYear)) {
        return false;
    }

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

    if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
        return false;
    }

    if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
        return false;
    }

    if (intMonth == 2) {
        if (intday < 1) {
            return false;
        }

        if (isLeapYear(intYear) == true) {
            if (intday > 29) {
                return false;
            }
        } else {
            if (intday > 28) {
                return false;
            }
        }
    }

    return true;
}


function isLeapYear(intYear) {
    if (intYear % 100 == 0) {
        if (intYear % 400 == 0) { return true; }
    }
    else {
        if ((intYear % 4) == 0) { return true; }
    }
    return false;
}

function isDateNotInFuture(strYear, strMonth, strDay) {

    var intday;
    var intMonth;
    var intYear;

    intday = parseInt(strDay, 10);
    if (isNaN(intday)) {
        return false;
    }

    intMonth = parseInt(strMonth, 10);
    if (isNaN(intMonth)) {
        return false;
    }

    intYear = parseInt(strYear, 10);
    if (isNaN(intYear)) {
        return false;
    }

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

    if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
        return false;
    }

    if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
        return false;
    }

    if (intMonth == 2) {
        if (intday < 1) {
            return false;
        }

        if (isLeapYear(intYear) == true) {
            if (intday > 29) {
                return false;
            }
        } else {
            if (intday > 28) {
                return false;
            }
        }
    }

    var curDate = new Date();
    var curYear = curDate.getYear();
    var curMonth = curDate.getMonth() + 1;
    var curDay = curDate.getDate();

    // Hack to get correct year.  If year < 200, then add 1900.
    if (curYear < 200) {
        curYear = curYear + 1900;
    }

    if (intYear > curYear) {
        return false;
    } else {
        if (intYear < curYear) {
            return true;
        }
    }

    if (intMonth > curMonth) {
        return false;
    } else {
        if (intMonth < curMonth) {
            return true;
        }
    }

    if (intday > curDay) {
        return false;
    } else {
        if (intday < curDay) {
            return true;
        }
    }

    // This line gets called if the date is today.
    return true;
}

// -->
