Ndays = new Array(12);
Ndays[0]  = 31;
Ndays[1]  = 28;
Ndays[2]  = 31;
Ndays[3]  = 30;
Ndays[4]  = 31;
Ndays[5]  = 30;
Ndays[6]  = 31;
Ndays[7]  = 31;
Ndays[8]  = 30;
Ndays[9]  = 31;
Ndays[10] = 30;
Ndays[11] = 31;
function makeArray(len) {
    for (var i=0; i<len; i++) this[i] = null;
    this.length = len;
}
function valid_year(year) {
    if ((year >= 1990) && (year <= 2100)) {
        return(1);
    } else {
        return(0);
    }
}
function valid_month(month) {
    if ((month >= 0) && (month <= 11)) {
        return(1);
    } else {
        return(0);
    }
}
function valid_day(day, month) {
    if ((day >= 1) && (day <= Ndays[month])) {
        return(1);
    } else {
        return(0);
    }
}
function isNumberString(a_string) {
    var ii;

    for (ii=0; ii<a_string.length; ii++) {
        if ( (a_string.charAt(ii) < "0") || (a_string.charAt(ii) > "9") ) {
            return(0);
        }
    }
    return(1);
}
function check_date(form) {
    var userDay   = 0;
    var userMonth = 0;
    var userYear  = 0;
    var nextDate;
    var nextDateStart;
    var nextDateStop;
    var nextDay   = 0;
    var nextMonth = 0;
    var nextYear  = 0;
    var nextDayStart   = 0;
    var nextMonthStart = 0;
    var nextYearStart  = 0;
    var nextDayStop   = 0;
    var nextMonthStop = 0;
    var nextYearStop  = 0;
    var cycle = (form.cycle.value == "" ? 28 : form.cycle.value); // defaults to 28
    //validates cycle range, from 22 to 45
    if (cycle != "" && (cycle < 22 || cycle > 45)) {
    alert("La durata del tuo ciclo mestruale è troppo breve o troppo lunga \n "
    + "Completiamo comunque il calcoli con la durata che hai inserito.");
    }
    if ( isNumberString(form.day.value)
         && isNumberString(form.month.value)
         && isNumberString(form.year.value) ) {
        userYear  = parseInt(form.year.value);
        userMonth = parseInt(form.month.value) - 1;
        userDay   = parseInt(form.day.value);
        if ( valid_year(userYear)
             && valid_month(userMonth)
             && valid_day(userDay, userMonth) ) {
            nextDate = new Date(Date.UTC(userYear, userMonth, userDay, 0, 0, 0) + (cycle-14)*24*60*60*1000);
            nextDay   = nextDate.getDate();
            nextMonth = nextDate.getMonth()+1;
            nextYear  = nextDate.getYear();		
	nextDateStart = new Date(Date.UTC(nextYear, nextMonth-1,nextDay, 0, 0,0) - 3*24*60*60*1000);
	nextDateStop = new Date(Date.UTC(nextYear, nextMonth-1,nextDay, 0, 0,0) + 3*24*60*60*1000);		
	nextDayStart   = nextDateStart.getDate();
            nextMonthStart = nextDateStart.getMonth()+1;
            nextYearStart  = nextDateStart.getYear();		
	nextDayStop   = nextDateStop.getDate();
            nextMonthStop = nextDateStop.getMonth()+1;
            nextYearStop  = nextDateStop.getYear();		
            if (nextYear<1900) nextYear+=1900;
form.dataok.value = (nextDay + " / " + nextMonth + " / " + nextYear);
if (nextYearStart<1900) nextYearStart+=1900;
form.datastartok.value = (nextDayStart + " / " + nextMonthStart + " / " + nextYearStart);
if (nextYearStop<1900) nextYearStop+=1900;
form.datastopok.value = (nextDayStop + " / " + nextMonthStop + " / " + nextYearStop);
           } else {
            alert("Digita una data corretta che sia in formato ad esempio 18 12 2008");
        }
    } else {
        alert("Il form accetta solo numeri, non lettere!");
    }
}