﻿function AjaxDropDown(options) {
    var _instance;
    this.targetUrl = options.targetUrl;
    this.data = options.data;
    this.targetControl = options.targetControl;
    this.onSelect = options.onSelect;
    this.populateControl = function() {
        $.ajax({
            url: _instance.targetUrl,
            global: false,
            type: "GET",
            contentType: "application/json",
            dataType: "json",
            data: _instance.data,
            success: function(data) {
                if (data.Result == 1) { // Success
                    $.each(data.Object, function(index) {
                        $(_instance.targetControl).
                                append($("<option></option>").attr("value", data.Object[index].Key).text(data.Object[index].Value));
                    });
                    $(_instance.targetControl).enable();
                    $(_instance.targetControl).change(function() {
                        _instance.onSelect(_instance.targetControl.val());
                    });
                }
            }
        });
    };
    _instance = this;
}
