diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index 5fa62c3465e86fb11c657d1a02c2c3cb61d5df77..fbb8425bdaab9f65da2b833974ab2f8f62e10bd8 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -523,7 +523,7 @@ class TicketsAjaxAPI extends AjaxController {
     }
 
     function massProcess($action, $w=null)  {
-        global $thisstaff;
+        global $thisstaff, $cfg;
 
         $actions = array(
                 'transfer' => array(
@@ -566,7 +566,7 @@ class TicketsAjaxAPI extends AjaxController {
             $w = 'me';
         case 'assign':
             $inc = 'assign.tmpl.php';
-            $info[':action'] = '#tickets/mass/assign';
+            $info[':action'] = "#tickets/mass/assign/$w";
             $info[':title'] = sprintf('Assign %s',
                     _N('selected ticket', 'selected tickets', $count));
 
@@ -579,10 +579,49 @@ class TicketsAjaxAPI extends AjaxController {
             $assignees = null;
             switch ($w) {
                 case 'agents':
+                    $depts = array();
+                    $tids = $_POST['tids'] ?: array_filter(explode(',', $_REQUEST['tids']));
+                    if ($tids) {
+                        $tickets = TicketModel::objects()
+                            ->distinct('dept_id')
+                            ->filter(array('ticket_id__in' => $tids));
+
+                        $depts = $tickets->values_flat('dept_id');
+                    }
+                    $members = Staff::objects()
+                        ->distinct('staff_id')
+                        ->filter(array(
+                                    'onvacation' => 0,
+                                    'isactive' => 1,
+                                    )
+                                );
+
+                    if ($depts) {
+                        $members->filter(Q::any( array(
+                                        'dept_id__in' => $depts,
+                                        Q::all(array(
+                                            'dept_access__dept__id__in' => $depts,
+                                            Q::not(array('dept_access__dept__flags__hasbit'
+                                                => Dept::FLAG_ASSIGN_MEMBERS_ONLY))
+                                            ))
+                                        )));
+                    }
+
+                    switch ($cfg->getAgentNameFormat()) {
+                    case 'last':
+                    case 'lastfirst':
+                    case 'legal':
+                        $members->order_by('lastname', 'firstname');
+                        break;
+
+                    default:
+                        $members->order_by('firstname', 'lastname');
+                    }
+
                     $prompt  = __('Select an Agent');
                     $assignees = array();
-                    foreach (Staff::getAvailableStaffMembers() as $id => $name)
-                        $assignees['s'.$id] = $name;
+                    foreach ($members as $member)
+                         $assignees['s'.$member->getId()] = $member->getName();
 
                     if (!$assignees)
                         $info['warn'] =  __('No agents available for assignment');
diff --git a/include/staff/templates/tickets-actions.tmpl.php b/include/staff/templates/tickets-actions.tmpl.php
index 10d274ad93f9a689b4f763c9ddd40df394fdca92..0535a5af2832a7da0354b6fa53dc88373ff6f6ca 100644
--- a/include/staff/templates/tickets-actions.tmpl.php
+++ b/include/staff/templates/tickets-actions.tmpl.php
@@ -56,15 +56,22 @@ if ($agent->hasPerm(Ticket::PERM_DELETE, false)) {?>
 ?>
 <script type="text/javascript">
 $(function() {
-    $(document).off('.tickets-actions');
-    $(document).on('click.tickets-actions', 'a.tickets-action', function(e) {
+
+    $(document).off('.tickets');
+    $(document).on('click.tickets', 'a.tickets-action', function(e) {
         e.preventDefault();
-        var count = checkbox_checker($('form#tickets'), 1);
+        var $form = $('form#tickets');
+        var count = checkbox_checker($form, 1);
         if (count) {
+            var tids = $('.ckb:checked', $form).map(function() {
+                    return this.value;
+                }).get();
             var url = 'ajax.php/'
             +$(this).attr('href').substr(1)
             +'?count='+count
+            +'&tids='+tids.join(',')
             +'&_uid='+new Date().getTime();
+            console.log(tids);
             $.dialog(url, [201], function (xhr) {
                 $.pjax.reload('#pjax-container');
              });
diff --git a/include/staff/tickets.inc.php b/include/staff/tickets.inc.php
index 690f6c2066fefa03c4b2e78b7e39a6919b08f610..d015d1cfad8bbf536fb74291209f3eb01d34a602 100644
--- a/include/staff/tickets.inc.php
+++ b/include/staff/tickets.inc.php
@@ -586,21 +586,6 @@ return false;">
 </div>
 <script type="text/javascript">
 $(function() {
-    $(document).off('.tickets');
-    $(document).on('click.tickets', 'a.tickets-action', function(e) {
-        e.preventDefault();
-        var count = checkbox_checker($('form#tickets'), 1);
-        if (count) {
-            var url = 'ajax.php/'
-            +$(this).attr('href').substr(1)
-            +'?count='+count
-            +'&_uid='+new Date().getTime();
-            $.dialog(url, [201], function (xhr) {
-                $.pjax({url: 'tickets.php', container: '#pjax-container'});
-             });
-        }
-        return false;
-    });
     $('[data-toggle=tooltip]').tooltip();
 });
 </script>