function makeArray(n) {
   for (var i = 1; i <= n; i++) this[i] = 0;
   return this;
}

function isDate(yr,mon,day) {
	valid = true
	var daysinmonth = makeArray(12);
	if ((yr == 'Year') || (mon == 'Month') || (day == 'Day')) return false;
	yr = parseInt(yr);
	mon = parseInt(mon);
	day = parseInt(day);
	daysinmonth[1] = 31;
	daysinmonth[2] = 29;   
	daysinmonth[3] = 31;
	daysinmonth[4] = 30;
	daysinmonth[5] = 31;
	daysinmonth[6] = 30;
	daysinmonth[7] = 31;
	daysinmonth[8] = 31;
	daysinmonth[9] = 30;
	daysinmonth[10] = 31;
	daysinmonth[11] = 30;
	daysinmonth[12] = 31;
	if ((yr.length == 2) && (yr.length == 4)) return false;
	if ((mon < 1) || (mon > 12)) return false;
	if (mon != 2) {
		if ((day < 1) || (day > daysinmonth[mon])) return false;
	} else {
		if (day < 1) return false;
		if ((yr % 4 == 0) && ( (!(yr % 100 == 0)) || (yr % 400 == 0) ) ) {
			if (day >29) return false;
		} else if (day > 28) return false;
	}
	return true;
}

function IsInteger(strNumeric) {
	if (isNaN(parseInt(strNumeric))) {
		return false;
	}
	if (strNumeric.indexOf(".") != -1) {
		return false;
	}
	return true;
}

function ValidateData() {
	var mon,yr,day,cycle;
	var CurDate,mydate;
	CurDate = new Date();
	
	mon = document.FertCalcForm.getmon.options[document.FertCalcForm.getmon.options.selectedIndex].value;
	yr = document.FertCalcForm.getyr.options[document.FertCalcForm.getyr.options.selectedIndex].value;
	day = document.FertCalcForm.getdt.options[document.FertCalcForm.getdt.options.selectedIndex].value;
	cycle = document.FertCalcForm.cycle.value;
	duration = document.FertCalcForm.duration.value;
		
	if (!IsInteger(cycle)) {
		alert("Please enter a valid number for the menstrual cycle");
		return false
	} else if (!IsInteger(duration)) {
		alert("Please enter a valid number for your period\'s length");
		return false
	} else if (cycle < 17) {
		alert("Please enter a valid number for the menstrual cycle");
		return false
	} else if (!isDate(yr,mon,day)) {
		alert("The date you've selected is invalid");
		return false
	} else {
		mydate = new Date(yr,mon-1,day);
		if (mydate > CurDate) {
			alert("Date selected cannot be later than today's date");
			return false
		} else {
			//alert("dates validated");
			document.FertCalcForm.submit();
			return true
		}
	}
}

