//<SCRIPT LANGUAGE="JavaScript">
// 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,year,monthnum) {
        num = parseInt(inputStr);
        var maxd;
        if ((year % 4)==0 && monthnum==1){
         	  maxd=29;
         }
         else
         {
             maxd= maxday [monthnum+1];
         }
        if (num>maxd){
                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,year,monthnum) {
		if (isEmpty(inputStr)) {
			alert("Por favor, preencha o campo Dia")
			return false;
		} else {
		if (!isNumber(inputStr)) {
			alert("Escreva um número no campo Dia")
			return false;
		} else {
			if (!inRangeDay(inputStr,year,monthnum)) {
                                alert("Escreva um número válido no campo Dia")
                                return false;
                        }
                }
        }
        return true;
}

// Master value validator routine for year
function isValidYear(inputStr) {
        if (isEmpty(inputStr)) {
                alert("Por favor preencha o campo Ano");
                return false;
        } else {
                if (!isNumber(inputStr)) {
                        alert("Escreva um número no campo Ano");
                } else {
                        if (!inRangeYear(inputStr)) {
                                alert("Escreva um ano válido.");
                                return false;
                        }
                }
        }
        return true;
}

function makeArray(n) {
	this.length = n;
	for (var i=1; i <= n; i++);
	this[i] = null;
	return this;
}

// 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 = newday + " de " + monthname[newmonth] + " de " + newyear;
return datestring;
}

// Get the date entered and calculate the rest of the dates

function calc(embarazo) {
	var day = document.embarazo.day.value;
	var year =document.embarazo.year.value;
	var monthnum = document.embarazo.month.selectedIndex;
	if (isValidDay(day,year,monthnum)) {
		if (isValidYear(year)){
//		  	document.embarazo.conception.value = calcNewDate(monthnum,day,year,adddays[1]);
//			document.embarazo.beginrisk.value = calcNewDate(monthnum,day,year,adddays[2]);
//			document.embarazo.endrisk.value = calcNewDate(monthnum,day,year,adddays[3]);	
//			document.embarazo.beginorgan.value = calcNewDate(monthnum,day,year,adddays[2]);
//			document.embarazo.endorgan.value = calcNewDate(monthnum,day,year,adddays[3]);
//			document.embarazo.endfirst.value = calcNewDate(monthnum,day,year,adddays[4]);
//			document.embarazo.preemies.value = calcNewDate(monthnum,day,year,adddays[5]);
//			document.embarazo.endsecond.value = calcNewDate(monthnum,day,year,adddays[6]);
			document.embarazo.duedate.value = calcNewDate(monthnum,day,year,adddays[7]);
		}
	}
}