// File: /include/js/Shared/Views/ActivationView.js
// Desc: Activation pane.
// $Revision: 27$
// $Date: 5/14/2007 12:10:34 PM$
// $Author: Donnie Tognazzini$
// $NoKeywords$

/******************************************************************************
  VM_ActivationView
 ******************************************************************************/
function VM_ActivationView( viewController )
{
    try
    {
        UTILS.inheritFromBase( this, "VM_ActivationView", "#activationViewFrame" );

        // Initialize members
        this.m_viewController = viewController;

        this.jStatus    = $( "#divActivationStatus" );
        this.jCarrierId = $( "#WIDGET_activationSelCarrierId" );

        this.addProperty( "activationStatus", true, TYPE_ACTIVATION_STATE_NOT_ACTIVATED );
        this.addProperty( "carrierId",        true, VM_CARRIER_ID_UNDEFINED );

        this.m_forwardCodeList   = null;
        this.m_carrierList       = null;
        this.m_lastSentCarrierId = VM_CARRIER_ID_UNDEFINED;
        this.m_carrierListLookup = { };

        this.m_activationHandler = new ACT_ActivationHandler( this.m_viewController.source,
                                                              this.m_viewController.version,
                                                              this.m_viewController.osVersion );

        // Event handlers

        this.bindToEvent( this.jCarrierId, "change", this.onCarrierIdSelected );
        this.bindToEvent( "SetCarrierIdSuccess" );
        this.bindToEvent( "SetCarrierIdFailure" );
        this.bindToEvent( PREF_KEY_AUTH_INFO + "Change" );

        this.bindToEvent( PREF_KEY_SHOW_ACTIVATION + "Change",  this.setVisibility );
        this.bindToEvent( "viewStateChange",                    this.setVisibility );
        this.bindToEvent( "activationStatusChange",             this.setVisibility );
    }
    catch ( e )
    {
        EX_ASSERT_NO_EXCEPTIONS( e, "VM_ActivationView::VM_ActivationView( )" );
    }
}

var VM_ActivationView_prototype =
{
    SET_CARRIER_ID_FAILED_STATUS_MESSAGE : "(Please reselect your carrier)",

    /***************************************************************************
     ***************************************************************************
      Event handlers
     ***************************************************************************
     ***************************************************************************/

    onCarrierIdSelected : function( e )
    {
        try
        {
            var carrierSelection  = e.target;
            var selectedCarrierId = carrierSelection.options[ carrierSelection.selectedIndex ].value;

            this.setCarrierInfo( selectedCarrierId );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_ActivationView::onCarrierIdSelected( )" );
        }
    },

    onSetCarrierIdSuccess : function( e, responseMessage )
    {
        try
        {
            this.jCarrierId.attr( "disabled", false );

            switch ( responseMessage.m_status )
            {
            case MSG_ResponseStatus.SUCCESS:
                {
                    this.jStatus.html( "(Account updated)" );
                    this.carrierId.set( this.m_lastSentCarrierId );
                    break;
                }

            case MSG_ResponseStatus.SETC_LOGIN_FAILURE:
                {
                    // Go to Login screen and present status message

                    this.m_viewController.AUTH_INFO.reset( );
                    this.m_viewController.viewState.set( this.m_viewController.VIEW_STATE_SHOW_LOGIN );

                    CW_GetMessageArea( ).display( responseMessage.m_statusMsg, CW_GetMessageArea( ).MESSAGE_TYPE_ALERT );

                    break;
                }

            case MSG_ResponseStatus.GENERAL_ERROR:
            default:
                {
                    this.jStatus.html( this.SET_CARRIER_ID_FAILED_STATUS_MESSAGE );
                    break;
                }
            }
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_ActivationView::onSetCarrierIdSuccess( )" );
        }
    },

    onSetCarrierIdFailure : function( e, statusMessage )
    {
        try
        {
            // Failed to update our account with the selected carrier id

            this.jStatus.html( this.SET_CARRIER_ID_FAILED_STATUS_MESSAGE );

            EX_Log( "VM_LoginView::onSetCarrierIdFailure( ) : " + statusMessage );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_ActivationView::onSetCarrierIdFailure( )" );
        }
    },

    oncarrierIdChange : function( e, oldCarrierId, newCarrierId )
    {
        if ( oldCarrierId == newCarrierId )
        {
            return;
        }

        this.displayActivationCodes( newCarrierId, true );
    },

    onAuthInfoChange : function( e, oldValue, newValue )
    {
        this.carrierId.reset( );
        this.activationStatus.reset( );
    },

    /***************************************************************************
     ***************************************************************************
      Set methods
     ***************************************************************************
     ***************************************************************************/

    setCarrierInfo : function( selectedCarrierId )
    {
        try
        {
            if ( this.carrierId.get( ) == selectedCarrierId )
            {
                // Don't bother re-submitting the same carrier
                return;
            }

            this.m_lastSentCarrierId = selectedCarrierId;

            this.jStatus.attr( "disabled", true );
            this.jStatus.html( "Updating carrier..." );

            this.m_activationHandler.setCarrierInfo(
                PREF_GetPreferences( ).AuthInfo.get( ).accountNumber,
                PREF_GetPreferences( ).AuthInfo.get( ).uString,
                selectedCarrierId );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_ActivationView::setCarrierInfo( )" );
        }
    },

    setAccountInfo : function( forwardCodeList,
                               carrierList,
                               carrierId,
                               activationStatus,
                               hideRegCarrier )
    {
        try
        {
            // Update metadata
            this.m_forwardCodeList = forwardCodeList;
            this.m_carrierList     = carrierList;

            this.activationStatus.set( activationStatus );

            // If the given carrierId is not in the carrier list, then set local carrierId to 'Other'
            this.carrierId.set( VM_CARRIER_ID_OTHER );

            if ( this.m_carrierList != null )
            {
                for ( var i = 0; i < this.m_carrierList.length; ++i )
                {
                    if ( carrierId == this.m_carrierList[i].m_id )
                    {
                        this.carrierId.set( carrierId );
                        break;
                    }
                }
            }

            // Update carrier menu
            this.refreshWirelessProviderPopup( );

            // Update activation code display
            this.displayActivationCodes( this.carrierId.get( ), hideRegCarrier === 0 );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_ActivationView::setAccountInfo( )" );
        }
    },

    handleLogout : function( )
    {
        try
        {
            this.m_forwardCodeList   = null;
            this.m_carrierList       = null;
            this.m_lastSentCarrierId = VM_CARRIER_ID_UNDEFINED;
            this.m_carrierListLookup = { };

            this.activationStatus.reset( );
            this.carrierId.reset( );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_ActivationView::handleLogout( )" );
        }
    },

    /***************************************************************************
     ***************************************************************************
      View painting methods
     ***************************************************************************
     ***************************************************************************/

    link : function( path, caption )
    {
        return tag(
            "a",
            {
                href : VM_URL.WWW + path + this.m_viewController.queryString.get( ),
                target: "_blank"
            },
            caption
        );
    },

    setVisibility : function( )
    {
        if ( this.activationStatus.get( ) == TYPE_ACTIVATION_STATE_NOT_ACTIVATED ||
             PREF_GetPreferences( ).ShowActivation.get( ) )
        {
            // Show the activation view if account is not activated or the
            // 'always show activation' preference is set.

            this.jObject[0].style.visible = true;
            this.jObject[0].style.display = "";

            UTILS.adjustHeight( );
        }
        else
        {
            // Hide the activation view.

            this.jObject[0].style.visible = false;
            this.jObject[0].style.display = "none";
        }
    },

    refreshWirelessProviderPopup : function( )
    {
        try
        {
            function AddOption( popup, option )
            {
                popup.options[ popup.options.length ] = option;
            }

            function AddDisabledOption( caption )
            {
                var option = new Option( caption, VM_CARRIER_ID_UNDEFINED );
                option.disabled = true;
                AddOption( wirelessProviderPopup, option );
            }

            this.m_carrierListLookup = { };

            if ( ! this.m_carrierList )
            {
                // Carrier list is empty
                return;
            }

            var bar                   = "-------------------";
            var carrierList           = this.m_carrierList;
            var selectedCarrierId     = ! this.carrierId.isReset( ) ? this.carrierId.get( ) : VM_CARRIER_ID_UNDEFINED;
            var wirelessProviderPopup = document.getElementById( "WIDGET_activationSelCarrierId" );
            var i                     = 0;

            var selectedOption        = null;

            // Remove all the existing providers
            while ( wirelessProviderPopup.length != 0 )
            {
                wirelessProviderPopup.remove( 0 );
            }

            AddDisabledOption( "Choose One" );
            AddDisabledOption( bar );

            // Add short-list first
            var shortListItemCount = 0;

            for ( i = 0; i < carrierList.length; ++i )
            {
                if ( carrierList[i].m_shortlist )
                {
                    option = new Option( carrierList[i].m_name, carrierList[i].m_id );

                    if ( carrierList[i].m_id == VM_CARRIER_ID_UNDEFINED )
                    {
                        option.disabled = true;
                    }
                    else if ( carrierList[i].m_id == selectedCarrierId )
                    {
                        selectedOption = option;
                    }

                    AddOption( wirelessProviderPopup, option );
                    this.m_carrierListLookup[ "id_" + carrierList[i].m_id ] = i;
                    ++shortListItemCount;
                }
            }

            if ( shortListItemCount > 0 )
            {
                AddDisabledOption( bar );
            }

            // Now add complete list

            for ( i = 0; i < carrierList.length; ++i )
            {
                if ( carrierList[i].m_name == "Other" )
                {
                    AddDisabledOption( bar );
                }

                var option = new Option( carrierList[i].m_name, carrierList[i].m_id );

                if ( ! carrierList[i].m_shortlist )
                {
                    if ( carrierList[i].m_id == VM_CARRIER_ID_UNDEFINED )
                    {
                        option.disabled = true;
                    }
                    else if ( carrierList[i].m_id == selectedCarrierId )
                    {
                        selectedOption = option;
                    }

                    this.m_carrierListLookup[ "id_" + carrierList[i].m_id ] = i;
                }

                AddOption( wirelessProviderPopup, option );
            }

            if ( selectedOption != null )
            {
                selectedOption.selected = true;
            }
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_ActivationView::refreshWirelessProviderPopup( )" );
        }
    },

    displayActivationCodes : function( carrierId, showCarrierSelection )
    {
        try
        {
            var forwardCodeId     = -1;
            var carrierName       = '';

            if ( this.m_carrierList && carrierId != VM_CARRIER_ID_OTHER &&
                 this.m_carrierListLookup.hasOwnProperty( "id_" + carrierId ) )
            {
                var carrier = this.m_carrierList[ this.m_carrierListLookup[ "id_" + carrierId ] ];
                forwardCodeId = carrier.m_fwdCodeId;
                carrierName   = carrier.m_name;
            }

            var step1Area               = document.getElementById( 'WIDGET_activationStep1TextArea' );
            var selectCarrierPopup      = document.getElementById( 'WIDGET_activationSelCarrierId');
            var step2Area               = document.getElementById( 'WIDGET_activationStep2TextArea' );
            var callToActionArea        = document.getElementById( 'WIDGET_callToActionTextArea' );
            var activationCodesArea     = document.getElementById( 'WIDGET_activationActCodesTextArea' );
            var step3Area               = document.getElementById( 'WIDGET_activationStep3TextArea' );
            var deactivationArea        = document.getElementById( 'WIDGET_activationDeactivationTextArea' );
            var deactivationCodesArea   = document.getElementById( 'WIDGET_activationDeactCodesTextArea' );

            step1Area.style.display             = 'none';
            selectCarrierPopup.style.display    = 'none';
            step2Area.style.display             = 'none';
            callToActionArea.style.display      = 'none';
            activationCodesArea.style.display   = 'none';
            step3Area.style.display             = 'none';
            deactivationArea.style.display      = 'none';
            deactivationCodesArea.style.display = 'none';

            activationCodesArea.innerHTML   = '';
            deactivationCodesArea.innerHTML = '';

            var stepNumber = 1;
            if ( showCarrierSelection )
            {
                step1Area.innerHTML = (stepNumber++) + '. Select your cell phone carrier:';

                step1Area.style.display = 'inline';
                selectCarrierPopup.style.display = 'inline';

                if ( carrierId == VM_CARRIER_ID_OTHER || forwardCodeId == "0" )
                {
                    step2Area.innerHTML = stepNumber + ". We do not have specific instructions for your carrier, but activation may be available.";
                    step2Area.style.display = 'block';

                    callToActionArea.innerHTML = this.link( "/v2w/faq.html", "Please check our FAQ for a complete list." );
                    callToActionArea.style.display = 'block';

                    UTILS.adjustHeight( );
                    return;
                }
            }

            if ( forwardCodeId == -1 )
            {
                var displayedCarrierName = "your carrier";

                if ( carrierName && carrierName.length && carrierId != VM_CARRIER_ID_OTHER )
                {
                    displayedCarrierName = carrierName;
                }

                deactivationArea.innerHTML = "";
                step3Area.innerHTML = "";

                step2Area.innerHTML =
                    stepNumber +
                    ". CallWave works with most major carriers including Cingular, Verizon, " +
                    "and T-Mobile. Unfortunately activation is not available for " + displayedCarrierName + ".";

                step2Area.style.display = 'block';

                callToActionArea.innerHTML = this.link( "/v2w/otherWidgets.html", "Check out CallWave's other widgets &amp; gadgets." );
                callToActionArea.style.display = 'block';

                UTILS.adjustHeight( );
                return;
            }

            var forwardCodeList        = this.m_forwardCodeList;
            var activationCodes        = "";
            var deactivationCodes      = "";
            var activationCodePhrase   = "this code";
            var deactivationCodePhrase = "this code";

            for ( var i = 0; i < forwardCodeList.length; ++i )
            {
                if ( forwardCodeList[i].m_id == forwardCodeId )
                {
                    var re = /\n/g;

                    activationCodes   = forwardCodeList[i].m_actStr;
                    deactivationCodes = forwardCodeList[i].m_deactStr;

                    if ( re.test( activationCodes ) )
                    {
                        activationCodes   = activationCodes.replace( /\n/g, "</td></tr><tr><td>" );
                        activationCodePhrase = "these codes";
                    }
                    if ( re.test( deactivationCodes ) )
                    {
                        deactivationCodes   = deactivationCodes.replace( /\n/g, "</td></tr><tr><td>" );
                        deactivationCodePhrase = "these codes";
                    }

                    activationCodes   = "<table><tr><td>" + activationCodes   + "</td></tr></table>";
                    deactivationCodes = "<table><tr><td>" + deactivationCodes + "</td></tr></table>";

                    break;
                }
            }

            step2Area.innerHTML = (stepNumber++) + ". Activate by dialing " + activationCodePhrase + " on your cell phone and then press SEND.<br/>";

            activationCodesArea.innerHTML = activationCodes;

            step3Area.innerHTML = (stepNumber++) + ". Try it out! Call your cell phone and leave a message. You'll see it right here.";

            // align to the right
            callToActionArea.innerHTML = tag(
                    "div",
                    {  style: "width: 100%; text-align: right" },
                    this.link( "/v2w/notWorking.html", "Didn't work?" ) );

            deactivationArea.innerHTML = "Should you ever need to deactivate, use " + deactivationCodePhrase + ":<br>";

            deactivationCodesArea.innerHTML = deactivationCodes;

            step2Area.style.display             = 'inline';
            callToActionArea.style.display      = 'inline';
            activationCodesArea.style.display   = 'inline';
            step3Area.style.display             = 'inline';
            deactivationArea.style.display      = 'inline';
            deactivationCodesArea.style.display = 'inline';

            UTILS.adjustHeight( );
        }
        catch ( e )
        {
            EX_ASSERT_NO_EXCEPTIONS( e, "VM_ActivationView::displayActivationCodes( )" );
        }
    }
};
