// calculator.js - routines for calculating pregnancy dates and risks
// Owen O Byrne 2006-08-23

var monthNames = new Array(12);
monthNames[0] = "January";
monthNames[1] = "February";
monthNames[2] = "March";
monthNames[3] = "April";
monthNames[4] = "May";
monthNames[5] = "June";
monthNames[6] = "July";
monthNames[7] = "August";
monthNames[8] = "September";
monthNames[9] = "October";
monthNames[10] = "November";
monthNames[11] = "December";

var trisomyData = MultiDimensionalArray(46, 3);
addToTrisomyData(20, 1052, 1797);
addToTrisomyData(21, 1038, 1773);
addToTrisomyData(22, 1019, 1741);
addToTrisomyData(23, 996, 1701);
addToTrisomyData(24, 966, 1650);
addToTrisomyData(25, 929, 1587);
addToTrisomyData(26, 884, 1509);
addToTrisomyData(27, 830, 1417);
addToTrisomyData(28, 768, 1312);
addToTrisomyData(29, 624, 1065);
addToTrisomyData(30, 617, 1054);
addToTrisomyData(31, 545, 931);
addToTrisomyData(32, 461, 788);
addToTrisomyData(33, 387, 661);
addToTrisomyData(34, 319, 544);
addToTrisomyData(35, 262, 448);
addToTrisomyData(36, 206, 351);
addToTrisomyData(37, 162, 277);
addToTrisomyData(38, 129, 220);
addToTrisomyData(39, 100, 170);
addToTrisomyData(40, 77, 131);
addToTrisomyData(41, 59, 100);
addToTrisomyData(42, 45, 76);
addToTrisomyData(43, 34, 58);
addToTrisomyData(44, 26, 44);
addToTrisomyData(45, 19, 33);

function addToTrisomyData(a, t21, t18) {
	trisomyData[a][0] = a;
	trisomyData[a][1] = t21;
	trisomyData[a][2] = t18;
}


function displayTrisomyRiskDetails() {
	var iAge = pInt(getQueryVariable("age"));
	
	for (i = 20; i<46; i++) {
		if (iAge == i) {
			document.write("<TR>");
			document.write("<TD class=data3 align=center  bgcolor='#CC9966'><font color='#FFFFFF'><b>" + trisomyData[i][0] + "</b></font></TD>");
			document.write("<TD class=data3 align=center  bgcolor='#CC9966'><font color='#FFFFFF'><b>" + trisomyData[i][1] + "</b></font></TD>");
			document.write("<TD class=data3 align=center  bgcolor='#CC9966'><font color='#FFFFFF'><b>" + trisomyData[i][2] + "</b></font></TD>");
			document.write("</TR>");
		} else {
			document.write("<TR>");
			document.write("<TD class=data3 align=center>" + trisomyData[i][0] + "</TD>");
			document.write("<TD class=data3 align=center>" + trisomyData[i][1] + "</TD>");
			document.write("<TD class=data3 align=center>" + trisomyData[i][2] + "</TD>");
			document.write("</TR>");
		}
	}
}

function createLMPYearDropDown() {
	var today = new Date();
	var thisYear = today.getFullYear();
	var lastYear = thisYear - 1;

	document.write("<option value='" + lastYear + "'>" + lastYear + "</option>\n");
	document.write("<option value='" + thisYear + "'>" + thisYear + "</option>\n");
}

function createEDDYearDropDown() {
	var today = new Date();
	var thisYear = today.getFullYear();
	var nextYear = thisYear + 1;

	document.write("<option value='" + thisYear + "'>" + thisYear + "</option>\n");
	document.write("<option value='" + nextYear + "'>" + nextYear + "</option>\n");
}

function selectLMPPage() {
	
	var iDate = pInt(document.lmp_form.optDate[document.lmp_form.optDate.selectedIndex].value);
	var iMonth = pInt(document.lmp_form.optMonth[document.lmp_form.optMonth.selectedIndex].value);
	var iYear = pInt(document.lmp_form.optYear[document.lmp_form.optYear.selectedIndex].value);

	if (iDate * iMonth * iYear == 0) {
		alert("Please select the date of your Last Menstrual Period (the date you started your last period) from the drop down boxes");
		return false;
	}

	var today = new Date();
	var lmp = new Date();
	lmp.setFullYear(iYear, iMonth - 1, iDate);

	daysGone = Math.round((today - lmp)/86400000);

	return redirectDependingOnDayGone(daysGone, "lmp_form");
}

function selectEDDPage() {
	
	var iDate = pInt(document.edd_form.optDate[document.edd_form.optDate.selectedIndex].value);
	var iMonth = pInt(document.edd_form.optMonth[document.edd_form.optMonth.selectedIndex].value);
	var iYear = pInt(document.edd_form.optYear[document.edd_form.optYear.selectedIndex].value);

	if (iDate * iMonth * iYear == 0) {
		alert("Please select your Estimated Due Date from the drop down boxes");
		return false;
	}

	var today = new Date();
	var edd = new Date();
	edd.setFullYear(iYear, iMonth - 1, iDate);

	daysLeft = Math.round((edd - today)/86400000);
	daysGone = 280 - daysLeft;

	return redirectDependingOnDayGone(daysGone, "edd_form");
} 

function redirectDependingOnDayGone(daysGone, form) {
	if (daysGone <= 0) {
		alert("Can you check that date - it implies that you have not conceived yet!");
		return false;
	}

	if (daysGone > 300) {
		monthsOld = Math.round((daysGone - 280)/30);
		alert("Are you sure that is the correct date - that date would mean your baby is about " + monthsOld + " month" + (monthsOld == 1 ? "" : "s") + " old...");
		return false;
	}

	weeksGone = Math.floor(daysGone / 7);
	daysInWeeksGone = daysGone % 7;

	chosenForm = eval("document." + form);
	if (weeksGone >= 11 && weeksGone <= 13) {
		chosenForm.action = 'results11-13.htm';
	} else if (weeksGone > 13 && weeksGone <= 18) {
		chosenForm.action = 'results14-18.htm';
	} else if (weeksGone > 18) {
		chosenForm.action = 'results19-40.htm';
	} else {
		chosenForm.action = 'results0-10.htm';
	}
	
	return true;
} 

function displayPregnancyDetails() {
	var iDate = pInt(getQueryVariable("optDate"));
	var iMonth = pInt(getQueryVariable("optMonth"));
	var iYear = pInt(getQueryVariable("optYear"));
	var fType = getQueryVariable("fType");

	if (iDate * iMonth * iYear == 0) {
		document.location.href = "index.htm";
		return false;
	}

	var today = new Date();
	var lmp = new Date();
	var edd = new Date();

	if (fType == "edd") {
		edd.setFullYear(iYear, iMonth - 1, iDate);
		lmp.setTime((Date.parse(edd.toUTCString()) - (280 * 86400000)));

		daysLeft = Math.round((edd - today)/86400000);
		daysGone = 280 - daysLeft;
	} else {
		lmp.setFullYear(iYear, iMonth - 1, iDate);
		edd.setTime((Date.parse(lmp.toUTCString()) + (280 * 86400000)));
	
		daysGone = Math.round((today - lmp)/86400000);
	}

	weeksGone = Math.floor(daysGone / 7);
	daysInWeeksGone = daysGone % 7;

	document.write("<tr><td colspan='2' class='DATA3'>You are <font color='#FF0000'><b>");
	document.write(weeksGone + " week" + (weeksGone == 1 ? "" : "s"));
	if (daysInWeeksGone > 0) {
		document.write(", " + daysInWeeksGone + " day" + (daysInWeeksGone == 1 ? "" : "s"));
	}
	document.write("</font></b> into your pregnancy</td></tr>");

	document.write("<td colspan='2' class='DATA3'>You ");
	document.write((weeksGone > 15 ? "were" : "will be"));
	document.write(" 15 weeks pregnant on: <b><font color='#FF0000'><b>");
	var d15weeks = new Date();
	d15weeks.setTime(Date.parse(lmp.toUTCString()) + (105 * 86400000));
	document.write(d15weeks.getDate() + " " + monthNames[d15weeks.getMonth()] + " " + d15weeks.getFullYear());
	document.write("</b></font></td></tr>\n");

	document.write("<td colspan='2' class='DATA3'>You ");
	document.write((weeksGone > 20 ? "were" : "will be"));
	document.write(" 20 weeks pregnant on: <b><font color='#FF0000'><b>");
	var d20weeks = new Date();
	d20weeks.setTime(Date.parse(lmp.toUTCString()) + (140 * 86400000));
	document.write(d20weeks.getDate() + " " + monthNames[d20weeks.getMonth()] + " " + d20weeks.getFullYear());
	document.write("</b></font></td></tr>\n");

	document.write("<td colspan='2' class='DATA3'>You ");
	document.write((weeksGone > 24 ? "were" : "will be"));
	document.write(" 24 weeks pregnant on: <b><font color='#FF0000'><b>");
	var d24weeks = new Date();
	d24weeks.setTime(Date.parse(lmp.toUTCString()) + (168 * 86400000));
	document.write(d24weeks.getDate() + " " + monthNames[d24weeks.getMonth()] + " " + d24weeks.getFullYear());
	document.write("</b></font></td></tr>\n");

	document.write("<td colspan='2' class='DATA3'>Your Estimated Due Date (EDD) is: <b><font color='#FF0000'>");
	document.write(edd.getDate() + " " + monthNames[edd.getMonth()] + " " + edd.getFullYear());
	document.write("</b></font></td></tr>\n");

}

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	} 
  	alert('Query Variable ' + variable + ' not found');
}

function MultiDimensionalArray(iRows,iCols) {
	var i;
	var j;
	var a = new Array(iRows);
	for (i=0; i < iRows; i++) {
		a[i] = new Array(iCols);
		for (j=0; j < iCols; j++) {
			a[i][j] = "";
		}
	}
	return(a);
} 

function pInt(strInt) {
	if (isNaN(parseInt(strInt))) return 0;
	if (parseInt(strInt) == 0) return 0;
	return parseInt(strInt);
}

