`;
});
form.innerHTML = html;
};
// --- 3. HELPER FUNCTIONS ---
const showError = (message) => {
errorMessageDiv.textContent = message;
errorMessageDiv.classList.remove('hidden');
resultsSection.classList.add('hidden');
window.scrollTo(0, errorMessageDiv.offsetTop - 20);
};
const clearError = () => errorMessageDiv.classList.add('hidden');
// --- 4. CORE CALCULATION LOGIC ---
const calculateScore = () => {
clearError();
let totalScore = 0;
let allAnswered = true;
for (let i = 0; i < QUESTIONS.length; i++) {
const selectedOption = form.querySelector(`input[name="q${i}"]:checked`);
if (selectedOption) {
totalScore += parseInt(selectedOption.value);
} else {
allAnswered = false;
break;
}
}
if (!allAnswered) {
showError('Please answer all questions before calculating your score.');
return;
}
renderResults(totalScore);
};
// --- 5. RENDER RESULTS ---
const renderResults = (score) => {
let severity, colorClass, interpretation;
if (score >= 20) {
severity = 'Severe';
colorClass = 'severity-severe';
interpretation = 'These symptoms are severe and likely causing significant distress or impairment. It is highly recommended to seek professional help immediately.';
} else if (score >= 15) {
severity = 'Moderately Severe';
colorClass = 'severity-moderately-severe';
interpretation = 'Your symptoms suggest moderately severe depression. A thorough evaluation with a healthcare professional is strongly advised.';
} else if (score >= 10) {
severity = 'Moderate';
colorClass = 'severity-moderate';
interpretation = 'Your symptoms may indicate moderate depression. Consulting with a healthcare provider to discuss treatment options is a good next step.';
} else if (score >= 5) {
severity = 'Mild';
colorClass = 'severity-mild';
interpretation = 'Your score suggests mild depression. Monitoring your symptoms and considering lifestyle changes or talking to a professional could be helpful.';
} else {
severity = 'Minimal';
colorClass = 'severity-minimal';
interpretation = 'Your score does not suggest significant depressive symptoms. However, if you are concerned, please consult a professional.';
}
// Special check for question 9 (suicidal ideation)
const lastQuestionValue = parseInt(form.querySelector('input[name="q8"]:checked').value);
let suicideWarning = '';
if (lastQuestionValue > 0) {
suicideWarning = `
`;
}
resultsOutput.innerHTML = `
${suicideWarning}
`;
resultsSection.classList.remove('hidden');
window.scrollTo(0, resultsSection.offsetTop - 20);
};
// --- 6. PDF GENERATION ---
const handlePdfDownload = () => {
const { jsPDF } = window.jspdf;
const contentToPrint = document.getElementById('printable-content');
const buttonContainer = document.getElementById('pdf-button-container');
if (!contentToPrint) return;
buttonContainer.classList.add('hide-for-pdf');
html2canvas(contentToPrint, { scale: 2, useCORS: true }).then(canvas => {
buttonContainer.classList.remove('hide-for-pdf');
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasWidth / pdfWidth;
const calculatedHeight = canvasHeight / ratio;
pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, calculatedHeight);
pdf.save('Depression_Severity_Report.pdf');
}).catch(err => {
buttonContainer.classList.remove('hide-for-pdf');
showError('An error occurred while generating the PDF.');
});
};
// --- 7. EVENT LISTENERS ---
calculateBtn.addEventListener('click', calculateScore);
downloadPdfBtn.addEventListener('click', handlePdfDownload);
// --- INITIALIZE ---
initQuestions();
});
Important Note
You indicated having thoughts of self-harm. Please seek help immediately. You can contact a crisis hotline or emergency services.
${severity} Depression
${score}
Total Score (out of 27)
${interpretation}
