(function($) {

    $.fn.addSelectOptions = function(url, foo) {
        return this.each(function() {
            //   $(this).bind('click', function()
            //   {
            if (this.nodeName.toLowerCase() != "select") {
                // Wrong element, i.e. not a DropDownList or ListBox
                return;
            }

            var elem = $(this);

            // get options
            var o = elem.options;

            if (o == null) {

                // Empty of select elements
                var opt = '';

                $.getJSON(url, function(result) {
                    for (var i = 0; i < result.length; i++) {
                        if (result[i].value == '-10') {
                            opt += '<optgroup label="' + result[i].name + '"></optgroup>' // Special för Optgroup Label
                        }
                        else {
                            opt += '<option value="' + result[i].value + '">' + result[i].name + '</option>';
                        }
                    }

                    elem.html(opt);
                    elem.show();

                    if (foo != null) {
                        foo();
                    }
                }); // end getJSON
            }
            //}); // onclick
        });
    }; //addOption


    $.fn.addSelectOptionsJSON = function(jSon, foo) {
        return this.each(function() {
            if (this.nodeName.toLowerCase() != "select") {
                // Wrong element, i.e. not a DropDownList or ListBox
                return;
            }

            var elem = $(this);

            // get options
            var o = elem.options;

            if (o == null) {

                // Empty of select elements
                var opt = '';

                for (var i = 0; i < jSon.length; i++) {
                    opt += '<option value="' + jSon[i].Key + '">' + jSon[i].Label + '</option>';
                }

                elem.html(opt);
                elem.show();

                if (foo != null) {
                    foo();
                }
            }
        });
    }; //addSelectOptionsJSON

})(jQuery);
