function showWaitImage(flag)
{
    $('waitImage').style.visibility = (flag == true ? 'visible' : 'hidden');
}

function showUsersWithConfluenceAccess(contextPath, startIndex)
{
    params = "";
    var url = contextPath + "/admin/users/dosearchuserswithconfluenceaccess.action";
    search(params, url);
}

function searchUsers(contextPath, startIndex)
{
    var usernameTerm=$F('usernameTerm');
    var fullnameTerm=$F('fullnameTerm');
    var emailTerm=$F('emailTerm');

    var params = "usernameTerm=" + usernameTerm + "&fullnameTerm=" + fullnameTerm + "&emailTerm=" + emailTerm;

    var url = contextPath + "/admin/users/dosearchusers.action";
    search(params, url, startIndex);
}

function searchUsersForSelection(contextPath, startIndex)
{
    var usernameTerm=$F('usernameTerm');
    var fullnameTerm=$F('fullnameTerm');
    var emailTerm=$F('emailTerm');
    var operator = $F('operator');

    var params = "usernameTerm=" + usernameTerm + "&fullnameTerm=" + fullnameTerm + "&emailTerm=" + emailTerm + "&operator=" + operator;

    var url = contextPath + "/admin/users/dosearchusersforselection.action";
    search(params, url, startIndex);
}


function searchGroups(contextPath, startIndex)
{
    var groupnameTerm=$F('groupnameTerm');
    var params = "groupnameTerm=" + groupnameTerm;

    var url = contextPath + "/admin/users/dosearchgroups.action";
    search(params, url, startIndex);
}

function searchGroupsForSelection(contextPath, startIndex)
{
    var groupnameTerm=$F('groupnameTerm');
    var params = "groupnameTerm=" + groupnameTerm;

    var url = contextPath + "/admin/users/dosearchgroupsforselection.action";
    search(params, url, startIndex);
}

function searchGroupsOfUser(contextPath, startIndex)
{
    var groupsOfUserTerm = $F('groupsOfUserTerm');
    var params = "groupsOfUserTerm=" + groupsOfUserTerm;

    var url = contextPath + "/admin/users/dosearchgroupsofuser.action";
    search(params, url, startIndex);
}

function searchMembersOfGroup(contextPath, startIndex)
{
    var membersOfGroupTerm = $F('membersOfGroupTerm');
    var params = "membersOfGroupTerm=" + membersOfGroupTerm;

    var url = contextPath + "/admin/users/dosearchmembersofgroupforselection.action";
    search(params, url, startIndex);
}


function search(params, url, startIndex)
{
    var check = parseInt(startIndex);

    if (check <= 0)
        startIndex = 0;

    params = params + "&decorator=none&startIndex=" + startIndex

    try
    {
        var repositoryKey = $F('repositoryKey');
        params = params + "&repositoryKey=" + repositoryKey;
    }
    catch(e){}

    showWaitImage(true);
    var update = new Ajax.Updater('searchResults', url, {method: 'get', parameters: params, onComplete: showWaitImage} );
}

function resetSearch()
{
    $('usernameTerm').value = "";
    $('fullnameTerm').value = "";
    $('emailTerm').value = "";
    $('groupnameTerm').value = "";
    $('groupsOfUserTerm').value = "";
    $('membersOfGroupTerm').value = "";
    $('repositoryKey').options[0].selected = true;
}
