`;
pdfHtml += `
${currentCostsResultsEl.innerHTML}
`;
}
// Section 2: Rent vs. Buy
if (rentVsBuyResultsEl && rentVsBuyResultsEl.style.display !== 'none' && rentVsBuyResultsEl.innerHTML.length > 20) {
pdfHtml += `
Rent vs. Buy Analyzer
`;
// Add inputs...
pdfHtml += `
`;
pdfHtml += `
${rentVsBuyResultsEl.innerHTML}
`;
}
// Section 3: Mortgage Optimizer (Refinance, Extra Payments, Down Payment)
if (refinanceResultsEl && refinanceResultsEl.style.display !== 'none' && refinanceResultsEl.innerHTML.length > 20) {
pdfHtml += `
Refinance Analysis
`;
pdfHtml += `
`;
pdfHtml += `
${refinanceResultsEl.innerHTML}
`;
}
if (extraPaymentsResultsEl && extraPaymentsResultsEl.style.display !== 'none' && extraPaymentsResultsEl.innerHTML.length > 20) {
pdfHtml += `
Extra Payments Analysis
`;
pdfHtml += `
`;
pdfHtml += `
${extraPaymentsResultsEl.innerHTML}
`;
}
if (downPaymentResultsEl && downPaymentResultsEl.style.display !== 'none' && downPaymentResultsEl.innerHTML.length > 20) {
pdfHtml += `
Down Payment Impact
`;
pdfHtml += `
`;
pdfHtml += `
${downPaymentResultsEl.innerHTML}
`;
}
// Section 4: Affordability
if (affordabilityResultsEl && affordabilityResultsEl.style.display !== 'none' && affordabilityResultsEl.innerHTML.length > 20) {
pdfHtml += `
Housing Affordability Estimation
`;
pdfHtml += `
`;
pdfHtml += `
${affordabilityResultsEl.innerHTML}
`;
}
pdfHtml += `
`; // Close hcot-pdf-output
pdfOutputEl.innerHTML = pdfHtml;
const opt = {
margin: [0.5, 0.4, 0.5, 0.4],
filename: `Housing_Cost_Optimization_Report_${new Date().toISOString().slice(0,10)}.pdf`,
image: { type: 'jpeg', quality: 0.95 },
html2canvas: { scale: 2, useCORS: true, logging: false, scrollX: 0, scrollY: 0, windowWidth: pdfOutputEl.scrollWidth, windowHeight: pdfOutputEl.scrollHeight + 50},
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' },
pagebreak: { mode: ['css', 'avoid-all', 'legacy'] } // 'css' or 'avoid-all' can be good
};
if (html2pdf && pdfOutputEl) {
html2pdf().from(pdfOutputEl).set(opt).save()
.catch(err => { console.error("PDF generation failed:", err); alert("PDF generation failed. See console for details."); });
} else {
alert("PDF generation library or output element not ready.");
}
});
}
// --- Tab Navigation ---
window.hcot_openTab = function(evt, tabName) {
tabs.forEach(tab => { if(tab) tab.style.display = 'none'; });
tabLinks.forEach(link => { if(link) link.classList.remove('active'); });
const activeTabContent = toolContainer.querySelector('#' + tabName);
if(activeTabContent) activeTabContent.style.display = 'block';
else { console.error(`Tab content for ${tabName} not found.`); return; }
let clickedIndex = -1;
if (evt && evt.currentTarget) {
evt.currentTarget.classList.add('active');
clickedIndex = tabLinks.indexOf(evt.currentTarget);
} else {
clickedIndex = tabLinks.findIndex(link => {
const onclickAttr = link.getAttribute('onclick');
return onclickAttr && onclickAttr.includes(tabName);
});
if (clickedIndex !== -1 && tabLinks[clickedIndex]) tabLinks[clickedIndex].classList.add('active');
}
currentTabIndex = clickedIndex !== -1 ? clickedIndex : 0;
hcot_updateNavButtons();
}
window.hcot_navigateTab = function(direction) {
let newIndex = currentTabIndex;
if (direction === 'next' && currentTabIndex < tabs.length - 1) newIndex++;
else if (direction === 'prev' && currentTabIndex > 0) newIndex--;
if (tabLinks[newIndex] && newIndex !== currentTabIndex) tabLinks[newIndex].click();
}
function hcot_updateNavButtons() {
if(prevButton) prevButton.disabled = currentTabIndex === 0;
if(nextButton) nextButton.disabled = currentTabIndex >= tabs.length - 1;
}
// --- Initialization ---
function initializeTool() {
// Check for Font Awesome (if icons are desired and not globally available)
// if (typeof FontAwesome === 'undefined') { /* Potentially load FA or inform user */ }
// Default values if elements exist
if(currentSituationRentingEl) currentSituationRentingEl.checked = true;
hcot_toggleCurrentSituationForms(); // Set initial form visibility
const today = new Date(); // Not used here but good practice if date inputs needed default
hcot_openTab(null, 'hcot-tab1'); // Open first tab
}
// Check if html2pdf is loaded (it's from CDN)
let attempts = 0;
function checkLibrariesAndInit_hcot() {
if (typeof html2pdf !== 'undefined') {
initializeTool();
} else if (attempts < 20) { // Try for 2 seconds
attempts++;
setTimeout(checkLibrariesAndInit_hcot, 100);
} else {
alert("Error: PDF generation library (html2pdf.js) could not be loaded. PDF export will not work.");
initializeTool(); // Initialize anyway
}
}
checkLibrariesAndInit_hcot();
})();