﻿(function(jQuery) {

    function GetProperty(option, name) {
        return eval('option.' + name);
    }

    jQuery.fn.AddOptions = function(objects, p) {
        p = jQuery.extend(true, {
            id: 'Id',
            value: 'Name',
            options: [],
            selectedId: 0
        }, p || {});

        var selector = "#" + this.attr("id") + " > option";
        jQuery(selector).remove();

        if (p.options) {
            for (var i = 0; i < p.options.length; i++) {
                jQuery("<option value=\"" + GetProperty(p.options[i], p.id) + "\">" + GetProperty(p.options[i], p.value) + "</option>").appendTo(this);
            }
        }

        if (objects) {
            for (var i = 0; i < objects.length; i++) {
                jQuery("<option value=\"" + GetProperty(objects[i], p.id) + "\">" + GetProperty(objects[i], p.value) + "</option>").appendTo(this);
            }
        }

        if (p.selectedId) { jQuery(this).val(p.selectedId); }
    }

})(jQuery);