﻿(function($) {
    $.fn.populate = function(options) {
        var defaults = {
            queryString: '',
            url: ''
        };

        var options = $.extend({}, defaults, options);
        var $this = this;

        this.initialize = function() {
            window.setTimeout(
                function() {
                    $.ajax({
                        type: "POST",
                        url: options.url,
                        data: '{' + options.queryString + '}',
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(data) {
                            var result = data.d;
                            $this.html(result.SelectOptions);
                            if (result.SelectedValue != '') {
                                $this.val(result.SelectedValue);
                            }
                        }
                    });
                },
            1);
        };

        this.initialize();
    }
})(jQuery);