 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 è stata inserita in un formato valido !")
    return false;
    }
    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[4];
    if (day < 1 || day > 31) {
    alert("Il valore del giorno deve essere compreso tra 1 e 31");
    return false;
    }
	if (month < 1 || month > 12) { // check month range
    alert("Il valore del mese deve essere compreso tra 1 e 12");
    return false;
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	if (month==4) {var qualeMese = 'Aprile';}
	else if (month==6) {var qualeMese = 'Giugno';}
	else if (month==9) {var qualeMese = 'Settembre';}
	else if (month==11) {var qualeMese = 'Novembre';}
    alert(qualeMese+" 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) {
    month = dateObj.getMonth()+1;
    month = (month < 10) ? "0" + month : month;

    day   = dateObj.getDate();
    day = (day < 10) ? "0" + day : day;

    year  = dateObj.getYear();
    if (year < 2000) year += 1900;

    return (month + "/" + day + "/" + 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

    //pregform.menstrual.value = pregform.menstrualH.value.substr(3, 2) + '/' +pregform.menstrualH.value.substr(0, 2) + '/' + pregform.menstrualH.value.substr(6, pregform.menstrualH.value.length);    
    pregform.menstrual.value = pregform.mese.value + '/' + pregform.giorno.value + '/' + pregform.anno.value;
    if (isValidDate(pregform.menstrual.value)) { // Validates menstual date
    menstrualinput = new Date(pregform.menstrual.value);
    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("La durata del ciclo è troppo breve o troppo lunga affinchè i calcoli possano essere corretti.\n"
    + "Procederemo comunque ad una previsione con il ciclo inserito.");
    }

    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("La durata della fase dopo l'ovulazione è troppo breve o troppo lunga affinchè i calcoli possano essere corretti.\n"
    + "Procederemo comunque ad una stima con il valore inserito.");
    }

    // 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.conceptionH.value = dispDate(ovulation);
    pregform.conception.value = pregform.conceptionH.value.substr(3, 2) + "/" +  pregform.conceptionH.value.substr(0, 2) + "/" + pregform.conceptionH.value.substr(6, pregform.conceptionH.value.length);

    // sets due date to ovulation date plus 266 days
    duedate.setTime(ovulation.getTime() + 266*86400000);
    pregform.conceptionH.value = dispDate(duedate);
    pregform.duedate.value = pregform.conceptionH.value.substr(3, 2) + "/" +  pregform.conceptionH.value.substr(0, 2) + "/" + pregform.conceptionH.value.substr(6, pregform.conceptionH.value.length);

    // 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" + (weeks > 1 ? "" : "") + " + " + days + " giorni";
    pregform.fetalage.value = fetalage;
    if (weeks>42)
    {
        pregform.fetalage.value = '';
        pregform.conception.value = '';
        pregform.duedate.value = '';
        alert("La data inserita non è valida");
    }
    return false; // form should never submit, returns false
    }
    
    function esegui()
    {
        document.dataParto.submit();
    }
 
    function esegui2()
    {
        document.dataParto2.giorno.value = document.dataParto.giorno.value;
        document.dataParto2.mese.value = document.dataParto.mese.value;
        document.dataParto2.anno.value = document.dataParto.anno.value;
        return pregnancyCalc(document.dataParto2);
    }