`;
});
html += '
';
summaryContainer.innerHTML = html;
};
const downloadPdf = () => {
downloadPdfBtn.classList.add('hidden');
pdfLoader.classList.remove('hidden');
setTimeout(() => {
try {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
doc.setFontSize(18);
doc.text("Online Defamation Risk Assessment", 14, 22);
doc.setFontSize(11);
doc.setTextColor(100);
doc.text(`Generated on: ${new Date().toLocaleDateString()}`, 14, 30);
doc.setFontSize(14);
doc.setFont(undefined, 'bold');
doc.text(`Final Assessment: ${riskLevelEl.textContent}`, 14, 45);
doc.setFont(undefined, 'normal');
doc.setFontSize(10);
const explanationLines = doc.splitTextToSize(riskExplanationEl.textContent, 180);
doc.text(explanationLines, 14, 52);
const tableBody = [
['Statement Assessed', userAnswers.statementText.value],
['Is it verifiably true?', userAnswers.is_true.text],
['Publication Platform?', userAnswers.platform.text],
['Subject of Statement?', userAnswers.subject.text],
['Potential for Harm?', userAnswers.harm.text]
];
doc.autoTable({
startY: doc.previousAutoTable ? doc.previousAutoTable.finalY + 20 : 75,
head: [['Question', 'Your Answer']],
body: tableBody,
theme: 'grid',
headStyles: { fillColor: [124, 58, 237] }, // --primary-color
columnStyles: { 0: { fontStyle: 'bold' } }
});
const noticeText = doc.splitTextToSize("Important Notice: This tool provides a general risk assessment based on common principles of U.S. defamation law and is for informational purposes only. It is not legal advice. Consult with a qualified attorney for advice on your specific circumstances.", 180);
doc.setFontSize(8);
doc.setTextColor(150);
doc.text(noticeText, 14, doc.autoTable.previous.finalY + 10);
doc.save('Defamation-Risk-Assessment.pdf');
} catch(e) {
console.error("Error generating PDF:", e);
alert("An error occurred. Please try again.");
} finally {
pdfLoader.classList.add('hidden');
downloadPdfBtn.classList.remove('hidden');
}
}, 500);
};
// --- Event Listeners ---
tabButtons.forEach(btn => {
btn.addEventListener('click', (e) => {
const targetIndex = parseInt(e.currentTarget.dataset.tabIndex);
if (!e.currentTarget.classList.contains('disabled')) {
if (targetIndex > currentTab && !validateTab(currentTab)) return;
currentTab = targetIndex;
if (currentTab === TOTAL_TABS - 1) calculateAndDisplayRisk();
updateUI();
}
});
});
prevBtn.addEventListener('click', () => { if (currentTab > 0) { currentTab--; updateUI(); } });
nextBtn.addEventListener('click', () => {
if (validateTab(currentTab)) {
if (currentTab < TOTAL_TABS - 1) {
currentTab++;
if (currentTab === TOTAL_TABS - 1) calculateAndDisplayRisk();
updateUI();
}
}
});
document.querySelectorAll('.radio-option').forEach(label => {
label.addEventListener('click', () => {
const name = label.querySelector('input').name;
document.querySelectorAll(`input[name="${name}"]`).forEach(radio => {
radio.parentElement.classList.remove('selected');
});
label.classList.add('selected');
});
});
downloadPdfBtn.addEventListener('click', downloadPdf);
// --- Initialisation ---
updateUI();
});
