function validateFormOnSubmit(theForm)
{
    var nameError = false;
    var emailError = false;
    var cityError = false;
    var phoneError = false;
    var letterError = false;

    var name = theForm.name.value;
    var email = theForm.email.value;
    var city = theForm.city.value;
    var phone = theForm.phone.value;
    var letter = theForm.letter.value;

    nameError = isEmptyField(theForm.name);
    emailError = isInvalidEmailField(theForm.email);
    cityError = isEmptyField(theForm.city);
    phoneError = isEmptyField(theForm.phone);
    letterError = isEmptyField(theForm.letter);

    if (nameError || emailError || cityError || phoneError || letterError)
    {
        var msg = "Some fields need correction:"; 
        
        if (nameError)
            msg += "\n'Name' field cannot be blank.";

        if (emailError)
            msg += "\n'Email' field cannot be blank and must be a valid email address format.";

        if (cityError)
            msg += "\n'City' field cannot be blank.";

        if (phoneError)
            msg += "\n'Phone' field cannot be blank.";

        if (letterError)
            msg += "\n'Letter' field cannot be blank.";

        alert(msg);
        return false;
    }

    return true;
}

function isEmptyField(fld)
{
    var isError = false;
    if (isEmpty(fld.value))
    {
        isError = true;
        //fld.style.background = 'Pink';
    }
    else
    {
        //fld.style.background = 'White';
    }
    return isError;
}

function isInvalidEmailField(fld)
{
    var isError = false;
    if (!isValidEmailAddress(fld.value))
    {
        isError = true;
        //fld.style.background = 'Pink';
    }
    else
    {
        //fld.style.background = 'White';
    }
    return isError;
}


/*
function validateForm()
{
    var lastname = document.emailform.lastname.value;
    var firstname = document.emailform.firstname.value;
    var email = document.emailform.username.value;
    var pwd1 = document.emailform.pwd1.value;
    var pwd2 = document.emailform.pwd2.value;

    if (isEmpty(lastname))
    {
        alert("Please enter your last name.");
        document.emailform.lastname.focus();
        return false;
    }
    if (isEmpty(firstname))
    {
        alert("Please enter your first name.");
        document.emailform.firstname.focus();
        return false;
    }
    if (!isValidEmailAddress(email))
    {
        alert("Please enter a valid email address.");
        document.emailform.username.focus();
        return false;
    }
    if (isEmpty(pwd1))
    {
        alert("Please enter a password.");
        document.emailform.pwd1.focus();
        return false;
    }
    if (!isValidPassword(pwd1))
    {
        alert("Password must be at least 6 characters in length and cannot contain spaces.");
        document.emailform.pwd1.focus();
        return false;
    }
    if (pwd1 != pwd2)
    {
        alert("Your passwords do not match.  Please try again.");
        document.emailform.pwd1.focus();
        return false;
    }
    return true;
}
*/

function isEmpty(val) 
{
    return val.length == 0;
}

function isRequiredMinimumLength(val, requiredLength)
{
    return val.length >= requiredLength;
}

function containsSpaces(val)
{
    var spacechar = " ";
    return val.indexOf(spacechar) > -1;
}

function isValidEmailAddress(val)
{
    var emailExp = /^[\w-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;
    if (val.match(emailExp))
    {
        return true;
    }
    return false;
}

function isValidPassword(val)
{
    var minimumPasswordLength = 6;
    if (!isRequiredMinimumLength(val, minimumPasswordLength) || containsSpaces(val))
    {
        return false;
    }
    return true;
}

function showDupError(errortext)
{
    alert(errortext);
}
