`;
if(pdfOutputEl) pdfOutputEl.innerHTML = pdfHtml;
const opt = {
margin: [0.5, 0.5, 0.5, 0.5],
filename: `Inflation_Impact_Savings_Report_${new Date().toISOString().slice(0,10)}.pdf`,
image: { type: 'jpeg', quality: 0.98 },
html2canvas: { scale: 2, useCORS: true, logging: false },
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};
if (typeof html2pdf !== 'undefined' && pdfOutputEl) {
html2pdf().from(pdfOutputEl).set(opt).save()
.catch(err => { console.error("PDF generation failed:", err); alert("PDF generation error."); });
} else {
alert("PDF library not loaded or output element missing.");
}
});
}
// --- Tab Navigation ---
window.iist_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 => {
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;
iist_updateNavButtons();
}
window.iist_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 iist_updateNavButtons() {
if(prevButton) prevButton.disabled = currentTabIndex === 0;
if(nextButton) nextButton.disabled = currentTabIndex >= tabs.length - 1;
}
function loadSettingsFromLocalStorage() {
try {
const settingsToLoad = [
{ el: projectionYearsSavingsEl, key: `${toolId}_projYearsSavings`, default: '10' },
{ el: inflationRateSavingsEl, key: `${toolId}_inflationSavings`, default: '3.0' },
{ el: yearsUntilGoalEl, key: `${toolId}_projYearsGoal`, default: '10' },
{ el: inflationRateGoalEl, key: `${toolId}_inflationGoal`, default: '3.0' },
{ el: lowInflationRateSavingsEl, key: `${toolId}_lowInflationSavings`, default: '' },
{ el: highInflationRateSavingsEl, key: `${toolId}_highInflationSavings`, default: '' },
{ el: lowInflationRateGoalEl, key: `${toolId}_lowInflationGoal`, default: '' },
{ el: highInflationRateGoalEl, key: `${toolId}_highInflationGoal`, default: '' }
];
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."); }
}
function saveSettingsToLocalStorage() {
try {
if(projectionYearsSavingsEl) localStorage.setItem(`${toolId}_projYearsSavings`, projectionYearsSavingsEl.value);
if(inflationRateSavingsEl) localStorage.setItem(`${toolId}_inflationSavings`, inflationRateSavingsEl.value);
if(yearsUntilGoalEl) localStorage.setItem(`${toolId}_projYearsGoal`, yearsUntilGoalEl.value);
if(inflationRateGoalEl) localStorage.setItem(`${toolId}_inflationGoal`, inflationRateGoalEl.value);
if(lowInflationRateSavingsEl) localStorage.setItem(`${toolId}_lowInflationSavings`, lowInflationRateSavingsEl.value);
if(highInflationRateSavingsEl) localStorage.setItem(`${toolId}_highInflationSavings`, highInflationRateSavingsEl.value);
if(lowInflationRateGoalEl) localStorage.setItem(`${toolId}_lowInflationGoal`, lowInflationRateGoalEl.value);
if(highInflationRateGoalEl) localStorage.setItem(`${toolId}_highInflationGoal`, highInflationRateGoalEl.value);
} catch (e) { console.warn("Could not save settings to local storage."); }
}
const settingInputs = [
projectionYearsSavingsEl, inflationRateSavingsEl,
yearsUntilGoalEl, inflationRateGoalEl,
lowInflationRateSavingsEl, highInflationRateSavingsEl,
lowInflationRateGoalEl, highInflationRateGoalEl
];
settingInputs.forEach(el => {
if(el) el.addEventListener('change', saveSettingsToLocalStorage);
});
// --- Initialization ---
function initializeTool() {
loadSettingsFromLocalStorage();
reachGoalSectionEl.style.display = 'none';
iist_openTab(null, 'iist-tab1');
}
let iist_attempts = 0;
function checkLibrariesAndInit_iist() {
if (typeof html2pdf !== 'undefined') {
initializeTool();
} else if (iist_attempts < 20) {
iist_attempts++;
setTimeout(checkLibrariesAndInit_iist, 100);
} else {
alert("Error: PDF generation library (html2pdf.js) could not be loaded. PDF export will not work.");
initializeTool();
}
}
checkLibrariesAndInit_iist();
})();
