function ddate(form){

// Check for missing fields
	if (form.MenMonth.value == "Month"){
	alert("Please choose what month your last cycle started in.\n\n If it is in the future we need the month it will start in.");
	form.MenMonth.focus();
	return (false);
	}
	if (form.MenDay.value == "Day"){
	alert("Please choose what day your last cycle started in.\n\n If it is in the future we need the day it will start in.");
	form.MenDay.focus();
	return (false);
	}
	if (form.MenYear.value == "Year"){
	alert("Please choose what year your last cycle started in.\n\n If it is in the future we need the year it will start in.");
	form.MenYear.focus();
	return (false);
	}
	if (form.cycle.value == ""){
	alert("Please choose a cycle length.\n\n If you aren't sure, we suggest you use 28 days");
	form.cycle.focus();
	return(false);
	}
	if (form.luteal.value == ""){
	alert("Please choose a luteal cycle length.\n\n If you aren't sure, we suggest you use 14 days");
	form.luteal.focus();
	return(false);
	}

// Join the components of the menstrual date

Mmonth = form.MenMonth.value;
Mday = form.MenDay.value;
Myear = form.MenYear.value;
Mcycle = form.cycle.value;
Mluteal = form.luteal.value;

// create new date objects
menstrual = new Date(Myear,Mmonth,Mday); 
ovulation = new Date();
duedate = new Date();
today = new Date();

// sets ovulation date to menstrual date + cycle days - luteal days
// the '*86400000' is necessary because date objects track time
// in milliseconds;  86400000 milliseconds equals one day
ovulation.setTime(menstrual.getTime() + (Mcycle*86400000) - (Mluteal*86400000));
form.conception.value = dispDate(ovulation);

// sets due date to ovulation date plus 266 days
duedate.setTime(ovulation.getTime() + 266*86400000);
form.duedate.value = dispDate(duedate);

// sets fetal age to 14 + 266 (pregnancy time) - time left
var fetalage = 14 + 266 - ((duedate - today) / 86400000);
weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
days = Math.floor(fetalage % 7); // sets days to the whole number remainder

// fetal age message, automatically includes 's' on week and day if necessary
fetalage = weeks + " week" + (weeks > 1 ? "s" : "") + ", " + days + " days";
form.fetalage.value = fetalage;

}

function dispDate(dateObj) {
month = dateObj.getMonth()+1;
month = (month < 10) ? "0" + month : month;

day   = dateObj.getDate();
day = (day < 10) ? "0" + day : day;

year  = dateObj.getYear();
if (year < 2000) year += 1900;

return (month + "/" + day + "/" + year);
}
