var fRolesNames = new Array();
var fRolesDescription = new Array();
var roleNames = new Array();
var uidsa = new Array();

if (typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  };
}

function addSRole(chb, type) {
    var id = chb.val();
    var checked = chb.is(':checked');

    if (checked) {
        selectedIds.push(id);
    }
    else {
        selectedIds = $.grep(selectedIds, function (i) { return i != val; });
    }

    rowChecked(id, checked);

    //if (chb.is(':checked')) {
    //    var id = chb.val();
    //    switch (type) {
    //        case "frole":
    //            fRolesNames["frn" + id] = $.trim(chb.parent().find('#name').text());
    //            fRolesDescription["frd" + id] = $.trim(chb.parent().find('#description').text());
    //            break;
    //        case "role":
    //            //roleNames["rn" + id] = $.trim(chb.parent().find('#name').text());
                
    //            selectedNames["id" + id] = $.trim($('#id' + id).attr('fullname'));
    //            break;
    //    }
        
    //}
}

function addSelectedRoles(id) {
    if (id) {
        uidsa.length = 0;
        uidsa.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 (froleids.length <= 0 && roleids.length <= 0 && uidsa <= 0) {
    //    alert(RoleResources.NoRoleSelected);
    //    return false;
    //}

    if (selectedIds.length <= 0) {
        alert(RoleResources.NoRoleSelected);
        return false;
    }

    //var fids = froleids.join(',');
    //var rids = roleids.join(',');
    //var uids = uidsa.join(',');

    var result = new Array();
    result["selectedIds"] = selectedIds;
    result["selectedNames"] = selectedNames;

    if (getURLParameter('cbmode') != null && getURLParameter('cbmode') != '') {
        result["callbackMode"] = getURLParameter('cbmode');
    }

    if (self.callback == null) self.callback = opener.rolesCallback;
    //self.callback(fids, rids, uids);
    self.callback(result);
    
    self.close();
    return false;
}

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

//function loadUsers() {
//    if ($('#users iframe').length == 0) {
//        var uurl ='workflowuser.ivp/list';
//        $('#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.toString(), $('#un' + id).text(),selectedIds);
//    }
//    return true;
//}

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

function rowChecked(id, check) {
    if ($('#id' + id).length > 0) {
        selectedNames['id' + id] = $('#id' + id).attr('fullname');
        selectedPaths["id" + id] = $("#id" + id).attr('path');
    }
}