//<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,foco) {        
        var checkOK = "0123456789";
	var checkStr = inputStr;
	var allValid = true;	
	var allNum = "";
	
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;	
		}
		allNum += ch;
	}
	if (!allValid)
	{
		if (parseInt(foco)==0){		    
		    document.embarazo.year.focus();
		}
		else{		    
		    document.embarazo.day.focus();
		}
		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){
        	document.embarazo.day.focus();	
                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) {
        	document.embarazo.year.focus();
                return false;
        }
        return true;
}

// Master value validator routine for day
function isValidDay(inputStr,year,monthnum) {
		if (isEmpty(inputStr)) {
			alert("Por favor, introduce el día")
		        document.embarazo.day.focus();
			return false;
		} else {
		if (!isNumber(inputStr,1)) {
			alert("Por favor introduce solo números");
			return false;
		} else {
			if (!inRangeDay(inputStr,year,monthnum)) {
                                alert("Escribe un número válido de día");                                	    
                                return false;
                        }
                }
        }
        return true;
}

// Master value validator routine for year
function isValidYear(inputStr) {
        if (isEmpty(inputStr)) {
                alert("Por favor introduce el año");
                 document.embarazo.year.focus();
                return false;
        } else {
                if (!isNumber(inputStr,0)) {
                        alert("Por favor introduce solo números");
                        return false;
                } else {
                        if (!inRangeYear(inputStr)) {
                                alert("Escribe un año 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 = monthname[newmonth] + " " + newday + ", " + newyear;
var datestring = newday + " " + monthname[newmonth] + " " + 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]);
		}
	}
}
