`;
renderChart(results);
resultsArea.style.display = 'block';
}
function calculateComparison() {
const getValue = id => parseFloat(document.getElementById(id)?.value) || 0;
const premium1 = getValue('premium1');
const deductible1 = getValue('deductible1');
const premium2 = getValue('premium2');
const deductible2 = getValue('deductible2');
const damageCost = getValue('damageCost');
if (premium1 <= 0 || deductible1 <= 0 || premium2 <= 0 || deductible2 <= 0) {
alert("Please enter valid premium and deductible values for both options.");
return null;
}
if (deductible2 <= deductible1) {
alert("Option 2's deductible must be higher than Option 1's to perform a valid comparison.");
return null;
}
const premiumSavings = premium1 - premium2;
if (premiumSavings <= 0) {
alert("Option 2's premium must be lower than Option 1's for there to be any savings.");
return null;
}
const outOfPocket1 = Math.min(damageCost, deductible1);
const outOfPocket2 = Math.min(damageCost, deductible2);
const totalCost1 = premium1 + outOfPocket1;
const totalCost2 = premium2 + outOfPocket2;
const deductibleDifference = deductible2 - deductible1;
const breakevenYears = deductibleDifference / premiumSavings;
return {
premiumSavings,
breakevenYears,
totalCost1,
totalCost2,
outOfPocket1,
outOfPocket2,
premium1,
premium2
};
}
function renderChart(results) {
const ctx = document.getElementById('comparisonChart');
if (!ctx) return;
if (comparisonChartInstance) {
comparisonChartInstance.destroy();
}
comparisonChartInstance = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Option 1 (Lower Deductible)', 'Option 2 (Higher Deductible)'],
datasets: [
{
label: 'Annual Premium',
data: [results.premium1, results.premium2],
backgroundColor: 'rgba(59, 130, 246, 0.7)',
},
{
label: 'Out-of-Pocket for Claim',
data: [results.outOfPocket1, results.outOfPocket2],
backgroundColor: 'rgba(239, 68, 68, 0.7)',
}
]
},
options: {
responsive: true,
plugins: {
title: {
display: true,
text: `Total Cost in a Claim Scenario (Damage: $${(document.getElementById('damageCost').value)})`
},
tooltip: {
callbacks: {
label: function(context) {
let label = context.dataset.label || '';
if (label) {
label += ': ';
}
if (context.parsed.y !== null) {
label += new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(context.parsed.y);
}
return label;
}
}
}
},
scales: {
x: {
stacked: true,
},
y: {
stacked: true,
beginAtZero: true,
title: {
display: true,
text: 'Total Cost ($)'
}
}
}
}
});
}
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('Home-Insurance-Deductible-Analysis.pdf');
});
}
});
