function RegisterSubmit(form)
{
    SetError("error", "");
    SetError("email_error", "");
    SetError("password_error", "");
    SetError("password_confirm_error", "");
    successful = true;
    with (form)
    {
        if (!ValidateRequired(email))
        {
            SetError("email_error", "Missing email address");
            successful = false;
        }
        else
        {
            if (!ValidateEmail(email))
            {
                SetError("email_error", "Invalid email address");
                successful = false;
            }
        }

        hasPassword = true;
        if (!ValidateRequired(password))
        {
            SetError("password_error", "Missing password");
            hasPassword = false;
        }       

        if (!ValidateRequired(password_confirm))
        {
            SetError("password_confirm_error", "Missing password");
            hasPassword = false;
        }

        if (hasPassword)
        {
            if (!ValidateEqual(password, password_confirm))
            {
                SetError("password_confirm_error", "Passwords do not match");
                hasPassword = false;            
            }
        }
        if (!hasPassword)
            successful = false;
    }
    return successful;
}

function ValidateRequired(field)
{
    with (field)
    {
        if (value == null || value == "")
            return false;
    }
    return true;
}

function ValidateEqual(field1, field2)
{
    return field1.value == field2.value;
}

function ValidateEmail(field)
{
    with (field)
    {
        at = value.indexOf("@");
        dot = value.lastIndexOf(".");
        if (at < 1 || dot - at < 2 || dot + 1 == value.length) 
            return false;
        invalid = "!#$%^&*()[]{}|;':,<>";
        for (i = 0; i < invalid.length; ++i)
        {
            ch = invalid.charAt(i);
            if (value.indexOf(ch) >= 0)
                return false;
        }
    }
    return true;
}

function SetError(id, text)
{
    document.getElementById(id).innerHTML = text;
}

