`;
const interpretationNote_pdf = document.getElementById('mrc_interpretation_note_container').innerText.replace("Understanding Your Refinance Analysis:", "").replace("Important Disclaimer:", "Disclaimer:").trim();
pdfContentEl.innerHTML = `
Mortgage Refinance Analysis Report
I. Input Summary
${inputsHtml}
II. Refinance Comparison Summary
${resultsHtml}
Important Notes & Disclaimer:${interpretationNote_pdf.replace(/\n/g, "
").replace(/
`;
document.body.appendChild(pdfContentEl);
html2canvas(pdfContentEl, { scale: 2, useCORS: true, logging:true, windowWidth: pdfContentEl.scrollWidth, windowHeight: pdfContentEl.scrollHeight }).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
let numPages = Math.ceil(canvas.height / ( (pdfHeight - 40) * (canvas.width / (pdfWidth - 40)) ) );
let pageCanvasHeight = (pdfHeight - 40) * (canvas.width / (pdfWidth - 40));
for (let i = 0; i < numPages; i++) {
if (i > 0) pdf.addPage();
let sourceY = i * pageCanvasHeight;
let sourceHeight = Math.min(pageCanvasHeight, canvas.height - sourceY);
const tempCanvas = document.createElement('canvas');
tempCanvas.width = canvas.width;
tempCanvas.height = sourceHeight;
const ctx = tempCanvas.getContext('2d');
ctx.drawImage(canvas, 0, sourceY, canvas.width, sourceHeight, 0, 0, canvas.width, sourceHeight);
const pageImgData = tempCanvas.toDataURL('image/png');
const pageImgHeight = (sourceHeight * (pdfWidth - 40)) / canvas.width;
pdf.addImage(pageImgData, 'PNG', 20, 20, pdfWidth - 40, pageImgHeight, undefined, 'FAST');
}
pdf.save('Mortgage_Refinance_Analysis.pdf');
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
}).catch(err => {
console.error("MRC PDF Error:", err); alert("Error generating PDF. See console.");
if(document.body.contains(pdfContentEl)) document.body.removeChild(pdfContentEl);
});
}
document.addEventListener('DOMContentLoaded', function() {
mrc_openTab({}, 'mrc_tab_current');
mrc_handleNewLoanTermChange(); // Initialize custom term field for new loan
// Default some values for quicker testing
const currentPrincipalEl = document.getElementById('mrc_currentPrincipal');
if (currentPrincipalEl && !currentPrincipalEl.value) currentPrincipalEl.value = '200000';
const currentRateEl = document.getElementById('mrc_currentAnnualRate');
if (currentRateEl && !currentRateEl.value) currentRateEl.value = '7.0';
const remTermYEl = document.getElementById('mrc_remainingTermYears');
if (remTermYEl && !remTermYEl.value) remTermYEl.value = '25';
const newRateEl = document.getElementById('mrc_newAnnualRate');
if (newRateEl && !newRateEl.value) newRateEl.value = '5.5';
// New term defaults to 15 years via select
const refiCostsEl = document.getElementById('mrc_refiClosingCosts');
if (refiCostsEl && !refiCostsEl.value) refiCostsEl.value = '3000';
if (!document.getElementById('mrc_currentPrincipal')) {
console.error("Critical input 'mrc_currentPrincipal' not found.");
}
});