<commit_before>const Client           = require('../../base/client');
const BinarySocket     = require('../../base/socket');
const getDecimalPlaces = require('../../common/currency').getDecimalPlaces;
const isCryptocurrency = require('../../common/currency').isCryptocurrency;
const FormManager      = require('../../common/form_manager');
const validEmailToken  = require('../../common/form_validation').validEmailToken;
const localize         = require('../../../_common/localize').localize;
const urlParam         = require('../../../_common/url').param;

const PaymentAgentWithdraw = (() => {
    const view_ids  = {
        error  : '#viewError',
        notice : '#viewNotice',
        success: '#viewSuccess',
        confirm: '#viewConfirm',
        form   : '#viewForm',
    };
    const field_ids = {
        ddl_agents: '#ddlAgents',
        txt_amount: '#txtAmount',
        txt_desc  : '#txtDescription',
    };

    let $views,
        agent_name;

    // -----------------------
    // ----- Agents List -----
    // -----------------------
    const populateAgentsList = (response) => {
        const $ddl_agents = $(field_ids.ddl_agents);
        $ddl_agents.empty();
        const pa_list = (response.paymentagent_list || {}).list;
        if (pa_list.length > 0) {
            checkToken($ddl_agents, pa_list);
        } else {
            showPageError(localize('The Payment Agent facility is currently not available in your country.'));
        }
    };

    const checkToken = ($ddl_agents, pa_list) => {
        const token = urlParam('token') || '';
        if (!token) {
            BinarySocket.send({ verify_email: Client.get('email'), type: 'paymentagent_withdraw' });
            setActiveView(view_ids.notice);
        } else if (!validEmailToken(token)) {
            showPageError('token_error');
        } else {
            insertListOption($ddl_agents, localize('Please select a payment agent'), '');
            for (let i = 0; i < pa_list.length; i++) {
                insertListOption($ddl_agents, pa_list[i].name, pa_list[i].paymentagent_loginid);
            }
            setActiveView(view_ids.form);

            const currency = Client.get('currency');
            const balance  = +Client.get('balance');
            const form_id  = `#${$(view_ids.form).find('form').attr('id')}`;
            const min      = isCryptocurrency(currency) ? 0.002 : 10;
            const max      = Math.min(isCryptocurrency(currency) ? 5 : 2000, balance);
            $(form_id).find('label[for="txtAmount"]').text(`${localize('Amount')} ${currency}`);
            FormManager.init(form_id, [
                { selector: field_ids.ddl_agents,        validations: ['req'], request_field: 'paymentagent_loginid' },
                { selector: field_ids.txt_amount,        validations: ['req', ['number', { type: 'float', decimals: getDecimalPlaces(currency), min, max }]], request_field: 'amount' },
                { selector: field_ids.txt_desc,          validations: ['general'], request_field: 'description' },

                { request_field: 'currency',              value: currency },
                { request_field: 'paymentagent_withdraw', value: 1 },
                { request_field: 'dry_run',               value: 1 },
            ], true);

            FormManager.handleSubmit({
                form_selector       : form_id,
                fnc_response_handler: withdrawResponse,
                fnc_additional_check: setAgentName,
                enable_button       : true,
            });
        }
    };
<commit_msg>check for insufficient balance<commit_after>const Client           = require('../../base/client');
const BinarySocket     = require('../../base/socket');
const getDecimalPlaces = require('../../common/currency').getDecimalPlaces;
const isCryptocurrency = require('../../common/currency').isCryptocurrency;
const FormManager      = require('../../common/form_manager');
const validEmailToken  = require('../../common/form_validation').validEmailToken;
const localize         = require('../../../_common/localize').localize;
const urlParam         = require('../../../_common/url').param;

const PaymentAgentWithdraw = (() => {
    const view_ids  = {
        error  : '#viewError',
        notice : '#viewNotice',
        success: '#viewSuccess',
        confirm: '#viewConfirm',
        form   : '#viewForm',
    };
    const field_ids = {
        ddl_agents: '#ddlAgents',
        txt_amount: '#txtAmount',
        txt_desc  : '#txtDescription',
    };

    let $views,
        agent_name;

    // -----------------------
    // ----- Agents List -----
    // -----------------------
    const populateAgentsList = (response) => {
        const $ddl_agents = $(field_ids.ddl_agents);
        $ddl_agents.empty();
        const pa_list = (response.paymentagent_list || {}).list;
        if (pa_list.length > 0) {
            checkToken($ddl_agents, pa_list);
        } else {
            showPageError(localize('The Payment Agent facility is currently not available in your country.'));
        }
    };

    const checkToken = ($ddl_agents, pa_list) => {
        const token = urlParam('token') || '';
        if (!token) {
            BinarySocket.send({ verify_email: Client.get('email'), type: 'paymentagent_withdraw' });
            setActiveView(view_ids.notice);
        } else if (!validEmailToken(token)) {
            showPageError('token_error');
        } else {
            insertListOption($ddl_agents, localize('Please select a payment agent'), '');
            for (let i = 0; i < pa_list.length; i++) {
                insertListOption($ddl_agents, pa_list[i].name, pa_list[i].paymentagent_loginid);
            }
            setActiveView(view_ids.form);

            const currency = Client.get('currency');
            const form_id  = `#${$(view_ids.form).find('form').attr('id')}`;
            const min      = isCryptocurrency(currency) ? 0.002 : 10;
            const max      = isCryptocurrency(currency) ? 5 : 2000;
            $(form_id).find('label[for="txtAmount"]').text(`${localize('Amount')} ${currency}`);
            FormManager.init(form_id, [
                { selector: field_ids.ddl_agents,        validations: ['req'], request_field: 'paymentagent_loginid' },
                { selector: field_ids.txt_amount,        validations: ['req', ['number', { type: 'float', decimals: getDecimalPlaces(currency), min, max }], ['custom', { func: () => +Client.get('balance') >= +$('#amount').val(), message: localize('Insufficient balance.') }]], request_field: 'amount' },
                { selector: field_ids.txt_desc,          validations: ['general'], request_field: 'description' },

                { request_field: 'currency',              value: currency },
                { request_field: 'paymentagent_withdraw', value: 1 },
                { request_field: 'dry_run',               value: 1 },
            ], true);

            FormManager.handleSubmit({
                form_selector       : form_id,
                fnc_response_handler: withdrawResponse,
                fnc_additional_check: setAgentName,
                enable_button       : true,
            });
        }
    };

{'fraction_non_alphanumeric': 0.1642579210672596, 'fraction_numerical': 0.004168982768204558, 'mean_word_length': 2.092823377739579, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3630426', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1945, 'n_words': 378}