﻿function ReadyConfigureContainer(ConfigureContainerID, SKUIDContainerID, btnClearID) {
    $('#' + btnClearID).click(function() {
        $('#' + ConfigureContainerID + ' select').each(function() {
            var $this = $(this);
            $this.html($this.data('allOptions'));
            $this.val(0);
        });
        return false;
    });
    $('#' + ConfigureContainerID + ' select').each(function() {
        $(this).data('allOptions', $(this).html());
        $(this).change(function() {
            $(this).addClass('CallingDropDownList');
            var availableSKUIDs = new Array();
            $('#' + ConfigureContainerID + ' select option:selected:not(:first-child)').each(function() {
                var classArr = $(this).attr('class').split(" ");
                availableSKUIDs = availableSKUIDs.length > 0 ? availableSKUIDs.intersect(classArr) : classArr;
            });

            //rebuild all options
            $('#' + ConfigureContainerID + ' select:not(.CallingDropDownList)').each(function() {
                var $this = $(this);
                var selectedValue = $this.val();
                $this.html($this.data('allOptions'));
                $this.val(selectedValue);
            });

            //if there are items in the available ids, disable all options (not the first) and enable only the ones allowed
            if (availableSKUIDs.length > 0) {
                //disable all options except the first
                $('#' + ConfigureContainerID + ' select:not(.CallingDropDownList)').children('option:not(:first-child)').attr('disabled', 'disabled');

                //select all options that have any of the classes in the availableSKUIDs array
                var selectors = new Array();
                for (var i = 0; i < availableSKUIDs.length; i++) {
                    selectors[selectors.length] = '#' + ConfigureContainerID + ' select option.' + availableSKUIDs[i];
                }
                $(selectors.join()).removeAttr('disabled');
            }

            //else enable all options
            else {
                $('#' + ConfigureContainerID + ' select option').removeAttr('disabled');
            }

            $('#' + ConfigureContainerID + ' select option:disabled').remove();
            $(this).removeClass('CallingDropDownList');

            //if there is only one available SKUID in the array, set the SKUIDContainer
            //else, remove any values in the SKUIDContainer
            $('#' + SKUIDContainerID).val(availableSKUIDs.length == 1 ? availableSKUIDs[0] : '');
        });
    });
}