`).join('')}
`;
}
function calculateScoreAndRenderReport() {
let totalScore = 0;
let maxScore = 0;
Object.keys(DB).forEach(categoryKey => {
DB[categoryKey].items.forEach(item => {
const answer = userAnswers[item.id];
if (answer === 'yes') {
totalScore += item.weight;
maxScore += item.weight;
} else if (answer === 'no') {
maxScore += item.weight;
}
// N/A doesn't count towards max score
});
});
const percentage = maxScore > 0 ? Math.round((totalScore / maxScore) * 100) : 100;
// Update Chart
const chartData = [percentage, 100 - percentage];
const scoreColor = percentage >= 80 ? '#22c55e' : (percentage >= 50 ? '#f59e0b' : '#ef4444');
if (scoreChart) {
scoreChart.data.datasets[0].data = chartData;
scoreChart.data.datasets[0].backgroundColor = [scoreColor, '#e5e7eb'];
scoreChart.update();
} else {
const ctx = document.getElementById('scoreChart').getContext('2d');
scoreChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['Compliant', 'Needs Improvement'],
datasets: [{
data: chartData,
backgroundColor: [scoreColor, '#e5e7eb'],
borderColor: '#ffffff',
borderWidth: 4,
hoverOffset: 4,
circumference: 180,
rotation: -90
}]
},
options: {
responsive: true,
plugins: {
legend: { display: false },
tooltip: { enabled: false }
},
cutout: '70%'
}
});
}
// Update text
document.getElementById('score-text').innerHTML = `Your score is ${percentage}%`;
const recommendations = document.getElementById('recommendations');
if (percentage >= 80) {
recommendations.textContent = "Excellent! Your business appears to have strong asset protection measures in place. Continue to review these periodically.";
} else if (percentage >= 50) {
recommendations.textContent = "Good start. There are several areas where your asset protection could be improved. Review the 'No' answers in your report for specific action items.";
} else {
recommendations.textContent = "Urgent attention needed. Your business may be significantly exposed to risks. It is highly recommended to consult with legal counsel to address the identified gaps.";
}
}
// --- TAB NAVIGATION ---
function updateTabDisplay() {
tabButtons.forEach(button => button.classList.toggle('active', parseInt(button.dataset.tab) === currentTab));
tabContents.forEach(content => content.style.display = 'none');
document.getElementById(`tab-content-${currentTab}`).style.display = 'block';
prevBtn.disabled = currentTab === 1;
nextBtn.textContent = currentTab === totalTabs - 1 ? 'See Report' : 'Next';
nextBtn.style.display = currentTab === totalTabs ? 'none' : 'inline-flex';
const progress = Math.max(0, (currentTab - 1) / (totalTabs - 1)) * 100;
progressBar.style.width = `${progress}%`;
}
function validateCurrentTab() {
if (currentTab === 1) {
const input = document.getElementById('company-name');
if (!input.value.trim()) {
input.reportValidity();
return false;
}
}
return true;
}
function changeTab(newTab) {
if (newTab > currentTab && !validateCurrentTab()) return;
if (newTab >= 1 && newTab <= totalTabs) {
currentTab = newTab;
if (currentTab === totalTabs) {
calculateScoreAndRenderReport();
}
updateTabDisplay();
}
}
// --- PDF GENERATION ---
function generatePdfHtml() {
const companyName = document.getElementById('company-name').value || "Your Company";
let score = 0, maxScore = 0;
Object.keys(DB).forEach(k => DB[k].items.forEach(i => {
if(userAnswers[i.id] === 'yes') score += i.weight;
if(userAnswers[i.id] !== 'na') maxScore += i.weight;
}));
const percentage = maxScore > 0 ? Math.round((score / maxScore) * 100) : 100;
let html = `Asset Protection Report for ${companyName}
Overall Compliance Score
${percentage}%
${category.title}
| Item | Response |
|---|---|
| ${item.text} | ${(userAnswers[item.id] || 'Not Answered').toUpperCase()} |
This report is an automated assessment for informational purposes only and does not constitute legal advice. Consult with qualified legal counsel to address specific asset protection needs.
`; return html; } async function handlePdfDownload() { document.getElementById('pdf-content').innerHTML = generatePdfHtml(); const { jsPDF } = window.jspdf; const button = document.getElementById('downloadPdfBtn'); button.textContent = 'Generating...'; button.disabled = true; const pdfContainer = document.getElementById('pdf-container'); try { const canvas = await html2canvas(pdfContainer, { scale: 2 }); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const imgProps = pdf.getImageProperties(imgData); const pageHeight = pdf.internal.pageSize.getHeight(); let heightLeft = (imgProps.height * pdfWidth) / imgProps.width; let position = 0; pdf.addImage(imgData, 'PNG', 10, 10, pdfWidth - 20, heightLeft - 20); heightLeft -= pageHeight; while (heightLeft > 0) { position = heightLeft - ((imgProps.height * pdfWidth) / imgProps.width); pdf.addPage(); pdf.addImage(imgData, 'PNG', 10, position - 10, pdfWidth - 20, heightLeft); heightLeft -= pageHeight; } pdf.save(`${companyName.value.replace(/ /g, '_')}_Asset_Protection_Report.pdf`); } catch (error) { console.error("PDF generation failed:", error); } finally { button.textContent = 'Download Full Report as PDF'; button.disabled = false; } } // --- INITIALIZATION & EVENT LISTENERS --- renderChecklist('structure', 'tab-content-2'); renderChecklist('insurance', 'tab-content-3'); renderChecklist('operations', 'tab-content-4'); renderChecklist('ip', 'tab-content-5'); updateTabDisplay(); nextBtn.addEventListener('click', () => changeTab(currentTab + 1)); prevBtn.addEventListener('click', () => changeTab(currentTab - 1)); tabButtons.forEach(button => button.addEventListener('click', (e) => changeTab(parseInt(e.target.dataset.tab)))); document.getElementById('downloadPdfBtn').addEventListener('click', handlePdfDownload); document.getElementById('asset-protection-container').addEventListener('change', e => { if (e.target.type === 'radio') { userAnswers[e.target.name] = e.target.value; } }); });