function Process_login()
{
    var email = GV( "email" );
    var password = GV( "password" );
    var remember_me = GE( "remember_me" ).checked ? "1" : "0";

    Set_login_status_text( "Please Wait" );
    var request = new XML_request( "login",
                                   "?page=main&option=login",
                                   "email=" + email + "&password=" + password
                                 + "&remember_me=" + remember_me,
                                   Main_guest_result_handler, true );
}

function Set_login_status_text( text, enable_login )
{
    Set_status_text( "login", text );
}

function Process_forgot_password()
{
    var email = GV( "forgot_password_email" );

    if( !Is_valid_email_address( email ) ) {
        Set_status_text( "forgot_password", MAIN_GUEST_INVALID_EMAIL_ADDRESS );
        return;
    }

    var request = new XML_request( "forgot_password", "?page=main&option=forgot_password",
                                   "email=" + email, Main_guest_result_handler, true );
}

function Process_activation_email()
{
    var email = GV( "activation_email" );

    if( !Is_valid_email_address( email ) ) {
        Set_status_text( "activation_email", MAIN_GUEST_INVALID_EMAIL_ADDRESS );
        return;
    }

    var request = new XML_request( "activation_email", "?page=main&option=send_activation_code",
                                   "email=" + email, Main_guest_result_handler, true );
}

function Process_activation_code()
{
    var code = GV( "activation_code" );

    if( code.length == 0 ) {
        Set_status_text( "activation_code", MAIN_GUEST_ENTER_A_CODE );
        return;
    }

    var request = new XML_request( "activation_code", "?page=main&option=activate",
                                   "code=" + code, Main_guest_result_handler, true );
}

function Process_join()
{
    var email = GV( "join_email" );
    var email_confirm = GV( "join_email_confirm" );
    var nickname = GV( "nickname" );
    var password = GV( "password" );
    var password_2 = GV( "password_2" );

    if( !Is_valid_email_address( email ) ) {
        Set_status_text( "join", MAIN_GUEST_INVALID_EMAIL_ADDRESS );
        return;
    } else if( email != email_confirm ) {
        Set_status_text( "join", MAIN_GUEST_EMAILS_DO_NOT_MATCH );
        return;
    } else if( nickname.length < 4 ) {
        Set_status_text( "join", MAIN_GUEST_NICKNAME_IS_TOO_SHORT );
        return;
    } else if( nickname.length > 64 ) {
        Set_status_text( "join", MAIN_GUEST_NICKNAME_IS_TOO_LONG );
        return;
    } else if( password != password_2 ) {
        Set_status_text( "join", MAIN_GUEST_PASSWORDS_DO_NOT_MATCH );
        return;
    } else if( password.length < 6 ) {
        Set_status_text( "join", MAIN_GUEST_PASSWORD_TOO_SHORT );
        return;
    }

    var request = new XML_request( "join", "?page=main&option=process_join",
                                   "email=" + email + "&nickname=" + nickname
                                 + "&password=" + password,
                                   Main_guest_result_handler, true );
}

function Main_guest_result_handler( id, result, data, callback_data )
{
    if( id == "login" ) {
        if( result ) {
            if( data == "ok" ) {
                Refresh_page();
            } else {
                Redirect( "?page=main&option=login_error&error=" + data );
            }
        } else {
            Set_login_status_text( BASE_ERROR_CONTACTING_SERVER, true );
        }
    } else if( id == "forgot_password" ) {
        if( result ) {
            if( data == "ok" ) {
                Redirect( "?page=main&option=login_error&error=password_reset" );
            } else if( data == "email" ) {
                Set_status_text( "forgot_password", MAIN_GUEST_INVALID_EMAIL_ADDRESS );
            } else if( data == "user_not_found" ) {
                Set_status_text( "forgot_password", MAIN_GUEST_EMAIL_NOT_FOUND );
            } else if( data == "inactive" ) {
                Redirect( "?page=main&option=show_activate" );
            } else {
                Set_status_text( "forgot_password", BASE_UNKNOWN_ERROR );
            }
        } else {
            Set_status_text( "forgot_password", BASE_ERROR_CONTACTING_SERVER );
        }
    } else if( id == "activation_email" ) {
        if( result ) {
            if( data == "ok" ) {
                Redirect( "?page=main&option=enter_activation_code" );
            } else if( data == "email" || data == "user_not_found" ) {
                Set_status_text( "activation_email", MAIN_GUEST_INVALID_EMAIL_ADDRESS );
            } else if( data == "active" ) {
                Redirect( "?page=main&option=activated" );
            } else {
                Set_status_text( "activation_email", BASE_UNKNOWN_ERROR );
            }
        } else {
            Set_status_text( "activation_email", BASE_ERROR_CONTACTING_SERVER );
        }
    } else if( id == "activation_code" ) {
        if( result ) {
            if( data == "ok" ) {
                Redirect( "?page=main&option=activated" );
            } else {
                Set_status_text( "activation_code", BASE_UNKNOWN_ERROR );
            }
        } else {
            Set_status_text( "activation_code", BASE_ERROR_CONTACTING_SERVER );
        }
    } else if( id == "join" ) {
        if( result ) {
            if( data == "ok" ) {
                Redirect( "?page=main&option=enter_activation_code" );
            } else if( data == "email_invalid" ) {
                Set_status_text( "join", MAIN_GUEST_INVALID_EMAIL_ADDRESS );
            } else if( data == "email_taken" ) {
                Set_status_text( "join", MAIN_GUEST_EMAIL_IN_USE );
            } else if( data == "nickname_too_short" ) {
                Set_status_text( "join", MAIN_GUEST_NICKNAME_IS_TOO_SHORT );
            } else if( data == "nickname_too_long" ) {
                Set_status_text( "join", MAIN_GUEST_NICKNAME_IS_TOO_LONG );
            } else if( data == "nickname_invalid" ) {
                Set_status_text( "join", MAIN_GUEST_NICKNAME_IS_INVALID );
            } else if( data == "email_in_use" ) {
                Set_status_text( "join", MAIN_GUEST_EMAIL_IS_IN_USE );
            } else if( data == "nickname_in_use" ) {
                Set_status_text( "join", MAIN_GUEST_NICKNAME_IN_USE );
            } else if( data == "password" ) {
                Set_status_text( "join", MAIN_GUEST_PASSWORD_REQUIRED );
            } else {
                Set_status_text( "join", BASE_UNKNOWN_ERROR );
            }
        } else {
            Set_status_text( "join", BASE_ERROR_CONTACTING_SERVER );
        }
    }
}
