Please enter valid numbers.
';
if(roiChart) roiChart.destroy();
return;
}
const expansionCost = size * type.costPerSqFt;
const valueAdded = expansionCost * (type.roiPercent / 100);
const newPropertyValue = currentValue + valueAdded;
renderResults(expansionCost, newPropertyValue, type.roiPercent);
updateChart(currentValue, expansionCost, valueAdded);
}
function renderResults(cost, newValue, roi) {
resultsContent.innerHTML = `
Estimated Expansion Cost
$${cost.toLocaleString('en-US', {maximumFractionDigits: 0})}
Potential New Property Value
$${newValue.toLocaleString('en-US', {maximumFractionDigits: 0})}
`;
}
function updateChart(initial, cost, added) {
const ctx = document.getElementById('roiChart').getContext('2d');
if (roiChart) {
roiChart.destroy();
}
roiChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Value'],
datasets: [
{
label: 'Initial Value',
data: [initial],
backgroundColor: 'rgba(74, 144, 226, 0.7)',
},
{
label: 'Value Added',
data: [added],
backgroundColor: 'rgba(76, 175, 80, 0.7)',
}
]
},
options: {
indexAxis: 'y',
scales: { x: { stacked: true }, y: { stacked: true } },
plugins: { title: { display: true, text: 'Property Value Breakdown' } }
}
});
}
function renderConfig() {
configTbody.innerHTML = costData.expansionTypes.map(e => `
| ${e.name} |
|
|
`).join('');
}
window.updateConfig = function(element) {
const { id, prop } = element.dataset;
const value = parseFloat(element.value);
if (isNaN(value)) return;
const type = costData.expansionTypes.find(e => e.id === id);
if (type) {
type[prop] = value;
calculateFeasibility();
}
}
// --- 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'));
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
if (!prevBtn || !nextBtn) return;
prevBtn.style.visibility = activeTabIndex === 0 ? 'hidden' : 'visible';
nextBtn.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 generate a calculation 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('Expansion-Feasibility.pdf');
}).catch(err => {
console.error("Error generating PDF:", err);
}).finally(() => {
downloadPdfBtn.innerHTML = originalButtonText;
downloadPdfBtn.disabled = false;
});
});
}
// --- INITIALIZATION ---
function initializeTool() {
populateControls();
calculateFeasibility();
renderConfig();
updateNavButtons();
}
initializeTool();
});