var calendarOn = false;



var DAY_IN_MILLIS = 86400000;
var ST_COMPENSATE_IN_MILLIS = 5400000;

function afterCalendarSelection(type, fromDate, toDate, numDays)
{
    if (calendarOn == true)
    {
        if (type == 'depart')
        {
            reflectDepartDateChange(fromDate, toDate, numDays);
        }
        else if (type == 'return')
        {
            calcNumDays(fromDate, toDate, numDays);
        }
        calendarOn = false;
    }
}

function reflectDepartDateChange(fromDate, toDate, numDays)
{
    var fromDateStr = fromDate.value;
    var toDateStr = toDate.value;
    
    var fromDay = fromDateStr.substring(0, 2);								
    var fromMonth = fromDateStr.substring(3, 5);    
    //bug in Javascript date object: months go from 0-11!
    var fromMonthFix = fromMonth - 1;    
    var fromYear = fromDateStr.substring(6);
    
    var toDay = toDateStr.substring(0, 2);
    var toMonth = toDateStr.substring(3, 5);
    //bug in Javascript date object: months go from 0-11!
    var toMonthFix = toMonth - 1;
    var toYear = toDateStr.substring(6);
    
    var calcFromDate = new Date(fromYear, fromMonthFix, fromDay);
    var calcToDate = new Date(toYear, toMonthFix, toDay);
    
    if (calcFromDate > calcToDate)
    {
        calcReturnDate(fromDate, numDays, toDate);
    }
    else
    {
        calcNumDaysUsingDates(calcFromDate, calcToDate, numDays);
    }
}

function calcNumDaysUsingDates(fromDate, toDate, numDays)
{
    var fromDateMillis = fromDate.valueOf();
    var toDateMillis = toDate.valueOf();
    
    var numDaysMillis = toDateMillis - fromDateMillis;
    if (numDaysMillis > 0)
    {
        //bugzilla 2047: num days was calculated wrong for some return dates in March/April and October.....this 
        // was because Javascript Date object seems to be including the clocks going forward (+1 hour) and
        // back (-1 hour), which happens during these months....added a rounding on the num days to fix this
        numDays.value = Math.round(numDaysMillis / DAY_IN_MILLIS);
    }
    else
    {
        numDays.value = '0';
    }
}

function calcNumDays(fromDate, toDate, numDays)
{
    var fromDateStr = fromDate.value;
    var toDateStr = toDate.value;
    
    var fromDay = fromDateStr.substring(0, 2);								
    var fromMonth = fromDateStr.substring(3, 5);
    //bug in Javascript date object: months go from 0-11!
    var fromMonthFix = fromMonth - 1;
    var fromYear = fromDateStr.substring(6);
    
    var toDay = toDateStr.substring(0, 2);
    var toMonth = toDateStr.substring(3, 5);
    //bug in Javascript date object: months go from 0-11!
    var toMonthFix = toMonth - 1;
    var toYear = toDateStr.substring(6);
    
    var calcFromDate = new Date(fromYear, fromMonthFix, fromDay);
    var calcToDate = new Date(toYear, toMonthFix, toDay);
        
    calcNumDaysUsingDates(calcFromDate, calcToDate, numDays);
}

function calcReturnDate(fromDate, numDays, toDate, dayField, monthField, yearField, dateField)
{
    numDaysStr = numDays.value;
    
    numDaysMillis = numDaysStr * DAY_IN_MILLIS;
    
    var fromDateStr = fromDate.value;
    var fromDay = fromDateStr.substring(0, 2);								
    var fromMonth = fromDateStr.substring(3, 5);
    //bug in Javascript date object: months go from 0-11!
    var fromMonthFix = fromMonth - 1;
 
    var fromYear = fromDateStr.substring(6);								
    
    if (numDaysMillis > 0)
    {
        var calcFromDate = new Date(fromYear, fromMonthFix, fromDay);
        
        var fromDateMillis = calcFromDate.valueOf();
        
        var toDateMillis = fromDateMillis + numDaysMillis;
        
        //bugzilla 2047: sometimes when you entered a depart date and then specified 1 as the number of days, the
        // return date would still be the same as the depart date (for example 28/10/2007).....this was because Javascript
        // Date object seems to be including the clocks going forward and back, which happens in March/April (+1 hour) and 
        // October (-1 hour)....needed to just compensate by a great enough value (chose 1.5 hours) to result in the correct
        // return date always being displayed
        toDateMillis = toDateMillis + ST_COMPENSATE_IN_MILLIS;
        
        var calcToDate = new Date();
        
        calcToDate.setTime(toDateMillis);
        
        var toDay = calcToDate.getDate();
        
        toDay = toDay > 9 ? toDay : '0' + toDay;
             
        var toMonth = calcToDate.getMonth();
     
        //bug in Javascript date object: months go from 0-11!
        var toMonthFix = toMonth + 1;
                
        toMonthFix = toMonthFix > 9 ? toMonthFix : '0' + toMonthFix;
        
        var toYear = calcToDate.getFullYear();
        
        var toDateStr = toDay + '/' + toMonthFix + '/' + toYear;
        
        toDate.value = toDateStr;
        
        document.getElementById(dayField).value = toDay;
        document.getElementById(monthField).value = toMonthFix;
        document.getElementById(yearField).value = toYear;
        document.getElementById(dateField).value = toDateStr;
        
    }
    else
    {
        toDate.value = fromDay + '/' + fromMonth + '/' + fromYear;
        document.getElementById(dayField).value = fromDay;
		document.getElementById(monthField).value = fromMonth;
		document.getElementById(yearField).value = fromYear;
        document.getElementById(dateField).value = fromDay + '/' + fromMonth + '/' + fromYear;
    }
}