// File: $callWave/web/web site/htdocs/widgets/sms/resources/js/VerifyView.js
// Desc: View-controller class for verify/activation screen.
// $Revision: 27$
// $Date: 8/28/2007 5:33:24 PM$
// $Author: Sean Alisea$

function SMS_GadgetVerifyView(parent)
{
    // Attributes
    this.m_viewController = parent;
    this.m_NoCodeRequestInProgress = false;

    var self = this;

    // ACT_ActivationHandler Callbacks
    var onSetCarrierInfoSucceeded = function(responseMessage) {self.onSetCarrierInfoSucceeded(responseMessage);};
    var onSetCarrierInfoFailed = function(statusMessage) {self.onSetCarrierInfoFailed(statusMessage);};
    var onVerifyPhoneSucceeded = function(responseMessage) {self.onVerifyPhoneSucceeded(responseMessage);};
    var onVerifyPhoneFailed = function(statusMessage) {self.onVerifyPhoneFailed(statusMessage);};

    this.m_activationHandler = new ACT_ActivationHandler(parent.m_source, parent.m_ver, parent.m_os, onSetCarrierInfoSucceeded, onSetCarrierInfoFailed, onVerifyPhoneSucceeded, onVerifyPhoneFailed);

    // UI-Controller Callbacks
    this.m_onVerificationSuccess = null;
    this.m_onVerificationFail = null;
}

SMS_GadgetVerifyView.prototype = new SMS_View;

SMS_GadgetVerifyView.prototype.ELEM_ID_VIEWFRAME = 'VERIFY_ViewFrame';
SMS_GadgetVerifyView.prototype.ELEM_ID_CARRIER = 'VERIFY_Carrier';
SMS_GadgetVerifyView.prototype.ELEM_ID_VCODE = 'VERIFY_VCode';
SMS_GadgetVerifyView.prototype.ELEM_ID_SUBMIT = 'VERIFY_SubmitButton';

SMS_GadgetVerifyView.prototype.VERIFY_STATE_INIT = 0;
SMS_GadgetVerifyView.prototype.VERIFY_STATE_REQUEST_VCODE = 1;
SMS_GadgetVerifyView.prototype.VERIFY_STATE_SUBMIT_VCODE = 2;

SMS_GadgetVerifyView.prototype.init = function()
{
    // Set defaults.
    this.getElement(this.ELEM_ID_VCODE).value = '';
    this.setDropdownPrompt(this.ELEM_ID_CARRIER);
}

SMS_GadgetVerifyView.prototype.display = function()
{
    this.m_NoCodeRequestInProgress = false;
    this.m_state = this.VERIFY_STATE_INIT;

    // Set defaults.
    this.getElement(this.ELEM_ID_VCODE).value = '';
    this.setDropdownPrompt(this.ELEM_ID_CARRIER);

    if ((SESSION.getVal(Session_Key.WX_PHONE_CC) == '1') && (SESSION.getVal(Session_Key.WX_CARRIER) != '-1') && (SESSION.getVal(Session_Key.WX_HIDE_REG_CARRIERS) === 0))
    {
        // NANP/Domestic
        try
        {
            this.getElement('instructionRow1SpacerRow').style.display = 'table-row';
            this.getElement('instructionRow1').style.display = 'table-row';
            this.getElement('carrierSpacerRow').style.display = 'table-row';
            this.getElement('carrierRow').style.display = 'table-row';
            this.getElement('VCodeRow').style.display = 'table-row';
        }
        catch (e)
        {
            this.getElement('instructionRow1SpacerRow').style.display = 'block';
            this.getElement('instructionRow1').style.display = 'block';
            this.getElement('carrierSpacerRow').style.display = 'block';
            this.getElement('carrierRow').style.display = 'block';
            this.getElement('VCodeRow').style.display = 'block';
        }

        this.getElement('InstrPhone').innerHTML = '2. Check your mobile phone +' + SESSION.getVal(Session_Key.WX_PHONE_CC) + ' ' + SESSION.getVal(Session_Key.WX_PHONE_NN) + ' for a text message from CallWave with your verification code.';
        this.getElement('InstrVCode').innerHTML = '3. Enter your verification code here:';

        // Set Carrier dropdown, if value is provided.
        if (SESSION.getVal(Session_Key.WX_CARRIER) != '-1')
        {
            this.setDropdown(this.ELEM_ID_CARRIER, SESSION.getVal(Session_Key.WX_CARRIER));
        }
        else
        {
            this.selectCarrier();
        }
    }
    else
    {
        this.getElement('InstrPhone').innerHTML = '1. Check your mobile phone +' + SESSION.getVal(Session_Key.WX_PHONE_CC) + ' ' + SESSION.getVal(Session_Key.WX_PHONE_NN) + ' for a text message from CallWave with your verification code.';
        this.getElement('InstrVCode').innerHTML = '2. Enter your verification code here:';
        this.getElement('instructionRow1SpacerRow').style.display = 'none';
        this.getElement('instructionRow1').style.display = 'none';
        this.getElement('carrierSpacerRow').style.display = 'none';
        this.getElement('carrierRow').style.display = 'none';
    }

    this.setStatusMsgInfo("Requesting verification code, please wait...");

    try
    {
        this.getElement(this.ELEM_ID_VIEWFRAME).style.display = 'table-row';  ;
    }
    catch(e)
    {
        this.getElement(this.ELEM_ID_VIEWFRAME).style.display = 'block';  ;
    }

    this.m_state = this.VERIFY_STATE_REQUEST_VCODE;

    // Request VCode.
    this.m_activationHandler.verifyPhone(
        VM_GetPreference( PREF_KEY_ACCOUNT ),
        VM_GetPreference( PREF_KEY_U_STRING ),
        '',
        SESSION.getVal(Session_Key.WX_PHONE_CC),
        SESSION.getVal(Session_Key.WX_PHONE_NN));
}

SMS_GadgetVerifyView.prototype.hide = function()
{
    this.hideElement(this.ELEM_ID_VIEWFRAME);
}

SMS_GadgetVerifyView.prototype.disableForm = function()
{
    this.disableElement(this.ELEM_ID_CARRIER)
    this.disableElement(this.ELEM_ID_VCODE)
    this.disableElement(this.ELEM_ID_SUBMIT)
    this.setCursorWait();
}

SMS_GadgetVerifyView.prototype.enableForm = function()
{
    this.setCursorDefault();
}

SMS_GadgetVerifyView.prototype.isInputEmpty = function(id) {
    return this.getElement(id).value == '';
}

SMS_GadgetVerifyView.prototype.getCarrier = function()
{
    return this.getElement(this.ELEM_ID_CARRIER).options[this.getElement(this.ELEM_ID_CARRIER).selectedIndex].value;
}

SMS_GadgetVerifyView.prototype.setCarrier = function(val)
{
    for (var i = 0; i < this.getElement(this.ELEM_ID_CARRIER).options.length; i++)
    {
        if (this.getElement(this.ELEM_ID_CARRIER).options[i].value == val)
        {
             this.getElement(this.ELEM_ID_CARRIER).options[i].selected = true;
             break;
        }
    }
}

SMS_GadgetVerifyView.prototype.selectCarrier = function()
{
    this.getElement(this.ELEM_ID_CARRIER).focus();
}

SMS_GadgetVerifyView.prototype.getVCode = function()
{
    return this.getElement(this.ELEM_ID_VCODE).value;
}

SMS_GadgetVerifyView.prototype.setVCode = function(val)
{
    return this.getElement(this.ELEM_ID_VCODE).value = val;
}

SMS_GadgetVerifyView.prototype.selectVCode = function()
{
    this.getElement(this.ELEM_ID_VCODE).focus();
    this.getElement(this.ELEM_ID_VCODE).select();
}

SMS_GadgetVerifyView.prototype.carrier_onChange = function(elem)
{
    if (elem.options[0].selected == true)
    {
        elem.className = 'inputPrompt';
    }
    else
    {
        elem.className = 'input';

        // Update the carrier on the account.
        var carrierId = elem.options[elem.selectedIndex].value;
        
        // Override any selection of non-valued items (separators).
        if (!this.isNumeric(carrierId) || (carrierId.length == 0))
        {        
            elem.selectedIndex = 0;
		    return;
        }
        
        this.disableElement(this.ELEM_ID_CARRIER);
        this.setStatusMsgInfo("Updating your account, please wait...");

        this.m_activationHandler.setCarrierInfo( VM_GetPreference( PREF_KEY_ACCOUNT ),
                                                 VM_GetPreference( PREF_KEY_U_STRING ),
                                                 carrierId );
    }
}

SMS_GadgetVerifyView.prototype.vCode_onKeyDown = function(evt) {
    this.onKeyDown(evt);
}

SMS_GadgetVerifyView.prototype.submitButton_onClick = function() {
    this.handleVerifySubmit();
}

SMS_GadgetVerifyView.prototype.onKeyDown = function(evt)
{
    if (this.keyDownIsEnter(evt)) {
        this.handleVerifySubmit();
    }
}

SMS_GadgetVerifyView.prototype.handleVerifySubmit = function() {
    this.disableForm();

    var vCode = this.getVCode();

    if (!this.validateVCode(vCode)) {
        return;
    }

    if (vCode.length == 6) {
        // Append prefix
        vCode = "V" + vCode;
    }

    this.setStatusMsgInfo("Sending...");

    this.m_state = this.VERIFY_STATE_SUBMIT_VCODE;

    this.m_activationHandler.verifyPhone(
        VM_GetPreference( PREF_KEY_ACCOUNT ),
        VM_GetPreference( PREF_KEY_U_STRING ),
        vCode,
        SESSION.getVal(Session_Key.WX_PHONE_CC),
        SESSION.getVal(Session_Key.WX_PHONE_NN));
}

SMS_GadgetVerifyView.prototype.validateVCode = function(vCode) {
    if (vCode.length > 7) {
        this.enableForm();
        this.enableElement(this.ELEM_ID_CARRIER);
        this.enableElement(this.ELEM_ID_VCODE);
        this.enableElement(this.ELEM_ID_SUBMIT);
        this.selectVCode();
        this.setStatusMsgError("Incorrect verification code.");
        return false;
    }

    // Check vCode.  THIS SHOULD BE REPLACED BY A REGEX!
    if (vCode.length == 7) {
        // Should be prefix then digits
        if (vCode.substring(0, 1).toUpperCase() != "V") {
            this.enableForm();
            this.enableElement(this.ELEM_ID_CARRIER);
            this.enableElement(this.ELEM_ID_VCODE);
            this.enableElement(this.ELEM_ID_SUBMIT);
            this.selectVCode();
            this.setStatusMsgError("Incorrect verification code.");
            return false;
        }
        else if (!this.isNumeric(vCode.substring(1, 7))) {
            this.enableForm();
            this.enableElement(this.ELEM_ID_CARRIER);
            this.enableElement(this.ELEM_ID_VCODE);
            this.enableElement(this.ELEM_ID_SUBMIT);
            this.selectVCode();
            this.setStatusMsgError("Incorrect verification code.");
            return false;
        }
    }
    else if (vCode.length == 6) {
        // User forgot the prefix...should just then be digits
        if (!this.isNumeric(vCode)) {
            this.enableForm();
            this.enableElement(this.ELEM_ID_CARRIER);
            this.enableElement(this.ELEM_ID_VCODE);
            this.enableElement(this.ELEM_ID_SUBMIT);
            this.selectVCode();
            this.setStatusMsgError("Incorrect verification code.");
            return false;
        }
    }
    else
    {
        this.enableForm();
        this.enableElement(this.ELEM_ID_CARRIER);
        this.enableElement(this.ELEM_ID_VCODE);
        this.enableElement(this.ELEM_ID_SUBMIT);
        this.selectVCode();
        this.setStatusMsgError("Incorrect verification code.");
        return false;
    }

    return true;
}

SMS_GadgetVerifyView.prototype.startOverLink_onClick = function()
{
	VM_SetPreference( PREF_KEY_ACCOUNT , '' );
    VM_SetPreference( PREF_KEY_U_STRING, '' );
        
    this.m_viewController.displayRegistrationView();
}

SMS_GadgetVerifyView.prototype.noCodeLink_onClick = function()
{
    if (!this.m_NoCodeRequestInProgress)
    {
        this.m_NoCodeRequestInProgress = true;

        this.setStatusMsgInfo("Sending...");

        this.m_activationHandler.verifyPhone(
            VM_GetPreference( PREF_KEY_ACCOUNT ),
            VM_GetPreference( PREF_KEY_U_STRING ),
            '',
            SESSION.getVal(Session_Key.WX_PHONE_CC),
            SESSION.getVal(Session_Key.WX_PHONE_NN));
     }
}

SMS_GadgetVerifyView.prototype.onSetCarrierInfoSucceeded = function(responseMessage)
{
    try
    {
        switch(responseMessage.m_status)
                {
            case MSG_ResponseStatus.SUCCESS:
                // Success
                {
                    this.enableForm();
                    this.enableElement(this.ELEM_ID_CARRIER);
                    this.enableElement(this.ELEM_ID_VCODE);
                    this.enableElement(this.ELEM_ID_SUBMIT);
                    this.getElement(this.ELEM_ID_VCODE).focus();
                    this.getElement(this.ELEM_ID_VCODE).select();

                    this.setStatusMsgInfo("Code sent.");

                    break;
                }

            default:
                {
                    this.enableForm();
                    this.enableElement(this.ELEM_ID_CARRIER);
                    this.enableElement(this.ELEM_ID_VCODE);
                    this.enableElement(this.ELEM_ID_SUBMIT);
                    this.setStatusMsgInfo(responseMessage.m_statusMsg);

                    break;
                }
        }

        GOOGLE_IG_AdjustIFrameHeight();
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetVerifyView.prototype.onSetCarrierInfoFailed = function(statusMessage)
{
    try
    {
        this.enableForm();
        this.enableElement(this.ELEM_ID_CARRIER);
        this.enableElement(this.ELEM_ID_VCODE);
        this.enableElement(this.ELEM_ID_SUBMIT);
        this.m_onVerificationFail(statusMessage);
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetVerifyView.prototype.onVerifyPhoneSucceeded = function(responseMessage)
{
    try
    {
        this.enableForm();
        this.enableElement(this.ELEM_ID_CARRIER);
        this.enableElement(this.ELEM_ID_VCODE);
        this.enableElement(this.ELEM_ID_SUBMIT);

        if (this.m_state == this.VERIFY_STATE_SUBMIT_VCODE)
        {
            if (responseMessage.m_status == MSG_ResponseStatus.SUCCESS)
            {
                this.m_onVerificationSuccess(responseMessage);
            }
            else
            {
                this.setStatusMsgError(responseMessage.m_statusMsg);
            }
        }
        else
        {
            this.m_NoCodeRequestInProgress = false;

            if (responseMessage.m_status == MSG_ResponseStatus.SUCCESS)
            {
                this.setStatusMsgInfo("Code sent.");
            }
            else
            {
                this.setStatusMsgError(responseMessage.m_statusMsg);
            }
        }

        GOOGLE_IG_AdjustIFrameHeight();
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}

SMS_GadgetVerifyView.prototype.onVerifyPhoneFailed = function(statusMessage)
{
    try
    {
        this.enableForm();
        this.enableElement(this.ELEM_ID_CARRIER);
        this.enableElement(this.ELEM_ID_VCODE);
        this.enableElement(this.ELEM_ID_SUBMIT);
        this.getElement(this.ELEM_ID_VCODE).focus();
        this.getElement(this.ELEM_ID_VCODE).select();

        if (this.m_state == this.VERIFY_STATE_SUBMIT_VCODE)
        {
            this.m_onVerificationFail(statusMessage);
        }
        else
        {
            this.m_NoCodeRequestInProgress = false;

            this.setStatusMsgError(responseMessage.m_statusMsg);
        }
    }
    catch (e)
    {
        this.setStatusMsgError("Application exception.");
    }
}