/* ============================================================ REPLACE the existing calculate(), showWhatIf(), and sendEmail() functions in Snippet 95 with these three. Everything else in the snippet (rendering functions, insight text, HTML) stays unchanged. Uses window.dsAjax (ajaxUrl + nonce) instead of window.dsNonce — dsNonce is not a documented plugin global and may be undefined. ============================================================ */ function calculate() { var debts = getDebts(); var budget = parseFloat(document.getElementById('ds-budget').value) || 0; if (debts.length === 0) { alert('Please enter at least one debt with a balance.'); return; } if (budget <= 0) { alert('Please enter your monthly budget.'); return; } var totalMin = debts.reduce(function(s,x) { return s + x.min; }, 0); if (budget < totalMin) { alert('Your budget (' + fmt(budget) + ') is less than your total minimum payments (' + fmt(totalMin) + '). Please increase your budget.'); return; } var btn = document.querySelector('.ds-btn-green[onclick="calculate()"]'); if (btn) { btn.textContent = 'Calculating...'; btn.disabled = true; } var ajax = window.dsAjax || {}; fetch((ajax.ajaxUrl || (window.location.origin + '/wp-admin/admin-ajax.php')), { method: 'POST', headers: {'Content-Type':'application/x-www-form-urlencoded'}, body: 'action=ds_calculate_t1' + '&nonce=' + encodeURIComponent(ajax.nonce || '') + '&debts=' + encodeURIComponent(JSON.stringify(debts)) + '&budget=' + encodeURIComponent(budget) + '&strategy=' + encodeURIComponent(strategy) }) .then(function(r) { return r.json(); }) .then(function(res) { if (btn) { btn.textContent = 'Calculate My Debt-Free Date →'; btn.disabled = false; } if (!res.success) { if (res.data && res.data.message === 'budget_below_minimum') { alert('Your budget is less than your total minimum payments (' + fmt(res.data.total_min) + '). Please increase your budget.'); } else { alert((res.data && res.data.message) || 'Something went wrong. Please try again.'); } return; } renderResults(res.data, debts, budget); }) .catch(function() { if (btn) { btn.textContent = 'Calculate My Debt-Free Date →'; btn.disabled = false; } alert('Something went wrong. Please check your connection and try again.'); }); } function renderResults(data, debts, budget) { var strategyMap = { avalanche: data.avalanche, snowball: data.snowball, smart: data.smart }; var mainRes = strategyMap[strategy]; var totalDebt = data.total_debt; var totalMin = data.total_min; // stash for What-If and email/save — same shape as before, now backend-sourced calcResult = { debts: debts, budget: budget, mainRes: { months: mainRes.months, totalInterest: mainRes.total_interest, firstMonthInterest: mainRes.first_month_interest, schedule: mainRes.schedule }, avRes: { months: data.avalanche.months, totalInterest: data.avalanche.total_interest }, snRes: { months: data.snowball.months, totalInterest: data.snowball.total_interest }, smRes: { months: data.smart.months, totalInterest: data.smart.total_interest }, totalDebt: totalDebt }; document.getElementById('res-date').textContent = getDebtFreeDate(mainRes.months); document.getElementById('res-months').textContent = mainRes.months + ' months from today'; var sNames = { avalanche: '🏔️ Avalanche Method — highest APR first', snowball: '⛄ Snowball Method — smallest balance first', smart: '🤖 Smart Focus Method — AI-balanced' }; document.getElementById('res-strategy-name').textContent = sNames[strategy]; document.getElementById('res-total-interest').textContent = fmt(mainRes.total_interest); document.getElementById('res-monthly-interest').textContent = fmt(mainRes.first_month_interest); document.getElementById('res-total-debt').textContent = fmt(totalDebt); var pct = Math.min(Math.round((1 / mainRes.months) * 100), 5); document.getElementById('res-progress-pct').textContent = pct + '% complete'; setTimeout(function() { document.getElementById('res-progress-bar').style.width = pct + '%'; }, 300); document.getElementById('res-halfway').textContent = getDebtFreeDate(Math.round(mainRes.months / 2)); document.getElementById('res-free-label').textContent = getDebtFreeDate(mainRes.months); var highDebt = debts.reduce(function(p,c) { return c.apr > p.apr ? c : p; }, debts[0]); var insight = ''; if (mainRes.months > 60) { insight = 'You\'re currently on track for over ' + mainRes.months + ' months. Every extra ' + sym + '50/month knocks months off that timeline. Your highest-cost debt is ' + highDebt.name + ' at ' + highDebt.apr + '% APR — that\'s where your money is disappearing fastest.'; } else if (mainRes.months > 24) { insight = 'You have a solid plan. At your current budget you\'ll be debt-free in ' + mainRes.months + ' months. You\'re losing ' + fmt(mainRes.first_month_interest) + ' to interest every single month. Even ' + sym + '50 extra monthly would make a real difference.'; } else { insight = 'You\'re in great shape. With ' + mainRes.months + ' months to debt freedom, you\'re ahead of most people. Stay consistent and you\'ll hit ' + getDebtFreeDate(mainRes.months) + '. Interest you\'re paying monthly: ' + fmt(mainRes.first_month_interest) + ' — keep that in mind as motivation.'; } document.getElementById('res-ai-insight').textContent = insight; var bestInterest = Math.min(data.avalanche.total_interest, data.snowball.total_interest, data.smart.total_interest); var compareHtml = ''; [{name:'🏔️ Avalanche',res:data.avalanche},{name:'⛄ Snowball',res:data.snowball},{name:'🤖 Smart Focus',res:data.smart}].forEach(function(s) { var isBest = Math.abs(s.res.total_interest - bestInterest) < 1; compareHtml += ''; compareHtml += '' + s.name + (isBest ? 'BEST' : '') + ''; compareHtml += '' + getDebtFreeDate(s.res.months) + ''; compareHtml += '' + fmt(s.res.total_interest) + ''; compareHtml += '' + s.res.months + ''; }); document.getElementById('res-compare-body').innerHTML = compareHtml; var wiHtml = ''; [50, 100, 200, 500].forEach(function(e) { wiHtml += ''; }); document.getElementById('ds-whatif-btns').innerHTML = wiHtml; var sortedByApr = debts.slice().sort(function(a,b) { return b.apr - a.apr; }); var insightsHtml = ''; insightsHtml += '
  • 🔥Your most expensive debt is ' + sortedByApr[0].name + ' at ' + sortedByApr[0].apr + '% APR. It costs you ' + fmtDec(sortedByApr[0].balance * sortedByApr[0].apr / 12 / 100) + ' every month in interest alone.
  • '; insightsHtml += '
  • 📅Using the ' + (strategy === 'avalanche' ? 'Avalanche' : strategy === 'snowball' ? 'Snowball' : 'Smart Focus') + ' method, you\'ll be debt-free by ' + getDebtFreeDate(mainRes.months) + '.
  • '; insightsHtml += '
  • 💸Over the life of your debt you\'ll pay ' + fmt(mainRes.total_interest) + ' in interest. That\'s money the banks keep — not you.
  • '; if (data.avalanche.total_interest < data.snowball.total_interest) { insightsHtml += '
  • 💡Switching to Avalanche over Snowball would save you ' + fmt(data.snowball.total_interest - data.avalanche.total_interest) + ' in interest over your repayment period.
  • '; } insightsHtml += '
  • 🚀Your budget is ' + fmt(budget) + '/month. Minimums total ' + fmt(totalMin) + '/month. You have ' + fmt(budget - totalMin) + ' extra attacking the focus debt.
  • '; document.getElementById('res-insights').innerHTML = insightsHtml; var emos = [ 'Every payment you make is money the banks never get to touch again. Keep going.', 'In ' + mainRes.months + ' months, every single penny of this debt is gone. That date is real.', 'You\'re not just paying off debt. You\'re buying back your freedom.', 'Most people never make a plan. You just did. That puts you ahead of almost everyone.' ]; document.getElementById('res-emotional').innerHTML = emos[Math.floor(Math.random() * emos.length)]; var tableHtml = ''; mainRes.schedule.forEach(function(row) { var isLast = row.remaining < 1; tableHtml += '' + (isLast ? '🎉 ' : '') + 'Month ' + row.month + ''; tableHtml += '' + fmt(row.payment) + ''; tableHtml += '' + fmt(row.interest) + ''; tableHtml += '' + fmt(row.principal) + ''; tableHtml += '' + (row.remaining < 1 ? 'DEBT FREE! 🎉' : fmt(row.remaining)) + ''; }); document.getElementById('res-table-body').innerHTML = tableHtml; updateSaveSection(); setDisclaimer(''); document.getElementById('ds-results').classList.add('show'); setTimeout(function() { document.getElementById('ds-results').scrollIntoView({ behavior: 'smooth', block: 'start' }); }, 100); } function showWhatIf(extra, btn) { document.querySelectorAll('.ds-whatif-btn').forEach(function(b) { b.classList.remove('active'); }); btn.classList.add('active'); btn.disabled = true; var ajax = window.dsAjax || {}; fetch((ajax.ajaxUrl || (window.location.origin + '/wp-admin/admin-ajax.php')), { method: 'POST', headers: {'Content-Type':'application/x-www-form-urlencoded'}, body: 'action=ds_calculate_t1' + '&nonce=' + encodeURIComponent(ajax.nonce || '') + '&debts=' + encodeURIComponent(JSON.stringify(calcResult.debts)) + '&budget=' + encodeURIComponent(calcResult.budget + extra) + '&strategy=' + encodeURIComponent(strategy) }) .then(function(r) { return r.json(); }) .then(function(res) { btn.disabled = false; if (!res.success) return; var newRes = res.data[strategy]; var saved = calcResult.mainRes.totalInterest - newRes.total_interest; var faster = calcResult.mainRes.months - newRes.months; document.getElementById('wi-date').textContent = getDebtFreeDate(newRes.months); document.getElementById('wi-saved').textContent = fmt(saved); document.getElementById('wi-faster').textContent = faster + ' months'; document.getElementById('ds-whatif-result').classList.add('show'); }) .catch(function() { btn.disabled = false; }); } function sendEmail() { var email = document.getElementById('ds-email-input').value.trim(); var status = document.getElementById('ds-email-status'); if (!email || !email.includes('@')) { status.textContent = 'Please enter a valid email address.'; status.className = 'ds-email-status error'; return; } if (!calcResult) return; status.textContent = 'Sending...'; status.className = 'ds-email-status'; var ajax = window.dsAjax || {}; var data = { action: 'ds_email_t1', email: email, debt_free_date: document.getElementById('res-date').textContent, months: calcResult.mainRes.months, total_interest: fmt(calcResult.mainRes.totalInterest), total_debt: fmt(calcResult.totalDebt), strategy: strategy, currency: sym, nonce: (ajax.nonce || '') }; fetch((ajax.ajaxUrl || (window.location.origin + '/wp-admin/admin-ajax.php')), { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: Object.keys(data).map(function(k) { return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]); }).join('&') }) .then(function(r) { return r.json(); }) .then(function(res) { if (res.success) { status.textContent = '✅ Plan sent! Check your inbox.'; status.className = 'ds-email-status success'; document.getElementById('ds-email-input').value = ''; } else { status.textContent = (res.data && res.data.message) || 'Something went wrong. Try again.'; status.className = 'ds-email-status error'; } }) .catch(function() { status.textContent = 'Something went wrong. Try again.'; status.className = 'ds-email-status error'; }); } https://debtshiftai.com/post-sitemap.xml 2026-07-14T10:38:00+00:00 https://debtshiftai.com/page-sitemap.xml 2026-07-12T00:11:17+00:00 https://debtshiftai.com/product-sitemap.xml 2026-07-12T11:28:35+00:00 https://debtshiftai.com/category-sitemap.xml 2026-07-14T10:38:00+00:00