
// This function validates a string to be the proper format for an email address
//
// The email address must have the following characteristics

//      It must have one and only one '@' character.
//      It must have at least one period '.' following the '@' 
//      It must have at least one character between the '@' and the first '.' following the '@'
//      It must have 2 or three characters following the right most '.'
//      It cannot have a comma or a space within it.
// 
// The shortest email address is a@b.cd
//
//
// Arguments:  theEmail -- A string which is the email address to validate.
// 
// Returns:    True or False

function isEmail ( theEmail )
{
    if ( theEmail == null )
        return false;
        
    var workString;
    var posAt;
    var posFirstDot;
    var posLastDot;
    var posComma;
    var posSpace;
    var posAddAt;
        
    workString = theEmail.toString();
    
    if ( theEmail == "" ) 
        return false;
        
    // Cannot have a comma or space in the email address

    posComma = workString.indexOf(",");
    posSpace = workString.indexOf(" ");
    posAt = workString.indexOf("@");
    posAddAt = workString.lastIndexOf("@");
    posFirstDot = workString.indexOf(".", posAt);
    posLastDot = workString.lastIndexOf(".") + 1;

           
    if ( posComma >= 0 || posSpace >= 0 )
        return false;    
    
    // make sure there is only one @ in the email and that at least one . follows it.
    // Neither can be the first character of the string.
    // make sure there is at least one character between the @ and the first .
    
    if ( posAt <= 0 || posFirstDot <= 0  || posAt != posAddAt || (posFirstDot - posAt) == 1 )
    {
        return false;
    }
    
    // make sure there are two or three characters following the last dot 
    if ( ( (workString.length - posLastDot ) < 2)  || ((workString.length - posLastDot) > 3))
    {
        return false;
    }
        
    return true;
}

