/* Toolbox — all tool logic. Runs entirely in the browser. No data leaves the page
except the currency tool, which calls a public exchange-rate API. */
(function () {
const $ = id => document.getElementById(id);
const on = (id, ev, fn) => { const e = $(id); if (e) e.addEventListener(ev, fn); };
const num = v => { const n = parseFloat(v); return isNaN(n) ? 0 : n; };
const int = v => { const n = parseInt(v, 10); return isNaN(n) ? 0 : n; };
const inr = n => "₹" + Number(n).toLocaleString("en-IN", { maximumFractionDigits: 2 });
const n2 = n => Number(n).toLocaleString("en-IN", { maximumFractionDigits: 2 });
const set = (id, html) => { const e = $(id); if (e) e.innerHTML = html; };
/* ---------- 1. Age ---------- */
function initAge() {
const go = () => {
const v = $("age-dob").value; if (!v) return;
const dob = new Date(v + "T00:00:00"), now = new Date();
if (dob > now) { set("age-out", 'Date of birth is in the future.'); return; }
let y = now.getFullYear() - dob.getFullYear();
let m = now.getMonth() - dob.getMonth();
let d = now.getDate() - dob.getDate();
if (d < 0) { m--; d += new Date(now.getFullYear(), now.getMonth(), 0).getDate(); }
if (m < 0) { y--; m += 12; }
const days = Math.floor((now - dob) / 86400000);
const nb = new Date(now.getFullYear(), dob.getMonth(), dob.getDate());
if (nb < now) nb.setFullYear(now.getFullYear() + 1);
const toB = Math.ceil((nb - now) / 86400000);
set("age-out", `${y} years, ${m} months, ${d} days
Total: ${days.toLocaleString()} days · ${Math.floor(days/7).toLocaleString()} weeks · next birthday in ${toB} days
`);
};
on("age-go", "click", go); on("age-dob", "change", go);
}
/* ---------- 2. Percentage ---------- */
function initPercent() {
const go = () => {
const a = num($("p-a").value), b = num($("p-b").value), mode = $("p-mode").value;
let out = "";
if (mode === "of") out = `${n2(a)}% of ${n2(b)} = ${n2(a*b/100)}`;
else if (mode === "is") out = `${n2(a)} is ${b ? n2(a/b*100) : 0}% of ${n2(b)}`;
else out = `Change from ${n2(a)} to ${n2(b)} = ${a ? n2((b-a)/Math.abs(a)*100) : 0}% ${b>=a?"increase":"decrease"}`;
set("p-out", out);
};
on("p-go", "click", go); ["p-a","p-b"].forEach(id => on(id, "input", go));
}
/* ---------- 3. EMI ---------- */
function initEmi() {
const go = () => {
const P = num($("emi-p").value), R = num($("emi-r").value), N = int($("emi-n").value), unit = $("emi-unit").value;
const months = unit === "years" ? N * 12 : N;
if (P <= 0 || months <= 0) { set("emi-out", "Enter loan amount and tenure."); return; }
const r = R / 12 / 100;
const emi = r === 0 ? P / months : P * r * Math.pow(1+r, months) / (Math.pow(1+r, months) - 1);
const total = emi * months, interest = total - P;
set("emi-out", `EMI: ${inr(emi)} / month
Principal: ${inr(P)} · Total interest: ${inr(interest)} · Total payment: ${inr(total)} over ${months} months
`);
};
on("emi-go", "click", go); ["emi-p","emi-r","emi-n"].forEach(id => on(id, "input", go));
}
/* ---------- 4. GST ---------- */
function initGst() {
const go = () => {
const amt = num($("gst-amt").value), rate = num($("gst-rate").value), mode = $("gst-mode").value;
let net, gst, gross;
if (mode === "add") { net = amt; gst = amt * rate / 100; gross = net + gst; }
else { gross = amt; net = amt / (1 + rate/100); gst = gross - net; }
set("gst-out", `GST: ${inr(gst)}
Base amount: ${inr(net)} · Total (incl. GST): ${inr(gross)} @ ${n2(rate)}%
`);
};
on("gst-go", "click", go); ["gst-amt","gst-rate"].forEach(id => on(id, "input", go));
}
/* ---------- 5. SIP ---------- */
function initSip() {
const go = () => {
const P = num($("sip-p").value), R = num($("sip-r").value), Y = num($("sip-y").value);
const i = R / 12 / 100, n = Y * 12;
const fv = i === 0 ? P * n : P * ((Math.pow(1+i, n) - 1) / i) * (1 + i);
const invested = P * n;
set("sip-out", `Maturity: ${inr(fv)}