// var conDate is overwriten
// var conMinSearchDays is overwriten
var conDefaultDays = 1;
var conMaxSearchDays = 28;

var lblNights = 'txtNights';
var lblYearArrival = 'ddYearArrival';
var lblMonthArrival = 'ddMonthArrival';
var lblDayArrival = 'ddDayArrival';
var lblYearDeparture = 'ddYearDeparture';
var lblMonthDeparture = 'ddMonthDeparture';
var lblDayDeparture = 'ddDayDeparture';

function cb_GetElement(name)
{
    var ctrl = document.getElementById(name);
    if(null == ctrl)
    {
        ctrl = document.getElementsByName(name)[0];
    }
    
    return ctrl;
}

/***
 * Get the days in the current month
 */
function cb_getDaysInMonth(monthId, year)
{
    var d = new Date();

    d.setFullYear(year, 1, 1);
    // +1 for next month, +1 for zero-indexed
    d.setMonth(monthId);
    d.setDate(1);
    d.setDate(d.getDate() - 1);

    return d.getDate();
}

/****
 * Update the days so they fit the number of days in the current month selected
 */
function cb_updateDays(yearSel, monthSel, daySel)
{
    var noDays = cb_getDaysInMonth(monthSel.options[monthSel.selectedIndex].value, yearSel.options[yearSel.selectedIndex].value);
    
    if(daySel.length > noDays)
    {
        var selInd = daySel.selectedIndex;
        // more days populated than there are in this month
        for(var n = daySel.length; n > noDays; n--)
        {
            daySel.options[n - 1] = null;
        }
        if(selInd >= daySel.length)
        {
            daySel.selectedIndex = daySel.length - 1;
        }
    }
    else if(daySel.length < noDays)
    {
        // less days populated than there are days in month
        for(var n = daySel.length; n < noDays; n++)
        {
            daySel.options[n] = new Option(n + 1, n + 1);
        }
    }
    // else correct populated
}

/*****
 * Make sure the arrival date is a valid one
 */
function cb_forceConsistArrival(ctrlDays, ctrlMonth, ctrlYear)
{
    var testDate = new Date(ctrlYear.options[ctrlYear.selectedIndex].value, 
                            ctrlMonth.options[ctrlMonth.selectedIndex].value - 1,
                            ctrlDays.options[ctrlDays.selectedIndex].value);
    if(cb_daysDif(testDate, conDate) < 0)
    {
        cb_setOptionDates(ctrlYear, ctrlMonth, ctrlDays, conDate);
    }
}

/*****
 * Get the number of days in the text box and correct if it is a bad one.
 */
function cb_getBDays()
{
    var txtNight = cb_GetElement(lblNights);
    
    var days = parseInt(txtNight.value);

    if(isNaN(days))
    {
        days = conDefaultDays;
        txtNight.value = days;
    }
    if(days < 1)
    {
        days = conDefaultDays;
        txtNight.value = days;
    }
    if(days > conMaxSearchDays)
    {
        days = conMaxSearchDays;
        txtNight.value = days;
    }
    // v1.9.42 >>> minimum of days
    if(days < conMinSearchDays)
    {
        days = conMinSearchDays;
        txtNight.value = days;
    }
    // v1.9.42 <<<
    return days;
}


/****
 * Function sets the selectboxes into the date described by myDate
 */
function cb_setOptionDates(ctrlYear, ctrlMonth, ctrlDays, myDate)
{
    for(var n = 0; n < ctrlYear.options.length; n++)
    {
        if(ctrlYear.options[n].value == myDate.getFullYear())
        {
            ctrlYear.selectedIndex = n;
        }
    }
    // Month already at an index lower
    ctrlMonth.selectedIndex = myDate.getMonth();

    cb_updateDays(ctrlYear, ctrlMonth, ctrlDays);
    ctrlDays.selectedIndex = myDate.getDate() - 1;
}

/*****
 * Return the number of days that differs the two dates
 * negative if date2 is greater than date1
 */
function cb_daysDif(date1, date2)
{
    // Number is number of milliseconds in one day.
    return Math.round((date1 - date2) / 86400000);
}

/*****
 * Function called when there are updates in the textbox
 */
function cb_OnDaysChange()
{

    var arrYear = cb_GetElement(lblYearArrival);
    var arrMonth = cb_GetElement(lblMonthArrival);
    var arrDay = cb_GetElement(lblDayArrival);

    cb_forceConsistArrival(arrDay, arrMonth, arrYear);
    
    var noDays = cb_getBDays();
    var theDate = new Date(arrYear.options[arrYear.selectedIndex].value,
        arrMonth.options[arrMonth.selectedIndex].value - 1,
        arrDay.options[arrDay.selectedIndex].value);
    theDate.setDate(theDate.getDate() + noDays);

    cb_setOptionDates(cb_GetElement(lblYearDeparture),
        cb_GetElement(lblMonthDeparture), 
        cb_GetElement(lblDayDeparture), theDate);
}

/*****
 * Function triggered whenever departure date is changed.
 */
function cb_OnChangeDeparture()
{
    var ctrlArrDays = cb_GetElement(lblDayArrival);
    var ctrlArrMonth = cb_GetElement(lblMonthArrival);
    var ctrlArrYear = cb_GetElement(lblYearArrival);
    
    var ctrlDepDays = cb_GetElement(lblDayDeparture);
    var ctrlDepMonth = cb_GetElement(lblMonthDeparture);
    var ctrlDepYear = cb_GetElement(lblYearDeparture);

    var txtDays = cb_GetElement(lblNights);

    var dateDeparture = new Date(ctrlDepYear.options[ctrlDepYear.selectedIndex].value, 
        ctrlDepMonth.options[ctrlDepMonth.selectedIndex].value - 1,
        ctrlDepDays.options[ctrlDepDays.selectedIndex].value);
    var dateArrival = new Date(ctrlArrYear.options[ctrlArrYear.selectedIndex].value,
        ctrlArrMonth.options[ctrlArrMonth.selectedIndex].value - 1,
        ctrlArrDays.options[ctrlArrDays.selectedIndex].value);
    if(cb_daysDif(dateDeparture, dateArrival) > 0)
    {
        // Date after arrival, update number in textbox

        var days = cb_daysDif(dateDeparture, dateArrival);
        if(days > conMaxSearchDays)
        {
            days = conMaxSearchDays;
        }
        // v1.9.42 >>> minimum of days
        if(days < conMinSearchDays) {
            days = conMinSearchDays;
        }
        // v1.9.42 <<<

        var arrDate = new Date(dateDeparture.getFullYear(), dateDeparture.getMonth(), dateDeparture.getDate());
        arrDate.setDate(arrDate.getDate() - days);
        txtDays.value = days;

        cb_setOptionDates(ctrlArrYear, ctrlArrMonth, ctrlArrDays, arrDate);

        cb_updateDays(ctrlDepYear, ctrlDepMonth, ctrlDepDays);
    }
    else
    {
        // Date before arrival, move date arrival back textbox days
        var arriDate;
        var intNoDays = cb_getBDays();
        if(intNoDays > conMaxSearchDays)
        {
            intNoDays = conMaxSearchDays;
        }
        arriDate = new Date(dateDeparture.getFullYear(), dateDeparture.getMonth(), dateDeparture.getDate());
        arriDate.setDate(arriDate.getDate() - intNoDays);

        if(cb_daysDif(arriDate, conDate) < 0)
        {
            // We are beyond acceptable dates
            cb_setOptionDates(ctrlArrYear, ctrlArrMonth, ctrlArrDays, conDate);
            dateDeparture = new Date(conDate.getFullYear(), conDate.getMonth(), conDate.getDate());
            dateDeparture.setDate(dateDeparture.getDate() + 1);
            txtDays.value = 1;
            cb_setOptionDates(ctrlDepYear, ctrlDepMonth, ctrlDepDays, dateDeparture);
        }
        else
        {
            txtDays.value = cb_daysDif(dateDeparture, arriDate);
            cb_setOptionDates(ctrlArrYear, ctrlArrMonth, ctrlArrDays, arriDate); 
        }
    }
}

/*****
 * Function called whenever there are updated in the arrival date
 */
function cb_OnChangeArrival()
{
    var ctrlDays = cb_GetElement(lblDayArrival);
    var ctrlMonth = cb_GetElement(lblMonthArrival);
    var ctrlYear = cb_GetElement(lblYearArrival);

    var intDays = cb_getBDays();

    cb_forceConsistArrival(ctrlDays, ctrlMonth, ctrlYear);

    cb_updateDays(ctrlYear, ctrlMonth, ctrlDays);

    var dte = new Date(ctrlYear.options[ctrlYear.selectedIndex].value, 
                            ctrlMonth.options[ctrlMonth.selectedIndex].value - 1,
                            ctrlDays.options[ctrlDays.selectedIndex].value);
    dte.setDate(dte.getDate() + intDays);

    cb_setOptionDates(cb_GetElement(lblYearDeparture), 
        cb_GetElement(lblMonthDeparture), 
        cb_GetElement(lblDayDeparture), dte);
}

