//var dataodierna = new Date();
//var ggodierdo = dataodierna.getDate();
//var meseodierno = dataodierna.getMonth() + 1;
//var annoodierno = dataodierna.getYear() + 1900;

//alert(ggodierdo + " " + meseodierno + " " + annoodierno);


//var selezione = false;

var valoredataattuale;
function trimNumber(s) {
    while (s.substr(0, 1) == '0' && s.length > 1) { s = s.substr(1, 9999); }
    return s;
}

function settadata() {
    try {
        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();
        }
    }
    catch (e) {
    }
}

function gestisciCalendario(memDay, dest, index, searchType,davolo) {
    
    //alert(index);
    //    months=getMonthName2();
    //    weekDay=SetgetweekDay();
    memorizzaGiorno = memDay;
    //window.alert(memorizzaGiorno);
    destinatario = dest;
    document.getElementById('cal1').style.display = 'inline';

    valoredataattuale = document.getElementById(dest).value;
    settadata();


    var nomectrl = "";
    if (document.getElementById("ctl00_ContentPlaceHolder1_ucSearchMenu1_embeddedono") != null)
        nomectrl = "ctl00_ContentPlaceHolder1_ucSearchMenu1_embeddedono";
    if (document.getElementById("ucSearchMenu1_embeddedono") != null)
        nomectrl = "ucSearchMenu1_embeddedono";
    if (document.getElementById("ctl00_ContentPlaceHolder1_QuickSearch1_embeddedono") != null)
        nomectrl = "ctl00_ContentPlaceHolder1_QuickSearch1_embeddedono";

    //alert(document.getElementById(nomectrl).value);

    if (nomectrl != "")
        if (document.getElementById(nomectrl).value == "no") {
        //alert(nomectrl);
        document.getElementById('cal1').style.top = document.getElementById(dest).offsetTop + 'px';
        document.getElementById('cal1').style.left = (20 + document.getElementById(dest).offsetWidth + document.getElementById(dest).offsetLeft) + 'px';
    }


    ridisegnaCalendari(index, searchType,davolo);
}

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];
    }
}

//ma dove è usata ????? boh ALEX
/*
function setCal(tg, davolo) {
    // 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,davolo)
}
*/

//render the top month in the calendar
function drawCal(firstDay, lastDate, date, monthName, year, target, dep_or_ret, searchType, davolo) {
    //alert(88);
    //selezione = false;
    //alert(firstDay + " " + lastDate + " " + davolo);

    //alert(firstDay + "*" + lastDate+ "*" +date+ "*" + monthName+ "*" +year+ "*" +target+ "*" + dep_or_ret+ "*" + searchType);
    // constant table settings
    //var headerHeight = 50 // height of the table's header cell
    //var border = 2 // 3D height of table's border
    //var cellspacing = 4 // width of table's border
    //var headerColor = "midnightblue" // color of table's header
    //var headerSize = "+3" // size of tables header font
    //var colWidth = 60 // width of columns in table
    //var dayCellHeight = 25 // height of cells containing days of the week
    //var dayColor = "darkblue" // color of font representing week days
    //var cellHeight = 40 // height of cells representing dates in the calendar
    //var todayColor = "red" // color specifying today's date in the calendar
    //var timeColor = "purple" // color of font representing current time
    //window.alert(firstDay, lastDate, date, monthName, year, target, dep_or_ret, searchType);
    searchProductType = searchType;
    //window.alert(searchType);
    //used to re-display hidden elements of hotel search page - but in Java!!!
    //var displayBlock = '';
    //if(searchType == 'hotel'){
    //	displayBlock = 	"document.getElementById(\'city\').style.display=\'block\';";
    //}
    //else if(searchType == 'air'){
    //	displayBlock = 	"document.getElementById(\'departAirport\').style.display=\'inline\';document.getElementById(\'numberChild\').style.display=\'inline\';document.getElementById(\'numberInfant\').style.display=\'inline\';";
    //}

    // 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()

                var ggdacontrollare = true;
                if (davolo == 'F') 
                {
                    ggdacontrollare = (giornoN < digit);
                }
                else 
                {
                    ggdacontrollare = (giornoN <= digit);
                }


                if ((anno > annoN) || ((anno == annoN) && (mese > meseN)) || ((anno == annoN) && (mese == meseN) && ggdacontrollare)) { //(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
                        {
                            //alert(memGiorno1 + " " + digit);
                            //permetto di cliccare ancora il cal selezionato...ALEX per il solovolo
                            //alert(davolo);
                            //if (davolo == 'F')
                            //    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a              href="cal_closeCal" onclick="injData(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;"              style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            //else
                            //if (selezione == false)
                            //    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
                            //    alert(8);

                            // 07-05-2010 Abilito il clicksul giorno corrente Angelo
                            //alert(davolo + '  ' + searchType);
                            if (davolo.indexOf("F") != -1)
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a href="javascript:void(0);" onclick="injData(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;"       style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + 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 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="cal_closeCal" 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="javascript:void(0);" 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
                        {
                            //alert(memGiorno1 + " " + digit);
                            //permetto di cliccare ancora il cal selezionato...ALEX per il solovolo
                            //alert(davolo);
                            //if (davolo == 'F')
                            //    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a    href="javascript:void(0);" onclick="injData(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;"    style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            //else
                            //if (selezione == false)
                            //    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
                            //    alert(8);

                            // 07-05-2010 Abilito il clicksul giorno corrente Angelo
                            //alert(davolo + '  ' + searchType);
                            if (davolo.indexOf("F") != -1)
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a href="javascript:void(0);" onclick="injData(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;"       style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + 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>'

                                
                        }
                        //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="javascript:void(0);" 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="javascript:void(0);" 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, davolo) {
    //selezione = false;
    //alert (anno + "*" + annoN + "*" + anno+ "*" + annoN+ "*" + mese+ "*" + meseN+ "*" + anno+ "*" + annoN+ "*" + mese+ "*" + meseN);
    // constant table settings
    //var headerHeight = 50 // height of the table's header cell
    //var border = 2 // 3D height of table's border
    //var cellspacing = 4 // width of table's border
    //var headerColor = "midnightblue" // color of table's header
    //var headerSize = "+3" // size of tables header font
    //var colWidth = 60 // width of columns in table
    //var dayCellHeight = 25 // height of cells containing days of the week
    //var dayColor = "darkblue" // color of font representing week days
    //var cellHeight = 40 // height of cells representing dates in the calendar
    //var todayColor = "red" // color specifying today's date in the calendar
    //var timeColor = "purple" // color of font representing current time

    searchProductType = searchType;

    //used to re-display hidden elements of hotel search page - but in Java!!!
    //var displayBlock = '';
    //if(searchType == 'hotel'){
    //	displayBlock = 	"document.getElementById(\'city\').style.display=\'block\';";
    //}
    //else if(searchType == 'air'){
    //		displayBlock = 	"document.getElementById(\'departAirport\').style.display=\'inline\';document.getElementById(\'numberChild\').style.display=\'inline\';document.getElementById(\'numberInfant\').style.display=\'inline\';";
    //}

    // 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)) 
                        {
                            //alert(memGiorno1 + " " + digit);
                            //permetto di cliccare ancora il cal selezionato...ALEX per il solovolo
                            //alert(davolo);
                            //if (davolo == 'F')
                            //    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a        href="javascript:void(0);" onclick="injData2(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;"       style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                            //else
                            //if (selezione == false)
                            //    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
                            //    alert(8);

                            //alert("evidenzio il "+digit)

                            // 07-05-2010 Abilito il clicksul giorno corrente Angelo
                           // alert(davolo + '  ' + searchType);
                            if (davolo.indexOf("F") != -1)
                                text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a href="javascript:void(0);" onclick="injData2(this,\'destinatario\', 1);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;"       style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + 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 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="javascript:void(0);" 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="javascript:void(0);" 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)) {
                        //alert(memGiorno1 + " " + digit);
                        //permetto di cliccare ancora il cal selezionato...ALEX per il solovolo
                        //alert(davolo);
                        //if (davolo == 'F')
                        //    text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a       href="javascript:void(0);" onclick="injData2(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;"        style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + digit + '</a></TD>'
                        //else
                        //if (selezione == false)
                        //    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
                        //    alert(8);

                        //alert("evidenzio il "+digit)

                        // 07-05-2010 Abilito il clicksul giorno corrente Angelo
                        //alert(davolo + '  ' + searchType);
                        if (davolo.indexOf("F") != -1)
                            text += '<TD ' + openCol + ' style="text-align:center;background-color:#ED3E13!important"><a href="javascript:void(0);" onclick="injData2(this,\'destinatario\', 2);document.getElementById(\'cal1\').style.display=\'none\'; mostraControlli(); return false;"       style="font-size:10px; background-color:#ED3E13!important;color:#000!important">' + 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>'

                        }
                        //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="javascript:void(0);" 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="javascript:void(0);" 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>'

    ///alert(davolo);
    if (dep_or_ret == 1) {
        text += "<div class='div_sx'><a class='sx' href='javascript:previousMonth(2, 1,\"" + davolo + "\")'>" + previousLabel + "</a><a class='dx' href='javascript:nextMonth(2, 1, \"" + davolo + "\")'>" + nextLabel + "</a></div>"
    }
    else if (dep_or_ret == 2) {
        //text += "<div class='div_dx'><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;

    if (document.getElementById('calfondo2') != null)
        document.getElementById('calfondo2').innerHTML = "<div class='div_dx'><a class='sx' href='javascript:previousMonth(2, 2 , \"" + davolo + "\")'>" + previousLabel + "</a><a class='dx' href='javascript:nextMonth(2, 2,\"" + davolo + "\")'>" + nextLabel + "</a></div>";
}


function previousMonth(perc, dep_ret,davolo) //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,davolo);
    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,davolo);
    document.getElementById('nomeMese2').innerText = (getMonthName(mese) + " " + anno2); //change contents of month title

    year2 = year;
    anno2 = anno;
}

function nextMonth(perc, dep_ret,davolo) //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,davolo);
    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,davolo);
    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;




function initCalendar() {

    // calendario
    now = new Date();
    show_date = new Date();
    anno = show_date.getFullYear();
    mese = show_date.getMonth() + 1;
}

initCalendar();

// 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, davolo)   //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, davolo);


    //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, davolo);
    }
    else {
        drawCal2(firstDay2 + 1, days2, date, monthName2, year, 'calNEW2', dep_or_ret, searchType, davolo);
    }
}



  
