// $Revision: 38$
// $Date: 2007-07-27 4:08:57 PM$
// $Author: Donnie Tognazzini$
// $NoKeywords$

/******************************************************************************
  Dependencies:

    Shared/Extensions/Object.js
    Shared/XML/XML.js
    Shared/XML/Exceptions.js
    Shared/Calls/Calls.js
    Shared/Types.js

 ******************************************************************************/

/******************************************************************************
  Constants
 ******************************************************************************/
var MSG_XML_RESPONSE_ELEMENT_NAME                       = "response";

/******************************************************************************
  MSG_ResponseStatus
    Defines all the response status codes for every response message.
 ******************************************************************************/
var MSG_ResponseStatus =
{
    // generic response statuses
    SUCCESS                 : 0,
    UNEXPECTED_ERROR        : 1,
    GENERAL_ERROR           : 2,
    RECOVERABLE_ERROR       : 3,

    REGA_PIN_MISMATCH       : 10,

    SETC_LOGIN_FAILURE      : 11,

    VPHO_LOGIN_FAILURE      : 11,
    VPHO_COUNTRY_NOT_SUPPORTED : 12,

    AUTH_PIN_MISMATCH       : 10,
    AUTH_ACCOUNT_LOCKED     : 11,
    AUTH_UPGRADE_REQUIRED   : 12,

    GETA_LOGIN_FAILURE      : 11,
    GETA_UPGRADE_REQUIRED   : 12,

    GCAL_LOGIN_FAILURE      : 11,

    SSMS_LOGIN_REQUIRED     : 11
};

/******************************************************************************
  MSG_ResponseBase
 ******************************************************************************/
function MSG_ResponseBase( xmlDom )
{
    this.m_type      = '';
    this.m_status    = MSG_ResponseStatus.SUCCESS;
    this.m_statusMsg = '';
    this.xmlProperties_base = ["type", "status", "statusMsg"];
    this.xmlProperties = this.xmlProperties_base;
    this.xmlTopTag   = ''; // TODO -- make it this MSG_XML_RESPONSE_ELEMENT_NAME;

    this.expectedType = null;
    if ( xmlDom ) {
    this.initFromXml( xmlDom );
    }
    else if (this.expectedType) {
    throw new MSG_EX_EmptyResponse( "No XML DOM passed to constructor.  Expected xmlDom with type[" + this.expectedType + "]" );
    }
}
MSG_ResponseBase.prototype =
{
    initFromXml: function(xmlDom)
    {
        if ( ! xmlDom.firstChild )
        {
            throw new MSG_EX_EmptyResponse( );
        }

        XML_InitializeMembersFromXML( xmlDom, this, MSG_XML_RESPONSE_ELEMENT_NAME );

        if ( this.expectedType && this.m_type != this.expectedType )
        {
            throw new MSG_EX_WrongMessageType( this.m_type, this.expectedType );
        }

        // make integer values integers
        this.m_status      = parseInt( this.m_status );
    },

    asXML: function()
    {
        var topTag = this.xmlTopTag;
        if ((!topTag || topTag.length == 0) && this.expectedType)
        {
            // XML_RESPONSE if not tag or blank tag specifified AND expectedType specified
            // TODO - This logic can go away and be just topTag = this.xmlTopTag
            //        if we can show that MSG_GetResponseBaseXMLTags is only used in this file.
            topTag = MSG_XML_RESPONSE_ELEMENT_NAME;
        }

        return UTILS.propertiesAsXml( this, this.xmlProperties, topTag );
    }
}

function MSG_GetResponseBaseXMLTags( obj )
{
    return UTILS.propertiesAsXml( obj, obj.xmlProperties_base );
}


/******************************************************************************
  MSG_RegisterAccountResponse c'tor

    Constructs the message from an XML DOM tree.

    Exceptions:
        MSG_EX_FailedCreatingRegisterAccountResponse
 ******************************************************************************/
function MSG_RegisterAccountResponse( xmlResponse )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

        // add some items for the base asXML() to work with
        this.xmlProperties.push( 'account' );
        this.xmlProperties.push( 'uString' );
        this.xmlProperties.push( 'successCase' );

        // initialize the attributes
        this.m_account     = 0;
        this.m_uString     = '';
        this.m_successCase = 0;

        this.expectedType = MSG_MESSAGE_TYPE_REGISTER_ACCOUNT;
        this.initFromXml( xmlResponse );

        // make integer values integers
        this.m_account     = parseInt( this.m_account );
        this.m_successCase = parseInt( this.m_successCase );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingRegisterAccountResponse( e );
    }
}


/******************************************************************************
  MSG_SetCarrierInfoResponse c'tor

    Constructs the message from an XML DOM tree.

    Exceptions:
        MSG_EX_FailedCreatingSetCarrierInfoResponse
 ******************************************************************************/
function MSG_SetCarrierInfoResponse( xmlResponse )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

    this.expectedType = MSG_MESSAGE_TYPE_SET_CARRIER_INFO;
    this.initFromXml( xmlResponse );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingSetCarrierInfoResponse( e );
    }
}


/******************************************************************************
  MSG_VerifyPhoneResponse c'tor

    Constructs the message from an XML DOM tree.

    Exceptions:
        MSG_EX_FailedCreatingSetCarrierInfoResponse
 ******************************************************************************/
function MSG_VerifyPhoneResponse( xmlResponse )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

        this.xmlProperties.push( 'successCase' );
        this.m_successCase = 0;

        this.expectedType = MSG_MESSAGE_TYPE_VERIFY_PHONE;
        this.initFromXml( xmlResponse );

        // make integer values integers
        this.m_successCase = parseInt( this.m_successCase );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingVerifyPhoneResponse( e );
    }
}


/******************************************************************************
  MSG_GetAccountInfoResponse c'tor

    Constructs the message from an XML DOM tree.

    Exceptions:
        MSG_EX_FailedCreatingGetAccountInfoResponse
 ******************************************************************************/
function MSG_GetAccountInfoResponse( xmlResponse )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

        // add some items for the base asXML() to work with
        this.xmlProperties.push( 'phone' );
        this.xmlProperties.push( 'email' );
        this.xmlProperties.push( 'name' );
        this.xmlProperties.push( 'actStatus' );
        this.xmlProperties.push( 'carrierId' );
        this.xmlProperties.push( 'fwdCodes' );
        this.xmlProperties.push( 'carriers' );
        this.xmlProperties.push( 'secondsBetweenSync' ); // this was not in the asXML of the previous version but it makes it easier to compare values when testing.
        this.xmlProperties.push( 'phoneCC' );
        this.xmlProperties.push( 'phoneNN' );
        this.xmlProperties.push( 'phoneVerified' );
        this.xmlProperties.push( 'smsBodyOH' );         // sms body overhead to account for server inserted text
        this.xmlProperties.push( 'maxSMSParts' );
        this.xmlProperties.push( 'showCarriers' );      // determines whether to show SMS carrier selection pull-down for NANP customers
        this.xmlProperties.push( 'hideRegCarrier' );
        this.xmlProperties.push( 'okButtonDisplayDelayMS' );

        // initialize the attributes
        this.m_phone              = '';
        this.m_email              = '';
        this.m_name               = '';
        this.m_actStatus          = '';
        this.m_carrierId          = 0;
        this.m_fwdCodes           = null;
        this.m_carriers           = null;
        this.m_secondsBetweenSync = 0;
        this.m_phoneCC            = '';
        this.m_phoneNN            = '';
        this.m_phoneVerified      = 0;
        this.m_smsBodyOH          = 0;
        this.m_maxSMSParts        = 0;
        this.m_showCarriers       = 0;
        this.m_hideRegCarrier     = 0;
        this.m_okButtonDisplayDelayMS = 0;

        this.expectedType = MSG_MESSAGE_TYPE_GET_ACCOUNT_INFO;
        this.initFromXml( xmlResponse );

        // make integer values integers
        this.m_carrierId                = parseInt( this.m_carrierId );
        this.m_secondsBetweenSync       = parseInt( this.m_secondsBetweenSync );
        this.m_phoneVerified            = parseInt( this.m_phoneVerified );
        this.m_smsBodyOH                = parseInt( this.m_smsBodyOH );
        this.m_maxSMSParts              = parseInt( this.m_maxSMSParts );
        this.m_showCarriers             = parseInt( this.m_showCarriers );
        this.m_hideRegCarrier           = parseInt( this.m_hideRegCarrier );
        this.m_okButtonDisplayDelayMS   = parseInt( this.m_okButtonDisplayDelayMS );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingGetAccountInfoResponse( e );
    }
}


/******************************************************************************
  MSG_AuthenticateResponse c'tor

    Constructs the message from an XML DOM tree.

    Exceptions:
        MSG_EX_FailedCreatingAuthenticateResponse
 ******************************************************************************/
function MSG_AuthenticateResponse( xmlResponse )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

        // add some items for the base asXML() to work with
        this.xmlProperties.push( 'account' );
        this.xmlProperties.push( 'uString' );

        // initialize the attributes
        this.m_account   = 0;
        this.m_uString   = '';

        this.expectedType = MSG_MESSAGE_TYPE_AUTHENTICATE;
        this.initFromXml( xmlResponse );

        // make integer values integers
        this.m_account    = parseInt( this.m_account );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingAuthenticateResponse( e );
    }
}


/******************************************************************************
  MSG_ForgotPinResponse c'tor

    Constructs the message from an XML DOM tree.

    Exceptions:
        MSG_EX_FailedCreatingForgotPinResponse
 ******************************************************************************/
function MSG_ForgotPinResponse( xmlResponse )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

        this.expectedType = MSG_MESSAGE_TYPE_FORGOT_PIN;
        this.initFromXml( xmlResponse );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingForgotPinResponse( e );
    }
}


/******************************************************************************
  MSG_GetCallsResponse c'tor

    Constructs the message from an XML DOM tree.

    Exceptions:
        MSG_EX_FailedCreatingGetCallsResponse
 ******************************************************************************/
function MSG_GetCallsResponse( xmlDOMTree )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

        // add some items for the base asXML() to work with
        this.xmlProperties.push( 'tz' );
        this.xmlProperties.push( 'newSyncLevel' );
        this.xmlProperties.push( 'successCase' );
        this.xmlProperties.push( 'numNewCalls' );
        this.xmlProperties.push( 'calls' );

        // initialize the attributes
        this.m_tz           = '';
        this.m_newSyncLevel = 0;
        this.m_successCase  = 0;
        this.m_numNewCalls  = 0;
        this.m_calls        = null;

        this.expectedType = MSG_MESSAGE_TYPE_GET_CALLS;
        this.initFromXml( xmlDOMTree );

        // make integer values integers
        this.m_newSyncLevel = parseInt( this.m_newSyncLevel );
        this.m_successCase  = parseInt( this.m_successCase );
        this.m_numNewCalls  = parseInt( this.m_numNewCalls );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingGetCallsResponse( e );
    }
}


/******************************************************************************
  MSG_SendSMSResponse c'tor

    Constructs the message from an XML DOM tree.

    Exceptions:
        MSG_EX_FailedCreatingSendSMSResponse
 ******************************************************************************/
function MSG_SendSMSResponse( xmlResponse )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

        this.expectedType = MSG_MESSAGE_TYPE_SEND_SMS;
        this.initFromXml( xmlResponse );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingSendSMSResponse( e );
    }
}

/******************************************************************************
  MSG_GetSMSSendPopResponse class
 ******************************************************************************/
function MSG_GetSMSSendPopResponse( xmlDOMTree )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

        // initialize the attributes
        this.m_smsSendPop = null;

        this.expectedType = MSG_MESSAGE_TYPE_SMS_SEND_EP;
        this.initFromXml( xmlDOMTree );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingGetSMSSendPopResponse( e );
    }
}


/******************************************************************************
  MSG_GetCRMContentResponse class
 ******************************************************************************/
function MSG_GetCRMContentResponse( xmlDOMTree )
{
    try
    {
        EXT_extend( this, new MSG_ResponseBase( ) );

        // initialize the attributes
        this.m_content = null;

        this.expectedType = MSG_MESSAGE_TYPE_GET_CRM_CONTENT;
        this.initFromXml( xmlDOMTree );
    }
    catch ( e )
    {
        throw new MSG_EX_FailedCreatingGetCRMContentResponse( e );
    }
}

