var whitespace = " \t\n\r";

var mPrefix = "Something must be entered in the "
var mSuffix = " field."

// s is an abbreviation for "string"
var sName = "Name"
var sEmail = "Email"
var sPassword = "Password"
var sQuestion = "Question or Comment"

var iEmail = "The email address must be a valid address (in the form who@where.com)."

var defaultEmptyOK = false

function checkEmpty (theField, s, emptyOK)
{ 
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    else return true;
}

function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

function checkEmail (theField)
{   
    if (isEmpty(theField.value)) return true;
    else if (!isEmail(theField.value)) 
       return warnInvalid (theField, iEmail);
    else return true;
}

function isEmail (s)
{
    if (isWhitespace(s)) return false;
    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 warnInvalid (theField, s)
{
    theField.focus()
    theField.select()
    alert(s)
    return false
}

function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}

function isWhitespace (s)
{
    var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        // Check that each character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}
