var valoredataattuale;
function trimNumber(s) {
    while (s.substr(0, 1) == '0' && s.length > 1) { s = s.substr(1, 9999); }
    return s;
}

function settadata() {
    if (valoredataattuale.length == 10) {
        tempvararr = valoredataattuale.split("/");
        tempday = parseInt(trimNumber(tempvararr[0]));
        tempmonth = parseInt(trimNumber(tempvararr[1]));
        tempyear = parseInt(trimNumber(tempvararr[2]));
        //alert(tempday + "***" + tempmonth + "***" + tempyear);
        if (isNaN(tempday) || isNaN(tempmonth) || isNaN(tempyear))
            return;



        year = tempyear;
        anno = tempyear;
        mese = tempmonth;
        month = tempmonth - 1;

        memGiorno1 = tempday;
        memMese1 = tempmonth;
        memAnno1 = tempyear;

        memGiorno2 = tempday;
        memMese2 = tempmonth;
        memAnno2 = tempyear;

        //memGiorno2=0; //tempday;
        //memMese2=0; //tempmonth;
        //memAnno2=0; //tempyear;


        monthName = getMonthName(month);
        firstDayInstance = new Date(year, month, 1);
        firstDay = firstDayInstance.getDay();
        days = getDays(month, year);
        monthName2 = getMonthName(month + 1);
        firstDayInstance2 = new Date(year, month + 1, 1);
        firstDay2 = firstDayInstance2.getDay();
        days2 = getDays(month + 1, year);
        days3 = getDays(0, year + 1); //used when bottom panel displays january


        now = new Date(mese + "/" + tempday + "/" + anno);
        date = now.getDate();
    }
}

function gestisciCalendario(memDay, dest, index, searchType) {
    //alert(index);
    //    months=getMonthName2();
    //    weekDay=SetgetweekDay();
    memorizzaGiorno = memDay;
    //window.alert(memorizzaGiorno);
    destinatario = dest;
    document.getElementById('cal1').style.display = 'block';

    valoredataattuale = document.getElementById(dest).value;
    settadata();

    ridisegnaCalendari(index, searchType);
}

function gestisciCalendario_Carnet(memDay, dest, index, searchType, date_mask, dtStart, dtEnd) {
    //alert(index);
    //    months=getMonthName2();
    //    weekDay=SetgetweekDay();
    memorizzaGiorno = memDay;
    //window.alert(memorizzaGiorno);
    destinatario = dest;
    document.getElementById('cal1').style.display = 'block';

    valoredataattuale = document.getElementById(dest).value;
    settadata();

    ridisegnaCalendari_Carnet(index, searchType, date_mask, dtStart, dtEnd);
}

function getTime() {
    // initialize time-related variables with current time settings
    var now = new Date()
    var hour = now.getHours()
    var minute = now.getMinutes()
    now = null
    var ampm = ""

    // validate hour values	and set value of ampm
    if (hour >= 12) {
        hour -= 12
        ampm = "PM"
    } else
        ampm = "AM"
    hour = (hour == 0) ? 12 : hour

    // add zero digit to a one digit minute
    if (minute < 10)
        minute = "0" + minute // do not parse this number!

    // return time string
    return hour + ":" + minute + " " + ampm
}

function leapYear(year) {
    if (year % 4 == 0) // basic rule
        return true // is leap year
    /* else */ // else not needed when statement is "return"
    return false // is not leap year
}

function getDays(month, year) {
    // create array to hold number of days in each month
    var ar = new Array(12)
    ar[0] = 31 // January
    ar[1] = (leapYear(year)) ? 29 : 28 // February
    ar[2] = 31 // March
    ar[3] = 30 // April
    ar[4] = 31 // May
    ar[5] = 30 // June
    ar[6] = 31 // July
    ar[7] = 31 // August
    ar[8] = 30 // September
    ar[9] = 31 // October
    ar[10] = 30 // November
    ar[11] = 31 // December

    // return number of days in the specified month (parameter)
    return ar[month]
}

function getMonthName(month) {

    // return name of specified month (parameter)
    //window.alert(month);
    if (month == 12) { //revert to january

        return months[0];
    } else {
        return months[month];
    }
}

function setCal(tg) {
    // standard time attributes
    var now = new Date()
    var year = now.getFullYear()
    var month = now.getMonth()
    var monthName = getMonthName(month)
    var date = now.getDate()
    now = null

    // create instance of first day of month, and extract the day on which it occurs
    var firstDayInstance = new Date(year, month, 1)
    var firstDay = firstDayInstance.getDay()
    firstDayInstance = null

    // number of days in current month
    var days = getDays(month, year)
    // call function to draw calendar
    drawCal(firstDay + 1, days, date, monthName, year, tg)
}

//render the top month in the calendar
function drawCal_Carnet(firstDay, lastDate, date, monthName, year, target, dep_or_ret, searchType, days_mask, dtStart, dtEnd) 
{
    // create basic table structure
    
    //dtStart = '2010/12/01';
    //dtEnd = '2010/12/20';
    //days_mask = '0111001';
    days_mask += '';
    dtStart += '';
    dtEnd += '';
    //
    var text = "" // initialize accumulative variable to empty string	
    text += '<TABLE class=\'tabCalendario\' CELLSPACING=1>' // table settings
    text += '<caption>' // create table header cell
    text += "<span id='nomeMese'>" + monthName + ' ' + year + "</span>"
    text += '</caption>' // close header cell
    //window.alert(text);
    
    // variables to hold constant settings
    var openCol = ''
    var closeCol = '</TH>'
    var lun = false;
    var mar = false;
    var mer = false;
    var gio = false;
    var ven = false;
    var sab = false;
    var dom = false;
    //
    if (days_mask.charAt(0) == '0')
        lun = true;
    if (days_mask.charAt(1) == '0')
        mar = true;
    if (days_mask.charAt(2) == '0')
        mer = true;
    if (days_mask.charAt(3) == '0')
        gio = true;
    if (days_mask.charAt(4) == '0')
        ven = true;
    if (days_mask.charAt(5) == '0')
        sab = true;
    if (days_mask.charAt(6) == '0')
        dom = true;
    //variabile per disabilitare i giorni della settimana
    var bDisable = false;

    // create first row of table to set column width and specify week day
    text += '<TR ALIGN="center" VALIGN="center">'
    for (var dayNum = 0; dayNum < 7; ++dayNum) {
        switch (dayNum) {
            case 6:
                {
                    openCol = '<TH class="sab" >'
                    break
                }
            case 0:
                {
                    openCol = '<TH class="dom" >'
                    break
                }
            default:
                {
                    openCol = '<TH>'
                }
        }
        text += openCol + weekDay[dayNum] + closeCol

    }
    text += '</TR>'

    // declaration and initialization of two variables to help with tables
    var digit = 1
    var curCell = 1
    //	
    for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
        text += '<TR ALIGN="right" VALIGN="top">'
        for (var col = 1; col <= 7; ++col) {
            switch (col) {
                case 7:
                    {
                        openCol = 'class="sab"'
                        if (sab)
                            bDisable = true;
                        break
                    }
                case 1:
                    {
                        openCol = 'class="dom"'
                        if (dom)
                            bDisable = true;
                        break
                    }
                default:
                    {
                        if ((col == 2 && lun) || (col == 3 && mar) || (col == 4 && mer) || (col == 5 && gio) || (col == 6 && ven))
                            bDisable = true;
                        openCol = 'class="oldays"'
                    }
            }
            if (bDisable) 
            {
                if (digit > lastDate)
                    break;
                else if (curCell < firstDay) 
                {
                    text += '<TD></TD>';
                    curCell++
                }
                else
                {
                    text += '<TD style="cursor:default!important" ' + openCol + '> ' + digit + '</TD>'
                    curCell++
                    bDisable = false;
                    digit++;
                }   
            }
            else {
                if (digit > lastDate)
                    break
                if (curCell < firstDay) {
                    text += '<TD></TD>';
                    bDisable = false;
                    curCell++
                } else {

                    var DATAnow = new Date()
                    var annoN = DATAnow.getFullYear()
                    var meseN = DATAnow.getMonth()
                    meseN++
                    var giornoN = DATAnow.getDate()

                    var dataInizioVoli = new Date(dtStart);
                    var dataFineVoli = new Date(dtEnd);
                    var data = new Date(anno +'/' + mese + '/' + digit);
					var DATAnow2 = new Date(DATAnow.getFullYear() +'/' + DATAnow.getMonth() + '/' + DATAnow.getDate());
                    //
					//alert(data);
					//alert(DATAnow2);
                    if(data >= DATAnow2 && data >= dataInizioVoli && data <= dataFineVoli)
                    {
                        if (dep_or_ret == 1) 
                        {
                            if ((memAnno1 == anno) && (memMese1 == mese) && (memGiorno1 == digit))   //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day
                            {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            }
                            else if ((memAnno2 == anno) && (memMese2 == mese) && (memGiorno2 == digit))    //
                            {
                                if (searchType == 'F') {
                                    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px;background-color:#ED3E13!important;color:#000!important" href="#" onclick="injData(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                                }
                                else {
                                    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                                }
                            }
                            else {
                                text += '<TD ' + openCol + ' style="text-align:center;"	><a style="font-size:10px;" href="#" onclick="injData(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                            }
                        }
                        else if (dep_or_ret == 2)   //if they clicked the return calendar
                        {
                            if ((memAnno2 == anno) && (memMese2 == mese) && (memGiorno2 == digit))   //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day
                            {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            }
                            //added this 'else if' to show the depart date on the return calendar.
                            else if ((memAnno1 == anno) && (memMese1 == mese) && (memGiorno1 == digit))   //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day
                            {
                                if (searchType == 'F') {
                                    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important" href="#" onclick="injData(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                                }
                                else {
                                    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                                }
                            }
                            else {
                                text += '<TD ' + openCol + ' style="text-align:center;"	><a style="font-size:10px;" href="#" onclick="injData(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                            }
                        }
                    }
                    else
                        text += '<TD style="cursor:default!important" ' + openCol + '> ' + digit + '</TD>'
                digit++
                }
                
            }
        }
        text += '</TR>'
    }

    text += '</TABLE>'
    //window.alert(text);
    document.getElementById(target).innerHTML = text


}

//render the top month in the calendar
function drawCal(firstDay, lastDate, date, monthName, year, target, dep_or_ret, searchType) {
    // create basic table structure
    var text = "" // initialize accumulative variable to empty string	
    text += '<TABLE class=\'tabCalendario\' CELLSPACING=1>' // table settings
    text += '<caption>' // create table header cell
    text += "<span id='nomeMese'>" + monthName + ' ' + year + "</span>"
    text += '</caption>' // close header cell
    //window.alert(text);

    // variables to hold constant settings
    var openCol = ''
    var closeCol = '</TH>'


    // create first row of table to set column width and specify week day
    text += '<TR ALIGN="center" VALIGN="center">'
    for (var dayNum = 0; dayNum < 7; ++dayNum) {
        switch (dayNum) {
            case 6:
                {
                    openCol = '<TH class="sab" >'
                    break
                }
            case 0:
                {
                    openCol = '<TH class="dom" >'
                    break
                }
            default:
                {
                    openCol = '<TH>'
                }
        }
        text += openCol + weekDay[dayNum] + closeCol

    }
    text += '</TR>'

    // declaration and initialization of two variables to help with tables
    var digit = 1
    var curCell = 1
    //	alert ("g" +memGiorno + " m" +memMese+ " a" +memAnno)

    for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
        text += '<TR ALIGN="right" VALIGN="top">'
        for (var col = 1; col <= 7; ++col) {
            switch (col) {
                case 7:
                    {
                        openCol = 'class="sab"'
                        break
                    }
                case 1:
                    {
                        openCol = 'class="dom"'
                        break
                    }
                default:
                    {
                        openCol = 'class="oldays"'
                    }
            }
            if (digit > lastDate)
                break
            if (curCell < firstDay) {
                text += '<TD></TD>';
                curCell++
            } else {

                var DATAnow = new Date()
                var annoN = DATAnow.getFullYear()
                var meseN = DATAnow.getMonth()
                meseN++
                var giornoN = DATAnow.getDate()

                if ((anno > annoN) || ((anno == annoN) && (mese > meseN)) || ((anno == annoN) && (mese == meseN) && (giornoN <= digit))) {
                    /*if (DE[0]==digit && DE[1]=mese && DE[2]=anno)
                    text += '<TD '+openCol+'><a style="font-size:10px;" href="seleziona il giorno" onclick="injData(this,\'destinatario\');document.getElementById(\'calendar\').style.display=\'none\';return false;"><strong>' + digit + '</strong></a></TD>'
                    else			*/
                    /*alert(date)
                    alert(monthName )
                    alert(year)*/

                    if (dep_or_ret == 1) //if they clicked the depart calendar
                    {
                        if ((memAnno1 == anno) && (memMese1 == mese) && (memGiorno1 == digit))   //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day
                        {
                            text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                        }
                        else if ((memAnno2 == anno) && (memMese2 == mese) && (memGiorno2 == digit))    //
                        {
                            if (searchType == 'F') {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px;background-color:#ED3E13!important;color:#000!important" href="#" onclick="injData(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                            }
                            else {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            }
                        }
                        else {
                            text += '<TD ' + openCol + ' style="text-align:center;"	><a style="font-size:10px;" href="#" onclick="injData(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                        }
                    }
                    else if (dep_or_ret == 2)   //if they clicked the return calendar
                    {
                        if ((memAnno2 == anno) && (memMese2 == mese) && (memGiorno2 == digit))   //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day
                        {
                            text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                        }
                        //added this 'else if' to show the depart date on the return calendar.
                        else if ((memAnno1 == anno) && (memMese1 == mese) && (memGiorno1 == digit))   //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day
                        {
                            if (searchType == 'F') {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important" href="#" onclick="injData(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                            }
                            else {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            }
                        }
                        else {
                            text += '<TD ' + openCol + ' style="text-align:center;"	><a style="font-size:10px;" href="#" onclick="injData(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                        }
                    }
                }
                else
                    text += '<TD style="cursor:default!important" ' + openCol + '> ' + digit + '</TD>'
                digit++
            }
        }
        text += '</TR>'
    }

    text += '</TABLE>'
    //window.alert(text);
    document.getElementById(target).innerHTML = text


}


//render the bottom month in the calendar
function drawCal2(firstDay, lastDate, date, monthName, year, target, dep_or_ret, searchType) {
    searchProductType = searchType;

    // create basic table structure
    var text = "" // initialize accumulative variable to empty string
    text += '<TABLE class=\'tabCalendario\' CELLSPACING=1>' // table settings
    text += '<caption>' // create table header cell

    text += "<span id='nomeMese2'>" + monthName + ' ' + year + "</span>"
    text += '</caption>' // close header cell

    // variables to hold constant settings
    var openCol = ''
    var closeCol = '</TH>'


    // create first row of table to set column width and specify week day
    text += '<TR ALIGN="center" VALIGN="center">'
    for (var dayNum = 0; dayNum < 7; ++dayNum) {
        switch (dayNum) {
            case 6:
                {
                    openCol = '<TH class="sab" >'
                    break
                }
            case 0:
                {
                    openCol = '<TH class="dom" >'
                    break
                }
            default:
                {
                    openCol = '<TH>'
                }
        }
        text += openCol + weekDay[dayNum] + closeCol

    }
    text += '</TR>'

    // declaration and initialization of two variables to help with tables
    var digit = 1
    var curCell = 1

    for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
        text += '<TR ALIGN="right" VALIGN="top">'
        for (var col = 1; col <= 7; ++col) {
            switch (col) {
                case 7:
                    {
                        openCol = 'class="sab"'
                        break
                    }
                case 1:
                    {
                        openCol = 'class="dom"'
                        break
                    }
                default:
                    {
                        openCol = 'class="oldays"'
                    }
            }
            if (digit > lastDate)
                break
            if (curCell < firstDay) {
                text += '<TD></TD>';
                curCell++
            } else {

                var DATAnow = new Date()
                var annoN = DATAnow.getFullYear()
                var meseN = DATAnow.getMonth()
                meseN++
                var giornoN = DATAnow.getDate()

                if ((anno > annoN) || ((anno == annoN) && (mese > meseN)) || ((anno == annoN) && (mese == meseN))) 
                {
                    //alert ("g" +digit +" XG" +memGiorno +" m" +mese + " XM" +memMese+" a" +anno +" XA" +memAnno)

                    if (dep_or_ret == 1) //if they clicked the depart calendar
                    {
                        if ((memAnno1 == anno) && (memMese1 == mese + 1) && (memGiorno1 == digit)) {
                            text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            //alert("evidenzio il "+digit)
                        }
                        else if ((memAnno2 == anno) && (memMese2 == mese + 1) && (memGiorno2 == digit)) {
                            if (searchType == 'F') {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important" href="#" onclick="injData2(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                            }
                            else {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                                //alert("evidenzio il "+digit)
                            }
                        }
                        else {
                            text += '<TD ' + openCol + ' style="text-align:center;"	><a style="font-size:10px;" href="#" onclick="injData2(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                        }
                    }
                    else if (dep_or_ret == 2) {
                        if ((memAnno2 == anno) && (memMese2 == mese + 1) && (memGiorno2 == digit)) {
                            text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            //alert("evidenzio il "+digit)
                        }
                        //added this 'else if' to show the depart date on the return calendar.
                        else if ((memAnno1 == anno) && (memMese1 == mese + 1) && (memGiorno1 == digit))   //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day
                        {
                            if (searchType == 'F') {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important" href="#" onclick="injData2(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                            }
                            else {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            }
                        }
                        else {
                            text += '<TD ' + openCol + ' style="text-align:center;"	><a style="font-size:10px;" href="#" onclick="injData2(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                        }
                    }
                }
                else
                    text += '<TD style="cursor:default!important" ' + openCol + '> ' + digit + '</TD>'
                digit++
            }
        }
        text += '</TR>'
    }

    text += '</TABLE>'

    if (dep_or_ret == 1) {
        text += "<div style='position:absolute; top:270px; left:0px'><a class='sx' href='javascript:previousMonth(2, 1)'>" + previousLabel + "</a><a class='dx' href='javascript:nextMonth(2, 1)'>" + nextLabel + "</a></div>"
    }
    else if (dep_or_ret == 2) {
        text += "<div style='position:absolute; top:270px; left:0px'><a class='sx' href='javascript:previousMonth(2, 2)'>" + previousLabel + "</a><a class='dx' href='javascript:nextMonth(2, 2)'>" + nextLabel + "</a></div>"
    }
    document.getElementById(target).innerHTML = text

}

function drawCal2_Carnet(firstDay, lastDate, date, monthName, year, target, dep_or_ret, searchType, days_mask, dtStart, dtEnd) {

    searchProductType = searchType;
    //
    days_mask += '';
    dtStart += '';
    dtEnd += '';
    // create basic table structure
    var text = "" // initialize accumulative variable to empty string
    text += '<TABLE class=\'tabCalendario\' CELLSPACING=1>' // table settings
    text += '<caption>' // create table header cell

    text += "<span id='nomeMese2'>" + monthName + ' ' + year + "</span>"
    text += '</caption>' // close header cell

    // variables to hold constant settings
    var openCol = ''
    var closeCol = '</TH>'
    var lun = false;
    var mar = false;
    var mer = false;
    var gio = false;
    var ven = false;
    var sab = false;
    var dom = false;
    //
    if (days_mask.charAt(0) == '0')
        lun = true;
    if (days_mask.charAt(1) == '0')
        mar = true;
    if (days_mask.charAt(2) == '0')
        mer = true;
    if (days_mask.charAt(3) == '0')
        gio = true;
    if (days_mask.charAt(4) == '0')
        ven = true;
    if (days_mask.charAt(5) == '0')
        sab = true;
    if (days_mask.charAt(6) == '0')
        dom = true;
    //variabile per disabilitare i giorni della settimana
    var bDisable = false;

    // create first row of table to set column width and specify week day
    text += '<TR ALIGN="center" VALIGN="center">'
    for (var dayNum = 0; dayNum < 7; ++dayNum) {
        switch (dayNum) {
            case 6:
                {
                    openCol = '<TH class="sab" >'
                    break
                }
            case 0:
                {
                    openCol = '<TH class="dom" >'
                    break
                }
            default:
                {
                    openCol = '<TH>'
                }
        }
        text += openCol + weekDay[dayNum] + closeCol

    }
    text += '</TR>'

    // declaration and initialization of two variables to help with tables
    var digit = 1
    var curCell = 1

    for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
        text += '<TR ALIGN="right" VALIGN="top">'
        for (var col = 1; col <= 7; ++col) {
            switch (col) {
                case 7:
                    {
                        openCol = 'class="sab"'
                        if (sab)
                            bDisable = true;
                        break
                    }
                case 1:
                    {
                        openCol = 'class="dom"'
                        if (dom)
                            bDisable = true;
                        break
                    }
                default:
                    {
                        if ((col == 2 && lun) || (col == 3 && mar) || (col == 4 && mer) || (col == 5 && gio) || (col == 6 && ven))
                            bDisable = true;
                        //
                        openCol = 'class="oldays"'
                    }
            }
            if (bDisable) 
            {
                if (digit > lastDate)
                    break;
                else if (curCell < firstDay) 
                {
                    text += '<TD></TD>';
                    bDisable = false;
                    curCell++
                }
                else
                {
                    text += '<TD style="cursor:default!important" ' + openCol + '> ' + digit + '</TD>'
                    curCell++;
                    bDisable = false;
                    digit++;
                }    
            }
            else {
                if (digit > lastDate)
                    break
                if (curCell < firstDay) {
                    text += '<TD></TD>';
                    curCell++
                } else {

                    var DATAnow = new Date()
                    var annoN = DATAnow.getFullYear()
                    var meseN = DATAnow.getMonth()
                    meseN++
                    var giornoN = DATAnow.getDate()

                    var dataInizioVoli = new Date(dtStart);
                    var dataFineVoli = new Date(dtEnd);
                    //
                    var meseNext = mese + 1;
                    if(meseNext == 13)
                        meseNext = 1;
                    //
                    var data = new Date(anno +'/' + meseNext + '/' + digit);
                    //
                    if(data >= DATAnow && data >= dataInizioVoli && data <= dataFineVoli)
                    {
                        //alert ("g" +digit +" XG" +memGiorno +" m" +mese + " XM" +memMese+" a" +anno +" XA" +memAnno)

                        if (dep_or_ret == 1) //if they clicked the depart calendar
                        {
                            if ((memAnno1 == anno) && (memMese1 == mese + 1) && (memGiorno1 == digit)) {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                                //alert("evidenzio il "+digit)
                            }
                            else if ((memAnno2 == anno) && (memMese2 == mese + 1) && (memGiorno2 == digit)) {
                                if (searchType == 'F') {
                                    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important" href="#" onclick="injData2(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                                }
                                else {
                                    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                                    //alert("evidenzio il "+digit)
                                }
                            }
                            else {
                                text += '<TD ' + openCol + ' style="text-align:center;"	><a style="font-size:10px;" href="#" onclick="injData2(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                            }
                        }
                        else if (dep_or_ret == 2) {
                            if ((memAnno2 == anno) && (memMese2 == mese + 1) && (memGiorno2 == digit)) {
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                                //alert("evidenzio il "+digit)
                            }
                            //added this 'else if' to show the depart date on the return calendar.
                            else if ((memAnno1 == anno) && (memMese1 == mese + 1) && (memGiorno1 == digit))   //backround colors below set the color of the selected day. 'memGiorno' is the selected dep day
                            {
                                if (searchType == 'F') {
                                    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important" href="#" onclick="injData2(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                                }
                                else {
                                    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                                }
                            }
                            else {
                                text += '<TD ' + openCol + ' style="text-align:center;"	><a style="font-size:10px;" href="#" onclick="injData2(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;">' + digit + '</a></TD>'
                            }
                        }
                    }
                    else
                        text += '<TD style="cursor:default!important" ' + openCol + '> ' + digit + '</TD>'
                    digit++;    
                }
                
            }
        }
        text += '</TR>'
    }

    text += '</TABLE>'

    if (dep_or_ret == 1) {
        text += "<div style='position:absolute; top:270px; left:0px'><a class='sx' href='javascript:previousMonth_Carnet(2, 1, \"" + days_mask + "\",\"" + dtStart + "\",\"" + dtEnd + "\")'>" + previousLabel + "</a><a class='dx' href='javascript:nextMonth_Carnet(2, 1, \"" + days_mask + "\",\"" + dtStart + "\",\"" + dtEnd + "\")'>" + nextLabel + "</a></div>"
    }
    else if (dep_or_ret == 2) {
        text += "<div style='position:absolute; top:270px; left:0px'><a class='sx' href='javascript:previousMonth_Carnet(2, 2, \"" + days_mask + "\",\"" + dtStart + "\",\"" + dtEnd + "\")'>" + previousLabel + "</a><a class='dx' href='javascript:nextMonth_Carnet(2, 2, \"" + days_mask + "\",\"" + dtStart + "\",\"" + dtEnd + "\")'>" + nextLabel + "</a></div>"
    }
    document.getElementById(target).innerHTML = text

}


function previousMonth(perc, dep_ret) //move to previous month
{


    mese = mese - perc;

    if (mese == -1) //it was Jan when they hit back, display Nov & Dec
    {
        mese = 11;
        anno--;
    }
    else if (mese == 0)//it was Feb when they hit back, display Dec & Jan
    {
        mese = 12;
        anno--;
    }


    var ora = new Date();
    var meseora = ora.getMonth() + 1;
    //alert(mese + " " + meseora);
    if ((meseora - 1) == mese) {
        mese++;
        //alert(0);        
    }
    show_date = new Date(mese + "/6" + "/" + anno);
    now = new Date(mese + "/6" + "/" + anno);

    year = now.getFullYear();
    month = now.getMonth();
    monthName = getMonthName(month);
    date = now.getDate();
    now = null;
    firstDayInstance = new Date(year, month, 1);
    firstDay = firstDayInstance.getDay();
    firstDayInstance = null;
    days = getDays(month, year);

    monthName2 = getMonthName(month + 1);
    firstDayInstance2 = new Date(year, month + 1, 1);
    firstDay2 = firstDayInstance2.getDay();
    days2 = getDays(month + 1, year);

    drawCal(firstDay + 1, days, date, monthName, year, 'calNEW', dep_ret, searchProductType);
    document.getElementById('nomeMese').innerText = (months[mese - 1] + " " + anno); //change contents of month title

    var year2 = year;
    var anno2 = anno;

    if (mese == 12) {
        monthName2 = getMonthName(0);   //set to january
        firstDayInstance2 = new Date(year + 1, 0, 1); //increment year
        firstDay2 = firstDayInstance2.getDay();
        days2 = getDays(0, year + 1);   //get days for january

        year2 += 1;
        anno2 += 1;
    }

    drawCal2(firstDay2 + 1, days2, date, monthName2, year2, 'calNEW2', dep_ret, searchProductType);
    document.getElementById('nomeMese2').innerText = (getMonthName(mese) + " " + anno2); //change contents of month title

    year2 = year;
    anno2 = anno;
}


function previousMonth_Carnet(perc, dep_ret, days_mask, dtStart, dtEnd) //move to previous month
{


    mese = mese - perc;

    if (mese == -1) //it was Jan when they hit back, display Nov & Dec
    {
        mese = 11;
        anno--;
    }
    else if (mese == 0)//it was Feb when they hit back, display Dec & Jan
    {
        mese = 12;
        anno--;
    }


    var ora = new Date();
    var meseora = ora.getMonth() + 1;
    //alert(mese + " " + meseora);
    if ((meseora - 1) == mese) {
        mese++;
        //alert(0);        
    }
    show_date = new Date(mese + "/6" + "/" + anno);
    now = new Date(mese + "/6" + "/" + anno);

    year = now.getFullYear();
    month = now.getMonth();
    monthName = getMonthName(month);
    date = now.getDate();
    now = null;
    firstDayInstance = new Date(year, month, 1);
    firstDay = firstDayInstance.getDay();
    firstDayInstance = null;
    days = getDays(month, year);

    monthName2 = getMonthName(month + 1);
    firstDayInstance2 = new Date(year, month + 1, 1);
    firstDay2 = firstDayInstance2.getDay();
    days2 = getDays(month + 1, year);

    drawCal_Carnet(firstDay + 1, days, date, monthName, year, 'calNEW', dep_ret, searchProductType, days_mask, dtStart, dtEnd);
    document.getElementById('nomeMese').innerText = (months[mese - 1] + " " + anno); //change contents of month title

    var year2 = year;
    var anno2 = anno;

    if (mese == 12) {
        monthName2 = getMonthName(0);   //set to january
        firstDayInstance2 = new Date(year + 1, 0, 1); //increment year
        firstDay2 = firstDayInstance2.getDay();
        days2 = getDays(0, year + 1);   //get days for january

        year2 += 1;
        anno2 += 1;
    }

    drawCal2_Carnet(firstDay2 + 1, days2, date, monthName2, year2, 'calNEW2', dep_ret, searchProductType, days_mask, dtStart, dtEnd);
    document.getElementById('nomeMese2').innerText = (getMonthName(mese) + " " + anno2); //change contents of month title

    year2 = year;
    anno2 = anno;
}

function nextMonth(perc, dep_ret) //move to next month
{
    mese = mese + perc;

    if (mese == 14) //it was december when they hit next, display Feb & Mar
    {
        mese = 2;
        anno++;
    }
    else if (mese == 13) //it was november when they hit next, display Jan & Feb
    {
        mese = 1;
        anno++;
    }

    show_date = new Date(mese + "/6" + "/" + anno);
    now = new Date(mese + "/6" + "/" + anno);

    year = now.getFullYear();
    month = now.getMonth();
    monthName = getMonthName(month);
    date = now.getDate();
    now = null;
    firstDayInstance = new Date(year, month, 1);
    firstDay = firstDayInstance.getDay();
    firstDayInstance = null;
    days = getDays(month, year);

    monthName2 = getMonthName(month + 1);
    firstDayInstance2 = new Date(year, month + 1, 1);
    firstDay2 = firstDayInstance2.getDay();
    days2 = getDays(month + 1, year);

    drawCal(firstDay + 1, days, date, monthName, year, 'calNEW', dep_ret, searchProductType);
    document.getElementById('nomeMese').innerText = (months[mese - 1] + " " + anno); //change contents of month title
    //window.alert(months[mese-1]);
    var year2 = year;
    var anno2 = anno;
    var mese2 = mese;

    if (mese == 12) {
        monthName2 = getMonthName(0);   //set to january
        firstDayInstance2 = new Date(year + 1, 0, 1); //increment year
        firstDay2 = firstDayInstance2.getDay();
        days2 = getDays(0, year + 1);   //get days for january

        year2 += 1;
        anno2 += 1;
        mese2 = 0;
    }

    drawCal2(firstDay2 + 1, days2, date, monthName2, year2, 'calNEW2', dep_ret, searchProductType);
    document.getElementById('nomeMese2').innerText = (months[mese2] + " " + anno2); //change contents of month title

    year2 = year;
    anno2 = anno;
    mese2 = mese;
}

function nextMonth_Carnet(perc, dep_ret, days_mask, dtStart, dtEnd) //move to next month
{
    mese = mese + perc;

    if (mese == 14) //it was december when they hit next, display Feb & Mar
    {
        mese = 2;
        anno++;
    }
    else if (mese == 13) //it was november when they hit next, display Jan & Feb
    {
        mese = 1;
        anno++;
    }

    show_date = new Date(mese + "/6" + "/" + anno);
    now = new Date(mese + "/6" + "/" + anno);

    year = now.getFullYear();
    month = now.getMonth();
    monthName = getMonthName(month);
    date = now.getDate();
    now = null;
    firstDayInstance = new Date(year, month, 1);
    firstDay = firstDayInstance.getDay();
    firstDayInstance = null;
    days = getDays(month, year);

    monthName2 = getMonthName(month + 1);
    firstDayInstance2 = new Date(year, month + 1, 1);
    firstDay2 = firstDayInstance2.getDay();
    days2 = getDays(month + 1, year);

    drawCal_Carnet(firstDay + 1, days, date, monthName, year, 'calNEW', dep_ret, searchProductType, days_mask, dtStart, dtEnd);
    document.getElementById('nomeMese').innerText = (months[mese - 1] + " " + anno); //change contents of month title
    //window.alert(months[mese-1]);
    var year2 = year;
    var anno2 = anno;
    var mese2 = mese;

    if (mese == 12) {
        monthName2 = getMonthName(0);   //set to january
        firstDayInstance2 = new Date(year + 1, 0, 1); //increment year
        firstDay2 = firstDayInstance2.getDay();
        days2 = getDays(0, year + 1);   //get days for january

        year2 += 1;
        anno2 += 1;
        mese2 = 0;
    }

    drawCal2_Carnet(firstDay2 + 1, days2, date, monthName2, year2, 'calNEW2', dep_ret, searchProductType, days_mask, dtStart, dtEnd);
    document.getElementById('nomeMese2').innerText = (months[mese2] + " " + anno2); //change contents of month title

    year2 = year;
    anno2 = anno;
    mese2 = mese;
}

function injData(who, where, dep_or_ret) {
    //alert(eval(who));
    //alert(eval(where));
    //alert ("g" + DE[0] + " m" + DE[0]+ " a" +DE[0])
    var day = who.innerHTML;    //stores the day selected in the top month
    var month = "" + eval(mese); //convert to string
    var year = anno;

    //Pad the month and day is required	
    if (day.length == 1) day = '0' + day;
    if (month.length == 1) month = '0' + month;
    //where='txtDepartureDateTime';
    document.getElementById(eval(where)).value = day + "/" + (month) + "/" + year
    /*DE[0]=who.innerHTML
    DE[1]=mese
    DE[2]=anno
    */
    //alert(who.innerText+"/"+mese+"/"+anno)

    if (dep_or_ret == 1) {
        memGiorno1 = who.innerHTML;
        memMese1 = mese;
        memAnno1 = anno;
    }
    else if (dep_or_ret == 2) {
        memGiorno2 = who.innerHTML;
        memMese2 = mese;
        memAnno2 = anno;
    }


    //alert ("g" +memGiorno + " m" +memMese+ " a" +memAnno)
}
function injData2(who, where, dep_or_ret) {
    //alert(eval(who));

    var day = who.innerHTML;    //stores the day selected in the bottom month
    var month = "" + eval(mese + 1); //convert to string
    var year = anno;

    //Alan: above code doesnt allow for top month being december, results in bottom month being 13 instead of 1
    if (month == 13) {
        month = "" + eval(01);  //set to january of next year
        year++;
    }

    //Pad the month and day is required	
    if (day.length == 1) day = '0' + day;
    if (month.length == 1) month = '0' + month;

    document.getElementById(eval(where)).value = day + "/" + (month) + "/" + year

    if (dep_or_ret == 1) {
        memGiorno1 = who.innerHTML;
        memMese1 = mese + 1;
        memAnno1 = anno;
    }
    else if (dep_or_ret == 2) {
        memGiorno2 = who.innerHTML
        memMese2 = mese + 1
        memAnno2 = anno
    }

    //alert(memGiorno+"/"+memMese+"/"+memAnno)
}

function getTheDay(aText) {
    myDays = ["Domenica", "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi", "Sabato", "Domenica"]
    myDate = new Date(eval('"' + aText + '"'))
    document.write(myDays[myDate.getDay()]);
}



//Introdotta per compatibilità Gestione visualizzazione controlli
function mostraControlli() {
}


//Introdotta per compatibilità Gestione visualizzazione controlli
function nascondiControlli() {

}

var now = null;
var show_date = null;
var anno = null;
var mese = null;
var memorizzaGiorno = 0;

initCalendar();

function initCalendar() {

    // calendario
    now = new Date();
    show_date = new Date();
    anno = show_date.getFullYear();
    mese = show_date.getMonth() + 1;
}
// gestione calendario in NLS
var destinatario;


// stores the selected day/month/year for depart (1) and return (2)
var memGiorno1 = 0
var memMese1 = 0
var memAnno1 = 0
var memGiorno2 = 0
var memMese2 = 0
var memAnno2 = 0

//called when page 1st loads
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
//window.alert(month);
var monthName = getMonthName(month);
//window.alert("MESE:"+monthName);

var date = now.getDate();
now = null;
var firstDayInstance = new Date(year, month, 1);

var firstDay = firstDayInstance.getDay();

firstDayInstance = null;
var days = getDays(month, year);
var monthName2 = getMonthName(month + 1);
var firstDayInstance2 = new Date(year, month + 1, 1);
var firstDay2 = firstDayInstance2.getDay();
var days2 = getDays(month + 1, year);
var days3 = getDays(0, year + 1); //used when bottom panel displays january

function ridisegnaCalendari(dep_or_ret, searchType)   //called each time the calendar is opened. dep_or_ret is 1 (depart) or 2 (return)
{

    //window.alert("MESE:" +monthName2);
    //render top month
    //window.alert("MESE:"+monthName);
    //
    drawCal(firstDay + 1, days, date, monthName, year, 'calNEW', dep_or_ret, searchType);


    //render bottom month
    //if(monthName == "December" || monthName == "Dicembre")
    if (month == 11) //if the calendar is opening on december/january, set bottom cal year to year+1
    {
        drawCal2(firstDay2 + 1, days3, date, monthName2, year + 1, 'calNEW2', dep_or_ret, searchType);
    }
    else {
        drawCal2(firstDay2 + 1, days2, date, monthName2, year, 'calNEW2', dep_or_ret, searchType);
    }
}

function ridisegnaCalendari_Carnet(dep_or_ret, searchType, date_mask, dtStart, dtEnd)   //called each time the calendar is opened. dep_or_ret is 1 (depart) or 2 (return)
{

    //window.alert("MESE:" +monthName2);
    //render top month
    //window.alert("MESE:"+monthName);
    //
    drawCal_Carnet(firstDay + 1, days, date, monthName, year, 'calNEW', dep_or_ret, searchType, date_mask, dtStart, dtEnd);


    //render bottom month
    //if(monthName == "December" || monthName == "Dicembre")
    if (month == 11) //if the calendar is opening on december/january, set bottom cal year to year+1
    {
        drawCal2_Carnet(firstDay2 + 1, days3, date, monthName2, year + 1, 'calNEW2', dep_or_ret, searchType, date_mask, dtStart, dtEnd);
    }
    else {
        drawCal2_Carnet(firstDay2 + 1, days2, date, monthName2, year, 'calNEW2', dep_or_ret, searchType, date_mask, dtStart, dtEnd);
    }
}

  
