Please enter valid numbers.
';
return;
}
const annualBill = monthlyBill * 12;
const annualSavings = annualBill * (upgrade.annualSavingsPercent / 100);
const paybackPeriodYears = cost / annualSavings;
const roi10Year = ((annualSavings * 10 - cost) / cost) * 100;
renderResults(annualSavings, paybackPeriodYears, roi10Year);
updateChart(cost, annualSavings);
}
function renderResults(annualSavings, payback, roi) {
resultsContent.innerHTML = `
Estimated Annual Savings
$${annualSavings.toFixed(2)}
Payback Period
${payback.toFixed(1)} years
10-Year ROI
${roi.toFixed(0)}%
`;
}
function updateChart(cost, annualSavings) {
const ctx = document.getElementById('roiChart').getContext('2d');
let cumulativeSavings = 0;
let years = Array.from({length: 11}, (_, i) => `Year ${i}`);
let data = years.map((_, i) => {
if (i > 0) cumulativeSavings += annualSavings;
return cumulativeSavings;
});
if (roiChart) {
roiChart.destroy();
}
roiChart = new Chart(ctx, {
type: 'line',
data: {
labels: years,
datasets: [{
label: 'Cumulative Savings ($)',
data: data,
borderColor: 'var(--primary-color)',
backgroundColor: 'rgba(0, 121, 107, 0.1)',
fill: true,
tension: 0.1
}, {
label: 'Initial Cost ($)',
data: Array(11).fill(cost),
borderColor: 'var(--secondary-color)',
borderDash: [5, 5],
fill: false
}]
},
options: {
scales: { y: { beginAtZero: true } },
plugins: { tooltip: { mode: 'index', intersect: false } }
}
});
}
function renderConfig() {
configTbody.innerHTML = upgradeData.map(u => `
| ${u.name} |
|
|
`).join('');
}
window.updateConfig = function(element) {
const { id, prop } = element.dataset;
const value = parseFloat(element.value);
if (isNaN(value)) return;
const upgrade = upgradeData.find(u => u.id === id);
if (upgrade) {
upgrade[prop] = value;
if (upgradeSelect.value === id) {
updateUpgradeCost();
}
calculateROI();
}
}
// --- TAB & NAVIGATION ---
window.openTab = function(evt, tabName) {
const tabContents = document.getElementsByClassName("tab-content");
Array.from(tabContents).forEach(tab => tab.style.display = "none");
const tabButtons = document.getElementsByClassName("tab-btn");
Array.from(tabButtons).forEach(btn => btn.classList.remove("active"));
document.getElementById(tabName).style.display = "block";
if (evt) {
evt.currentTarget.classList.add("active");
} else {
const btnToActivate = Array.from(tabButtons).find(btn => btn.getAttribute('onclick').includes(`'${tabName}'`));
if (btnToActivate) btnToActivate.classList.add("active");
}
updateNavButtons();
}
window.navigateTabs = function(direction) {
const tabs = Array.from(document.querySelectorAll('.tab-btn'));
const activeTabIndex = tabs.findIndex(tab => tab.classList.contains('active'));
let newIndex = (direction === 'next')
? (activeTabIndex + 1) % tabs.length
: (activeTabIndex - 1 + tabs.length) % tabs.length;
tabs[newIndex].click();
}
function updateNavButtons() {
const tabs = Array.from(document.querySelectorAll('.tab-btn'));
const activeTabIndex = tabs.findIndex(tab => tab.classList.contains('active'));
document.getElementById('prev-btn').style.visibility = activeTabIndex === 0 ? 'hidden' : 'visible';
document.getElementById('next-btn').style.visibility = activeTabIndex === tabs.length - 1 ? 'hidden' : 'visible';
}
// --- PDF DOWNLOAD ---
if(downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', function() {
const { jsPDF } = window.jspdf;
const contentToDownload = document.getElementById('results-to-download');
if (!contentToDownload || !document.querySelector('.value')) {
console.warn("Please calculate an ROI before downloading.");
return;
}
const originalButtonText = downloadPdfBtn.innerHTML;
downloadPdfBtn.innerHTML = 'Generating...';
downloadPdfBtn.disabled = true;
html2canvas(contentToDownload, { scale: 2, useCORS: true }).then(canvas => {
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 imgHeight = (imgProps.height * pdfWidth) / imgProps.width;
pdf.addImage(imgData, 'PNG', 10, 10, pdfWidth - 20, imgHeight > 0 ? imgHeight - 20 : 0);
pdf.save('Energy-Upgrade-ROI.pdf');
}).catch(err => {
console.error("Error generating PDF:", err);
}).finally(() => {
downloadPdfBtn.innerHTML = originalButtonText;
downloadPdfBtn.disabled = false;
});
});
}
// --- INITIALIZATION ---
function initializeTool() {
populateControls();
updateUpgradeCost();
calculateROI();
renderConfig();
updateNavButtons();
}
initializeTool();
});