// $Revision: 28$
// $Date: 8/31/2007 5:51:16 PM$
// $Author: Sean Alisea$

// Constructor ---------------------------------------------------------------------------------------------
function SMS_GadgetLoginView(parent)
{
    // Attributes
    this.m_viewController = parent;
    this.m_mobileNumberControl = null;
    this.m_FPINRequestInProgress = false;
    this.m_accountHandler = new ACC_AccountHandler(parent.m_source, parent.m_ver, parent.m_os);

    var self = this;

    this.m_slotManager = new SIGSLOT_SlotManager( );

    // UI-Controller Callbacks
    this.m_onAuthenticateSuccess = null;
    this.m_onAuthenticateFail = null;

    // ACC_AccountHandler Callbacks
    this.m_accountHandler.m_onAuthenticateSucceeded = function(responseMessage) {self.onAuthenticateSucceeded(responseMessage);};
    this.m_accountHandler.m_onAuthenticateFailed = function(statusMessage) {self.onAuthenticateFailed(statusMessage);};
    this.m_accountHandler.m_onGetAccountInfoSucceeded = function(responseMessage) {self.onGetAccountInfoSucceeded(responseMessage);};
    this.m_accountHandler.m_onGetAccountInfoFailed = function(statusMessage) {self.onGetAccountInfoFailed(statusMessage);};
    this.m_accountHandler.m_onForgotPinSucceeded = function(responseMessage) {self.onForgotPinSucceeded(responseMessage);};
    this.m_accountHandler.m_onForgotPinFailed = function(statusMessage) {self.onForgotPinFailed(statusMessage);};
}

// Prototype -----------------------------------------------------------------------------------------------
SMS_GadgetLoginView.prototype = new SMS_View;

// Constants -----------------------------------------------------------------------------------------------
// DOM element ids.
SMS_GadgetLoginView.prototype.ELEM_ID_VIEWFRAME = 'LOGIN_ViewFrame';
SMS_GadgetLoginView.prototype.ELEM_ID_COUNTRY = 'LOGIN_CountryCd';
SMS_GadgetLoginView.prototype.ELEM_ID_PHONE = 'LOGIN_PhoneNumber';
SMS_GadgetLoginView.prototype.ELEM_ID_PIN = 'LOGIN_PIN';
SMS_GadgetLoginView.prototype.ELEM_ID_PINPROMPT = 'LOGIN_PINPrompt';
SMS_GadgetLoginView.prototype.ELEM_ID_SUBMIT = 'LOGIN_SubmitButton';
SMS_GadgetLoginView.prototype.ELEM_ID_LNK_FPIN = 'LOGIN_PINRequestLink';

// Defaults.
SMS_GadgetLoginView.prototype.ELEM_DEFAULT_PHONE = 'Mobile Number';
SMS_GadgetLoginView.prototype.ELEM_DEFAULT_PIN = 'Enter PIN';

SMS_GadgetLoginView.prototype.GetSlotManager = function( )
{
    return this.m_slotManager;
};

// Init -----------------------------------------------------------------------------------------------------
SMS_GadgetLoginView.prototype.init = function()
{
    this.m_mobileNumberControl = new SMS_MobileNumberControl( this.ELEM_ID_COUNTRY, this.ELEM_ID_PHONE );

    this.m_mobileNumberControl.GetNumberFieldGainFocusSignal( ).Connect( this, "phoneNumber_onFocus" );
    this.m_mobileNumberControl.GetNumberFieldLoseFocusSignal( ).Connect( this, "phoneNumber_onBlur" );
    this.m_mobileNumberControl.GetCountryPullDownChangeSignal( ).Connect( this, "checkFields" );

    this.m_viewController.LoginAndRegisterControlSynchronizer.AddControl( this.m_mobileNumberControl );

    // Set defaults.
    this.setTextInputPrompt(this.ELEM_ID_PHONE, this.ELEM_DEFAULT_PHONE);
    this.setDropdownPrompt(this.ELEM_ID_COUNTRY);

    // PIN is always set to display entry prompt.
    this.setTextInputPrompt(this.ELEM_ID_PINPROMPT, this.ELEM_DEFAULT_PIN);
    this.hideElement(this.ELEM_ID_PIN);
}

// Display -------------------------------------------------------------------------------------------------
SMS_GadgetLoginView.prototype.display = function()
{
    this.m_FPINRequestInProgress = false;

    // Ready the form for data entry.
    this.enableForm();

    // PIN is always set to display entry prompt.
    this.setTextInputPrompt(this.ELEM_ID_PINPROMPT, this.ELEM_DEFAULT_PIN);

    // The PIN prompt is read-only and overlays the actual PIN input.  Display by default.
    // Elements switch visibility on keydown or click events.
    this.displayElement(this.ELEM_ID_PINPROMPT);
    this.getElement(this.ELEM_ID_PIN).value = "";
    this.hideElement(this.ELEM_ID_PIN);
    
    this.updateInputStyle( this.ELEM_ID_PHONE, this.ELEM_DEFAULT_PHONE );

    this.setStatusMsgInfo("Please enter the following information to log in:")

    // Display the parent DIV.
    this.displayElement(this.ELEM_ID_VIEWFRAME);
}

SMS_GadgetLoginView.prototype.hide = function()
{
    this.hideElement(this.ELEM_ID_VIEWFRAME);
}

SMS_GadgetLoginView.prototype.displayManualLoginViewRegPINMismatch = function(phoneNumber, countryCd)
{
    this.display();
    this.setTextInput(this.ELEM_ID_PHONE, phoneNumber);
    this.setDropdown(this.ELEM_ID_COUNTRY, countryCd);
    this.selectElement(this.ELEM_ID_PINPROMPT);
}

SMS_GadgetLoginView.prototype.disableForm = function()
{
    this.disableElement(this.ELEM_ID_COUNTRY);
    this.disableElement(this.ELEM_ID_PHONE);
    this.disableElement(this.ELEM_ID_PINPROMPT);
    this.disableElement(this.ELEM_ID_PIN);
    this.disableElement(this.ELEM_ID_SUBMIT);
    this.setCursorWait();
}

SMS_GadgetLoginView.prototype.enableForm = function()
{
    this.enableElement(this.ELEM_ID_COUNTRY);
    this.enableElement(this.ELEM_ID_PHONE);
    this.enableElement(this.ELEM_ID_PINPROMPT);
    this.enableElement(this.ELEM_ID_PIN);
    this.enableElement(this.ELEM_ID_SUBMIT);
    this.setCursorDefault();
}

SMS_GadgetLoginView.prototype.checkFields = function( )
{
    try
    {
        this.updateInputStyle( this.ELEM_ID_PHONE, this.ELEM_DEFAULT_PHONE );

        if ( this.isInputEmpty( this.ELEM_ID_PIN ) )
        {
            this.showPINPrompt();
        }
        else
        {
            this.setNonEmptyInputStyle( this.ELEM_ID_PIN );
        }

        if ( this.m_mobileNumberControl.GetComboBox( ).GetSelectedIndex( ) == 0 )
        {
            this.setEmptyInputStyle( this.ELEM_ID_COUNTRY );
        }
        else
        {
            this.setNonEmptyInputStyle( this.ELEM_ID_COUNTRY );
        }
    }
    catch ( e )
    {
        EX_ASSERT_NO_EXCEPTIONS( e, "SMS_GadgetLoginView::checkFields( )" );
    }
}

SMS_GadgetLoginView.prototype.showPIN = function()
{
    this.hideElement(this.ELEM_ID_PINPROMPT);
    this.getElement(this.ELEM_ID_PIN).value = '';
    this.enableElement(this.ELEM_ID_PIN);
    this.displayElement(this.ELEM_ID_PIN);

    this.getElement(this.ELEM_ID_PIN).focus();
}

SMS_GadgetLoginView.prototype.showPINPrompt = function()
{
    this.hideElement(this.ELEM_ID_PIN);
    this.getElement(this.ELEM_ID_PIN).value = '';
    this.displayElement(this.ELEM_ID_PINPROMPT);
}

SMS_GadgetLoginView.prototype.selectPhoneNumber = function()
{
    this.getElement(this.ELEM_ID_PHONE).focus();
    this.getElement(this.ELEM_ID_PHONE).select();
}

SMS_GadgetLoginView.prototype.getPIN = function()
{
    return this.getElement(this.ELEM_ID_PIN).value;
}

SMS_GadgetLoginView.prototype.setPIN = function(val)
{
    this.getElement(this.ELEM_ID_PIN).value = val;
}

SMS_GadgetLoginView.prototype.selectPINPrompt = function()
{
    this.getElement(this.ELEM_ID_PINPROMPT).focus();
    this.getElement(this.ELEM_ID_PINPROMPT).select();
}

SMS_GadgetLoginView.prototype.countryCd_onKeyDown = function(evt)
{
    this.onKeyDown(evt);
}

SMS_GadgetLoginView.prototype.phoneNumber_onFocus = function()
{
	this.checkFields();
    this.getElement(this.ELEM_ID_PHONE).select();
}

SMS_GadgetLoginView.prototype.phoneNumber_onBlur = function()
{
    this.updateInputStyle( this.ELEM_ID_PHONE, this.ELEM_DEFAULT_PHONE );
}

SMS_GadgetLoginView.prototype.phoneNumber_onClick = function()
{
    this.checkFields();
    if (this.getElement(this.ELEM_ID_PHONE).value == this.ELEM_DEFAULT_PHONE)
    {
        this.setTextInput(this.ELEM_ID_PHONE, '');
        this.getElement(this.ELEM_ID_PHONE).select();
    }
}

SMS_GadgetLoginView.prototype.phoneNumber_onKeyDown = function(evt)
{
    this.onKeyDown(evt);
    this.flipStyle(this.ELEM_DEFAULT_PHONE, this.ELEM_ID_PHONE);
}

SMS_GadgetLoginView.prototype.pinPrompt_onFocus = function(elem)
{
    elem.select();
}

SMS_GadgetLoginView.prototype.pinPrompt_onKeyDown = function(evt)
{
    this.showPIN();
}

SMS_GadgetLoginView.prototype.pinPrompt_onKeyUp = function(evt)
{
    this.getElement(this.ELEM_ID_PIN).value = "";
}

SMS_GadgetLoginView.prototype.pinPrompt_onClick = function()
{
    this.checkFields();
    this.showPIN();
}

SMS_GadgetLoginView.prototype.pin_onFocus = function(elem)
{
    elem.select();
}

SMS_GadgetLoginView.prototype.pin_onKeyDown = function(evt)
{
    this.onKeyDown(evt);
}

SMS_GadgetLoginView.prototype.pin_onClick = function() {
    this.checkFields();
}

SMS_GadgetLoginView.prototype.lnkRegister_onFocus = function()
{
    this.checkFields();
}

SMS_GadgetLoginView.prototype.lnkRegister_onClick = function()
{
    this.checkFields();
    this.m_viewController.displayRegistrationView();
}

SMS_GadgetLoginView.prototype.lnkForgotPIN_onFocus = function()
{
    this.checkFields();
}

SMS_GadgetLoginView.prototype.lnkForgotPIN_onClick = function()
{
    this.checkFields();
    this.handlePINRequest();
}

SMS_GadgetLoginView.prototype.submitButton_onClick = function()
{
    this.checkFields();
    this.handleLoginSubmit();
}

SMS_GadgetLoginView.prototype.onKeyDown = function(evt)
{
    if (this.keyDownIsEnter(evt))
    {
        this.handleLoginSubmit();
    }
}

SMS_GadgetLoginView.prototype.getValidatedNumber = function( )
{
    var numberValidationData = this.m_mobileNumberControl.GetValidatedNumber( );
    if ( ! numberValidationData.ERROR_MESSAGE )
    {
        return numberValidationData.PHONE_NUMBER;
    }

    if ( numberValidationData.ERROR_MESSAGE === SMS_MobileNumberControlBase.prototype.INVALID_NANP_NUMBER )
    {
        this.bruteForceSelectElement( this.ELEM_ID_PINPROMPT );
        this.bruteForceSelectElement( this.ELEM_ID_PIN );
    }

    this.setStatusMsgError( numberValidationData.ERROR_MESSAGE );

    this.selectPhoneNumber( );

    this.enableElement( this.ELEM_ID_LNK_FPIN );

    return null;
}

SMS_GadgetLoginView.prototype.handleLoginSubmit = function()
{
    try
    {
        this.disableForm( );

        this.m_mobileNumberControl.OnFormSubmit( );

        // Get validated number
        var validatedNumber = this.getValidatedNumber( );

        if ( validatedNumber === null )
        {
            this.enableForm( );
            return;
        }

        // Get and validate PIN
        var pin = this.getPIN();
        if ( pin.length < 4 || pin.length > 10 || ! this.isNumeric( pin ) )
        {
            this.setPIN("");
            this.setStatusMsgError("Invalid PIN.  Must be numeric and 4-10 characters in length.");
            this.enableForm( );
            this.showPINPrompt( );
            this.selectPINPrompt( );
            return;
        }

        this.setStatusMsgInfo( "Connecting...please wait..." );

        this.m_accountHandler.authenticate( validatedNumber.GetFullyQualifiedString( ), pin );
    }
    catch (e)
    {
        this.enableForm();
        this.setStatusMsgError("Login failed.");
    }
}


SMS_GadgetLoginView.prototype.handlePINRequest = function()
{
    // Disable to prevent double-clicking.
    this.disableElement( this.ELEM_ID_LNK_FPIN );

    this.m_mobileNumberControl.OnFormSubmit( );

    var validatedNumber = this.getValidatedNumber( );

    if ( validatedNumber === null )
    {
        this.enableElement( this.ELEM_ID_LNK_FPIN );
        return;
    }

    if ( ! this.m_FPINRequestInProgress )
    {
        this.m_FPINRequestInProgress = true;

        this.setStatusMsgInfo("Sending request.&nbsp;&nbsp;Please wait...");

        this.m_accountHandler.forgotPin( validatedNumber.GetFullyQualifiedString( ) );
    }
}

SMS_GadgetLoginView.prototype.onAuthenticateSucceeded = function(responseMessage)
{
    try
    {
        switch (responseMessage.m_status)
        {
            case MSG_ResponseStatus.SUCCESS:
            {
                // Store credentials
                VM_SetPreference( PREF_KEY_ACCOUNT,  responseMessage.m_account );
                VM_SetPreference( PREF_KEY_U_STRING, responseMessage.m_uString );

                // Get account info.
                this.m_accountHandler.getAccountInfo(responseMessage.m_account, responseMessage.m_uString);

                break;
            }

            case MSG_ResponseStatus.GENERAL_ERROR:
            case MSG_ResponseStatus.AUTH_PIN_MISMATCH:
            case MSG_ResponseStatus.AUTH_ACCOUNT_LOCKED:
            // In these cases, display the message that comes back with the response,
            // since these are known conditions.
            {
                this.enableForm();

                // Display the response status message.
                this.setStatusMsgError(responseMessage.m_statusMsg);

                break;
            }

            default:
            {
                this.enableForm();

                // Display generic message.
                this.setStatusMsgError("Login failed.  Please try again.");

                break;
            }
        }
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetLoginView.prototype.onAuthenticateFailed = function(statusMessage)
{
    try
    {
        this.enableForm();

        if (statusMessage.substring(0, 30) == 'Unable to process HTTP request')
        {
            this.setStatusMsgError("Unable to connect to server.  Please try again later.");
        }
        else
        {
            this.setStatusMsgError("Login failed.  Please try again later.");
        }
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetLoginView.prototype.onGetAccountInfoSucceeded = function(responseMessage)
{
    try
    {
        this.enableForm();
        this.m_onAuthenticateSuccess(responseMessage);
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetLoginView.prototype.onGetAccountInfoFailed = function(statusMessage)
{
    try
    {
        this.enableForm();
        this.m_onAuthenticateFail(statusMessage);
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetLoginView.prototype.onForgotPinSucceeded = function(responseMessage)
{
    this.m_FPINRequestInProgress = false;
    this.enableElement(this.ELEM_ID_LNK_FPIN);

    try
    {
        switch (responseMessage.m_status)
        {
            case MSG_ResponseStatus.SUCCESS:
            {
                // Display the response status message.
                this.setStatusMsgInfo(responseMessage.m_statusMsg);
                break;
            }

            case MSG_ResponseStatus.GENERAL_ERROR:
            {
                // Display the response status message.
                this.setStatusMsgError(responseMessage.m_statusMsg);
                break;
            }

            default:
            {
                this.setStatusMsgError(responseMessage.m_statusMsg);
                break;
            }
        }
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetLoginView.prototype.onForgotPinFailed = function(statusMessage)
{
    try
    {
        this.m_FPINRequestInProgress = false;
        this.enableElement(this.ELEM_ID_LNK_FPIN);
        this.setStatusMsgError(statusMessage);
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

