﻿function confirmEmail(email)
{
    /*
        INVARIANT:
        Returns empty string if the email is well-formatted
        Returns an error message if email is invalid:
            1 = A space in the email address
            2 = No '@' sign
            3 = Multiple '@' signs
            4 = No '.'
            5 = There is no '.' after the '@' sign
            6 = No email handle (handle@host.extension)
            7 = No email host (handle@host.extension)
            8 = No email host extension (handle@host.extension)
            9 = No email address provided
    */
    email=trim(email);
    
    var errorcode=0;
    
    if (email=="" && errorcode==0)
        errorcode=9;
        
    var space=email.indexOf(" ");
    if (space!=-1 && errorcode==0)
        errorcode = 1;
    var at=email.indexOf("@");
    if (at==-1 && errorcode==0)
        errorcode = 2;
    var at_last=email.lastIndexOf("@");
    if (at!=at_last && errorcode==0)
        errorcode = 3
    var period=email.indexOf(".");
    if (period==-1 && errorcode==0)
        errorcode = 4;
    var period_last=email.lastIndexOf(".");
    if (period_last<at_last && errorcode==0)
        errorcode = 5;
    if (at==0 && errorcode==0)
        errorcode = 6;
    if (at_last+1==period_last && errorcode==0)
        errorcode = 7;
    if (period_last+1==email.length && errorcode==0)
        errorcode = 8;
    
    var error="";
    switch(errorcode)
    {
        case 1:
            error="Invalid character: space";
            break;
        case 2:
            error="No '@' sign found";
            break;
        case 3:
            error="Multiple '@' signs found";
            break;
        case 4:
            error="No '.' found";
            break;
        case 5:
            error="No '.' found after '@' sign";
            break;
        case 6:
            error="No email handle found (HANDLE@host.extension)";
            break;
        case 7:
            error="No email host found (handle@HOST.extension)";
            break;
        case 8:
            error=" No email host extension found (handle@host.EXTENSION)";
            break;
        case 9:
            error="No email address provided";
        default:
            break;
    }
    if (error!="")
        return "Invalid email address: "+error;
    return "";
}
function trim(str)
{
    if (str.length>0)
    {
        var c=str.substring(0,1);
        while (isTrimmableChar(c))
        {
            str=str.substring(1);
            c=str.substring(0,1);
        }
    }
    if (str.length>0)
    {
        var c=str.charAt(str.length-1);
        while (isTrimmableChar(c))
        {
            str=str.substring(0,str.length-1);
            c=str.charAt(str.length-1);
        }
    }
    return str;
}
function isTrimmableChar(cstr)
{
    if (cstr==" ")
        return true;
    if (cstr=="\n")
        return true;
    if (cstr=="\r")
        return true;
    return false;
}
function replace(str,old,newstr)
{
    var retVal="";
    while (str.indexOf(old)!=-1)
    {
      retVal+=str.substring(0,str.indexOf(old))+newstr;
      str=str.substring(str.indexOf(old)+old.length);
    }
    retVal+=str;
    return retVal;
}
function isMSIE() {
    return (navigator.userAgent.indexOf("MSIE")!=-1);
}
