$(document).ready(function() {
    if (!window.inArray) {
        function inArray(what, where) {
           for (var i = 0; i < where.length; i++) {
                if (what == where[i]) {
                    return true;
                }
            }
            return false;
        }
    }
    
    /**
     * Single location selection component
     */
    var countriesDropDownSelector = $("div.singleLocationSelector > div:eq(0) > div.formFieldCon select");
    var regionsDropDownSelector = $("div.singleLocationSelector > div:eq(1) > div.formFieldCon select");
    var locationsDropDownSelector = $("div.singleLocationSelector > div:eq(2) > div.formFieldCon select");

    var regionCurrentValueSelector = $("div.singleLocationSelector input:eq(0)");
    var locationCurrentValueSelector = $("div.singleLocationSelector input:eq(1)");

    var singleDefaultOptionValue = $("div.singleLocationSelector input:eq(2)").val();
    var singleDefaultOptionText = $("div.singleLocationSelector input:eq(3)").val();
    countriesDropDownSelector.change(function() {
        var countryId = $(this).val();
        if (countryId == "0") {
            /**
             * Clean and hide regions and locations lists
             */
            regionsDropDownSelector.find("option").remove();
            regionsDropDownSelector.parent().parent().hide();
            locationsDropDownSelector.find("option").remove();
            locationsDropDownSelector.parent().parent().hide();
            return;
        }
        $.post("ajax",
               {
                   func: "loadRegionsList",
                   country_id: countryId
               },
               function(data){
                   var len = data.length;
                   if (len > 0) {
                       var regionId = regionCurrentValueSelector.val();
                       var options = '<option value="' + singleDefaultOptionValue + '">' + singleDefaultOptionText + '</option>';
                       for (var i = 0; i < data.length; i++) {
                           if (data[i]['id'] == regionId) {
                               options += '<option value="' + data[i]['id']+ '" selected="yes">';
                           } else {
                               options += '<option value="' + data[i]['id']+ '">';
                           }
                           options += data[i]['title'] + '</option>';
                       }
                       regionsDropDownSelector.html(options);
                       regionsDropDownSelector.parent().parent().show();
                   } else {
                       regionsDropDownSelector.find("option").remove();
                       regionsDropDownSelector.parent().parent().hide();
                   }
                   regionsDropDownSelector.change();
               },
               "json"
        );
    });
    regionsDropDownSelector.change(function() {
        var regionId = $(this).val();
        if (regionId == "0") {
            locationsDropDownSelector.find("option").remove();
            locationsDropDownSelector.parent().parent().hide();
            return;
        }
        $.post("ajax",
               {
                   func: "loadLocationsList",
                   region_id: regionId
               },
               function(data){
                   var len = data.length;
                   if (len > 0) {
                       var mainLocationId = locationCurrentValueSelector.val();
                       var options = '<option value="' + singleDefaultOptionValue + '">' + singleDefaultOptionText + '</option>';
                       for (var i = 0; i < data.length; i++) {
                           if (data[i]['id'] == mainLocationId) {
                               options += '<option value="' + data[i]['id']+ '" selected="yes">';
                           } else {
                               options += '<option value="' + data[i]['id']+ '">';
                           }
                           options += data[i]['title'] + '</option>';
                       }
                       locationsDropDownSelector.html(options);
                       locationsDropDownSelector.parent().parent().show();
                   } else {
                       locationsDropDownSelector.find("option").remove();
                       locationsDropDownSelector.parent().parent().hide();
                   }
               },
               "json"
        );
    });
    countriesDropDownSelector.change();

    /**
     * Single region selection component
     */
    var countriesDropDownSelector1 = $("div.singleRegionSelector > div:eq(0) > div.formFieldCon select");
    var regionsDropDownSelector1 = $("div.singleRegionSelector > div:eq(1) > div.formFieldCon select");

    var regionCurrentValueSelector1 = $("div.singleRegionSelector input:eq(0)");

    var singleDefaultOptionValue1 = $("div.singleRegionSelector input:eq(1)").val();
    var singleDefaultOptionText1 = $("div.singleRegionSelector input:eq(2)").val();
    countriesDropDownSelector1.change(function() {
        var countryId = $(this).val();
        if (countryId == "0") {
            regionsDropDownSelector1.find("option").remove();
            regionsDropDownSelector1.parent().parent().hide();
            
            return;
        }
        $.post("ajax",
               {
                   func: "loadRegionsList",
                   country_id: countryId
               },
               function(data){
                   var len = data.length;
                   if (len > 0) {
                       var regionId = regionCurrentValueSelector1.val();
                       var options = '<option value="' + singleDefaultOptionValue1 + '">' + singleDefaultOptionText1 + '</option>';
                       for (var i = 0; i < data.length; i++) {
                           if (data[i]['id'] == regionId) {
                               options += '<option value="' + data[i]['id']+ '" selected="yes">';
                           } else {
                               options += '<option value="' + data[i]['id']+ '">';
                           }
                           options += data[i]['title'] + '</option>';
                       }
                       regionsDropDownSelector1.html(options);
                       regionsDropDownSelector1.parent().parent().show();
                   } else {
                       regionsDropDownSelector1.find("option").remove();
                       regionsDropDownSelector1.parent().parent().hide();
                   }
               },
               "json"
        );
    });
    countriesDropDownSelector1.change();

    /**
     * Locations multiple selection
     */
    var countriesListBoxSelector = $("div.multiLocationSelector > div:eq(0) > div.formFieldCon select");
    var regionsListBoxSelector = $("div.multiLocationSelector > div:eq(1) > div.formFieldCon select");
    var locationsListBoxSelector = $("div.multiLocationSelector > div:eq(2) > div.formFieldCon select");

    var regionsCurrentValueSelector = $("div.multiLocationSelector input:eq(0)");
    var locationsCurrentValueSelector = $("div.multiLocationSelector input:eq(1)");

    var multiDefaultOptionValue = $("div.multiLocationSelector input:eq(2)").val();
    var multiDefaultOptionText = $("div.multiLocationSelector input:eq(3)").val();
    countriesListBoxSelector.change(function() {
        var countryId = $(this).val();
        if (countryId == "0") {
            /**
             * Clean and hide regions and locations lists
             */
            regionsListBoxSelector.find("option").remove();
            regionsListBoxSelector.parent().parent().hide();
            locationsListBoxSelector.find("option").remove();
            locationsListBoxSelector.parent().parent().hide();
            return;
        }
        $.post("ajax",
               {
                   func: "loadRegionsList",
                   country_id: countryId
               },
               function(data){
                   var len = data.length;
                   if (len > 0) {
                       var regionIds = regionsCurrentValueSelector.val().split(",");
                       var options = "";
                       for (var i = 0; i < data.length; i++) {
                           if (inArray(data[i]['id'], regionIds)) {
                               options += '<option value="' + data[i]['id']+ '" selected="yes">';
                           } else {
                               options += '<option value="' + data[i]['id']+ '">';
                           }
                           options += data[i]['title'] + '</option>';
                       }
                       regionsListBoxSelector.html(options);
                       regionsListBoxSelector.parent().parent().show();
                   } else {
                       regionsListBoxSelector.find("option").remove();
                       regionsListBoxSelector.parent().parent().hide();
                   }
                   regionsListBoxSelector.change();
               },
               "json"
        );
    });
    regionsListBoxSelector.change(function(){
        var regionIds = $(this).val();
        if (regionIds == []) {
            locationsListBoxSelector.find("option").remove();
            locationsListBoxSelector.parent().parent().hide();
            return;
        }
        $.post("ajax",
               {
                   'func': "loadLocationsList",
                   'region_id[]': regionIds
               },
               function(data){
                   var len = data.length;
                   if (len > 0) {
                       var locationIds = $(locationsCurrentValueSelector).val().split(",");
                       var options = "";
                       for (var i = 0; i < data.length; i++) {
                           if (inArray(data[i]['id'], locationIds)) {
                               options += '<option value="' + data[i]['id']+ '" selected="yes">' + data[i]['title'] + '</option>';
                           } else {
                               options += '<option value="' + data[i]['id']+ '">' + data[i]['title'] + '</option>';
                           }
                       }
                       locationsListBoxSelector.html(options);
                       locationsListBoxSelector.parent().parent().show();
                   } else {
                       locationsListBoxSelector.find('option').remove();
                       locationsListBoxSelector.parent().parent().hide();
                   }
               },
               "json"
        );
    });
    countriesListBoxSelector.change();

    var selectAllLocationsLink = $("div.multiLocationSelector > div:eq(2) > div.formFieldCon a");
    selectAllLocationsLink.click(function() {
        locationsListBoxSelector.find('option').attr('selected', "yes");
        return false;
    });

    /*
     * City tour dates hide when location not selected.
     */
    var tourCountriesDropDownSelector = $("div.tourLocationSelector > div:eq(0) > div.formFieldCon select");
    var tourRegionsDropDownSelector = $("div.tourLocationSelector > div:eq(1) > div.formFieldCon select");
    var tourLocationsDropDownSelector = $("div.tourLocationSelector > div:eq(2) > div.formFieldCon select");

    var tourRegionCurrentValueSelector = $("div.tourLocationSelector input:eq(0)");
    var tourLocationCurrentValueSelector = $("div.tourLocationSelector input:eq(1)");

    var tourDefaultOptionValue = $("div.tourLocationSelector input:eq(2)").val();
    var tourDefaultOptionText = $("div.tourLocationSelector input:eq(3)").val();
    tourCountriesDropDownSelector.change(function() {
        var countryId = $(this).val();
        if (countryId == "0") {
            /**
             * Clean and hide regions and locations lists
             */
            tourRegionsDropDownSelector.find("option").remove();
            tourRegionsDropDownSelector.parent().parent().hide();
            tourLocationsDropDownSelector.find("option").remove();
            tourLocationsDropDownSelector.parent().parent().hide();
            $("select[name='tour_location_id']").change();
            return;
        }
        $.post("ajax",
               {
                   func: "loadRegionsList",
                   country_id: countryId
               },
               function(data){
                   var len = data.length;
                   if (len > 0) {
                       var regionId = tourRegionCurrentValueSelector.val();
                       var options = '<option value="' + tourDefaultOptionValue + '">' + tourDefaultOptionText + '</option>';
                       for (var i = 0; i < data.length; i++) {
                           if (data[i]['id'] == regionId) {
                               options += '<option value="' + data[i]['id']+ '" selected="yes">';
                           } else {
                               options += '<option value="' + data[i]['id']+ '">';
                           }
                           options += data[i]['title'] + '</option>';
                       }
                       tourRegionsDropDownSelector.html(options);
                       tourRegionsDropDownSelector.parent().parent().show();
                   } else {
                       tourRegionsDropDownSelector.find("option").remove();
                       tourRegionsDropDownSelector.parent().parent().hide();
                   }
                   tourRegionsDropDownSelector.change();
               },
               "json"
        );
    });
    tourRegionsDropDownSelector.change(function() {
        var regionId = $(this).val();
        if (regionId == "0") {
            tourLocationsDropDownSelector.find("option").remove();
            tourLocationsDropDownSelector.parent().parent().hide();
            $("select[name='tour_location_id']").change();
            return;
        }
        $.post("ajax",
               {
                   func: "loadLocationsList",
                   region_id: regionId
               },
               function(data){
                   var len = data.length;
                   if (len > 0) {
                       var mainLocationId = tourLocationCurrentValueSelector.val();
                       var options = '<option value="' + tourDefaultOptionValue + '">' + tourDefaultOptionText + '</option>';
                       for (var i = 0; i < data.length; i++) {
                           if (data[i]['id'] == mainLocationId) {
                               options += '<option value="' + data[i]['id']+ '" selected="yes">';
                               $("input[name='city_tour_from']").parent().parent().show();
                               $("input[name='city_tour_to']").parent().parent().show();
                           } else {
                               options += '<option value="' + data[i]['id']+ '">';
                           }
                           options += data[i]['title'] + '</option>';
                       }
                       tourLocationsDropDownSelector.html(options);
                       tourLocationsDropDownSelector.parent().parent().show();
                   } else {
                       tourLocationsDropDownSelector.find("option").remove();
                       tourLocationsDropDownSelector.parent().parent().hide();
                   }
               },
               "json"
        );
    });
    tourCountriesDropDownSelector.change();

    $("select[name='tour_location_id']").change(function(){
        var locationId = $(this).val();
        if (locationId == null || locationId == "0") {
            $("input[name='city_tour_from']").parent().parent().hide();
            $("input[name='city_tour_to']").parent().parent().hide();
        } else {
            $("input[name='city_tour_from']").parent().parent().show();
            $("input[name='city_tour_to']").parent().parent().show();
        }
    });
    $("select[name='tour_location_id']").change();
});