`;
return content;
}
function downloadPdf() {
// Need to ensure calculations have been run
if (Object.keys(calculatedMetrics).length === 0) {
errorMessageDiv.textContent = "Please analyze exposure first before downloading the PDF.";
errorMessageDiv.style.display = 'block';
return;
}
const printWindow = window.open('', '_blank');
if (!printWindow) {
alert("Please allow pop-ups for PDF download.");
return;
}
printWindow.document.write('Factor Exposure Report ');
printWindow.document.write('');
printWindow.document.write('');
printWindow.document.write(generatePdfContent());
printWindow.document.write('');
printWindow.document.close();
printWindow.print();
printWindow.close();
}
// --- Event Listeners ---
tab1Button.addEventListener('click', () => activateTab('tab1Content'));
tab2Button.addEventListener('click', () => activateTab('tab2Content'));
calculateButton.addEventListener('click', analyzeExposure);
resetButton.addEventListener('click', resetCalculator);
pdfDownloadButton.addEventListener('click', downloadPdf);
addFactorButton.addEventListener('click', addFactorInputRow);
// Event delegation for dynamically added remove buttons
factorInputsContainer.addEventListener('click', function(event) {
if (event.target.classList.contains('remove-factor-button')) {
if (currentFactorCount > 1) { // Ensure at least one factor remains
event.target.closest('.factor-input-row').remove();
currentFactorCount--;
}
}
});
prevTabButton.addEventListener('click', function() {
const currentTab = document.querySelector('.factor-tab-content.active');
const tabs = [tab1Content, tab2Content];
const currentIndex = tabs.indexOf(currentTab);
if (currentIndex > 0) {
activateTab(tabs[currentIndex - 1].id);
}
});
nextTabButton.addEventListener('click', function() {
const currentTab = document.querySelector('.factor-tab-content.active');
const tabs = [tab1Content, tab2Content];
const currentIndex = tabs.indexOf(currentTab);
if (currentIndex < tabs.length - 1) {
// If on input tab and trying to go to results, calculate first
if (currentTab.id === 'tab1Content') {
analyzeExposure(); // Attempt calculation
// If calculation fails, do not proceed to tab 2
if (errorMessageDiv.style.display === 'block') {
return;
}
}
activateTab(tabs[currentIndex + 1].id);
}
});
// Initial setup
resetCalculator(); // Call reset to set up default inputs and initial state
updateNavigationButtons();
});