var defaultEmptyOK = false;
var minPasswordLength = 6;
var whitespace = "\r\n 	";
var mPrefix = "You did not enter a value into the ";
var mSuffix = " field. This is a required field.";

// s is an abbreviation for "string"
// i is an abbreviation for "invalid"
// p is an abbreviation for "prompt"

var sEmail = "Email";
var iEmail = "This field must be a valid email address (like me@here.com).";
var pEmail = "valid email address (like foo@bar.com).";
var iPhone = "Phone must be a valid number including area code like '(604)-301-2003'.";
var iSelect = "This field must be a valid selection.";
var iPassword = "The password must be a minimum of " + minPasswordLength + " characters in length.";
var iConfirmPassword = "The confirmation password does not match.";

// 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, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length <= 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) {
       if (checkString.arguments.length >= 2)
          return warnEmpty (theField, s);
       else
          return false;
    }
    return true;
}

// checkPassword (TEXTFIELD pwd, [, BOOLEAN emptyOK==false])
//
// Check that string pwd.value is not all whitespace.
// Check for minimum required length.
//
function checkPassword (pwd, emptyOK)
{   if (!isPassword(pwd, false)) return warnInvalid (pwd, iPassword);
    return true;
}

// checkConfirmPassword (TEXTFIELD theField, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
// Check that the passwords match.
//
function checkConfirmPassword (pwd, confirmPwd, emptyOK)
{   if (checkConfirmPassword.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(Pwd.value)) && (isEmpty(confirmPwd.value)) ) return true;
    else if (confirmPwd.value != pwd.value) return warnInvalid (confirmPwd, iConfirmPassword);
    return true;
}

// checkSelect (TEXTFIELD theField, STRING s, [BOOLEAN firstInvalid==false,] [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
// firstInvalid ensures user did not select the first option (eg. "--- Select ---")
//
function checkSelect (theField, s, firstInvalid, emptyOK)
{   
    if (!checkString(theField, s, emptyOK)) return false;
    if (firstInvalid && theField.value == 0) {
       theField.focus();
       alert(iSelect);
       return false;
    }
    return true;
}

// checkPhone (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Phone Number.
//
function checkPhone (theField, emptyOK)
{
/*    
    if (!isPhone(theField, false)) return warnInvalid (theField, iPhone);
    return true;
*/
   if (checkPhone.arguments.length == 1) emptyOK = defaultEmptyOK;
   if ((emptyOK == true) && (isEmpty(theField.value))) return true;
   else if (!isPhone(theField, false))
      return warnInvalid (theField, iPhone);
   else 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 (!isEmail(theField.value, false))
       return warnInvalid (theField, iEmail);
    else return true;
*/
/*
   if (!isEmail(theField.value, false)) return warnInvalid (theField, iEmail);
   return true;
*/
   if (!isEmail(theField, false)) return warnInvalid (theField, iEmail);
   return true;
}

// isPhone (TEXTFIELD theField [, BOOLEAN emptyOK])
//
// Phone number must be 10 digits of form 999 999 9999
// * Brackets can surround the area code (999)
// * The separator can be blank space or dash (999)-999-9999
// * No alpha
//
function isPhone (theField)
{
   if (!checkString(theField)) return false;

    // must be 10 digits + 2 spacers = 12 characters
    // or 14 characters if brackets around area code
    var ss, s = theField.value;
    var i = 0;
    var sLength = s.length;
    if (sLength != 12 && sLength != 14) return false;

    if (sLength == 12) {
       if ((s.charAt(3) != " " && s.charAt(3) != "-") || 
           (s.charAt(7) != " " && s.charAt(7) != "-")) return false;

       // Adjust phone field to standard format (999)-999-9999
       ss = "(" + s.substring(0,3) +  ")-" + s.substring(4,7) + "-" + s.substring(8,12);
    }
    else { // sLength == 14
       if ((s.charAt(0) != "(") || (s.charAt(4) != ")") ||
           (s.charAt(5) != " " && s.charAt(5) != "-")   ||
           (s.charAt(9) != " " && s.charAt(9) != "-")) return false;

       // Adjust phone field to standard format (999)-999-9999
       ss = "(" + s.substring(1,4) +  ")-" + s.substring(6,9) + "-" + s.substring(10,14);
    }

    for (i = 0; i < ss.length; i++){
        var c = ss.charAt(i);
        if (c < "0" || c > "9")
           if (c != "(" && c != ")" && c != "-") return false;
    }

    theField.value = ss; // Update form field to standard format
    return true;
}

// isPassword (TEXTFIELD theField [, BOOLEAN emptyOK])
//
// Password be min length as defined by minPasswordLength.
//
function isPassword (theField)
{   if (!checkString(theField)) return false;
    if (theField.value.length < minPasswordLength) return false;
    return true;
}

// isEmail (TEXTFIELD theField [, 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)
function isEmail (theField)
{
/*
   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);

    // is s whitespace?
    if (isWhitespace(s)) return false;
*/
/*
    if (!checkString(s)) return false;
*/
    if (!checkString(theField)) return false;

    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var s = theField.value;
    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;
}

// 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;
}

// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.
function warnEmpty (theField, s)
{   theField.focus();
    alert(mPrefix + s + mSuffix);
    return false;
}

// 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;
}


