var fRolesNames = new Array();
var fRolesDescription = new Array();
var roleNames = new Array();
var uidsa = new Array();
var selectedGroups = new Array();
var gidsa = new Array();
var fridsa = new Array();
var ridsa = new Array();

function addSRole(chb,type) {
    if (chb.is(':checked')) {
        var id = chb.val();
        switch (type) {
            case "frole":
                fRolesNames["id" + id] = $.trim(chb.parent().find('#name').text());
                fRolesDescription["id" + id] = $.trim(chb.parent().find('#description').text());
                break;
            case "role":
                roleNames["id" + id] = $.trim(chb.parent().find('#name').text());
                break;
        }
        
    }
}

function addSelectedRoles(id,type) {
    if (id) {
        if (type == 'user') {
            uidsa.length = 0; uidsa.push(id);
        }
        if (type == 'group') {
            gidsa.length = 0; gidsa.push(id);
        }
        if (type == 'frole') {
            fridsa.length = 0; fridsa.push(id);
        }
        if (type == 'role') {
            ridsa.length = 0; ridsa.push(id);
        }
    }
    var froleids = new Array();
    var roleids = new Array();
    $('.fids:checked').each(function(i, id) { froleids.push($(this).val()); });
    $('.rids:checked').each(function(i, id) { roleids.push($(this).val()); });
    if (fridsa.length <= 0 && ridsa.length <= 0 && uidsa <= 0 && gidsa <= 0 && froleids.length <= 0 && roleids.length <= 0) {
        alert(WorkflowResources.NoRoleSelected);
        return false;
    }

    var result = new Array();
    result["selectedFunctionalRoles"] = $.merge(fridsa, froleids);
    result["selectedRoles"] = $.merge(ridsa, roleids);
    result["selectedUsers"] = uidsa;
    result["selectedGroups"] = gidsa;
    result["functionalRoleNames"] = fRolesNames;
    result["functionalRoleDescriptions"] = fRolesDescription;
    result["roleNames"] = roleNames;
    result["userNames"] = selectedNames;
    result["groupNames"] = selectedGroups;

    //var fids = fridsa.join(',') + froleids.join(',');
    //var rids = ridsa.join(',') + roleids.join(',');
    //var uids = uidsa.join(',');
    //var gids = gidsa.join(',');
    //if (self.callback == null) self.callback = opener.addSelected;
    //self.callback(result);
    //self.callback(fids,rids,uids,gids);

    if (self.callback == null) {
        if (getURLParameter('cbmode') != null && getURLParameter('cbmode') != '') {
            eval('opener.' + getURLParameter('cbmode') + '.workflowsCallback(result)');
        } else {
            self.callback = opener.workflowsCallback;
            self.callback(result);
        }
    } else {
        self.callback(result);
    }

    self.close();
    return false;
}

function addUsers(id) {
    parent.selectedNames['id'+id] = $('#id' + id).attr('fullname');
    parent.addSelectedRoles(id,'user');
    return false;
}


function loadUsers(isConcurrence) {
    if ($('#users iframe').length == 0) {
        if(isConcurrence)
            var uurl = 'workflowuser.ivp/Concurrencelist';
        else
            var uurl = 'workflowuser.ivp/list';

        var cbMode = getURLParameter('cbmode');

        if (cbMode != null && cbMode != '') {

            if (uurl.indexOf('?') > -1) {
                uurl += "&";
            } else {
                uurl += "?";
            }

            uurl += '&cbmode=' + cbMode;
        }

        $('#users').prepend('<iframe src="' + uurl + '" frameborder="0" />');
        resize();
    }
    return setTab('users');
}

function resize() {
    var h = $(window).height() - $('#header').height() - $('#footer').height() - $('.formheader').height() - $('#tabs').height() - $('.formbottom').height()-100;
    $('#users iframe').css('min-height', h).css('height',h).css('width','100%');
}

function rowChecked(id, check) {
    if ($('#id' + id).length > 0) {
        parent.getUsers(id, $('#id' + id).attr('fullname'),selectedIds);
    }

    return true;
}
function getUsers(id ,value, sid) {
    selectedNames['id' + id] = value;
    uidsa = sid;
}

function loadGroups(isConcurrence) {
    if ($('#groups iframe').length == 0) {
        if (isConcurrence)
            var uurl = 'workflowexecutorgroup.ivp/Concurrencelist';
        else 
            var uurl = 'workflowexecutorgroup.ivp/list';
        
        $('#groups').prepend('<iframe src="' + uurl + '" frameborder="0" />');
        resizeGroup();
    }
    return setTab('groups');
}

function resizeGroup() {
    var h = $(window).height() - $('#header').height() - $('#footer').height() - $('.formheader').height() - $('#tabs').height() - $('.formbottom').height() - 100;
    $('#groups iframe').css('min-height', h).css('height', h).css('width', '100%');
}

function addGroups(id) {
    parent.selectedNames['id' + id] = $('#id' + id).attr('fullname');
    parent.addSelectedRoles(id,'group');
    return false;
}
function getGroups(id, value, sid) {
    selectedGroups[id] = value;
    gidsa = sid;
}
function addFRoles(id) {
    parent.fRolesNames['id' + id] = $.trim($('#frn_' + id + ' #name').text());
    parent.addSelectedRoles(id, 'frole');
    return false;
}
function addRoles(id) {
    parent.roleNames['id' + id.toString()] = $.trim($('#rn_' + id).text());
    parent.addSelectedRoles(id, 'role');
    return false;
}