// <!-- Begin
function isValidDate(dateStr) {

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("La data non è in un formato valido. Ridigitarla")
return false;
}
day = matchArray[1];
month = matchArray[3]; // parse date into variables
year = matchArray[4];
if (day < 1 || day > 31) {
alert("I giorni devono essere compresi fra 1 e 31");
return false;
}
if (month < 1 || month > 12) { // check month range
alert("I mesi devono essere compresi fra 1 e 12");
return false;

}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Il mese digitato"+month+" non ha 31 giorni!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("Febbraio " + year + " non ha " + day + " giorni!");
return false;
   }
}
return true;
}

function dispDate(dateObj) {

day   = dateObj.getDate();
day = (day < 10) ? "0" + day : day;

month = dateObj.getMonth()+1;
month = (month < 10) ? "0" + month : month;

year  = dateObj.getYear();
if (year < 2000) year += 1900;

return (day + "/" + month + "/" + year);
}

function pregnancyCalc(pregform) {
menstrual = new Date(); // creates new date objects
ovulation = new Date();
duedate = new Date();
today = new Date();
cycle = 0, luteal = 0; // sets variables to invalid state ==> 0

if (isValidDate(pregform.menstrual.value)) { // Validates menstual date 
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var dateStr = pregform.menstrual.value;
var matchArray = dateStr.match(datePat);
day = matchArray[1];
month = matchArray[3]; // parse date into variables
year = matchArray[4];
finalStringDate = month + "/" + day + "/" + year;
menstrualinput = new Date(finalStringDate);
menstrual.setTime(menstrualinput.getTime())

}
else return false; // otherwise exits

cycle = (pregform.cycle.value == "" ? 28 : pregform.cycle.value); // defaults to 28
// validates cycle range, from 22 to 45
if (pregform.cycle.value != "" && (pregform.cycle.value < 22 || pregform.cycle.value > 45)) {
alert("Your cycle length is either too short or too long for \n"
+ "calculations to be very accurate!  We will still try to \n"
+ "complete the calculation with the figure you entered. ");
}

luteal = (pregform.luteal.value == "" ? 14 : pregform.luteal.value); // defaults to 14
// validates luteal range, from 9 to 16
if (pregform.luteal.value != "" && (pregform.luteal.value < 9 || pregform.luteal.value > 16)) {
alert("Your luteal phase length is either too short or too long for \n"
+ "calculations to be very accurate!  We will still try to complete \n"
+ "the calculation with the figure you entered. ");
}



// sets ovulation date to menstrual date + cycle days - luteal days
// the '*86400000' is necessary because date objects track time
// in milliseconds;  86400000 milliseconds equals one day
ovulation.setTime(menstrual.getTime() + (cycle*86400000) - (luteal*86400000));
pregform.conception.value = dispDate(ovulation);

// sets due date to ovulation date plus 266 days
duedate.setTime(ovulation.getTime() + 266*86400000);
pregform.duedate.value = dispDate(duedate);

// sets fetal age to 14 + 266 (pregnancy time) - time left
var fetalage = 14 + 266 - ((duedate - today) / 86400000);
weeks = parseInt(fetalage / 7); // sets weeks to whole number of weeks
days = Math.floor(fetalage % 7); // sets days to the whole number remainder

// fetal age message, automatically includes 's' on week and day if necessary
fetalage = weeks + " settimane" + ", " + days + " giorni";
pregform.fetalage.value = fetalage;

return false; // form should never submit, returns false
}
//  End -->

function lunghFeto(form) {
lunghfemore=eval(form.lunghfemore.value);
Lfeto=(((lunghfemore* 0.59) + 6.18));
form.lunghfeto.value = (Math.round(Lfeto*100))/100;
}

function pesoFetale(form) {
form.pesoFetaleResult.value =Math.pow(10,(-1.7492+(0.0166*form.BPD.value)+(0.0046*form.ABDO.value)-(2.646*(form.BPD.value*form.ABDO.value)/100000)));
form.pesoFetaleResult.value=100000*form.pesoFetaleResult.value;
form.pesoFetaleResult.value= (Math.round(form.pesoFetaleResult.value))/100;
if (form.BPD.value == ""  || form.ABDO== "")
{form.pesoFetaleResult.value=0};
}


//Parte per Indice Massa Corporea

// Funzione di arrotondamento
// il numero di cifre decimali è dato dal valore della costante decimali

function arrota(x) {

var decimali=1;

var zero=Math.pow(10,decimali);
var uno=(x*zero)
var due=Math.round(uno);
var tre=(due/zero);

return tre;

}


// Funzione per il calcolo dell'Indice di Massa Corporea
// calcola anche il peso ideale e il peso massimo e minimo
// a seconda della deviazione identifica la tipologia di
// persona.

function bmi() {

var peso=document.bmicalc.peso.value;
var alte=document.bmicalc.altezza.value;

var cubo=(alte*alte);
var imc=(peso/cubo);
var id_da=(cubo*20);
var id_a=(cubo*23.4);

var id_da_arr=arrota(id_da);
var id_a_arr=arrota(id_a);

var ideale=(id_da+((id_a-id_da)/2));
var ideale_arr=arrota(ideale);

var anoressico=(id_da_arr-20);
var fort_sotto=(id_da_arr-10);
var legg_sotto=(id_da_arr-5);

var legg_sopra=(id_a_arr+5);
var fort_sopra=(id_a_arr+10);
var obeso=(id_a_arr+20);

// Calcolo della Tipologia.

if (peso>id_a_arr) { 
	if (peso>legg_sopra) {
		if (peso>fort_sopra) {
			if (peso>obeso) stato="Obeso";
			else stato="Fortemente Sovrappeso";
		}
		else stato="Sovrappeso";
	}
	else stato="Leggermente Sovrappeso";
}

else if (peso<id_da_arr) {
		if (peso<legg_sotto) {
			if (peso<fort_sotto) {
				if (peso<anoressico) stato="Anoressico";
				else stato="Fortemente Sottopeso";
			}
			else stato="Sottopeso";
		}
		else stato="Leggermente Sottopeso";
}

else stato="Normale";

// Assegnazione delle variabili ai campi di testo
// per la visualizzazione.

document.bmicalc.risultato.value=arrota(imc);
document.bmicalc.ideale_da.value=id_da_arr;
document.bmicalc.ideale_a.value=id_a_arr;
document.bmicalc.ideale.value=ideale_arr;
document.bmicalc.stato.value=stato;
}
