Please enter valid numbers in all fields.
';
return;
}
let costs = {};
// Calculate costs based on config
costs['Real Estate Commission'] = salePrice * (configData.commissionRate / 100);
costs['Transfer Tax'] = salePrice * (configData.transferTaxRate / 100);
costs['Title Insurance'] = configData.titleInsurance;
costs['Escrow Fee'] = configData.escrowFee;
costs['Attorney Fee'] = configData.attorneyFee;
costs['Repairs & Staging'] = repairsStaging;
costs['Seller Concessions'] = sellerConcessions;
const totalCosts = Object.values(costs).reduce((sum, val) => sum + val, 0);
const netProceeds = salePrice - mortgagePayoff - totalCosts;
renderResults(netProceeds);
updateChart(salePrice, mortgagePayoff, costs);
}
function renderResults(netProceeds) {
resultsContent.innerHTML = `
Your Estimated Net Proceeds
$${netProceeds.toLocaleString('en-US', {maximumFractionDigits: 0})}
`;
}
function updateChart(salePrice, mortgagePayoff, costs) {
const ctx = document.getElementById('breakdownChart').getContext('2d');
const totalCosts = Object.values(costs).reduce((sum, val) => sum + val, 0);
const netProceeds = salePrice - mortgagePayoff - totalCosts;
const chartData = {
labels: ['Net Proceeds', 'Mortgage Payoff', 'Closing Costs & Fees'],
datasets: [{
data: [netProceeds, mortgagePayoff, totalCosts],
backgroundColor: ['#2E7D32', '#C62828', '#FF8F00'],
}]
};
if (breakdownChart) {
breakdownChart.destroy();
}
breakdownChart = new Chart(ctx, {
type: 'doughnut',
data: chartData,
options: {
responsive: true,
plugins: {
legend: { position: 'top' },
title: { display: true, text: `Breakdown of $${salePrice.toLocaleString()}` },
tooltip: {
callbacks: {
label: function(context) {
let label = context.label || '';
if (label) { label += ': '; }
if (context.parsed !== null) {
label += new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(context.parsed);
}
return label;
}
}
}
}
}
});
}
function renderConfig() {
configTbody.innerHTML = `
| Real Estate Commission (%) | |
| Transfer Tax (% of Sale Price) | |
| Title Insurance ($) | |
| Escrow Fee ($) | |
| Attorney Fee ($) | |
`;
}
window.updateConfig = function(element) {
const { prop } = element.dataset;
const value = parseFloat(element.value);
if (isNaN(value)) return;
configData[prop] = value;
calculateNet();
}
// --- 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 the net proceeds 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('Seller-Net-Sheet.pdf');
}).catch(err => {
console.error("Error generating PDF:", err);
}).finally(() => {
downloadPdfBtn.innerHTML = originalButtonText;
downloadPdfBtn.disabled = false;
});
});
}
// --- INITIALIZATION ---
function initializeTool() {
calculateNet();
renderConfig();
updateNavButtons();
}
initializeTool();
});