Total Annual HOA Fee
$${(results.totalMonthly * 12).toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2})}
`;
// --- BREAKDOWN DISPLAY ---
let breakdownHTML = '
Monthly Fee Breakdown
';
results.breakdown.forEach(item => {
breakdownHTML += `
-
${item.name}
$${item.cost.toFixed(2)}
`;
});
breakdownHTML += '
';
breakdownDiv.innerHTML = breakdownHTML;
// --- CHART GENERATION ---
renderChart(results.breakdown);
resultsArea.style.display = 'block';
}
function calculateHoaFee() {
const getValue = id => parseFloat(document.getElementById(id)?.value) || 0;
const getSelectValue = id => document.getElementById(id)?.value || 'monthly';
const baseFee = getValue('baseFee');
const frequency = getSelectValue('feeFrequency');
let monthlyBaseFee = baseFee;
if (frequency === 'quarterly') {
monthlyBaseFee = baseFee / 3;
} else if (frequency === 'annually') {
monthlyBaseFee = baseFee / 12;
}
const breakdown = [{ name: 'Base Fee', cost: monthlyBaseFee }];
let totalMonthly = monthlyBaseFee;
const selectedAmenities = document.querySelectorAll('.amenity-checkbox:checked');
selectedAmenities.forEach(checkbox => {
const cost = parseFloat(checkbox.dataset.cost);
const label = document.querySelector(`label[for="${checkbox.id}"]`).textContent;
breakdown.push({ name: label, cost: cost });
totalMonthly += cost;
});
return { totalMonthly, breakdown };
}
function renderChart(breakdown) {
const ctx = document.getElementById('feeChart');
if (!ctx) return;
const labels = breakdown.map(item => item.name);
const data = breakdown.map(item => item.cost);
if (feeChartInstance) {
feeChartInstance.destroy();
}
feeChartInstance = new Chart(ctx, {
type: 'pie',
data: {
labels: labels,
datasets: [{
label: 'Cost Breakdown',
data: data,
backgroundColor: [
'#6366F1', '#818CF8', '#A5B4FC', '#C7D2FE',
'#34D399', '#6EE7B7', '#A7F3D0',
'#F59E0B', '#FBBF24', '#FCD34D'
],
hoverOffset: 4
}]
},
options: {
responsive: true,
plugins: {
legend: {
position: 'top',
},
title: {
display: true,
text: 'HOA Fee Cost Distribution'
}
}
}
});
}
function downloadPDF() {
const { jsPDF } = window.jspdf;
const pdfContent = document.getElementById('pdf-output');
const pdfTitle = document.getElementById('pdf-title');
if (!pdfContent || !pdfTitle) return;
pdfTitle.style.display = 'block';
html2canvas(pdfContent, { scale: 2 }).then(canvas => {
pdfTitle.style.display = 'none';
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 imgWidth = pdfWidth - 20;
const imgHeight = (imgProps.height * imgWidth) / imgProps.width;
pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight);
pdf.save('HOA-Fee-Analysis.pdf');
});
}
// --- RUN INITIALIZATION ---
initializeAmenities();
});