function validateEmailData(formName) {
    var alerts = '';
    var pass = 'true';
    var passconf = 'true';
    var phonemax = 12;
    var commentsmax = 250;
    var ValidEmailChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.@-_1234567890';
    var invalidNameChars = '`~!@#$%^&*()_+=[]\\{}|:;\"<>?/';
    var skipOtherEmailChecks = 0;
    var commentDefaultText = 'Enter your message here. Maximum 250 characters.';

    //Validate the fields
    if (0) {
        alerts += "Please enter a First Name.\n";
    } else {
        if (0) {
            alerts += "Frist Name must not be a ' ' blank space.\n";
        }
        if (formName.name.value.indexOf("--") != -1) {
            alerts += "Full Name must not contain '--' dashes.\n";
        }
        for (var i = 0; i < formName.name.value.length; i++) {
            if (invalidNameChars.indexOf(formName.name.value.charAt(i)) != -1) {
                alerts += " Full Name may not contain the following special characters:\n";
                alerts += "??????`~!@#$%^&*)(_+=][}{|:;\"<>?/\\\n";
                break;
            }
        }
    }
    
    if (formName.day_phone_number.value && (formName.day_phone_number.value.length < phonemax && formName.day_phone_number.value.length != 10)) {
        alerts = alerts + "Please enter a valid 10-digit Daytime Phone Number.\n";
    }
    if (formName.night_phone_number && formName.night_phone_number.value && (formName.night_phone_number.value.length < phonemax && formName.night_phone_number.value.length != 10)) {
        alerts = alerts + "Please enter a valid 10-digit Evening Phone Number.\n";
    }
    if (!formName.email_address.value) {
        alerts = alerts + "Please enter an Email address.\n";
        skipOtherEmailChecks = 1;
    }

    //comments
    if (formName.comment.value.length > commentsmax) {
        alerts = alerts + "Comments must be no more than 250 characters.\n";
    }
    if (formName.comment.value && formName.comment.value == commentDefaultText) {
        formName.comment.value = "";
    }

    // start email validation rules
    if (formName.email_address.value) {
        if (formName.email_address.value.indexOf(" ") != -1) {
            alerts = alerts + "Email Address has a space in it.\n";
        }
        if (formName.email_address.value.indexOf("..") != -1) {
            alerts = alerts + "Email Address has double periods in it.\n";
        }
        if (formName.email_address.value.indexOf("--") != -1) {
            alerts = alerts + "Email Address has double dashes in it.\n";
        }
        if (formName.email_address.value.indexOf("@@") != -1) {
            alerts = alerts + "Email Address has a double @ in it.\n";
        }
        for (var i = 0; i < formName.email_address.value.length; i++) {
            if (ValidEmailChars.indexOf(formName.email_address.value.charAt(i)) == -1) {
                alerts = alerts + "Email Address may only contain alpha-numeric characters.\n";
                break;
            }
        }
    }

    // more email validation checks
    // there must be >= 1 character before @, so we
    // start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = formName.email_address.value.length;
    var j = 0;
    var failedFirstCheck = 0;
    var s = formName.email_address.value;
    // look for @
    while ((i < sLength) && (s.charAt(i) != "@")) {
        i++;
    }
    if (((i >= sLength) || (s.charAt(i) != "@")) && skipOtherEmailChecks == 0) {
        alerts = alerts + "There must be a valid Email address (like jsmith@somewhere.com). Please reenter it now.\n";
        failedFirstCheck = 1;
    } else {
        i += 2;
    }
    // look for . starting with second character after @
    while ((i < sLength) && (s.charAt(i) != ".")) {
        i++;
    }
    // there must be at least one character after the .
    if (((i >= sLength - 1) || (s.charAt(i) != ".")) && failedFirstCheck == 0 && skipOtherEmailChecks == 0) {
        alerts = alerts + "There must be a valid Email address (like jsmith@somewhere.com). Please reenter it now.\n";
    }
    // email cannot begin with @, ., or -
    if (s.charAt(j) == "@" | s.charAt(j) == "." | s.charAt(j) == "-") {
        alerts = alerts + "Your Email address may not begin with a '" + s.charAt(j) + "' character.\n";
    }
    // email cannot end with @, ., or -
    var numb = (formName.email_address.value.substring((formName.email_address.value.length)-1, formName.email_address.value.length));
    if ((numb == "@") | (numb == ".") | (numb == "-")) {
        alerts = alerts + "Your Email address may not end with a '" + numb + "' character.\n";
    }

    //inform or submit
    if (alerts) {
        alert (alerts);
        return false;
    } else {
        return true;
    }
}

var defaultEmptyOK = true
var dUSPhone = "10 digit U.S. phone number (like 415 555 1212)."
var pEmail = "valid Email address (like jsmith@somewhere.com)."
var pEntryPrompt = "Please enter a "
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
var digitsInUSPhoneNumber = 10;
var iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now."
var iEmail = "This field must be a valid Email address (like jsmith@somewhere.com). Please reenter it now!"
// whitespace characters
var whitespace = " \t\n";

function validateUSPersonalInfo() {
    return (checkUSPhone(formName.elements["Phone"]));
}

function checkUSPhone(theField, emptyOK) {
    if (checkUSPhone.arguments.length == 1) {
        emptyOK = defaultEmptyOK;
    }
    if ((emptyOK == true) && (isEmpty(theField.value))) {
        return true;
    } else {
        var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters);
        if (!isUSPhoneNumber(normalizedPhone, false)) {
            return warnInvalid(theField, iUSPhone);
        } else {
            // if you don't want to reformat as (123) 456-789, comment next line out
            theField.value = reformatUSPhone(normalizedPhone);
            return true;
        }
    }
}

// Check whether string s is empty.
function isEmpty(s) {
    return ((s == null) || (s.length == 0));
}

// 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;
}

function isUSPhoneNumber(s) {
    if (isEmpty(s)) {
        if (isUSPhoneNumber.arguments.length == 1) {
            return defaultEmptyOK;
        } else {
            return (isUSPhoneNumber.arguments[1] == true);
        }
    }
    return (isInteger(s) && s.length == digitsInUSPhoneNumber);
}

// 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;
}

// takes USPhone, a string of 10 digits
// and reformats as (123) 456-789
function reformatUSPhone(USPhone) {
    return (reformat (USPhone, "(", 3, ") ", 3, "-", 4));
}

// Display data entry prompt string s in status bar.
function promptEntry(s) {
    window.status = pEntryPrompt + s;
}

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;
    }
}

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 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;
}

// Returns true if character c is a digit
// (0 .. 9).
function isDigit(c) {
    return ((c >= "0") && (c <= "9"));
}

function isUSPhoneNumber(s) {
    if (isEmpty(s)) {
        if (isUSPhoneNumber.arguments.length == 1) {
            return defaultEmptyOK;
        } else {
            return (isUSPhoneNumber.arguments[1] == true);
        }
        return (isInteger(s) && s.length == digitsInUSPhoneNumber);
    }
}

function reformat(s) {
    var arg;
    var sPos = 0;
    var resultString = "";
    for (var i = 1; i < reformat.arguments.length; i++) {
        arg = reformat.arguments[i];
        if (i % 2 == 1) {
            resultString += arg;
        } else {
            resultString += s.substring(sPos, sPos + arg);
            sPos += arg;
        }
    }
    return resultString;
}

// 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;
}

//limit character entry in the comments field
function textCounter(field, maxlimit) {
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
        alert('Comments must be no more than 250 characters.');
    }
}

//clear the value of the comments field on focus if the value is default text
function clearComments(field) {
    if (field.value == "Enter your message here. Maximum 250 characters.") {
        field.value = "";
    }
}
