// File: $callWave/web/web site/htdocs/widgets/sms/resources/js/shared/clienthandlers/accounthandler/AccountHandler.js
// Desc: Handler class for account-management server functions.
// $Revision: 35$
// $Date: 4/24/2007 11:14:07 AM$
// $Author: Donnie Tognazzini$

function LoginHandler( source,
                             version,
                             os )
{
    this.m_source    = source;
    this.m_version   = version;
    this.m_os        = os;
    this.m_proxyPath = null;

    this.m_onAuthenticateSucceeded    = null;
    this.m_onAuthenticateFailed       = null;
   
    var self = this;  

    // 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 );
    };
}

LoginHandler.prototype =
{
    setProxyPath : function(path)
    {
        this.m_proxyPath = path; 
    },
      
    /**************************************************************************
       Invoke callback functions
     **************************************************************************/

    // 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 );
        }
    },

    /**************************************************************************
       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,
                                                      this.m_proxyPath,
                                                      this.m_proxyPath,
                                                      "text/xml",
                                                      requestMessage.asXML( ) );
        }
        catch ( e )
        {
            this.invokeOnAuthenticateFailedCallback( "LoginHandler::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( "LoginHandler::handleAuthenticateResponse( ) : Failed to process Authenticate response :\n" + e.message );
        }
    },

    handleAuthenticateError:function( errorMessage )
    {
        this.invokeOnAuthenticateFailedCallback( errorMessage );
    }
};

