

(function($) {

    $.fn.linkedSelect = function(url, destinationSelect, params) {

        var params = $.extend({

            firstOption: 'Please Select',

            loadingText: 'Loading...'

        }, params);

        var $dest = $(destinationSelect);

        return this.each(function() {

            $(this).bind('change', function() {

                var $$ = $(this);

                $dest.attr('disabled', 'false')
                 .append('<option value="">' + params.loadingText + '</option>')
                 .ajaxStart(function() {

                     $$.show();

                 });

                $.getJSON(url, { str: $$.val() }, function(j) {
                    var options = '<option value="">' + params.firstOption + '</option>';
                    if (j.length > 0) {


                        for (var i = 0; i < j.length; i++) {

                            options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';

                        }
                       
                    }
                    $dest.removeAttr('disabled');
                    $dest.html(options)
                    .find('option:first')
                    .attr('selected', 'selected');

                }); // end getJSON

            });  // end change

        }); // end return each

    };  // end function

})(jQuery);