// File: /include/js/Shared/Views/LoginView.js
// Desc: Login view
// $Revision: 35$
// $Date: 5/14/2007 12:10:36 PM$
// $Author: Donnie Tognazzini$
// $NoKeywords$

/******************************************************************************
  VM_LoginView
 ******************************************************************************/
function VM_LoginView( viewController )
{
    try
    {
        UTILS.inheritFromBase( this, "VM_LoginView", "#loginViewFrame" );

        // Initialize members
        this.m_viewController     = viewController;
        this.m_cellNumberTextArea = new NANPPhoneInput( "txtLoginCellNumber", "btnLoginSubmit" );
        this.m_pinTextArea        = new PasswordInput( "txtLoginPIN", "btnLoginSubmit" );
        this.m_submitButton       = document.getElementById ( "btnLoginSubmit" );

        // Event handlers

        this.bindToEvent( "#lnkForgotPIN",         "click", this.onSubmitForgotPIN );
        this.bindToEvent( this.m_submitButton,     "click", this.onSubmitLoginAttempt );

        this.bindToEvent( "AuthenticationSuccess" );
        this.bindToEvent( "AuthenticationFailure" );
        this.bindToEvent( "ForgotPinSuccess" );
        this.bindToEvent( "ForgotPinFailure" );
        this.bindToEvent( "viewStateChange" );
    }
    catch( e )
    {
        EX_ASSERT_NO_EXCEPTIONS( e, "VM_LoginView::VM_LoginView( )" );
    }
}

var VM_LoginView_prototype =
{
    DEFAULT_MESSAGE             : "Please enter your registered phone number and PIN to continue.",
    DEFAULT_PIN_REQUEST_MESSAGE : "Failed to send a PIN reminder. Please try again later.",
    PIN_SUCCESS_MESSAGE         : "PIN reminder sent to your cell phone. Enter your PIN when ready.",
    CELL_NUMBER_MESSAGE         : "Please enter your 10-digit registered cell number.",
    PIN_MISSING_MESSAGE         : "Please enter your PIN.",
    LOGIN_FAILED_MESSAGE        : "Login failed. Please try again.",

    /***************************************************************************
     ***************************************************************************
      Event handlers
     ***************************************************************************
     ***************************************************************************/

    onSubmitForgotPIN : function( e )
    {
        try
        {
            e.preventDefault( ); // TBD: what is this?

            if ( this.m_viewController.m_accountHandler.IsAuthenticationRequestInProgress( ) )
            {
                // Ignore if request already in progress
                return;
            }

            // Validate input

            var cellNumber = this.m_cellNumberTextArea.GetValidatedInput( this.CELL_NUMBER_MESSAGE, this.CELL_NUMBER_MESSAGE );

            if ( cellNumber === null )
            {
                return;
            }

            // Clear PIN input field
            this.m_pinTextArea.jInput.val( "" );

            // Submit request
             PREF_GetPreferences( ).CellNumber.set( cellNumber );
            this.m_viewController.m_accountHandler.forgotPin( cellNumber );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_LoginView::onSubmitForgotPIN( )" );
        }
    },

    onForgotPinSuccess : function( e, responseMessage )
    {
        try
        {
            switch ( responseMessage.m_status )
            {
            case MSG_ResponseStatus.SUCCESS:
                {
                    CW_GetMessageArea( ).display( this.PIN_SUCCESS_MESSAGE, CW_GetMessageArea( ).MESSAGE_TYPE_STATUS );
                    break;
                }

            case MSG_ResponseStatus.UNEXPECTED_ERROR:
            case MSG_ResponseStatus.RECOVERABLE_ERROR:
                {
                    CW_GetMessageArea( ).display( this.DEFAULT_PIN_REQUEST_MESSAGE, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );
                    break;
                }

            case MSG_ResponseStatus.GENERAL_ERROR:
                {
                    CW_GetMessageArea( ).display( responseMessage.m_statusMsg, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );
                    break;
                }

            default:
                // Other error
                {
                    var message = ( responseMessage.m_statusMsg != null && responseMessage.m_statusMsg.length != 0 ) ?
                                        responseMessage.m_statusMsg :
                                        this.DEFAULT_PIN_REQUEST_MESSAGE;

                    CW_GetMessageArea( ).display( message, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );

                    break;
                }
            }
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_LoginView::onForgotPinSuccess( )" );
        }
    },

    onForgotPinFailure : function( e, statusMessage )
    {
        try
        {
            CW_GetMessageArea( ).display( this.DEFAULT_PIN_REQUEST_MESSAGE, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );

            EX_Log( "VM_LoginView::onForgotPinFailure( ) : " + statusMessage );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_LoginView::onForgotPinFailure( )" );
        }
    },

    onSubmitLoginAttempt : function( e )
    {
        try
        {
            if ( this.m_viewController.m_accountHandler.IsAuthenticationRequestInProgress( ) )
            {
                // Ignore if request already in progress
                return;
            }

            // Validate input

            var cellNumber = this.m_cellNumberTextArea.GetValidatedInput( this.CELL_NUMBER_MESSAGE, this.CELL_NUMBER_MESSAGE );

            if ( cellNumber === null )
            {
                return;
            }

            var PIN = this.m_pinTextArea.GetValidatedInput( this.PIN_MISSING_MESSAGE, this.LOGIN_FAILED_MESSAGE );

            if ( PIN === null )
            {
                return;
            }

            PREF_GetPreferences( ).CellNumber.set( cellNumber );

            CW_GetMessageArea( ).display( "Connecting, please wait...", CW_GetMessageArea( ).MESSAGE_TYPE_STATUS );

            this.enableSubmitButton( false /* disable */ );

            // Submit request
            this.m_viewController.m_accountHandler.authenticate( cellNumber, PIN );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_LoginView::onSubmitLoginAttempt( )" );
        }
    },

    onAuthenticationSuccess : function( e, responseMessage )
    {
        try
        {
            this.enableSubmitButton( true /* enable */ );

            switch ( responseMessage.m_status )
            {
            case MSG_ResponseStatus.SUCCESS:
                {
                    this.m_viewController.updateQueryString( responseMessage.m_uString );

                    PREF_GetPreferences( ).AuthInfo.set( { accountNumber: responseMessage.m_account,
                                                           uString:       responseMessage.m_uString } );

                    this.m_viewController.onGetAccountInfo( );

                    break;
                }

            case MSG_ResponseStatus.GENERAL_ERROR:
            default:
                {
                    CW_GetMessageArea( ).display( responseMessage.m_statusMsg, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );
                    break;
                }

            case MSG_ResponseStatus.AUTH_PIN_MISMATCH:
                {
                    PREF_GetPreferences( ).AuthInfo.reset( );

                    CW_GetMessageArea( ).display( responseMessage.m_statusMsg, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );

                    break;
                }
            }
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_LoginView::onAuthenticationSuccess( )" );
        }
    },

    onAuthenticationFailure : function( e, statusMessage )
    {
        try
        {
            this.enableSubmitButton( true /* enable */ );

            CW_GetMessageArea( ).display( this.LOGIN_FAILED_MESSAGE, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );

            EX_Log( "VM_LoginView::onAuthenticationFailure( ) : " + statusMessage );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_LoginView::onAuthenticationFailure( )" );
        }
    },

    /***************************************************************************
     ***************************************************************************
      View painting methods
     ***************************************************************************
     ***************************************************************************/

    onviewStateChange : function( e, oldValue, newValue )
    {
        try
        {
            if ( oldValue == this.m_viewController.VIEW_STATE_SHOW_LOGIN )
            {
                this.enableSubmitButton( true /* enable */ );
            }
            else if ( newValue == this.m_viewController.VIEW_STATE_SHOW_LOGIN )
            {
                CW_GetMessageArea( ).display( this.DEFAULT_MESSAGE, CW_GetMessageArea( ).MESSAGE_TYPE_STATUS );
                this.m_cellNumberTextArea.Set( PREF_GetPreferences( ).CellNumber.get( ) );
                this.m_pinTextArea.jInput.val( "" );
                UTILS.adjustHeight( );
            }
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_LoginView::onviewStateChange( )" );
        }
    },

    /***************************************************************************
     ***************************************************************************
      Set methods
     ***************************************************************************
     ***************************************************************************/

    enableSubmitButton : function( enabled )
    {
        if ( enabled )
        {
            this.m_submitButton.disabled = false;
            $( this.m_submitButton ).removeClass( "disabled" );
        }
        else
        {
            this.m_submitButton.disabled = true;
            $( this.m_submitButton ).addClass( "disabled" );
        }
    }
};
