// general purpose function to see if an input value has been entered at all
function isEmpty (inputStr) {
	if (inputStr == "" || inputStr == null) {
		return true;
	}
	return false;
}
// general purpose function to see if a suspected numeric input
// is a positive integer
function isNumber (inputStr) {
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			return false;
		}
	}
	return true;
}
// function to determine if value is in acceptable range for this application
function inRangeDay (inputStr) {
	num = parseInt(inputStr);
	if (num < 1 || num > 31) {
		return false;
	}
	return true;
}
// function to determine if value is in acceptable range for this application
function inRangeYear (inputStr) {
	num = parseInt(inputStr);
	if (num < 1900 || num > 3000) {
		return false;
	}
	return true;
}
// Master value validator routine for day
function isValidDay (inputStr) {
	if (isEmpty(inputStr)) {
		alert ("Please enter the number of the day into the day field before clicking the Calculate button.");
		return false;
	} else {
		if (! isNumber(inputStr)) {
			alert ("Please make sure the day is a number only.");
			return false;
		} else {
			if (! inRangeDay(inputStr)) {
				alert ("Please enter a valid day.");
				return false;
			}
		}
	}
	return true;
}
// Master value validator routine for year
function isValidYear (inputStr) {
	if (isEmpty(inputStr)) {
		alert ("Please enter the year into the year field before clicking the Calculate button.");
		return false;
	} else {
		if (! isNumber(inputStr)) {
			alert ("Please make sure the year is a number only.");
			return false;
		} else {
			if (! inRangeYear(inputStr)) {
				alert ("Please enter a valid year.");
				return false;
			}
		}
	}
	return true;
}
function makeArray (n) {
	this.length = n;
	for (var i=1; i <= n; i++) {
		this[i] = null;
	}
	return this;
}
var maxday = new makeArray(12);
maxday[1] = 31;
maxday[2] = 28;
maxday[3] = 31;
maxday[4] = 30;
maxday[5] = 31;
maxday[6] = 30;
maxday[7] = 31;
maxday[8] = 31;
maxday[9] = 30;
maxday[10] = 31;
maxday[11] = 30;
maxday[12] = 31;
var monthname = new makeArray(12);
monthname[1] = "Enero";
monthname[2] = "Febrero";
monthname[3] = "Marzo";
monthname[4] = "Abril";
monthname[5] = "Mayo";
monthname[6] = "Junio";
monthname[7] = "Julio";
monthname[8] = "Agosto";
monthname[9] = "Septiembre";
monthname[10] = "Octubre";
monthname[11] = "Noviembre";
monthname[12] = "Diciembre";
var adddays = new makeArray(7);
adddays[1] = 14;
adddays[2] = 35;
adddays[3] = 70;
adddays[4] = 84;
adddays[5] = 161;
adddays[6] = 189;
adddays[7] = 280;
// Calculate the date string
function calcNewDate (month,day,year,adddays) {

	newday = eval(day) + adddays;

	newmonth = month + 1;

	newyear = eval(year)
	 ;
	var max;

	for (var i = 0; i < 12; i++) {

		if (newmonth == 2 && (newyear % 4) == 0) {
			max = 29;

		} else {

			max = maxday[newmonth];
		}
		if (newday > max) {
			newday = newday - max;

			newmonth = newmonth + 1;

			if (newmonth > 12) {

				newyear = newyear + 1;

				newmonth = 1;

			}

		} else {

			break;

		}
	}

	var datestring = monthname[newmonth] + " " + newday + ", " + newyear;

	return datestring;

}
// Get the date entered and calculate the rest of the dates

function calc (form) {

	day = form.day.value;

	year = form.year.value;

	monthnum = form.month.selectedIndex
	   ;
	if (isValidDay(day)) {

		if (isValidYear(year)) {
			document.getElementById("embarazo").style.display = "inherit";

			form.conception.value = calcNewDate(monthnum,day,year,adddays[1]);

			form.beginrisk.value = calcNewDate(monthnum,day,year,adddays[2]);

			form.endrisk.value = calcNewDate(monthnum,day,year,adddays[3])
			 ;
			form.beginorgan.value = calcNewDate(monthnum,day,year,adddays[2])
			 ;
			form.endorgan.value = calcNewDate(monthnum,day,year,adddays[3]);

			form.endfirst.value = calcNewDate(monthnum,day,year,adddays[4]);

			form.preemies.value = calcNewDate(monthnum,day,year,adddays[5]);

			form.endsecond.value = calcNewDate(monthnum,day,year,adddays[6]);

			form.duedate.value = calcNewDate(monthnum,day,year,adddays[7]);
		}
	}
}