`;
pdfHtml += `
Qualitative Factors
Manual Payments: More direct control but time-consuming and higher risk of human error (late fees, missed payments).
Automatic Payments: Convenient, saves time, reduces late payments, may offer discounts. Risk of overdraft if funds are low; regular monitoring of statements is still advised.
`;
pdfHtml += `
`;
if(pdfOutputEl) {
pdfOutputEl.innerHTML = pdfHtml;
} else { console.error("PDF output element missing for autopay tool."); return; }
const opt = {
margin: [0.5, 0.5, 0.5, 0.5],
filename: `Autopay_vs_Manual_Comparison_${new Date().toISOString().slice(0,10)}.pdf`,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, useCORS: true, logging: false, width: pdfOutputEl.scrollWidth, height: pdfOutputEl.scrollHeight + 20 },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
if (typeof html2pdf !== 'undefined' && pdfOutputEl) {
const originalDisplayStyle = pdfOutputEl.style.display;
pdfOutputEl.style.display = 'block'; // Make visible for html2pdf
// Ensure dimensions are recalculated based on content
opt.html2canvas.width = pdfOutputEl.scrollWidth;
opt.html2canvas.height = pdfOutputEl.scrollHeight + 20;
html2pdf().from(pdfOutputEl).set(opt).save()
.then(() => { pdfOutputEl.style.display = originalDisplayStyle; pdfOutputEl.innerHTML = ''; })
.catch(err => {
console.error("PDF generation failed:", err);
alert("PDF generation error.");
pdfOutputEl.style.display = originalDisplayStyle; pdfOutputEl.innerHTML = '';
});
} else {
alert("PDF library not loaded or output element missing.");
}
}
// --- Tab Navigation ---
window.apmt_openTab = function(evt, tabName) {
if (!tabs.length || !tabLinks.length) { return; }
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 {
if (tabs.length > 0 && tabs[0]) tabs[0].style.display = 'block';
if (tabLinks.length > 0 && tabLinks[0]) tabLinks[0].classList.add('active');
currentTabIndex = 0;
apmt_updateNavButtons();
return;
}
let clickedIndex = -1;
if (evt && evt.currentTarget) {
evt.currentTarget.classList.add('active');
clickedIndex = tabLinks.indexOf(evt.currentTarget);
} else {
clickedIndex = tabLinks.findIndex(link => {
if (!link) return false;
const onclickAttr = link.getAttribute('onclick');
return onclickAttr && onclickAttr.includes(`'${tabName}'`);
});
if (clickedIndex !== -1 && tabLinks[clickedIndex]) {
tabLinks[clickedIndex].classList.add('active');
} else {
if (tabLinks.length > 0 && tabLinks[0]) tabLinks[0].classList.add('active');
clickedIndex = 0;
}
}
currentTabIndex = (clickedIndex !== -1 && clickedIndex < tabs.length) ? clickedIndex : 0;
apmt_updateNavButtons();
// If moving to results tab, ensure calculations are displayed
if (tabName === 'apmt-tab2' && lastComparisonResults) {
apmt_calculateAndDisplayComparison(); // Re-render results to ensure visibility
}
}
window.apmt_navigateTab = function(direction) {
if (!tabs.length || !tabLinks.length) { return; }
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 apmt_updateNavButtons() {
if(prevButton) prevButton.disabled = currentTabIndex === 0;
if(nextButton && tabs) nextButton.disabled = currentTabIndex >= tabs.length - 1;
}
function loadSettingsFromLocalStorage_apmt() {
try {
const settingsToLoad = [
{ el: valueTimeEl, key: `${toolId}_valueTime`, default: '20'},
{ el: stampCostEl, key: `${toolId}_stampCost`, default: '0.73'}, // Update with current rate
{ el: avgOverdraftFeeEl, key: `${toolId}_overdraftFee`, default: '30'}
];
settingsToLoad.forEach(item => {
if (item.el) {
const storedVal = localStorage.getItem(item.key);
item.el.value = storedVal !== null ? storedVal : item.default;
}
});
} catch(e) { console.warn("Could not load settings from local storage for APMT tool."); }
}
function saveSettingsToLocalStorage_apmt() {
try {
if(valueTimeEl) localStorage.setItem(`${toolId}_valueTime`, valueTimeEl.value);
if(stampCostEl) localStorage.setItem(`${toolId}_stampCost`, stampCostEl.value);
if(avgOverdraftFeeEl) localStorage.setItem(`${toolId}_overdraftFee`, avgOverdraftFeeEl.value);
} catch (e) { console.warn("Could not save settings to local storage for APMT tool."); }
}
const settingInputs_apmt = [valueTimeEl, stampCostEl, avgOverdraftFeeEl]; // Add others if they should persist
settingInputs_apmt.forEach(el => {
if(el) el.addEventListener('change', saveSettingsToLocalStorage_apmt);
});
// --- Initialization ---
function initializeTool() {
if (!tabs.length || !tabLinks.length || !tabs[0] || !tabLinks[0]) {
console.error("APMT Critical: Tab UI elements missing."); return;
}
loadSettingsFromLocalStorage_apmt();
apmt_openTab(null, 'apmt-tab1');
}
let apmt_attempts = 0;
function checkLibrariesAndInit_apmt() {
if (!document.getElementById(toolId)) {
if (apmt_attempts < 20) { apmt_attempts++; setTimeout(checkLibrariesAndInit_apmt, 100); }
else { console.error("Tool container not found for apmt tool."); }
return;
}
apmt_attempts = 0;
function checkPdfLib() {
if (typeof html2pdf !== 'undefined') {
initializeTool();
} else if (apmt_attempts < 20) {
apmt_attempts++;
setTimeout(checkPdfLib, 100);
} else {
console.warn("Warning: PDF generation library (html2pdf.js) could not be loaded for apmt tool.");
initializeTool();
}
}
checkPdfLib();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', checkLibrariesAndInit_apmt);
} else {
checkLibrariesAndInit_apmt();
}
})();