/*****************************************************************************************************/
/*                                   GLOBAL VARIABLES DEFINED HERE                                   */
/*****************************************************************************************************/

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;

/****************************************************************************************************/
/*************************                   CORE PROGRAM                   *************************/
/****************************************************************************************************/


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 nextDay   = 0;
    var nextMonth = 0;
    var nextYear  = 0;

    var cycle = form.cycle.options[form.cycle.selectedIndex].value;

    /* First we check if the entered date is numeric */
    if ( isNumberString(form.day.value)
         && isNumberString(form.month.value)
         && isNumberString(form.year.value) ) {

        /* The months are coded from 0 up to 11 */
        userYear  = parseInt(form.year.value);
        userMonth = parseInt(form.month.value) - 1;
        userDay   = parseInt(form.day.value);

        /* First we check if the entered date is correct */
        if ( valid_year(userYear)
             && valid_month(userMonth)
             && valid_day(userDay, userMonth) ) {

            /* Then we compute the next date */
            nextDate = new Date(Date.UTC(userYear, userMonth, userDay, 0, 0, 0) + cycle*24*60*60*1000);

            nextDay   = nextDate.getDate();
            nextMonth = nextDate.getMonth()+1;
            nextYear  = nextDate.getYear();
            if (nextYear<1900) nextYear+=1900;

			  form.dataok.value = (nextDay + " / " + nextMonth + " / " + nextYear);
            

        } else {
            alert("Data errata");
        }

    } else {
        alert("Data non espressa in numeri");
    }
}
