// Function to show the appropriate form based on the selected option function showForm() { const selectedOption = document.getElementById("calculationOption").value; const forms = ["lastPeriodForm", "ultrasoundForm", "conceptionForm", "ivfForm"]; forms.forEach(formId => { const form = document.getElementById(formId); form.style.display = "none"; }); if (selectedOption === "lastPeriod") { document.getElementById("lastPeriodForm").style.display = "block"; } else if (selectedOption === "ultrasound") { document.getElementById("ultrasoundForm").style.display = "block"; } else if (selectedOption === "conception") { document.getElementById("conceptionForm").style.display = "block"; } else if (selectedOption === "ivf") { document.getElementById("ivfForm").style.display = "block"; } } // Function to calculate the due date and milestones function calculateDueDate() { const selectedOption = document.getElementById("calculationOption").value; let dueDate; let milestones = []; if (selectedOption === "lastPeriod") { const lastPeriodDate = new Date(document.getElementById("lastPeriodDate").value); const cycleLength = parseInt(document.getElementById("cycleLength").value); dueDate = new Date(lastPeriodDate.getTime() + (cycleLength * 24 * 60 * 60 * 1000 * 7)); } else if (selectedOption === "ultrasound") { const ultrasoundDate = new Date(document.getElementById("ultrasoundDate").value); const pregnancyLength = parseInt(document.getElementById("pregnancyLength").value); dueDate = new Date(ultrasoundDate.getTime() + (pregnancyLength * 24 * 60 * 60 * 1000 * 7)); } else if (selectedOption === "conception") { const conceptionDate = new Date(document.getElementById("conceptionDate").value); dueDate = new Date(conceptionDate.getTime() + (280 * 24 * 60 * 60 * 1000)); } else if (selectedOption === "ivf") { const transferDate = new Date(document.getElementById("transferDate").value); const embryoAge = parseInt(document.getElementById("embryoAge").value); dueDate = new Date(transferDate.getTime() + (280 * 24 * 60 * 60 * 1000) - (embryoAge * 24 * 60 * 60 * 1000)); } const tableBody = document.getElementById("milestonesTableBody"); tableBody.innerHTML = ""; const trimesterMilestones = [ { week: 12, milestone: "End of first trimester" }, { week: 20, milestone: "End of second trimester" }, { week: 40, milestone: "Full-term" } ]; for (let i = 0; i <= 40; i++) { const milestoneRow = document.createElement("tr"); const weekCell = document.createElement("td"); weekCell.textContent = i; milestoneRow.appendChild(weekCell); const dateCell = document.createElement("td"); const date = new Date(dueDate.getTime() - (i * 7 * 24 * 60 * 60 * 1000)); dateCell.textContent = date.toLocaleDateString(); milestoneRow.appendChild(dateCell); const trimesterCell = document.createElement("td"); const milestoneCell = document.createElement("td"); const trimester = trimesterMilestones.find(milestone => i <= milestone.week); if (trimester) { trimesterCell.textContent = trimester.week; milestoneCell.textContent = trimester.milestone; } milestoneRow.appendChild(trimesterCell); milestoneRow.appendChild(milestoneCell); tableBody.appendChild(milestoneRow); } document.getElementById("dueDate").textContent = dueDate.toLocaleDateString(); document.getElementById("result").style.display = "block"; } // Event listeners document.getElementById("calculationOption").addEventListener("change", showForm); document.getElementById("calculateBtn").addEventListener("click", calculateDueDate);
top of page
bottom of page