Error: Please fill in the Mission and Call to Action fields.
';
return;
}
var letterHtml = `
${letterDate}
${data.recipientName}
Dear ${data.recipientName.split(' ')[0]},
Your support is needed for the ${data.campaignTitle} campaign.
At ${data.orgName}, our core mission is simple:
${formatParagraphs(data.mission)}
This work is urgent, and we need your help today.
${data.impact ? `
${formatSimpleText(data.impact)}
` : ''}
We urgently need to raise ${data.goalAmount} to ${data.goalPurpose}.
Your generous gift ensures that this vital work continues. Every dollar you contribute goes directly toward [specific impact]. For example, a gift of $50 provides [tangible item/service].
Please join us in making this goal a reality. ${formatParagraphs(data.ctaAsk)}
With heartfelt gratitude,
${sigName}
${sigTitle}
${formatSimpleText(data.orgAddress)}
`;
letterContentDiv.innerHTML = letterHtml;
};
generateBtn.addEventListener("click", function() {
generateLetter();
showTab(1); // Switch to Dashboard
});
// --- Utility: Format Date ---
var formatDate = function(dateString) {
if (!dateString) return "[Date N/A]";
try {
return new Date(dateString).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
} catch (e) {
return dateString;
}
};
// --- PDF Download ---
pdfBtn.addEventListener("click", function() {
var jsPDF = window.jspdf.jsPDF;
var orgSlug = orgNameInput.value.replace(/[^a-zA-Z0-9\s]/g, '').replace(/\s/g, '_') || 'Fundraising';
var fileName = `${orgSlug}_Appeal_Letter.pdf`;
html2canvas(exportArea, {
scale: 2,
useCORS: true,
backgroundColor: '#ffffff'
}).then(function(canvas) {
var imgData = canvas.toDataURL('image/png');
var doc = new jsPDF({
orientation: 'p',
unit: 'pt',
format: 'letter'
});
var pdfWidth = doc.internal.pageSize.getWidth();
var pdfHeight = doc.internal.pageSize.getHeight();
var imgProps = doc.getImageProperties(imgData);
var imgWidth = imgProps.width;
var imgHeight = imgProps.height;
var margin = 40;
var usableWidth = pdfWidth - (2 * margin);
var ratio = usableWidth / imgWidth;
var scaledHeight = imgHeight * ratio;
var usablePageHeight = pdfHeight - (2 * margin);
var heightLeft = scaledHeight;
var position = 0;
while (heightLeft > 0) {
doc.addImage(imgData, 'PNG', margin, position + margin, usableWidth, scaledHeight);
heightLeft -= usablePageHeight;
position -= usablePageHeight;
if (heightLeft > 0) {
doc.addPage();
}
}
doc.save(fileName);
}).catch(function(err) {
console.error("FLG PDF Error:", err);
// alert("An error occurred while generating the PDF."); // Per spec
});
});
// --- Initial Load ---
var today = new Date().toISOString().split('T')[0];
if (dateInput) {
dateInput.value = today;
}
generateLetter(); // Generate initial view with sample data
showTab(0); // Start on Config tab
});