`;
differenceOutputDiv.innerHTML = `
Comparison Highlights
Difference in Future Value (Passive - Active): ${pac_formatCurrency(passiveValue - activeValue)}
Difference in Total Costs (Active - Passive): ${pac_formatCurrency(totalActiveCosts - totalPassiveCosts)}
`;
// Populate and show breakdown table
const breakdownTableBody = document.getElementById('pac_yearBreakdownTableBody');
breakdownTableBody.innerHTML = '';
pac_yearlyBreakdownDataStore.forEach(data => {
const row = breakdownTableBody.insertRow();
row.insertCell().textContent = data.year;
row.insertCell().textContent = pac_formatCurrency(data.passiveValue);
row.insertCell().textContent = pac_formatCurrency(data.passiveCosts);
row.insertCell().textContent = pac_formatCurrency(data.activeValue);
row.insertCell().textContent = pac_formatCurrency(data.activeCosts);
row.insertCell().textContent = pac_formatCurrency(data.difference);
});
document.getElementById('pac_breakdownToggle').style.display = 'block';
document.getElementById('pac_yearBreakdownContainer').style.display = 'none'; // Keep it hidden initially
document.getElementById('pac_downloadPdfButton').style.display = 'block';
}
function pac_toggleBreakdownTable() {
const tableContainer = document.getElementById('pac_yearBreakdownContainer');
const toggleButton = document.getElementById('pac_breakdownToggle');
if (tableContainer.style.display === 'none' || tableContainer.style.display === '') {
tableContainer.style.display = 'block';
toggleButton.textContent = 'Hide Year-by-Year Breakdown â–²';
} else {
tableContainer.style.display = 'none';
toggleButton.textContent = 'Show Year-by-Year Breakdown â–¼';
}
}
function pac_downloadPDF() {
if (typeof jspdf === 'undefined' || typeof jspdf.jsPDF === 'undefined' || typeof html2canvas === 'undefined') {
alert('PDF generation libraries not loaded.'); return;
}
if (!window.pac_pdfData) {
alert('Please run the comparison first to generate a report.'); return;
}
console.log("pac_downloadPDF called, data:", window.pac_pdfData);
const { jsPDF } = jspdf;
const pdf = new jsPDF('p', 'pt', 'a4');
const pdfCreationTimestamp = new Date().toLocaleString();
const pdfContentEl = document.createElement('div');
pdfContentEl.style.position = 'absolute'; pdfContentEl.style.left = '-9999px';
pdfContentEl.style.width = '700px'; pdfContentEl.style.fontFamily = 'Arial, sans-serif';
const { inputs, results, yearlyBreakdown } = window.pac_pdfData;
let inputsHtml = `
Common Investment Parameters
| Initial Investment: | ${pac_formatCurrency(inputs.initialInvestment)} |
| Additional Annual Contribution: | ${pac_formatCurrency(inputs.annualContribution)} |
| Investment Horizon: | ${inputs.investmentHorizon} years |
| Assumed Gross Annual Return: | ${pac_formatPercentage(inputs.grossAnnualReturn)} |
Passive Strategy Costs
| Annual Expense Ratio: | ${pac_formatPercentage(inputs.passive.expenseRatio)} |
${inputs.passive.otherFeesType !== 'none' ? `
| Other Annual Fees Type: | ${inputs.passive.otherFeesType.charAt(0).toUpperCase() + inputs.passive.otherFeesType.slice(1)} |
| Other Annual Fees Value: | ${inputs.passive.otherFeesType === 'fixed' ? pac_formatCurrency(inputs.passive.otherFeesValue) : pac_formatPercentage(inputs.passive.otherFeesValue)} |
` : '| Other Annual Fees: | None |
'}
Active Strategy Costs
| Annual Expense Ratio: | ${pac_formatPercentage(inputs.active.expenseRatio)} |
| Advisor Fee (% AUM): | ${pac_formatPercentage(inputs.active.advisorFee)} |
| Trading Cost Drag (% p.a.): | ${pac_formatPercentage(inputs.active.tradingCostDrag)} |
${inputs.active.otherFeesType !== 'none' ? `
| Other Annual Fees Type: | ${inputs.active.otherFeesType.charAt(0).toUpperCase() + inputs.active.otherFeesType.slice(1)} |
| Other Annual Fees Value: | ${inputs.active.otherFeesType === 'fixed' ? pac_formatCurrency(inputs.active.otherFeesValue) : pac_formatPercentage(inputs.active.otherFeesValue)} |
` : '| Other Annual Fees: | None |
'}
`;
let resultsSummaryHtml = `
Overall Comparison Summary
| Passive Strategy | Active Strategy |
| Projected Future Value: | ${pac_formatCurrency(results.passiveFutureValue)} | ${pac_formatCurrency(results.activeFutureValue)} |
| Total Costs Incurred: | ${pac_formatCurrency(results.totalPassiveCosts)} | ${pac_formatCurrency(results.totalActiveCosts)} |
| |
| Difference in Future Value (Passive - Active): | ${pac_formatCurrency(results.differenceInValue)} |
| Difference in Total Costs (Active - Passive): | ${pac_formatCurrency(results.differenceInCosts)} |
`;
let breakdownTableHtml = `
| Year | Passive Val ($) | Passive Costs ($) | Active Val ($) | Active Costs ($) | Diff. in Val ($) |
`;
if (yearlyBreakdown && yearlyBreakdown.length > 0) {
yearlyBreakdown.forEach(data => {
breakdownTableHtml += `| ${data.year} | ${pac_formatCurrency(data.passiveValue)} | ${pac_formatCurrency(data.passiveCosts)} | ${pac_formatCurrency(data.activeValue)} | ${pac_formatCurrency(data.activeCosts)} | ${pac_formatCurrency(data.difference)} |
`;
});
} else {
breakdownTableHtml += `| No breakdown available. |
`;
}
breakdownTableHtml += ``;
const interpretationNote_pdf = `This comparison illustrates the long-term impact of costs on investment growth. Lower costs can lead to higher net returns over time, assuming the same gross performance. Active management aims to outperform, which, if successful after costs, could differ. Assumes consistent returns and fees.`;
pdfContentEl.innerHTML = `
Passive vs. Active Investing Cost Comparison
${inputsHtml}
${resultsSummaryHtml}
Year-by-Year Breakdown
Note: ${interpretationNote_pdf}
`;
document.body.appendChild(pdfContentEl);
html2canvas(pdfContentEl, { scale: 2, useCORS: true, logging:true, width: pdfContentEl.scrollWidth, height: pdfContentEl.scrollHeight }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgProps = pdf.getImageProperties(imgData);
const imgWidth = pdfWidth - 40;
const imgHeight = (imgProps.height * imgWidth) / imgProps.width;
let finalHeight = imgHeight;
if (imgHeight > pdfHeight - 40) {
finalHeight = pdfHeight - 40;
}
pdf.addImage(imgData, 'PNG', 20, 20, imgWidth, finalHeight, undefined, 'FAST');
pdf.save('Passive_vs_Active_Cost_Comparison.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("Cost Comparison PDF Error:", err); alert("Error generating Cost Comparison PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
// Initialize conditional fields
pac_toggleOtherFeesField('passive');
pac_toggleOtherFeesField('active');
pac_openTab({}, 'pac_setupTab'); // Ensure first tab is active
if (!document.getElementById('pac_initialInvestment')) {
console.error("Critical input 'pac_initialInvestment' not found. Tool may not function correctly.");
}
// Corrected initialization of year breakdown data store.
pac_yearlyBreakdownDataStore = [];
});