﻿jQuery(document).ready(function() {
    // add the list of destinations when changing country
    jQuery("#" + ddlAccomDestinationId + "").cascade("#" + ddlAccomCountryId + "", {
        list: accomCountriesCitiesList,
        template: commonTemplate,
        match: commonMatch
    })
    // add an event when the list of destinations is loaded
    .bind("loaded.cascade", function(e, target) {
        // add a default 1st option 'Please select...'
        jQuery(this).prepend("<option value='0' selected='true'>Please select...</option>");
        // if no country is selected, disable destination ddl
        if (document.getElementById(ddlAccomCountryId).value == "0") {
            document.getElementById(ddlAccomDestinationId).disabled = true;
        }
        // select 1st option please select by default
        document.getElementById(ddlAccomDestinationId).selectedIndex = 0;
         if($.browser.msie && $.browser.version=="6.0")
        {                              
            setTimeout("document.getElementById(ddlAccomDestinationId).options.value = '0'",5);            
        }  
        //document.getElementById(ddlAccomDestinationId).options[0].setAttribute("selected", true); 
        // if there is a value in the hidDestinationId field, use it to preselect automatically the destination (this is to fix the issue when going back)
        if (document.getElementById(accomSearchControlId + "hidDestinationId") != null && document.getElementById(accomSearchControlId + "hidDestinationId").value != '') {
            document.getElementById(ddlAccomDestinationId).value = document.getElementById(accomSearchControlId + "hidDestinationId").value;
            //alert(document.getElementById(ddlAccomDestinationId).value);
            // however the problem now is that when changing country, it will attempt to select the value stored in hidDestinationId, but wont find it anymore and therefore
            // will be set to 0 by default... so need to reset the hidDestinationId too.
            if (document.getElementById(ddlAccomDestinationId).value == "0" || document.getElementById(ddlAccomDestinationId).value == "") {
                document.getElementById(ddlAccomDestinationId).selectedIndex = 0;
                document.getElementById(accomSearchControlId + "hidDestinationId").value = '';
            }
        }
    });

    jQuery("#" + ddlAccomLocationId + "").cascade("#" + ddlAccomDestinationId + "", {
        list: accomCitiesLocationsList,
        template: commonTemplate,
        match: commonMatch
    })
    .bind("loaded.cascade", function(e, target) {

        if (document.getElementById(ddlAccomDestinationId).value == "0") {
            document.getElementById(ddlAccomLocationId).disabled = true;
            jQuery(this).prepend("<option value='0' selected='true'></option>");
        }
        else {
            jQuery(this).prepend("<option value='0' selected='true'>Search All</option>");
        }
        document.getElementById(ddlAccomLocationId).selectedIndex = 0;
         if($.browser.msie && $.browser.version=="6.0")
        {                              
            setTimeout("document.getElementById(ddlAccomLocationId).options.value = '0'",5);            
        }  
        if (document.getElementById(accomSearchControlId + "hidResortId") != null && document.getElementById(accomSearchControlId + "hidResortId").value != '') {
            document.getElementById(ddlAccomLocationId).value = document.getElementById(accomSearchControlId + "hidResortId").value;
        }
    });
});

$(function() {
$('.depSelectorAccom').datepicker({ buttonImage: '/Content/Images/IBE/calendar.jpg', dateFormat: 'dd/mm/yy', showOn: 'button', minDate: '+' + minimumDays + 'd', maxDate: '+' + maxMonths + 'm', buttonImageOnly: true, buttonText: 'Choose a departure date', onSelect: function(dateText, inst) { UpdateDepartureDate(accomSearchControlId); } });

$('.retSelectorAccom').datepicker({ buttonImage: '/Content/Images/IBE/calendar.jpg', dateFormat: 'dd/mm/yy', showOn: 'button', minDate: '+' + minimumDays + 'd', maxDate: '+' + maxMonths + 'm', buttonImageOnly: true, buttonText: 'Choose a return date', onSelect: function(dateText, inst) { UpdateReturnDate(accomSearchControlId); } });
});
