//***********************************************************************************************************************
//***********************************************************************************************************************
//	Title			: Online Form Validation
//	Date Created	: 01 - Oct - 2008
//  Last Updated	: 01 - Oct - 2008
//	Programmer		: Damian Duarte
//***********************************************************************************************************************
//***********************************************************************************************************************


//----------------------------------------------------------------------------
// Send a supplied Email Address String for Validating ...                   | 
//----------------------------------------------------------------------------
function emailAddressValidation(emailAddressIn)
{
    if(checkEmailAddress(emailAddressIn) == 0)
	{
        return (true);
    }
    return ( false );
}

//----------------------------------------------------------------------------
// Check Email Address String Supplied for Validity ...                      |
//----------------------------------------------------------------------------
function checkEmailAddress(emailAddressIn)
{
	var errorFlag = 0;
    if(checkstringEmpty(emailAddressIn))
        errorFlag = 1;
    else if(checkForAtSign(emailAddressIn))
        errorFlag = 2;
    else if(checkBeforeAt(emailAddressIn))
        errorFlag = 3;
    else if(checkForLeftBracket(emailAddressIn))
        errorFlag = 4;
    else if(checkForRightBracket(emailAddressIn))
        errorFlag = 5;
    else if(checkForValidDot(emailAddressIn))
        errorFlag = 6;
    else if(checkForValidSuffix(emailAddressIn))
        errorFlag = 7;
    else
        return (errorFlag);

    return (errorFlag);
}

//----------------------------------------------------------------------------
// Check to see if the string (Email Address field) is empty ...             |
//----------------------------------------------------------------------------
function checkstringEmpty(emailAddressIn)
{
    if(emailAddressIn.length < 1)
		return(true);
    else 
		return(false);
}

//----------------------------------------------------------------------------
// Check to see if there is an '@' character within the Email string ...     |
//----------------------------------------------------------------------------
function checkForAtSign(emailAddressIn)
{
    if(emailAddressIn.indexOf('@',0) == -1)
		return(true);
    else 
		return(false);
}

//----------------------------------------------------------------------------
// Check to see if there is at least one character before the '@' sign ...   |
//----------------------------------------------------------------------------
function checkBeforeAt(emailAddressIn)
 {
    if(emailAddressIn.indexOf('@',0) < 1)
		return(true);
    else 
		return(false);
}

//----------------------------------------------------------------------------
// Check if there is a left bracket for IP Emails ...                        |
//----------------------------------------------------------------------------
function checkForLeftBracket(emailAddressIn)
{
    if(emailAddressIn.indexOf('[',0) == -1 && emailAddressIn.charAt(emailAddressIn.length - 1) == ']')
		return(true);
    else 
		return(false);
}

//----------------------------------------------------------------------------
// Check if there is a right bracket for IP Emails ...                       |
//----------------------------------------------------------------------------
function checkForRightBracket(emailAddressIn)
{
    if(emailAddressIn.indexOf('[',0)> -1 && emailAddressIn.charAt(emailAddressIn.length - 1) != ']')
		return(true);
    else 
		return(false);
}

//----------------------------------------------------------------------------
// If IP Email ... then no need for this check ...                           |
// Else ... Check if there is at the least one dot in the Email string ...   |
//----------------------------------------------------------------------------
function checkForValidDot(emailAddressIn)
{
    if(emailAddressIn.indexOf('@', 0) > 1 && emailAddressIn.charAt(emailAddressIn.length - 1) == ']')
        return ( false );

    if(emailAddressIn.indexOf('.',0)== -1)
        return(true);

    return(false);
}

//----------------------------------------------------------------------------
// If IP Email ... then no need for this check ...                           |
// Else ... Check if there is at the least 2-3 characters after the dot ...  |
//----------------------------------------------------------------------------
function checkForValidSuffix(emailAddressIn)
{
    if(emailAddressIn.indexOf('@',0) > 1 && emailAddressIn.charAt(emailAddressIn.length - 1)== ']')
        return(false);

    var len = emailAddressIn.length;
    var pos = emailAddressIn.lastIndexOf('.',len - 1) + 1;
    if((len - pos) < 2 || (len - pos) > 3)
		return(true);
    else 
		return(false);
}