﻿//----------------------------------------------------------------------------------------------------------
// Email verification that detects errors in a single textbox with multiple checks.
// Therefore the error messages can be more informative.
//----------------------------------------------------------------------------------------------------------
function checkEmail()
{
    var addr;
    
    if (document.getElementById("name").value == "")
    {
        alert("You failed to enter your name");
        highlightField("name");
        return false;
    }
    
    else if (document.getElementById("message").value == "")
    {
        alert("Don't forget to write your message");
        highlightField("message");
        return false;
    }

    else if (document.getElementById("txtAddress").value == "")
    {
        alert("You failed to enter an email address. " +
            "Please enter an email address in the format " +
            "username@domain.ext, where domain is domain " +
            "name and ext is the extension like com or edu.");
        highlightField("txtAddress");
        return false;
    }
    else if (!isNaN(document.getElementById("txtAddress").value))
    {
        alert("You entered a number instead of an address. " +
            "Please enter an email address in the format " +
            "username@domain.ext, where domain is domain " +
            "name and ext is the extension like com or edu.");
        highlightField("txtAddress");
        return false;
    }
    else if (document.getElementById("txtAddress").value.length < 7)
    {
        alert("There don't seem to be an adequate number of characters. " +
            "Please enter an email address in the format " +
            "username@domain.ext, where domain is domain " +
            "name and ext is the extension like com or edu.");
        highlightField("txtAddress");
        return false;
    }
    else if (document.getElementById("txtAddress").value.indexOf("@") < 0)
    {
        alert("Your email entry does not have an '@'. " +
            "Please enter an email address in the format " +
            "username@domain.ext, where domain is domain " +
            "name and ext is the extension like com or edu.");
        highlightField("txtAddress");
        return false;
    }
    else if (document.getElementById("txtAddress").value.indexOf(".") < 0)
    {
        alert("Your email entry does not have a '.'. " +
            "Please enter an email address in the format " +
            "username@domain.ext, where domain is domain " +
            "name and ext is the extension like com or edu.");
        highlightField("txtAddress");
        return false;
    }
    else if (document.getElementById("txtAddress").value.lastIndexOf(".") -
              document.getElementById("txtAddress").value.lastIndexOf("@") <= 1)
    {
        alert("Your '@' and '.' are too close together. " +
            "Please enter an email address in the format " +
            "username@domain.ext, where domain is domain " +
            "name and ext is the extension like com or edu.");
        highlightField("txtAddress");
        return false;
    }
    else if (document.getElementById("txtAddress").value.lastIndexOf(".") >
              document.getElementById("txtAddress").value.length - 3)
    {
        alert("Your last '.' seems too close to the end. " +
            "Please enter an email address in the format " +
            "username@domain.ext, where domain is domain " +
            "name and ext is the extension like com or edu.");
        highlightField("txtAddress");
        return false;
    }

    // This else branch, and thus the input and processing statements within, will only be
    // executed if no invalid input was detected.
    else
    {
        addr = document.getElementById("txtAddress").value;
        return true;
    }
} 
//---------------------------------------------------------------------------------------------
// End E-mail verification
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------
// This function accepts a text box id as a parameter,
// sets the focus to that box, and highlights the text.
//---------------------------------------------------------------------------
function highlightField(fieldName)
{
    document.getElementById(fieldName).focus();
    document.getElementById(fieldName).select();
}


