$(function topup() { var $topup = $('#topup'); $.get('/asp/topup_form.asp') .done(function(html) { $topup.html(html); steam(); playstationInitSelect2() initTabSwitching() }) .fail(function(reason) { console.error(reason); $topup.html(''); }); var psloaded = false; function initTabSwitching() { var $title = $('.topup-form h2'); $('#topup_steam_tab').on('show.bs.tab', function() { $title.text($title.data('steam')); $('#topup_steam_amount').closest('.field').removeClass('field--error'); $('#topup_steam_login').closest('.field').removeClass('field--error'); }); $('#topup_playstation_tab').on('show.bs.tab', function() { $title.text($title.data('playstation')); $('#topup_playstation_region').next('.select2-container').removeClass('select2-container--error'); $('#topup_playstation_amount').next('.select2-container').removeClass('select2-container--error'); if (!psloaded) { psloaded = true; playstation(); } }); } function steam() { var $amount = $('#topup_steam_amount'); var $amounts = $('#topup_steam_amounts'); var $login = $('#topup_steam_login'); var $currs = $('.id_topup_steam_currs'); var $symbol = $('#topup_steam_curr_symbol'); var $sold = $('#topup_steam_sold'); var $resp = $('#topup_steam_responses'); var $sellerUrl = $('#topup_steam_seller_url'); var $sellerName = $('#topup_steam_seller_name'); var $buyBtn0 = $('#topup_steam_buy'); var $buyBtn = $('#topup_steam_buy span'); var $amountLbl = $amount.closest('label'); var $amountField = $amount.closest('.field'); var $loginField = $login.closest('.field'); var buying = false; var currentRequest = null; var debouncedGetPrice = $.debounce(1500, function() { getPrice() }); var cntMin, cntMax, cntFree; calcMinMax(); $currs.change(function() { var curr = $(this).val(); getRand(curr); }); $amount .on('focus', function() { $amountLbl.addClass('input--focused'); }) .on('blur', function() { $amountLbl.removeClass('input--focused'); }) .on('input', function() { $amountField.removeClass('field--error'); $buyBtn.text($buyBtn.data('topup')); var a = $amount.val(); if (a.length > 6) a = a.substring(0, 6); if (a.length > 0) a = '' + parseInt(a, 10); if ($amount.val() !== a) $amount.val(a); fixInpWidth(); debouncedGetPrice(); }); $amounts.on('click', 'button', function() { $amountField.removeClass('field--error'); var a = $(this).data('amount'); $amount.val(a); fixInpWidth(); getPrice(); }); $login .on('input', function() { $loginField.removeClass('field--error'); }); $buyBtn0.click(function() { if (buying) return; buying = true; var amount = $amount.val(); var login = $.trim($login.val()); var eAmount = amount.length < 1; var eLogin = login.length < 1; $amountField.toggleClass('field--error', eAmount); $loginField.toggleClass('field--error', eLogin); if (eAmount) { // $amount.focus(); buying = false; return; } if (eLogin) { // $login.focus(); buying = false; return; } $buyBtn0.attr('data-loading', ''); var p = { ['Option_text_' + $amount.data('opt')]: login, ID_D: $amount.data('prod'), Agent: $amount.data('agent'), // FailPage: '', customerid: $amount.data('customerid'), vz: $amount.data('vz'), // promocode: '', lang: $amount.data('lang'), product_id: $amount.data('prod'), _ow: '1', TypeCurr: $amount.data('curr') }; if ($buyBtn.data('amount') !== '0') p.unit_amount = $buyBtn.data('amount'); p.unit_cnt = amount; var url = $buyBtn.data('url'); var precheck = +$amount.data('precheck') > 0; if (!precheck) { postForm(url, p); $buyBtn0.removeAttr('data-loading'); buying = false; return; } var precheckUrl = $buyBtn.data('precheck-url'); $.post(precheckUrl, p) .done(function(data) { if (data && data.cart_err) { showNotif(data.cart_err, 'error'); return; } postForm(url, p); }) .fail(function(reason) { console.error(reason); postForm(url, p); }) .always(function() { $buyBtn0.removeAttr('data-loading'); buying = false; }); }); function getRand(curr) { $.get('/asp/topup_steam_rand.asp?curr=' + curr) .done(function(d) { $buyBtn.text($buyBtn.data('topup')); $amount.val(''); fixInpWidth(); $symbol.text(d.symbol); $amounts.html( d.amounts.map(function(amount) { return ''; }).join('') ); $sold.text($sold.data('text') + d.cnt_sold); $resp.text($resp.data('text') + d.cnt_resp); $sellerName.text(d.seller_name); $sellerUrl.attr('href', d.seller_url).attr('title', d.seller_name); $amount.attr('data-prod', d.prod_id).data('prod', d.prod_id); $amount.attr('data-opt', d.opt_id).data('opt', d.opt_id); $amount.attr('data-precheck', d.precheck).data('precheck', d.precheck); $amount.attr('data-min', d.cnt_min).data('min', d.cnt_min); $amount.attr('data-max', d.cnt_max).data('max', d.cnt_max); $amount.attr('data-free', d.cnt_free).data('free', d.cnt_free); calcMinMax(); }); } function getPrice() { if (currentRequest) currentRequest.abort(); $buyBtn.attr('data-amount', '0').data('amount', '0'); var a = $amount.val(); if (a && cntMin && parseInt(a) < parseInt(cntMin)) a = cntMin; if (a && cntMax && parseInt(a) > parseInt(cntMax)) a = cntMax; if ($amount.val() !== a) { $amount.val(a); fixInpWidth(); } var p = { p: $amount.data('prod'), n: a, c: $amount.data('curr'), e: $amount.data('email') }; currentRequest = $.get('/asp/price_options.asp', p) .done(function(d) { d = JSON.parse(d); if (+d.err == 0 && +d.amount > 0) { $buyBtn.attr('data-amount', '' + d.amount).data('amount', '' + d.amount); $buyBtn.text($buyBtn.data('topup') + $buyBtn.data('for') + d.amount + ' ' + $amount.data('symbol')); } }) } function fixInpWidth() { var a = $amount.val(); var w = 5; if (a === '') { w = $amount.data('width'); } else { var dw = [104, 78, 100, 106, 106, 101, 104, 98, 102, 104]; for (var d of a) { w += (dw[+d] - 4) / 10.0; } } $amount.css('width', '' + w + 'px'); } function calcMinMax() { cntMin = $amount.data('min'); cntMax = $amount.data('max'); cntFree = $amount.data('free'); if (cntMin) cntMin = '' + parseInt(cntMin); if (cntMax) cntMax = '' + parseInt(cntMax); if (cntFree) cntFree = '' + parseInt(cntFree); if (!cntMax && cntFree || cntMax && cntFree && parseInt(cntMax) > parseInt(cntFree)) cntMax = cntFree; } } function playstationInitSelect2() { var $region = $('#topup_playstation_region'); var $amount = $('#topup_playstation_amount'); -->