window.afterInitTable = function () {
    InitResizableGrid('table.resizable-grid', getHeightTableHolder(95));
    $('.filterform').validate();
    $('#uf_companyid').change(function() { reloadTable(); });
    $('#uf_roleid').change(function() { reloadTable(); });
    $('#uf_archive').click(function() { reloadTable(); });

    //$('#search').bind('keydown', dosearch);
    $('#search').focus();
};


function importUsers() {
    var dlg = openDialog('userimport.ivp', '', {height: 300, width: 600});
    dlg.callback = function() { reloadTable(); };
    return false;
}

function reloadTable() {
    $(tableRootDiv).load(tableUrl(), { nolayout: true, timestamp: new Date().toString() }, initTable);
}

function deleteUsers(options) {
    if (selectedIds.length == 0) { alert(TableResources.NoRecordsSelected); return false; }
    jConfirm(TableResources.SureDeleteUsers + '<br/><label class="padded5"><input type="checkbox" id="deleteall" name="deleteall" onclick="cbchanged($(this));"/>' + TableResources.DeleteAll + '</label>', MeetingResources.ConfirmAction, function (confirmed) {
        if (!confirmed) {
            $('#dropconstraints').val("False");
            return false;
        }
        else {
            var params = { id: selectedIds.join(','), dropconstraints: $('#dropconstraints').val(), nolayout: true, timestamp: new Date().toString() };
            if (options != null) $.extend(params, options);
            var url = base + tableController() + 'Delete';
            selectedIds.length = 0;
            $(tableRootDiv).load(url, params, initTable);
            return false;
        }
    });
    return false;
}

function cbchanged($cb) {
    if ($cb.attr('checked')) {
        $('#dropconstraints').val("True");
    }
    else {
        $('#dropconstraints').val("False");
    }
}

//$('#uf_showarchive:checked').size() > 0 ? "True" : "False"

var UserTriggerActions = {
    init: function () {
        var that = this;
        $(document).on('change', 'input.rowid', function (e) {
            that.checkDisableOptions();
        });

        return this;
    },

    checkDisableOptions: function () {
        var that = this;
        var $options = $('#usertriggeractions > option[value!=""]');
        $.each($options, function (i, v) {
            v.disabled = that['disabled_' + v.value]();
        });
    },

    trigger: function () {
        var $select = $('#usertriggeractions');
        var action = $select.val();
        $select.val('');
        this['trigger_' + action]();
    },

    trigger_mergeTwoUsers: function () {
        var that = this;
        var submitFunc = function () {
            if (!confirm(UserResources.ConfirmDeleteMergeUsers + ' "' + $('#mergeusersform').find('option:not(:selected)').text().trim() + '"?'))
                return false;

            $.post(base + 'user.ivp/MergeUsersFormSubmit', $('#mergeusersform').serialize(), function (response) {
                if (response) {
                    var result = JSON.parse(response);
                    if (result.IsOK) {
                        selectedIds = new Array(); //Снятие выделения строк
                        reloadTable();                       
                        alert(result.Result);
                        that.checkDisableOptions();
                    }
                    else {
                        alert(result.Description);
                    }
                }
                else {
                    alert('Undefine response');
                }
            });
        };

        $.post(base + 'user.ivp/MergeUsersForm/', { ids: selectedIds.join(',') }, function (response) {
            createModalDialog(UserResources.DialogTitleMergeUsers, response, submitFunc, function () { $('#mergeusersform').validate(); }, { width: 600, minWidth: 600 });
        });
    },

    disabled_mergeTwoUsers: function () {
        return selectedIds.length != 2;
    }
}.init();