var dayName = new Array("Sunday", "Monday", "Tuesday",	"Wednesday", "Thursday", "Friday", "Saturday");

var monName = new Array("January", "February", "March", "April",	"May", "June", "July", "August", "September", "October",
	"November", "December"); 

	//  Create array of maximum days
	var maxDays = new Array(12);

	maxDays[0] = 31;	// January has 31 days
	maxDays[1] = 28;
	maxDays[2] = 31;
	maxDays[3] = 30;
	maxDays[4] = 31;
	maxDays[5] = 30;
	maxDays[6] = 31;
	maxDays[7] = 31;
	maxDays[8] = 30;
	maxDays[9] = 31;
	maxDays[10] = 30;
	maxDays[11] = 31;

// if ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4)) {var isNS4 = 1}
if (document.layers) {var isNS4 = 1};
var docObj = (isNS4) ? "document.layers" : "document.all";
var styleObj = (isNS4) ? "" : ".style";

function whatDay(form) {
	var month = form.month.selectedIndex;
	var date = form.date.selectedIndex + 1;
	var year = form.year.value;

	var whatDate = new Date(monName[month] + " " + date + ", " +
		year + " 12:00:00");

	form.day.selectedIndex = whatDate.getDay();
	form.whatTime.value = whatDate.getTime();
	
	return true
}


function dateDiff(form,calcType) {
/*	calcType tells which field to calculate
	0 = Calculate End Date
	1 = Calculate End Time
	2 = Calculate Start Date
	3 = Calculate Start Day #
*/

	var startDay = document.startDateForm.day.selectedIndex;
	var startMonth = document.startDateForm.month.selectedIndex;
	var startDate = document.startDateForm.date.selectedIndex + 1;
	var startYear = document.startDateForm.year.value;
	var startDayNum = document.startDateForm.startDayNum.value;
	
	var endDay = document.endDateForm.day.selectedIndex;
	var endMonth = document.endDateForm.month.selectedIndex;
	var endDate = document.endDateForm.date.selectedIndex + 1;
	var endYear = document.endDateForm.year.value;
	var endDayNum = document.endDateForm.endDayNum.value;
	var endSelect = document.endDateForm.endSelect.selectedIndex;
	
	if (endSelect == 0) {var endSelectText = "day"}
	if (endSelect == 1) {var endSelectText = "week"}
	if (endSelect == 2) {var endSelectText = "month"}
	if (endSelect == 3) {var endSelectText = "year"}

// Check Start Day Number.

	if ((calcType == 0) || (calcType == 1) || (calcType == 2)) {	
		if (isNaN(parseInt(startDayNum))) {
			alert("Please enter the start day number.");
			document.startDateForm.startDayNum.focus();
			document.startDateForm.startDayNum.select();
			return false;
		}
	}

// Check Start Year and Start Date

	if ((calcType == 0) || (calcType == 1) || (calcType == 3)) {
		if (isNaN(parseInt(startYear))) {
			alert("Please enter the start year.");
			document.startDateForm.year.focus();
			document.startDateForm.year.select();
			return false;
		}
		if (!checkDate(startMonth,startDate,startYear)) {
			alert(monName[startMonth] + " has " + maxDays[startMonth] + 
				" days.");
			document.startDateForm.date.focus();
			return false;
		}
		var startingDate = new Date(monName[startMonth] + " " + startDate + ", " +
			startYear + " 12:00:00");
	}

// Check End Day Number

	if ((calcType == 0) || (calcType == 2) || (calcType == 3)) {	
		if (isNaN(parseFloat(endDayNum))) {
			alert("Please enter the end " + endSelectText + " number.");
			document.endDateForm.endDayNum.focus();
			document.endDateForm.endDayNum.select();
			return false;
		}
//		if (endSelect == 1) {								// if using weeks
			if (endDayNum.indexOf("/") != -1) {			// is there a fraction?
				endDayNum = evalFrac(endDayNum,0);
				if (endDayNum == false) {return false}
			}
//		}
		if (endSelect == 0) {var endDays = endDayNum}
		if (endSelect == 1) {var endDays = endDayNum * 7}
		if (endSelect == 2) {var endDays = endDayNum * 30}
		if (endSelect == 3) {var endDays = endDayNum * 365.26}
		if (isNaN(endDayNum)) {
			alert("Please enter the end " + endSelectText + " number.");
			document.endDateForm.endDayNum.focus();
			document.endDateForm.endDayNum.select();
			return false;
		}
	}

// Check End Year and End Date

	if ((calcType == 1) || (calcType == 2) || (calcType == 3)) {
		if (isNaN(parseInt(endYear))) {
			alert("Please enter the end year.");
			document.endDateForm.year.focus();
			document.endDateForm.year.select();
			return false;
		}
		if (!checkDate(endMonth,endDate,endYear)) {
			alert(monName[endMonth] + " has " + maxDays[endMonth] + 
				" days.");
			document.endDateForm.date.focus();
			return false;
		}
		var endingDate = new Date(monName[endMonth] + " " + endDate + ", " +
			endYear + " 12:00:00");
	}

// Calculate the End Date
	if (calcType == 0) {
		if (document.endDateForm.endSelect.selectedIndex == 2) {	// If adding months
			var endMonthRound = Math.floor(endDayNum);
			var endMonthDays = roundNum(((endDayNum - endMonthRound) * 30),0);
			var endMonthYears = Math.floor(endMonthRound / 12);
			
			var endYear = startingDate.getFullYear() + endMonthYears;
			var addMonthsMon = roundNum((endMonthRound % 12),0);

			var endMonth = startingDate.getMonth() + addMonthsMon;
			if (endMonth > 11) {
				endYear += 1;
				endMonth -= 12;
			}

			var endDate = startingDate.getDate() + endMonthDays;
			if (endMonthDays > maxDays[endMonth]){
				if (endMonthDays == 0) {endDate = maxDays[endMonth]} else {
					endMonth += 1;
					endDate -= 30;
					if (endMonth > 11) {
						endYear += 1;
						endMonth -= 12;
					}
				}
			}
			
			var endingDate = new Date(monName[endMonth] + " " + endDate + ", " +
			endYear + " 12:00:00");
		} else {
			var addTime = endDays - startDayNum;
			addTime = addTime * 24 * 60 * 60 * 1000;
			var endingDate = new Date((startingDate.getTime() + addTime));
		}
		
		document.endDateForm.day.selectedIndex = endingDate.getDay();
		document.endDateForm.month.selectedIndex = endingDate.getMonth();
		document.endDateForm.date.selectedIndex = endingDate.getDate() - 1;
		document.endDateForm.year.value = endingDate.getFullYear();
	}


// Calculate the End Time
	if (calcType == 1) {
		var addTime = endingDate.getTime() - startingDate.getTime();
		
		if (addTime < 0) {
			alert("The ending date should be AFTER the starting date.");
			return false;
		}
		
		var addDays = addTime / (1000*60*60*24);
		var addWeeks = addDays / 7;
		var oldaddWeeks = addWeeks;
//		var addMonths = addDays / 30;
			var addMonthsYr = 12 * (endingDate.getFullYear() - startingDate.getFullYear());
			var addMonthsMon = endingDate.getMonth() - startingDate.getMonth();
			var addMonthsDay = (endingDate.getDate() - startingDate.getDate()) / 30;
			
			addMonths = addMonthsYr + addMonthsMon + roundNum(addMonthsDay,1);
//		var addYears = addDays / 365.26;
		var addYears = addMonths / 12;
		
		var addWeekDays = roundNum((addDays % 7),0);
		if (addWeekDays == 7) {
			addWeeks += 1;
			oldaddWeeks += 1;
			addWeekDays = 0
		}
		var addYearMonths = roundNum((addMonths % 12),1);
		
		if (document.endDateForm.endSelect.selectedIndex == 0) {
			document.endDateForm.endDayNum.value = roundNum(addDays,0);
		}
		if (document.endDateForm.endSelect.selectedIndex == 1) {
			addWeeks = Math.floor(addWeeks);
			var oldaddWeeks = addWeeks;
			if (addWeekDays != 0) {addWeeks = addWeeks + " " + addWeekDays + "/7"}
			document.endDateForm.endDayNum.value = addWeeks
		}
		if (document.endDateForm.endSelect.selectedIndex == 2) {
			document.endDateForm.endDayNum.value = roundNum(addMonths,1);
		}
		if (document.endDateForm.endSelect.selectedIndex == 3) {
			document.endDateForm.endDayNum.value = roundNum(addYears,1);
		}
		
		
		if (roundNum(addDays,0) == 1) {var dateDiffText = roundNum(addDays,0) + " day"}
			else {var dateDiffText = roundNum(addDays,0) + " days"};
		if (Math.floor(oldaddWeeks) != 0) {
			dateDiffText += "\n" + Math.floor(oldaddWeeks);
			if (Math.floor(oldaddWeeks) == 1) {dateDiffText += " week"}
				else {dateDiffText += " weeks"};
			if (addWeekDays == 1) {dateDiffText += ", 1 day"}
			if (addWeekDays > 1) {dateDiffText += ", " + addWeekDays + " days"}
		}
		if (Math.floor(addMonths) != 0) {
			dateDiffText += "\n" + roundNum(addMonths,1);
			if (roundNum(addMonths,1) == 1) {dateDiffText += " month"}
				else {dateDiffText += " months"}
		}
		if (Math.floor(addYears) != 0) {
			dateDiffText += "\n" + Math.floor(addYears);
			if (Math.floor(addYears) == 1) {dateDiffText += " year"}
				else {dateDiffText += " years"}
			if (addYearMonths == 1) {dateDiffText += ", 1 month"}
			if ((addYearMonths > 0) && (addYearMonths != 1)) {
				dateDiffText += ", " + addYearMonths + " months"
			}
		}

		document.endDateForm.dateDifferenceText.value = dateDiffText;
		}

// Calculate the Start Date
	if (calcType == 2) {
		var addTime = endDays - startDayNum;
		addTime = addTime * 24 * 60 * 60 * 1000;
		var startingDate = new Date((endingDate.getTime() - addTime));
		
		document.startDateForm.day.selectedIndex = startingDate.getDay();
		document.startDateForm.month.selectedIndex = startingDate.getMonth();
		document.startDateForm.date.selectedIndex = startingDate.getDate() - 1;
		document.startDateForm.year.value = startingDate.getFullYear();
	}
	
// Calculate the Start Time
	if (calcType == 3) {
		var addTime = endingDate.getTime() - startingDate.getTime();
		
		if (addTime < 0) {
			alert("The ending date should be AFTER the starting date.");
			return false;
		}
		
		var addDays = endDays - (addTime / (1000*60*60*24));
		document.startDateForm.startDayNum.value = roundNum(addDays,0);
	}


// Corrected Gestational Age?
	if (document.gestCheck.gestAgeSelect.checked) {
		var gestAgeBirth = document.gestAgeForm.gestAgeBirth.value;
		
		// eval frac
		if (gestAgeBirth.indexOf("/") != -1) {			// is there a fraction?
			gestAgeBirth = evalFrac(gestAgeBirth,1);
			if (gestAgeBirth == false) {return false}
		}
		
		if (isNaN(parseInt(gestAgeBirth))) {
			alert("Please enter the gestational age at birth.");
			document.gestAgeForm.gestAgeBirth.focus();
			document.gestAgeForm.gestAgeBirth.select();
			return false;
		}
		
		// Calculate Estimated Due Date
		var weekstoBirth = 40 - gestAgeBirth;
		weekstoBirth = weekstoBirth * 7 * 24 * 60 * 60 * 1000	// convert to milliseconds
		var edd = new Date ((startingDate.getTime() + weekstoBirth));
		
		document.gestAgeForm.day.selectedIndex = edd.getDay();
		document.gestAgeForm.month.selectedIndex = edd.getMonth();
		document.gestAgeForm.date.selectedIndex = edd.getDate() - 1;
		document.gestAgeForm.year.value = edd.getFullYear();
		document.gestAgeForm.whatTime.value = edd.getTime();
		
		// Corrected Gestational Age
		
		if (endingDate.getTime() > edd.getTime()) {				// end time is later than EDD
			var correctGest = endingDate.getTime() - edd.getTime();
			document.gestAgeForm.gestDayWeek.selectedIndex = 2		// calculate corrected age in months
		}
		if (edd.getTime() > endingDate.getTime()) {				// EDD is later than end time
			var correctGest = endingDate.getTime() - startingDate.getTime() + (gestAgeBirth * 7*24*60*60*1000);
			document.gestAgeForm.gestDayWeek.selectedIndex = 1		// calculate weeks gestation
		}

		changeGestage(correctGest);
	}
	return true;
}

function changeGestage(correctGest) {
	var gestDayWeek = document.gestAgeForm.gestDayWeek.selectedIndex;
	
	if (gestDayWeek == 0) {				// Gestational age in days
		document.gestAgeForm.gestAge.value = roundNum((correctGest/(1000*60*60*24)),0)
	}
	if (gestDayWeek == 1) {				// Gestational age in weeks
		var correctGestDays = correctGest / (1000*60*60*24);
		var correctGestWeeks = Math.floor(correctGestDays / 7);
		var correctGestFrac = roundNum((correctGestDays % 7),0);
		
		var correctGestText = correctGestWeeks;
		
		if (correctGestFrac != 0) {
			correctGestText += " " + correctGestFrac + "/7";
		}
		
		document.gestAgeForm.gestAge.value = correctGestText;
	}
	if (gestDayWeek == 2) {				// Gestational age in months
		var startingDate = new Date(parseInt(document.startDateForm.whatTime.value));
		var endingDate = new Date(parseInt(document.endDateForm.whatTime.value));
		var edd = new Date(parseInt(document.gestAgeForm.whatTime.value));

//		document.endDateForm.dateDifferenceText.value = "startingDate ="+startingDate +
//			"\nendingDate ="+endingDate+"\nedd ="+edd;

		if (endingDate.getTime() > edd.getTime()) {		// if endingDate is past edd
			var correctGestYears = (endingDate.getFullYear() - edd.getFullYear()) * 12;
			var correctGestMonths = endingDate.getMonth() - edd.getMonth();
			var correctGestDays = (endingDate.getDate() - edd.getDate()) / 30;
		}	

		if (edd.getTime() > endingDate.getTime()) {		// if edd is past endingDate
			var gestAgeBirth = document.gestAgeForm.gestAgeBirth.value;
			if (gestAgeBirth.indexOf("/") != -1) {gestAgeBirth = evalFrac(gestAgeBirth,1)};
			var lmp = new Date(startingDate.getTime() - gestAgeBirth*7*24*60*60*1000);
						
			var correctGestYears = (endingDate.getFullYear() - lmp.getFullYear()) * 12;
			var correctGestMonths = endingDate.getMonth() - lmp.getMonth();
			var correctGestDays = (endingDate.getDate() - lmp.getDate()) / 30;
		}
		
		correctGestMonths += correctGestYears + correctGestDays;
		document.gestAgeForm.gestAge.value = roundNum(correctGestMonths,1);
	}
	
	document.gestAgeForm.correctGest.value = correctGest;
	return true;
}


function checkDate(month,day,year) {
	//  Reset February's "maxdays"
	maxDays[1] = 28;

	//  Check February leap year
	//  Years divisible by 100 are NOT leap years (1700, 1800, 1900), 
	//  	except years divisible by 400 (2000, 2400) are leap years.

	if ((month == 1) && ((year % 4) == 0)) {
		if (((year % 100) == 0) && ((year % 400) != 0)) {maxDays[1] = 28}
			else {maxDays[1] = 29}
	}

	if (day > maxDays[month]) {return false}

	return true;
}


function evalFrac(frac,isEdd) {
/*	isEdd = 0		use document.endDateForm.endDayNum.value (End Date Form)
	isEdd = 1		use document.gestAgeForm.gestAgeBirth.value (Gest Age Form)
*/

	var slash = frac.indexOf("/");
	var denomLength = frac.length - slash - 1;
	var denomNum = frac.substr((slash+1), (slash+denomLength));	// Denominator
	var whereSpace = frac.indexOf(" ");
	var numerNum = frac.substring((whereSpace+1), (slash));		// Numerator

// Evaluate weeks
	if ((document.endDateForm.endSelect.selectedIndex == 1) && (isEdd == 0)) {
		if (denomNum != 7) {					// Is denominator 7?
			alert("Weeks are expressed in fractions of 7.");
			document.endDateForm.endDayNum.focus();
			return false;
		}
	}

	if (isEdd == 1) {
		if (denomNum != 7) {					// Is denominator 7?
			alert("Weeks are expressed in fractions of 7.");
			document.gestAgeForm.gestAgeBirth.focus();
			return false;
		}
	}
	
	//  Check numerator
	if ((Number(numerNum) > (denomNum-1)) || 
	   //isNaN(chaz) || frac.chuckhu(swish)
	   (isNaN(frac.charAt(slash-1)))) {
		alert("Please enter a proper fraction.");
		if (isEdd == 0) {	document.endDateForm.endDayNum.focus()} else 
			{document.gestAgeForm.gestAgeBirth.focus()};
		return false;
	}

	var fraction = numerNum / denomNum;

	//  Anything before the fraction?
	if (frac.indexOf("/") == 1) {
		if (fraction == 0) {
			fraction = 0.001
				//  Prevents script from interpreting
				//  this as a 'false' value
		}
		return fraction
	}

	var fullNum = frac.substring(0,frac.indexOf(" "));
	if (isNaN(fullNum)) {
		alert("Please enter a proper number of weeks.");
		if (isEdd == 0) {document.endDateForm.endDayNum.focus()} else 
			{document.gestAgeForm.gestAgeBirth.focus()};

		return false
	}

	frac = Number(fullNum) + fraction;
		if (frac == 0) {frac = 0.001}
	return frac;
}	

function displayGest(elementName) {
	var dom = eval(docObj + "." + elementName + styleObj);
/*	var state = dom.visibility;
	if (state == "visible" || state == "show") {dom.visibility = "hidden"}
		else {dom.visibility = "visible"}
*/
	var state = dom.display;
	if (state == "block") {dom.display = "none"}
		else {dom.display = "block"}

	return true	
}


function resetForm() {
	document.DueDate.reset();
	document.eddForm.reset();
	document.weeksPreg.reset();
	return true
}


function dateSelect(Month,dd,Year) {

var today = new Date();

//
// JavaScript:  Choose Day
//

//  	writeDay(isEdd, today.getDay());
document.writeln("<select name=\"day\" " +
	"onChange=\"whatDay(this.form)\">");


//	Select the names for the days

for (var i=0; i < 7; i++) {
	if (i == today.getDay()) {
		document.writeln("<option selected>" +
		dayName[i]);
	} else {
		document.writeln("<option>" + 
		dayName[i]);
	}
}
document.writeln("</select>, ");


//
// JavaScript:  Choose Month
//

document.writeln("<select name=\"month\" " +
	"onChange=\"whatDay(this.form)\">");


//	Select the names for the months

for (var i=0; i < 12; i++) {
	if (i == Month) {
		document.writeln("<option selected>" +
		monName[i]);
	} else {
		document.writeln("<option>" + 
		monName[i]);
	}
}
document.writeln("</select>");


//
// JavaScript:  Choose Date
//

document.writeln("<select name=\"date\" " +
	"onChange=\"whatDay(this.form)\">");

for (var d = 1; d < 32; d++) {
	if (d == dd) {
		document.writeln("<option selected>" + dd);
	} else {
		document.writeln("<option>" + d);
	}
}

document.writeln("</select>, ");


//
// JavaScript:  Choose Year
//

//	For dates before the year 2000 (ex. 1999), the year
//	is stored as "xx" (ex. 99).  However, for dates on or 
//	after the year 2000 (ex. 2003), the year is stored as 
//	"xxxx" (ex. 2003).

document.writeln("<input name=\"year\" type=text size=4 maxlength=4 " +
	"value=\"" + Year + "\" onChange=\"whatDay(this.form);\" " +
	"onFocus=\"form.year.select();\">");

document.writeln("<input name=\"whatTime\" type=hidden value=\"" + today.getTime() + "\">");

return true;
}

function writeDay(whatEdd, chosenDay) {
	var chooseDay;
	if (whatEdd == 0) {chooseDay = eval(getLabel("lmpDay"));} else
		if (whatEdd == 1) {chooseDay = eval(getLabel("eddDay"));} else
		if (whatEdd == 2) {chooseDay = eval(getLabel("wksPreg"));}
	chooseDay.innerHTML = dayName[chosenDay] + ", ";
	chooseDay.style.display = "";

	return;
}

function getLabel(nameID) {
	var whatTag;
	
	if (ie4) {whatTag = "document.all[\"" + nameID + "\"]";}
	if (ns6) {whatTag = "document.getElementById(\"" + nameID + "\")";}
	return whatTag;	
}

function roundNum(thisNum,dec) {
	thisNum = thisNum * Math.pow(10,dec)
	thisNum = Math.round(thisNum)
	thisNum = thisNum / Math.pow(10,dec)
	return thisNum
}