// G-PHRASE :
// G-URL :

function getBlackOutDays(destinationId)
{
    var ret = "";
    switch(destinationId)
    {
        case "DST000002" :
            return DST000002_LGW;
        break;
        case "DST000004" :
            return DST000004_LGW;
        break;
        case "DST000007" :
            return DST000007_LGW;
        break;
        case "DST000008" :
            return DST000008_LGW;
        break;
        case "DST000011" :
            return DST000011_LGW;
        break;
        default :
            return "";
        break;
    } 
}

function isValidDate(date, dest1, dest2) 
{ 
    // Check for black-out days
    var destinationId = "";

    var ddlDestination = $get(dest1); 
    if (ddlDestination != undefined && ddlDestination != null) 
    { 
        var indexValue = ddlDestination.selectedIndex;
        destinationId = ddlDestination.options[indexValue].value;
//alert("Using ddlDestination value: " + destinationId);
    }
    else
    {
        var hiddenDestinationId = $get(dest2); 
        if (hiddenDestinationId != undefined && hiddenDestinationId != null) 
        { 
            destinationId = hiddenDestinationId.value;
//alert("Using hidden value: " + destinationId);
        }
    }
        
//alert("isValidDate: destinationId = " + destinationId);

    
    return checkDate(date, destinationId);

} 

function checkDate(date, destinationId)
{
    var blackOutDays = getBlackOutDays(destinationId);

    for (i = 0; i < blackOutDays.length; i++) { 
        if (date.getDate() == blackOutDays[i][0] && date.getMonth() == blackOutDays[i][1] - 1 && date.getFullYear() == blackOutDays[i][2]) 
        { 
            return [false, '', 'Sorry, no flights available on this day']; 
        } 
    } 
    return [true, ''];
}

function setNextNonBlackOutDate(destination, datePickerId)
{
    var datePicker = $get(datePickerId);

    if (datePicker != undefined && datePicker != null) 
    {
        // Set the Date object from the datePicker text
        var datePickerDate = new Date();
        var day = datePicker.value.split('/')[0];
        var month = datePicker.value.split('/')[1];
        var year = datePicker.value.split('/')[2];
        // For some unknown-to-me reason, the month is zero based!
        datePickerDate.setFullYear(year, month-1, day);

        var indexValue = destination.selectedIndex;
        var destinationId = destination.options[indexValue].value;
        
//alert("destinationId = " + destinationId);
        
        // Loop through the black out dates to find the next non black out date
        var found = false;
        while(!found)
        {
            if(checkDate(datePickerDate, destinationId)[0])
            {
                // Nasty work around for zero padding on day / month
                var dayString = '0' + datePickerDate.getDate();
                dayString = dayString.substring(dayString.length - 2);

                // For some unknown-to-me reason, the month is zero based!
                var monthString = '0' + (datePickerDate.getMonth() + 1);
                monthString = monthString.substring(monthString.length - 2);
                
                var formattedDate = dayString + '/' + monthString + '/' + datePickerDate.getFullYear();

                // Set the datePicker text
                datePicker.value = formattedDate;
                found = true;
            }
            else
            {
                // Increment the date by 1 day, and try again
                datePickerDate.setDate(datePickerDate.getDate()+1);
            }            
        }
    }
    
    return false;
}