(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) {
                //loadingShow();
                
                // Empty of select elements
                var opt = '';

                $.getJSON(url, function(result) {
                    for (var i = 0; i < result.length; i++) {
                        opt += '<option value="' + result[i].Key + '">' + result[i].Label + '</option>';
                    }

                    elem.html(opt);
                    elem.show();

                    if (foo != null) {
                        foo();
                    }

                    //loadingHide(document.getElementById('loading'));
                }); // end getJSON
            }
            //}); // onclick
        });
    }; //addOption

    //To exclude the URL if you allready have the Json! M.S Was here :P
    $.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);