Net Estimated Financial Outcome with Hybrid
${netSavingsWithHybrid >= 0 ?
`
Potential Savings: $${netSavingsWithHybrid.toFixed(2)}
` :
`
Potential Additional Cost: $${Math.abs(netSavingsWithHybrid).toFixed(2)}
`}
This is the difference in total outlay over the ${ownershipPeriod}-year ownership period based on your inputs.
`;
resultsOutput.innerHTML = outputHTML;
}
// --- PDF Download Functionality ---
function hcfDownloadPDF() {
if (!hcfLastEstimationData) {
alert("Please estimate savings first before downloading PDF.");
hcfNavigateTab('hcfSavingsTab'); hcfEstimateSavings();
if (!hcfLastEstimationData) return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
const { inputs, convCar, hybridCar, savings } = hcfLastEstimationData;
const primaryColor = [0, 121, 107];
let y = 15;
doc.setFontSize(16); doc.setTextColor(primaryColor[0], primaryColor[1], primaryColor[2]);
doc.text("Hybrid Car Financing Savings Estimation", doc.internal.pageSize.getWidth() / 2, y, { align: 'center' });
y += 8;
doc.setFontSize(8); doc.setTextColor(100);
doc.text(`Estimations over ${inputs.ownershipPeriod}-year ownership period. For illustrative purposes.`, doc.internal.pageSize.getWidth() / 2, y, { align: 'center' });
y += 10;
doc.setFontSize(10); doc.setTextColor(51, 51, 51);
doc.text("Common Inputs:", 14, y); y += 6;
doc.text(` Ownership Period: ${inputs.ownershipPeriod} years`, 14, y);
doc.text(`Avg Miles/Year: ${inputs.milesPerYear.toLocaleString()}`, 100, y); y += 6;
doc.text(` Fuel Price: $${inputs.fuelPrice.toFixed(2)}/gallon`, 14, y); y += 10;
const addCarDetailsToPdf = (carType, carData, initialPrice, downPayment, loanRate, loanTerm, rebates = 0) => {
doc.setFontSize(12); doc.setTextColor(primaryColor[0], primaryColor[1], primaryColor[2]);
doc.text(`${carType} Details:`, 14, y); y += 7;
doc.setFontSize(10); doc.setTextColor(51, 51, 51);
doc.text(` Purchase Price: $${initialPrice.toFixed(2)}`, 18, y);
if (rebates > 0) doc.text(`Rebates: $${rebates.toFixed(2)}`, 100, y);
y += 6;
if (rebates > 0) doc.text(` Net Price (after rebates): $${carData.netPriceAfterRebate ? carData.netPriceAfterRebate.toFixed(2) : initialPrice.toFixed(2)}`, 18, y);
else doc.text(" ", 18, y); // Placeholder for alignment if no rebates
doc.text(`Down Payment: $${downPayment.toFixed(2)}`, 100, y); y += 6;
doc.text(` Loan Amount: $${carData.loanAmount.toFixed(2)}`, 18, y);
doc.text(`Loan: ${loanTerm > 0 ? `${loanTerm} yrs @ ${loanRate}%` : 'N/A (Cash)'}`, 100, y); y += 6;
doc.text(` Est. Monthly Loan Pymt: $${carData.monthlyPayment.toFixed(2)}`, 18, y); y += 6;
doc.text(` Est. Total Interest (full term): $${carData.totalInterestFullTerm.toFixed(2)}`, 18, y); y += 6;
doc.text(` Est. Annual Fuel Cost: $${carData.annualFuelCost.toFixed(2)}`, 18, y); y += 6;
doc.text(` Est. Total Fuel Cost (${inputs.ownershipPeriod} yrs): $${carData.totalFuelCostOwnership.toFixed(2)}`, 18, y); y += 6;
doc.setFont(undefined, 'bold');
doc.text(` Est. Total Outlay (${inputs.ownershipPeriod} yrs): $${carData.totalOutlayOwnership.toFixed(2)}`, 18, y); y += 10;
doc.setFont(undefined, 'normal');
if (y > 250 && carType === "Conventional") { doc.addPage(); y = 20; } // Add page if conventional car details take too much space
};
addCarDetailsToPdf("Conventional Car", convCar, inputs.convPrice, inputs.convDownPayment, inputs.convLoanRate, inputs.convLoanTerm);
if (y > 180 ) {doc.addPage(); y = 20;} // Ensure hybrid details have enough space or start on new page
addCarDetailsToPdf("Hybrid Car", hybridCar, inputs.hybridPrice, inputs.hybridDownPayment, inputs.hybridLoanRate, inputs.hybridLoanTerm, inputs.hybridRebates);
if (y > 240) { doc.addPage(); y = 20; }
doc.setFontSize(14); doc.setTextColor(primaryColor[0], primaryColor[1], primaryColor[2]);
doc.text("Net Estimated Financial Outcome with Hybrid:", 14, y); y+=8;
doc.setFontSize(16); doc.setFont(undefined, 'bold');
if (savings >= 0) {
doc.setTextColor(39, 174, 96); // Green
doc.text(`Potential Savings: $${savings.toFixed(2)}`, 14, y);
} else {
doc.setTextColor(192, 57, 43); // Red
doc.text(`Potential Additional Cost: $${Math.abs(savings).toFixed(2)}`, 14, y);
}
y+=8;
doc.setFontSize(9); doc.setTextColor(100); doc.setFont(undefined, 'normal');
doc.text(`This is the difference in total outlay over the ${inputs.ownershipPeriod}-year ownership period.`, 14, y); y+=10;
if (y > 260) { doc.addPage(); y = 20; }
doc.setFontSize(8); doc.setTextColor(120);
const footerText = "Estimator provides approximate costs. Actual figures vary due to driving habits, conditions, exact prices, maintenance, insurance, resale values (not included), and financing terms.";
const splitFooter = doc.splitTextToSize(footerText, doc.internal.pageSize.getWidth() - 28);
doc.text(splitFooter, 14, y);
doc.save("Hybrid_Car_Savings_Estimation.pdf");
}
// Initial setup
hcfUpdateNavButtons();
// Make functions globally accessible
window.hcfSwitchTab = hcfSwitchTab;
window.hcfNavigateTab = hcfNavigateTab;
window.hcfEstimateSavings = hcfEstimateSavings;
window.hcfDownloadPDF = hcfDownloadPDF;