var dlg = null;

$(document).ready(function () {
    $('body').on('click', '.company-user-changeRole', function () {
        var readOnly = $("#Readonlyform");
        if (readOnly == undefined || $(readOnly).val() == "False") {
            var $link = $(this);
            var td = $link.parent();
            $('.popupwrapper').hide();
            if (td.find('.popupwrapper').length > 0)
                td.find('.popupwrapper').show();
            else {
                var html = '<div class="popupwrapper"><div class="popup">' + $('#companyuserselectroletemplate').html() + '</div></div>';
                td.append(html).find('.popupwrapper').css({ left: td.position().left, top: td.position().top + 10 }).show();
            }
        }
        return false;
    });
});

String.prototype.replaceAll = function (search, replace) {
    return this.split(search).join(replace);
};
var companyId = 0;
$(function() { serviceId = parent.$('#id').val(); });

function reloadTable(params) {    
    if (!params) params = { nolayout: true, timestamp: new Date().toString() };
    $('#users #iframeholder').load(tableUrl(), params, initTable);
}

function reloadUsers() {    
    var params = new Array();
    var rid = parseInt($('#uf_roleid', top.document).val(), 10);
    params.push({ name: 'nolayout', value: true }); params.push({ name: 'timestamp', value: new Date().toString() });
    params.push({ name: 'tb_search', value: $('#search', top.document).val() });
    params.push({ name: 'fttRoleId', value: rid > 0 ? 1 : 0 }); params.push({ name: 'flt1RoleId', value: $('#uf_roleid', top.document).val() });
    reloadTable(params);
    return false;
}

function hideRoles(link) {
    $(link).parents('.popupwrapper').hide();
    return false;
}

function company_user_setRole(link, roleid) {
    var td = $(link).parents('td');
    $(link).parents('.popupwrapper').remove();
    var url = base +'companyuser.ivp/changerole';
    $.getJSON(url, { roleid: roleid, userid: getId(td.find('a:first').attr('id')), timestamp: new Date().toString() }, function(result) {
        if (result.hasOwnProperty('IsOK') && !result.IsOK)
            alertDialog(CommonResources.Error, result.Description);
        else {
            if (result.Result == 'reload')
                reloadTable();
            else {
                td.find('a:first').text($(link).text());
            }
        }
    });
    return false;
}

function deleteSelectedUsers() {
    var selected = new Array();    
    $(tableRootDiv).find('td input:checked').each(function (i, el) { selected.push(el.value); });    
    if (selected.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 {
            $.getJSON(base + 'user.ivp/delete/', { id: selected.join(','), dropconstraints: $('#dropconstraints').val(), json: true }, function (result) {
                if (result && result.hasOwnProperty('Description') && result.Description) {
                    jAlert(result.Description, TableResources.DeleteError);
                }
                reloadTable();
            });
            return false;
        }
    });
    return false;
}

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

function createUser(companyid) {
    dlg = openDialog('user.ivp/create/?&companyid=' + companyid + '&from=company', 'createuser', { width: 840, height: 650 });
    dlg.callback = function() { reloadUsers(); }
    return false;
}

function editUser(userid) {
    dlg = openDialog('user.ivp/view/' + userid + '?&from=company&popup=true', 'edituser', { width: 840, height: 650 });
    dlg.callback = function () { reloadUsers(); }
    return false;
}

function block(options) {
    var selected = new Array();
    $(tableRootDiv).find('td input:checked').each(function (i, el) { selected.push(el.value); });
    
    if (selected.length == 0) { alert(ServiceResources.SelectUsersToBlock); return false; }
    if (!confirm(ServiceResources.SureBlockUsers)) return false;
    var params = { id: selected.join(','), nolayout: true, timestamp: new Date().toString() };
    if (options != null) $.extend(params, options);
    var url = base + tableController() + 'Block';
    selected.length = 0;
    $(tableRootDiv).load(url, params, initTable);
    return false;
}

function unblock(options) {
    var selected = new Array();
    $(tableRootDiv).find('td input:checked').each(function (i, el) { selected.push(el.value); });
    if (selected.length == 0) { alert(ServiceResources.SelectUsersToUnblock); return false; }
    if (!confirm(ServiceResources.SureUnblockUsers)) return false;
    var params = { id: selected.join(','), nolayout: true, timestamp: new Date().toString() };
    if (options != null) $.extend(params, options);
    var url = base + tableController() + 'Unblock';
    selected.length = 0;
    $(tableRootDiv).load(url, params, initTable);
    return false;
}