// $Revision: 34$
// $Date: 8/31/2007 5:51:17 PM$

// Constructor ---------------------------------------------------------------------------------------------
function SMS_GadgetRegistrationView(parent)
{
    // Attributes
    this.m_viewController = parent;
    this.m_mobileNumberControl = null;
    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_onRegisterAccountSuccess = null;
    this.m_onRegisterAccountFail = null;

    // ACC_AccountHandler Callbacks
    this.m_accountHandler.m_onRegisterAccountSucceeded = function(responseMessage) {self.onRegisterAccountSucceeded(responseMessage);};
    this.m_accountHandler.m_onRegisterAccountFailed = function(statusMessage) {self.onRegisterAccountFailed(statusMessage);};
    this.m_accountHandler.m_onGetAccountInfoSucceeded = function(responseMessage) {self.onGetAccountInfoSucceeded(responseMessage);};
    this.m_accountHandler.m_onGetAccountInfoFailed = function(statusMessage) {self.onGetAccountInfoFailed(statusMessage);};
}

// Prototype -----------------------------------------------------------------------------------------------
SMS_GadgetRegistrationView.prototype = new SMS_View;

// Constants -----------------------------------------------------------------------------------------------
// DOM element ids.
SMS_GadgetRegistrationView.prototype.ELEM_ID_VIEWFRAME = 'REGISTER_ViewFrame';
SMS_GadgetRegistrationView.prototype.ELEM_ID_COUNTRY = 'REGISTER_CountryCd';
SMS_GadgetRegistrationView.prototype.ELEM_ID_CUSTNAME = 'REGISTER_CustName';
SMS_GadgetRegistrationView.prototype.ELEM_ID_PHONE = 'REGISTER_PhoneNumber';
SMS_GadgetRegistrationView.prototype.ELEM_ID_PIN = 'REGISTER_PIN';
SMS_GadgetRegistrationView.prototype.ELEM_ID_PINPROMPT = 'REGISTER_PINPrompt';
SMS_GadgetRegistrationView.prototype.ELEM_ID_EMAIL = 'REGISTER_EmailAddress';
SMS_GadgetRegistrationView.prototype.ELEM_ID_SUBMIT = 'REGISTER_SubmitButton';

// Defaults.
SMS_GadgetRegistrationView.prototype.ELEM_DEFAULT_CUSTNAME = 'Name';
SMS_GadgetRegistrationView.prototype.ELEM_DEFAULT_PHONE = 'Mobile Number';
SMS_GadgetRegistrationView.prototype.ELEM_DEFAULT_PIN = 'Choose PIN';
SMS_GadgetRegistrationView.prototype.ELEM_DEFAULT_EMAIL = 'Email';

SMS_GadgetRegistrationView.prototype.GetSlotManager = function( )
{
    return this.m_slotManager;
};

// Init -----------------------------------------------------------------------------------------------------
SMS_GadgetRegistrationView.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_CUSTNAME, this.ELEM_DEFAULT_CUSTNAME );
    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 );

    this.setTextInputPrompt( this.ELEM_ID_EMAIL, this.ELEM_DEFAULT_EMAIL );
}

// Display -------------------------------------------------------------------------------------------------
SMS_GadgetRegistrationView.prototype.display = function()
{
    // 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);
    this.displayElement(this.ELEM_ID_PINPROMPT);
    this.getElement(this.ELEM_ID_PIN).value = "";
    this.hideElement(this.ELEM_ID_PIN);

    this.setStatusMsgInfo("Please enter the following information to register:")

    // Display the parent DIV.
    this.displayElement(this.ELEM_ID_VIEWFRAME);
}

SMS_GadgetRegistrationView.prototype.hide = function()
{
    this.hideElement(this.ELEM_ID_VIEWFRAME);
}

SMS_GadgetRegistrationView.prototype.disableForm = function( )
{
    this.disableElement( this.ELEM_ID_CUSTNAME );
    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.disableElement( this.ELEM_ID_EMAIL );
    this.setCursorWait( );
}

SMS_GadgetRegistrationView.prototype.enableForm = function( )
{
    this.enableElement( this.ELEM_ID_CUSTNAME );
    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.enableElement( this.ELEM_ID_EMAIL );
    this.setCursorDefault( );
}

SMS_GadgetRegistrationView.prototype.checkFields = function( )
{
    try
    {
    //alert('cf');
    
        this.updateInputStyle( this.ELEM_ID_CUSTNAME, this.ELEM_DEFAULT_CUSTNAME );
        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 );
        }

        this.updateInputStyle( this.ELEM_ID_EMAIL, this.ELEM_DEFAULT_EMAIL );

        if ( this.m_mobileNumberControl.GetComboBox( ).GetSelectedIndex( ) == 0 )
        {
            this.setDropdownPrompt( this.ELEM_ID_COUNTRY );
        }
        else
        {
            this.setNonEmptyInputStyle( this.ELEM_ID_COUNTRY );
        }
    }
    catch ( e )
    {
        EX_ASSERT_NO_EXCEPTIONS( e, "SMS_GadgetRegistrationView::checkFields( )" );
    }
}

SMS_GadgetRegistrationView.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_GadgetRegistrationView.prototype.showPINPrompt = function()
{
    this.hideElement(this.ELEM_ID_PIN);
    this.getElement(this.ELEM_ID_PIN).value = '';
    this.displayElement(this.ELEM_ID_PINPROMPT);
}

SMS_GadgetRegistrationView.prototype.getCustName = function( )
{
    return this.getElement( this.ELEM_ID_CUSTNAME ).value;
}

SMS_GadgetRegistrationView.prototype.selectCustName = function()
{
    this.getElement(this.ELEM_ID_CUSTNAME).focus();
    this.getElement(this.ELEM_ID_CUSTNAME).select();
}

SMS_GadgetRegistrationView.prototype.selectPhoneNumber = function()
{
    this.getElement(this.ELEM_ID_PHONE).focus();
    this.getElement(this.ELEM_ID_PHONE).select();
}

SMS_GadgetRegistrationView.prototype.getPIN = function()
{
    return this.getElement(this.ELEM_ID_PIN).value;
}

SMS_GadgetRegistrationView.prototype.setPIN = function(val)
{
    this.getElement(this.ELEM_ID_PIN).value = val;
}

SMS_GadgetRegistrationView.prototype.selectPINPrompt = function()
{
    this.getElement(this.ELEM_ID_PINPROMPT).focus();
    this.getElement(this.ELEM_ID_PINPROMPT).select();
}

SMS_GadgetRegistrationView.prototype.getEmailAddress = function()
{
    return this.getElement(this.ELEM_ID_EMAIL).value;
}

SMS_GadgetRegistrationView.prototype.setEmailAddress = function(val)
{
    this.getElement(this.ELEM_ID_EMAIL).value = val;
}

SMS_GadgetRegistrationView.prototype.selectEmailAddress = function()
{
    this.getElement(this.ELEM_ID_EMAIL).focus();
    this.getElement(this.ELEM_ID_EMAIL).select();
}

SMS_GadgetRegistrationView.prototype.custName_onClick = function( )
{
    this.checkFields( );
    if ( this.getElement( this.ELEM_ID_CUSTNAME ).value == this.ELEM_DEFAULT_CUSTNAME )
    {
        this.setTextInput( this.ELEM_ID_CUSTNAME, '' );
    }
}

SMS_GadgetRegistrationView.prototype.custName_onKeyDown = function( evt )
{
    this.onKeyDown( evt );
    this.flipStyle( this.ELEM_DEFAULT_CUSTNAME, this.ELEM_ID_CUSTNAME );
}

SMS_GadgetRegistrationView.prototype.custName_onFocus = function()
{
    try
    {
        this.checkFields();
    }
    catch ( e )
    {
        EX_Log( "SMS_GadgetRegistrationView::custName_onFocus( )\n" + e.message );
    }
}

SMS_GadgetRegistrationView.prototype.custName_onBlur = function( )
{
	this.updateInputStyle( this.ELEM_ID_CUSTNAME, this.ELEM_DEFAULT_CUSTNAME );
}

SMS_GadgetRegistrationView.prototype.phoneNumber_onFocus = function()
{
	this.checkFields();
    this.getElement(this.ELEM_ID_PHONE).select();
}

SMS_GadgetRegistrationView.prototype.phoneNumber_onBlur = function()
{
    this.updateInputStyle( this.ELEM_ID_PHONE, this.ELEM_DEFAULT_PHONE );
}

SMS_GadgetRegistrationView.prototype.phoneNumber_onClick = function()
{
    this.checkFields();
    if (this.getElement(this.ELEM_ID_PHONE).value == this.ELEM_DEFAULT_PHONE)
    {
        this.setTextInput(this.ELEM_ID_PHONE, '');
    }
}

SMS_GadgetRegistrationView.prototype.phoneNumber_onKeyDown = function(evt)
{
    this.onKeyDown(evt);
    this.flipStyle(this.ELEM_DEFAULT_PHONE, this.ELEM_ID_PHONE);
}

SMS_GadgetRegistrationView.prototype.pinPrompt_onFocus = function(elem)
{
    elem.select();
}

SMS_GadgetRegistrationView.prototype.pinPrompt_onKeyDown = function(evt)
{
    this.showPIN();
}

SMS_GadgetRegistrationView.prototype.pinPrompt_onKeyUp = function(evt)
{
    this.getElement(this.ELEM_ID_PIN).value = "";
}

SMS_GadgetRegistrationView.prototype.pinPrompt_onClick = function()
{
    this.checkFields();
    this.showPIN();
}

SMS_GadgetRegistrationView.prototype.pin_onFocus = function(elem)
{
    elem.select();
}

SMS_GadgetRegistrationView.prototype.pin_onKeyDown = function(evt)
{
    this.onKeyDown(evt);
}

SMS_GadgetRegistrationView.prototype.pin_onClick = function()
{
    this.checkFields();
}


SMS_GadgetRegistrationView.prototype.emailAddress_onFocus = function()
{
    this.checkFields();
    this.getElement(this.ELEM_ID_EMAIL).select();
}

SMS_GadgetRegistrationView.prototype.emailAddress_onClick = function()
{
    this.checkFields();
    if (this.getElement(this.ELEM_ID_EMAIL).value == this.ELEM_DEFAULT_EMAIL)
    {
        this.setTextInput(this.ELEM_ID_EMAIL, '');
    }
}

SMS_GadgetRegistrationView.prototype.emailAddress_onKeyDown = function(evt)
{
    this.onKeyDown(evt);
    this.flipStyle(this.ELEM_DEFAULT_EMAIL, this.ELEM_ID_EMAIL);
}

SMS_GadgetRegistrationView.prototype.submitButton_onClick = function()
{
    this.handleRegistrationSubmit();
}

SMS_GadgetRegistrationView.prototype.learnMoreLink_onFocus = function()
{
    this.checkFields();
}

SMS_GadgetRegistrationView.prototype.alreadyRegisteredLink_onFocus = function()
{
    this.checkFields();
}

SMS_GadgetRegistrationView.prototype.alreadyRegisteredLink_onClick = function()
{
    this.m_viewController.displayManualLoginView();
}

SMS_GadgetRegistrationView.prototype.onKeyDown = function(evt)
{
    if (this.keyDownIsEnter(evt))
    {
        this.handleRegistrationSubmit();
    }
}

SMS_GadgetRegistrationView.prototype.getValidatedNumber = function( )
{
    var numberValidationData = this.m_mobileNumberControl.GetValidatedNumber( );
    if ( ! numberValidationData.ERROR_MESSAGE )
    {
        return numberValidationData.PHONE_NUMBER;
    }

    this.enableForm();

    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( );

    return null;
}

SMS_GadgetRegistrationView.prototype.getValidatedCustName = function( )
{
    try
    {
        if ( this.getElement( this.ELEM_ID_CUSTNAME ).value == this.ELEM_DEFAULT_CUSTNAME )
        {
            return null; 
        }

        return new USER_SubscriberName( this.getCustName( ) ).name;
    }
    catch ( e )
    {   
        if ( !( e instanceof USER_EX_FailedNameValidation ) )
        {
            throw e;
        }

        return null;
    }
}

SMS_GadgetRegistrationView.prototype.handleRegistrationSubmit = function( )
{
    try
    {
        this.disableForm( );

        var validatedCustName = this.getValidatedCustName( );
        
        if ( validatedCustName === null )
        {   
            this.setStatusMsgError( "Please enter a valid name." );
            this.enableForm( );
            this.selectCustName( );
            return;
        }

        this.m_mobileNumberControl.OnFormSubmit( );

        // Get validated number
        var validatedNumber = this.getValidatedNumber( );

        if ( validatedNumber === null )
        {
            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;
        }

        // Get and validate email
        var emailAddress = this.getEmailAddress( );

        if ( ! this.isValidEmail( emailAddress ) )
        {
            this.setStatusMsgError("Please enter a valid email address.");
            this.enableForm();
            this.selectEmailAddress();
            return;
        }

        this.setStatusMsgInfo( "Connecting, please wait..." );

        this.m_accountHandler.registerAccount( validatedNumber.GetFullyQualifiedString( ), 
                                               pin, 
                                               emailAddress, 
                                               validatedCustName );
    }
    catch ( e )
    {
        this.enableForm();
        this.setStatusMsgError("Registration failed.");
    }
}

SMS_GadgetRegistrationView.prototype.onRegisterAccountSucceeded = function(responseMessage)
{
    try
    {
        this.enableForm();

        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.REGA_PIN_MISMATCH:
            {
                this.m_viewController.handleRegPINMismatch(this.getElement(this.ELEM_ID_PHONE).value, this.getElement(this.ELEM_ID_COUNTRY).value);

                break;
            }

            default:
            {
                this.setStatusMsgError(responseMessage.m_statusMsg);

                break;
            }
        }
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetRegistrationView.prototype.onRegisterAccountFailed = function(statusMessage)
{
    try
    {
        this.enableForm();
        this.m_onRegisterAccountFail(statusMessage);
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetRegistrationView.prototype.onGetAccountInfoSucceeded = function(responseMessage)
{
    try
    {
        this.enableForm();

        switch (responseMessage.m_status)
        {
            case MSG_ResponseStatus.SUCCESS:
            {
                this.m_onRegisterAccountSuccess(responseMessage);
                break;
            }

            default:
            {
                this.setStatusMsgError(responseMessage.m_statusMsg);
                break;
            }
        }
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetRegistrationView.prototype.onGetAccountInfoFailed = function(statusMessage)
{
    try
    {
        this.enableForm();
        this.m_onRegisterAccountFail(statusMessage);
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}
