// File: /include/js/Shared/ClientHandlers/AccountHandler/AccountHandler.js
// Desc: Account handler
// $Revision: 21$
// $Date: 2007-07-27 15:28:49$
// $Author: Donnie Tognazzini$
// $NoKeywords$

/******************************************************************************
  Dependencies:

    Shared/Extensions/Object.js
    Shared/XML/XML.js
    Shared/XML/Exceptions.js
    Shared/Http/HTTP.js
    Shared/Http/URL.js
    Shared/Messages/Exceptions.js
    Shared/Messages/RequestMessages.js
    Shared/Messages/ResponseMessages.js

 ******************************************************************************/

/******************************************************************************
  ACC_ActivationHandler C'tor

    Parameters:
        [in] source
        [in] version
        [in] os

 ******************************************************************************/
function ACC_AccountHandler( source,
                             version,
                             os )
{
    this.m_source    = source;
    this.m_version   = version;
    this.m_os        = os;

    this.m_onRegisterAccountSucceeded = null;
    this.m_onRegisterAccountFailed    = null;

    this.m_onGetAccountInfoSucceeded  = null;
    this.m_onGetAccountInfoFailed     = null;

    this.m_onAuthenticateSucceeded    = null;
    this.m_onAuthenticateFailed       = null;

    this.m_onForgotPinSucceeded       = null;
    this.m_onForgotPinFailed          = null;

    // HTTP client for RegisterAccount messages
    var self = this;
    this.m_registerAccountHttpClient = new HttpClient( );
    this.m_registerAccountHttpClient.onError        = function( errorMessage )
    {
        self.handleRegisterAccountError( errorMessage );
    };
    this.m_registerAccountHttpClient.onSuccess      = function( xmlResponse )
    {
        self.handleRegisterAccountResponse( xmlResponse );
    };

    // HTTP client for GetAccountInfo messages
    this.m_getAccountInfoHttpClient = new HttpClient( );
    this.m_getAccountInfoHttpClient.onError         = function( errorMessage )
    {
        self.handleGetAccountInfoError( errorMessage );
    };
    this.m_getAccountInfoHttpClient.onSuccess       = function( xmlResponse )
    {
        self.handleGetAccountInfoResponse( xmlResponse );
    };

    // HTTP client for Authenticate messages
    this.m_authenticateHttpClient = new HttpClient( );
    this.m_authenticateHttpClient.onError         = function( errorMessage )
    {
        self.handleAuthenticateError( errorMessage );
    };
    this.m_authenticateHttpClient.onSuccess       = function( xmlResponse )
    {
        self.handleAuthenticateResponse( xmlResponse );
    };

    // HTTP client for ForgotPin messages
    this.m_forgotPinHttpClient = new HttpClient( );
    this.m_forgotPinHttpClient.onError         = function( errorMessage )
    {
        self.handleForgotPinError( errorMessage );
    };
    this.m_forgotPinHttpClient.onSuccess       = function( xmlResponse )
    {
        self.handleForgotPinResponse( xmlResponse );
    };
}

ACC_AccountHandler.prototype =
{
    IsAuthenticationRequestInProgress : function( )
    {
        try
        {
            return this.m_authenticateHttpClient.IsRequestInProgress( ) ||
                   this.m_forgotPinHttpClient.IsRequestInProgress( )    ||
                   this.m_getAccountInfoHttpClient.IsRequestInProgress( );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "ACC_AccountHandler::IsAuthenticationRequestInProgress( )" );
        }
    },

    IsRegistrationRequestInProgress : function( )
    {
        try
        {
            return this.m_registerAccountHttpClient.IsRequestInProgress( ) ||
                   this.m_getAccountInfoHttpClient.IsRequestInProgress( );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "ACC_AccountHandler::IsRegistrationRequestInProgress( )" );
        }
    },

    StopAuthenticationRequests : function( )
    {
        try
        {
            this.m_authenticateHttpClient.reset( )
        }
        catch ( e )
        {
            EX_Log( "ACC_AccountHandler::StopAuthenticationRequests( ) - stopping authenticate request\n" + e.message );
        }

        try
        {
            this.m_forgotPinHttpClient.reset( );
        }
        catch ( e )
        {
            EX_Log( "ACC_AccountHandler::StopAuthenticationRequests( ) - stopping forgot pin request\n" + e.message );
        }

        this.StopGetAccountInfoRequest( );
    },

    StopGetAccountInfoRequest : function( )
    {
        try
        {
            this.m_getAccountInfoHttpClient.reset( );
        }
        catch ( e )
        {
            EX_Log( "ACC_AccountHandler::StopGetAccountInfoRequest( )\n" + e.message );
        }
    },

    StopRegistrationRequests : function( )
    {
        try
        {
            this.m_registerAccountHttpClient.reset( );
        }
        catch ( e )
        {
            EX_Log( "ACC_AccountHandler::StopRegistrationRequests( )\n" + e.message );
        }

        this.StopGetAccountInfoRequest( );
    },

    StopAllRequests : function( )
    {
        try
        {
            this.StopRegistrationRequests( );

            this.StopAuthenticationRequests( );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "ACC_AccountHandler::StopAllRequests( )" );
        }
    },

    /**************************************************************************
       Invoke callback functions
     **************************************************************************/

    // RegisterAccount
    invokeOnRegisterAccountSucceededCallback:function( responseMessage )
    {
        if ( this.m_onRegisterAccountSucceeded != null )
        {
            this.m_onRegisterAccountSucceeded( responseMessage );
        }
    },

    invokeOnRegisterAccountFailedCallback:function( statusMessage )
    {
        if ( this.m_onRegisterAccountFailed != null )
        {
            this.m_onRegisterAccountFailed( statusMessage );
        }
    },

    // GetAccountInfo
    invokeOnGetAccountInfoSucceededCallback:function( responseMessage )
    {
        if ( this.m_onGetAccountInfoSucceeded != null )
        {
            this.m_onGetAccountInfoSucceeded( responseMessage );
        }
    },

    invokeOnGetAccountInfoFailedCallback:function( statusMessage )
    {
        if ( this.m_onGetAccountInfoFailed != null )
        {
            this.m_onGetAccountInfoFailed( statusMessage );
        }
    },

    // Authenticate
    invokeOnAuthenticateSucceededCallback:function( responseMessage )
    {
        if ( this.m_onAuthenticateSucceeded != null )
        {
            this.m_onAuthenticateSucceeded( responseMessage );
        }
    },

    invokeOnAuthenticateFailedCallback:function( statusMessage )
    {
        if ( this.m_onAuthenticateFailed != null )
        {
            this.m_onAuthenticateFailed( statusMessage );
        }
    },

    // ForgotPin
    invokeOnForgotPinSucceededCallback:function( responseMessage )
    {
        if ( this.m_onForgotPinSucceeded != null )
        {
            this.m_onForgotPinSucceeded( responseMessage );
        }
    },

    invokeOnForgotPinFailedCallback:function( statusMessage )
    {
        if ( this.m_onForgotPinFailed != null )
        {
            this.m_onForgotPinFailed( statusMessage );
        }
    },

    /**************************************************************************
       RegisterAccount methods
     **************************************************************************/

    registerAccount:function( phoneNumber,
                              pin,
                              emailAddress,
                              custName,
                              referralSource )
    {
        try
        {
            var requestMessage = new MSG_RegisterAccountRequest( this.m_source,
                                                                 this.m_version,
                                                                 this.m_os,
                                                                 phoneNumber,
                                                                 pin,
                                                                 emailAddress,
                                                                 custName,
                                                                 referralSource );

            this.m_registerAccountHttpClient.sendRequest( HTTP_VERB_POST,
                                                          ( VM_WIDGET_SOURCE != 'SG' &&
                                                            VM_WIDGET_SOURCE != 'VG' &&
                                                            VM_WIDGET_SOURCE != 'SW' ) ? VM_URL.WSS1
                                                                                       : VM_URL.WS1,
                                                          ( VM_WIDGET_SOURCE != 'SG' &&
                                                            VM_WIDGET_SOURCE != 'VG' &&
                                                            VM_WIDGET_SOURCE != 'SW' ) ? VM_URL.WSS2
                                                                                       : VM_URL.WS2,
                                                          "text/xml",
                                                          requestMessage.asXML( ) );
        }
        catch ( e )
        {
            this.invokeOnRegisterAccountFailedCallback( "ACC_AccountHandler::registerAccount( ) : Failed to send RegisterAccount message :\n" + e.message );
        }
    },

    handleRegisterAccountResponse:function( xmlResponse )
    {
        try
        {
            var responseMessage = new MSG_RegisterAccountResponse( xmlResponse );

            this.invokeOnRegisterAccountSucceededCallback( responseMessage );
        }
        catch ( e )
        {
            // Failed to parse/process the response
            this.invokeOnRegisterAccountFailedCallback( "ACC_AccountHandler::handleRegisterAccountResponse( ) : Failed to process RegisterAccount response :\n" + e.message );
        }
    },

    handleRegisterAccountError:function( errorMessage )
    {
        this.invokeOnRegisterAccountFailedCallback( errorMessage );
    },


    /**************************************************************************
       GetAccountInfo methods
     **************************************************************************/

    getAccountInfo:function( accountNumber, uString )
    {
        try
        {
            var requestMessage = new MSG_GetAccountInfoRequest( this.m_source,
                                                                this.m_version,
                                                                this.m_os,
                                                                accountNumber,
                                                                uString );

            this.m_getAccountInfoHttpClient.sendRequest( HTTP_VERB_POST,
                                                    VM_URL.WX1,
                                                    VM_URL.WX2,
                                                    "text/xml",
                                                    requestMessage.asXML( ) );
        }
        catch ( e )
        {
            this.invokeOnGetAccountInfoFailedCallback( "ACC_AccountHandler::getAccountInfo( ) : Failed to send GetAccountInfo message :\n" + e.message );
        }
    },

    handleGetAccountInfoResponse:function( xmlResponse )
    {
        try
        {
            var responseMessage = new MSG_GetAccountInfoResponse( xmlResponse );

            this.invokeOnGetAccountInfoSucceededCallback( responseMessage );
        }
        catch ( e )
        {
            // Failed to parse/process the response
            this.invokeOnGetAccountInfoFailedCallback( "ACC_AccountHandler::handleGetAccountInfoResponse( ) : Failed to process GetAccountInfo response :\n" + e.message );
        }
    },

    handleGetAccountInfoError:function( errorMessage )
    {
        this.invokeOnGetAccountInfoFailedCallback( errorMessage );
    },


    /**************************************************************************
       authenticate methods
     **************************************************************************/

    authenticate:function( phoneNumber, pin )
    {
        try
        {
            var requestMessage = new MSG_AuthenticateRequest( this.m_source,
                                                              this.m_version,
                                                              this.m_os,
                                                              phoneNumber,
                                                              pin );

            this.m_authenticateHttpClient.sendRequest( HTTP_VERB_POST,
                                                      VM_URL.WX1,
                                                      VM_URL.WX2,
                                                      "text/xml",
                                                      requestMessage.asXML( ) );
        }
        catch ( e )
        {
            this.invokeOnAuthenticateFailedCallback( "ACC_AccountHandler::authenticate( ) : Failed to send Authenticate message :\n" + e.message );
        }
    },

    handleAuthenticateResponse:function( xmlResponse )
    {
        try
        {
            var responseMessage = new MSG_AuthenticateResponse( xmlResponse );

            this.invokeOnAuthenticateSucceededCallback( responseMessage );
        }
        catch ( e )
        {
            // Failed to parse/process the response
            this.invokeOnAuthenticateFailedCallback( "ACC_AccountHandler::handleAuthenticateResponse( ) : Failed to process Authenticate response :\n" + e.message );
        }
    },

    handleAuthenticateError:function( errorMessage )
    {
        this.invokeOnAuthenticateFailedCallback( errorMessage );
    },


    /**************************************************************************
       ForgotPin methods
     **************************************************************************/

    forgotPin:function( phoneNumber )
    {
        try
        {
            var requestMessage = new MSG_ForgotPinRequest( this.m_source,
                                                           this.m_version,
                                                           this.m_os,
                                                           phoneNumber );

            this.m_forgotPinHttpClient.sendRequest( HTTP_VERB_POST,
                                                    VM_URL.WS1,
                                                    VM_URL.WS2,
                                                    "text/xml",
                                                    requestMessage.asXML( ) );
        }
        catch ( e )
        {
            this.invokeOnForgotPinFailedCallback( "ACC_AccountHandler::forgotPin( ) : Failed to send ForgotPin message :\n" + e.message );
        }
    },

    handleForgotPinResponse:function( xmlResponse )
    {
        try
        {
            var responseMessage = new MSG_ForgotPinResponse( xmlResponse );

            this.invokeOnForgotPinSucceededCallback( responseMessage );
        }
        catch ( e )
        {
            // Failed to parse/process the response
            this.invokeOnForgotPinFailedCallback( "ACC_AccountHandler::handleForgotPinResponse( ) : Failed to process ForgotPin response:\n" + e.message );
        }
    },

    handleForgotPinError:function( errorMessage )
    {
        this.invokeOnForgotPinFailedCallback( errorMessage );
    }
};
