// FormCheck.js
// isWhitespace (s)                    Check whether string s is empty or whitespace.
// isEmail (s [,eok])                  True if string s is a valid email address.

// whitespace characters
var whitespace = " \t\n\r";
// Is empty string OK?
var defaultEmptyOK = false

var digits = "0123456789";

// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";

// characters which are allowed in US phone numbers
var validPhoneChars = digits + phoneNumberDelimiters;

// At least 7 digits in phone
var minDigitsInPhoneNumber = 7

// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0));
}

// Returns true if character c is an English letter 
function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}

// Returns true if character c is a digit 
function isDigit (c)
{   return ((c >= "0") && (c <= "9"));
}

// Returns true if character c is a letter or digit.
function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c));
}

// 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;
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// Returns true if all characters in string s are numbers.
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;
}

// 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

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) != ".") && (s.charAt(i) != "@"))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".") || (s.charAt(i) == "@")) return false;
    else i += 1;
    
    // Cant be any more @'s
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }
    
    // Error if we found an @
    if (i < sLength) return false;
    
    // Error if last char is .
    if (s.charAt(sLength-1) == ".") return false;
    
    return true;
}

// isPhoneNumber (STRING s [, BOOLEAN emptyOK])
function isPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isPhoneNumber.arguments[1] == true);
    var normalizedPhone = stripCharsInBag(s, phoneNumberDelimiters);
    return (isInteger(normalizedPhone) && normalizedPhone.length >= minDigitsInPhoneNumber);
}

// 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;
}
