Area around checkboxes is now also responsive to the fancy row highlighting and auto...
[squirrelmail.git] / functions / mailbox_display.php
index 420ddf0db7eaa59b9326c826ad64f0e20388cd6a..ad3355a2d1456d045f13ede080bc93c9f087e61f 100644 (file)
@@ -1,17 +1,17 @@
 <?php
 
 /**
-* mailbox_display.php
-*
-* Copyright (c) 1999-2004 The SquirrelMail Project Team
-* Licensed under the GNU GPL. For full terms see the file COPYING.
-*
-* This contains functions that display mailbox information, such as the
-* table row that has sender, date, subject, etc...
-*
-* @version $Id$
-* @package squirrelmail
-*/
+ * mailbox_display.php
+ *
+ * Copyright (c) 1999-2005 The SquirrelMail Project Team
+ * Licensed under the GNU GPL. For full terms see the file COPYING.
+ *
+ * This contains functions that display mailbox information, such as the
+ * table row that has sender, date, subject, etc...
+ *
+ * @version $Id$
+ * @package squirrelmail
+ */
 
 /** The standard includes.. */
 require_once(SM_PATH . 'functions/strings.php');
@@ -24,18 +24,18 @@ require_once(SM_PATH . 'functions/mime.php');
 require_once(SM_PATH . 'functions/forms.php');
 
 /**
-* default value for page_selector_max
-*/
+ * default value for page_selector_max
+ */
 define('PG_SEL_MAX', 10);
 
 /**
-* The number of pages to cache msg headers
-*/
+ * The number of pages to cache msg headers
+ */
 define('SQM_MAX_PAGES_IN_CACHE',5);
 
 /**
-* Sort constants used for sorting of messages
-*/
+ * Sort constants used for sorting of messages
+ */
 define('SQSORT_NONE',0);
 define('SQSORT_DATE_ASC',1);
 define('SQSORT_DATE_DEC',2);
@@ -63,8 +63,8 @@ define('SQM_MAX_MBX_IN_CACHE',3);
 // define('MBX_PREF_FUTURE',unique integer key);
 
 /**
-* @param mixed $start UNDOCUMENTED
-*/
+ * @param mixed $start UNDOCUMENTED
+ */
 function elapsed($start) {
 
     $end = microtime();
@@ -80,11 +80,11 @@ function elapsed($start) {
 }
 
 /**
-* Displays message header row in messages list
-*
-* @param  array $aMsg contains all message related parameters
-* @return void
-*/
+ * Displays message header row in messages list
+ *
+ * @param  array $aMsg contains all message related parameters
+ * @return void
+ */
 
 function printMessageInfo($aMsg) {
     // FIX ME, remove these globals as well by adding an array as argument for the user settings
@@ -98,7 +98,9 @@ function printMessageInfo($aMsg) {
         $email_address,
         $show_recipient_instead,    /* show recipient name instead of default identity */
         $use_icons,                 /* indicates to use icons or text markers */
-        $icon_theme;                /* icons theming */
+        $icon_theme,                /* icons theming */
+        $javascript_on,
+        $fancy_index_highlite;
 
     $color_string = $color[4];
 
@@ -124,6 +126,8 @@ function printMessageInfo($aMsg) {
     $aFlags   = (isset($msg['FLAGS'])) ? $msg['FLAGS'] : array();
     $iPrio    = (isset($msg['PRIORITY'])) ? $msg['PRIORITY'] : 3;
     $iSize    = (isset($msg['SIZE'])) ? $msg['SIZE'] : 0;
+
+    // These don't appear to be used... are they safe to remove
     $sType0   = (isset($msg['TYPE0'])) ? $msg['TYPE0'] : 'text';
     $sType1   = (isset($msg['TYPE1'])) ? $msg['TYPE1'] : 'plain';
     if (isset($msg['INTERNALDATE'])) {
@@ -157,8 +161,8 @@ function printMessageInfo($aMsg) {
         // If the From address is the same as $email_address, then handle as Sent
         $from_array = parseAddress($sFrom, 1);
         if (!isset($email_address)) {
-            global $datadir, $username;
-            $email_address = getPref($datadir, $username, 'email_address');
+            global $data_dir, $username;
+            $email_address = getPref($data_dir, $username, 'email_address');
         }
         $bHandleAsSent = ((isset($from_array[0][0])) && ($from_array[0][0] == $email_address));
     } else {
@@ -197,8 +201,8 @@ function printMessageInfo($aMsg) {
     }
     // If Sent, prefix with To: but only if not Sent folder
     if ($bHandleAsSent ^ $bSentFolder) {
-        $senderName = _("To:") . ' ' . $senderName;
-        $senderAddress = _("To:") . ' ' . $senderAddress;
+        $senderName = _("To") . ': ' . $senderName;
+        $senderAddress = _("To") . ': ' . $senderAddress;
     }
 
     // this is a column property which can apply to multiple columns. Do not use vars for one column
@@ -213,7 +217,7 @@ function printMessageInfo($aMsg) {
         $senderName = truncateWithEntities($senderName, $truncate_sender);
     }
 
-    $flag = $flag_end = $bold = $bold_end = $fontstr = $fontstr_end = $italic = $italic_end = '';
+    $flag = $flag_end = $fontstr = $fontstr_end = $italic = $italic_end = '';
     $bold = '<b>';
     $bold_end = '</b>';
 
@@ -296,44 +300,126 @@ function printMessageInfo($aMsg) {
     if (!isset($hlt_color)) {
         $hlt_color = $color_string;
     }
+
     $col = 0;
     $sSubject = str_replace('&nbsp;', ' ', decodeHeader($sSubject));
     $subject = processSubject($sSubject, $iIndent);
 
-    echo html_tag( 'tr','','','','VALIGN="top"') . "\n";
+
+    $row_extra = 'valign="top"';
+
+
+    // this stuff does the auto row highlighting on mouseover
+    //
+    if ($javascript_on && $fancy_index_highlite) {
+
+        $mouseoverColor = $color[5];
+
+        // set this to an empty string to turn off extra 
+        // highlighting of checked rows
+        //
+        //$clickedColor = '';
+        if (!empty($color[16]))
+           $clickedColor = $color[16];
+        else
+           $clickedColor = $color[2];
+
+        $row_extra .= ' onmouseover="setPointer(this, ' . $t . ', \'over\', \'' . $hlt_color . '\', \'' . $mouseoverColor . '\', \'' . $clickedColor . '\');" onmouseout="setPointer(this, ' . $t . ', \'out\', \'' . $hlt_color . '\', \'' . $mouseoverColor . '\', \'' . $clickedColor . '\');" onmousedown="setPointer(this, ' . $t . ', \'click\', \'' . $hlt_color . '\', \'' . $mouseoverColor . '\', \'' . $clickedColor . '\');"';
+
+    }
+
+
+    if ($javascript_on && $fancy_index_highlite)
+        echo "<script language='javascript' type='text/javascript'>\n<!--\n"
+           . "orig_row_colors[" . $t . "] = '" . $hlt_color . "';\n//-->\n</script>";
+    echo html_tag( 'tr', '', '', '', $row_extra) . "\n";
+
+
+    // this does the auto-checking of the checkbox no matter 
+    // where on the row you click
+    //
+    $javascript_auto_click = '';
+    $checkbox_javascript = '';
+    if ($javascript_on && $fancy_index_highlite) {
+        $javascript_auto_click = ' onMouseDown="row_click(\'msg[' . $t . ']\')"';
+        $checkbox_javascript = ' onClick="this.checked = !this.checked;"';
+    }
+
 
     if (sizeof($index_order)) {
+
+        // figure out which columns should serve as labels for checkbox:
+        // we try to grab the two columns before and after the checkbox,
+        // except the subject column, since it is the link that opens
+        // the message view
+        //
+        // if $javascript_on is set, then the highlighting code takes 
+        // care of this; just skip it
+        //
+        $show_label_columns = array();
+        if (!($javascript_on && $fancy_index_highlite)) {
+            $get_next_two = 0;
+            $last_order_part = 0;
+            $last_last_order_part = 0;
+            foreach ($index_order as $index_order_part) {
+    
+                if ($index_order_part == 1) {
+                    $get_next_two = 1;
+                    if ($last_last_order_part != 4)
+                       $show_label_columns[] = $last_last_order_part;
+                    if ($last_order_part != 4)
+                       $show_label_columns[] = $last_order_part;
+    
+                } else if ($get_next_two > 0 && $get_next_two < 3 && $index_order_part != 4) {
+                    $show_label_columns[] = $index_order_part;
+                    $get_next_two++;
+                }
+                $last_last_order_part = $last_order_part;
+                $last_order_part = $index_order_part;
+            }
+        }
+
+
+        // build the actual columns for display
+        //
         foreach ($index_order as $index_order_part) {
+            if (in_array($index_order_part, $show_label_columns)) {
+                $label_start = '<label for="msg[' . $t . ']">';
+                $label_end = '</label>';
+            } else {
+                $label_start = '';
+                $label_end = '';
+            }
             switch ($index_order_part) {
             case 1: /* checkbox */
                 echo html_tag( 'td',
-                    addCheckBox("msg[$t]", $checkall, $iId),
+                    addCheckBox("msg[$t]", $checkall, $iId, $checkbox_javascript),
                             'center',
-                            $hlt_color );
+                            $hlt_color, $javascript_auto_click);
                 break;
             case 2: /* from */
                 if ($senderAddress != $senderName) {
                     $senderAddress = strtr($senderAddress, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
-                    $title = ' title="' . str_replace('"', "''", $senderAddress) . '"';
+                    $title = ' title="' . str_replace(array('"', '<', '>'), array("''", '&lt;', '&gt;'), $senderAddress) . '"';
                 } else {
                     $title = '';
                 }
                 echo html_tag( 'td',
-                            $italic . $bold . $flag . $fontstr . $senderName .
-                            $fontstr_end . $flag_end . $bold_end . $italic_end,
+                            $label_start . $italic . $bold . $flag . $fontstr . $senderName .
+                            $fontstr_end . $flag_end . $bold_end . $italic_end . $label_end,
                             'left',
-                            $hlt_color, $title );
+                            $hlt_color, $title . $javascript_auto_click);
                 break;
             case 3: /* date */
                 if ($sDate == '') {
                     $sDate = _("Unknown date");
                 }
                 echo html_tag( 'td',
-                            $bold . $flag . $fontstr . $sDate .
-                            $fontstr_end . $flag_end . $bold_end,
+                            $label_start . $bold . $flag . $fontstr . $sDate .
+                            $fontstr_end . $flag_end . $bold_end . $label_end,
                             'center',
                             $hlt_color,
-                            'nowrap' );
+                            'style="white-space: nowrap;"' . $javascript_auto_click );
                 break;
             case 4: /* subject */
                 $td_str = $bold;
@@ -343,16 +429,25 @@ function printMessageInfo($aMsg) {
                 $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox
                         .  '&amp;passed_id='. $iId
                         .  '&amp;startMessage='.$start_msg.$searchstr.'"';
+
+                // don't highlight the row or check the checkbox 
+                // when clicking subject link (when fancy highlighting is on)
+                //
+                // parentNode property is DOM Level 1
+                //
+                if ($javascript_on && $fancy_index_highlite)
+                    $td_str .= ' onmousedown="row_click(\'msg[' . $t . ']\'); setPointer(this.parentNode.parentNode, ' . $t . ', \'click\', \'' . $hlt_color . '\', \'' . $mouseoverColor . '\', \'' . $clickedColor . '\');"';
+
                 $td_str .= ' ' .concat_hook_function('subject_link', array($start_msg, $searchstr));
                 if ($subject != $sSubject) {
                     $title = get_html_translation_table(HTML_SPECIALCHARS);
                     $title = array_flip($title);
                     $title = strtr($sSubject, $title);
-                    $title = str_replace('"', "''", $title);
+                    $title = str_replace(array('"', '<', '>'), array("''", '&lt;', '&gt;'), $title);
                     $td_str .= " title=\"$title\"";
                 }
                 $td_str .= ">$flag$subject$flag_end</a>$bold_end";
-                echo html_tag( 'td', $td_str, 'left', $hlt_color );
+                echo html_tag( 'td', $td_str, 'left', $hlt_color, $javascript_auto_click );
                 break;
             case 5: /* flags */
 
@@ -401,10 +496,10 @@ function printMessageInfo($aMsg) {
                     $td_str .= '<img src="' . $msg_icon . '.png" border="0" alt="'. $msg_alt . '" title="' . $msg_title . '" height="12" width="18" />';
                     $td_str .= '</small></b>';
                     echo html_tag( 'td',
-                                $td_str,
+                                $label_start . $td_str . $label_end,
                                 'right',
                                 $hlt_color,
-                                'nowrap' );
+                                'style="white-space: nowrap;"' . $javascript_auto_click );
                 }
 
                 // plain text message markers
@@ -439,18 +534,18 @@ function printMessageInfo($aMsg) {
                     }
                     $td_str .= '</small></b>';
                     echo html_tag( 'td',
-                                $td_str,
+                                $label_start . $td_str . $label_end,
                                 'center',
                                 $hlt_color,
-                                'nowrap' );
+                                'style="white-space: nowrap;"' . $javascript_auto_click );
                 }
                 break;
             case 6: /* size */
                 echo html_tag( 'td',
-                            $bold . $fontstr . show_readable_size($iSize) .
-                            $fontstr_end . $bold_end,
+                            $label_start . $bold . $fontstr . show_readable_size($iSize) .
+                            $fontstr_end . $bold_end . $label_end,
                             'right',
-                            $hlt_color );
+                            $hlt_color, $javascript_auto_click );
                 break;
             }
             ++$col;
@@ -492,6 +587,7 @@ function sqm_api_mailbox_select($imapConnection,$mailbox,$aConfig,$aProps) {
     /**
      * In case the properties arrays are empty set the defaults.
      */
+    // Doesn't appear to be used... safe to remove?
     $aDefaultMbxPref = array ();
 //                          MBX_PREF_SORT => 0,
 //                          MBX_PREF_LIMIT => 15,
@@ -595,7 +691,6 @@ function sqm_api_mailbox_select($imapConnection,$mailbox,$aConfig,$aProps) {
                         (!($newsort % 2) && ($newsort - 1 == $oldsort))) {
                         $aMailbox['UIDSET'][$iSetIndx] = array_reverse($aCachedMailbox['UIDSET'][$iSetIndx]);
                     } else {
-                        $server_sort_array = false;
                         $aMailbox['MSG_HEADERS'] = false;
                         $aMailbox['ID'] = false;
                     }
@@ -710,12 +805,12 @@ function sqm_api_mailbox_select($imapConnection,$mailbox,$aConfig,$aProps) {
 
 
 /**
-* Does the $srt $_GET var to field mapping
-*
-* @param int $srt Field to sort on
-* @param bool $bServerSort Server sorting is true
-* @return string $sSortField Field to sort on
-*/
+ * Does the $srt $_GET var to field mapping
+ *
+ * @param int $srt Field to sort on
+ * @param bool $bServerSort Server sorting is true
+ * @return string $sSortField Field to sort on
+ */
 function getSortField($sort,$bServerSort) {
     switch($sort) {
         case SQSORT_NONE:
@@ -770,9 +865,9 @@ function get_sorted_msgs_list($imapConnection,&$aMailbox,&$error) {
         $aRes = get_thread_sort($imapConnection,$aMailbox['SEARCH'][$iSetIndx]);
         if ($aRes === false) {
             $error = '<b><small><center><font color=red>' .
-                    _("Thread sorting is not supported by your IMAP server.") . '<br />' .
-                    _("Please report this to the system administrator.").
-                    '</center></small></b>';
+                _("Thread sorting is not supported by your IMAP server.") . '<br />' .
+                _("Please contact your system administrator and report this error.") .
+                '</center></small></b>';
             $aMailbox['SORT'] -= SQSORT_THREAD;
         } else {
             $aMailbox['UIDSET'][$iSetIndx] = $aRes[0];
@@ -784,8 +879,8 @@ function get_sorted_msgs_list($imapConnection,&$aMailbox,&$error) {
         $id = sqimap_get_sort_order($imapConnection, $sSortField, $bDirection, $aMailbox['SEARCH'][$iSetIndx]);
         if ($id === false) {
             $error =  '<b><small><center><font color=red>' .
-                _( "Server-side sorting is not supported by your IMAP server.") . '<br />' .
-                _("Please report this to the system administrator.").
+                _("Server-side sorting is not supported by your IMAP server.") . '<br />' .
+                _("Please contact your system administrator and report this error.") .
                 '</center></small></b>';
         } else {
             $aMailbox['UIDSET'][$iSetIndx] = $id;
@@ -836,7 +931,7 @@ function fetchMessageHeaders($imapConnection, &$aMailbox) {
     }
 
     // initialize the fields we want to retrieve:
-    $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Content-Type');
+    $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Importance', 'Priority', 'Content-Type');
     $aFetchItems = array('FLAGS', 'RFC822.SIZE');
 
     // Are we sorting on internaldate then retrieve the internaldate value as well
@@ -932,10 +1027,10 @@ function fetchMessageHeaders($imapConnection, &$aMailbox) {
             }
 
             /**
-            * retrieve messages by sequence id's and fetch the UID to retrieve
-            * the UID. for sorted lists this is not needed because a UID FETCH
-            * automaticly add the UID value in fetch results
-            **/
+             * retrieve messages by sequence id's and fetch the UID to retrieve
+             * the UID. for sorted lists this is not needed because a UID FETCH
+             * automaticly add the UID value in fetch results
+             **/
             $aFetchItems[] = 'UID';
 
             //create id range
@@ -999,14 +1094,14 @@ function fetchMessageHeaders($imapConnection, &$aMailbox) {
 }
 
 /**
-* This function loops through a group of messages in the mailbox
-* and shows them to the user.
-*
-* @param mixed $imapConnection
-* @param array $aMailbox associative array with mailbox related vars
-*/
+ * This function loops through a group of messages in the mailbox
+ * and shows them to the user.
+ *
+ * @param mixed $imapConnection
+ * @param array $aMailbox associative array with mailbox related vars
+ */
 function showMessagesForMailbox($imapConnection, &$aMailbox) {
-    global $color;
+    global $color, $javascript_on, $compact_paginator;
 
     // to retrieve the internaldate pref: (I know this is not the right place to do that, move up in front
     // and use a properties array as function argument to provide user preferences
@@ -1021,18 +1116,26 @@ function showMessagesForMailbox($imapConnection, &$aMailbox) {
              $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $aMailbox['EXISTS'];
 
     $paginator_str = get_paginator_str($aMailbox['NAME'], $aMailbox['PAGEOFFSET'],
-                                    $aMailbox['EXISTS'], $aMailbox['LIMIT'], $aMailbox['SHOWALL'][$iSetIndx]);
+      $aMailbox['EXISTS'], $aMailbox['LIMIT'], $aMailbox['SHOWALL'][$iSetIndx]);
 
     $msg_cnt_str = get_msgcnt_str($aMailbox['PAGEOFFSET'], $iEnd,$aMailbox['EXISTS']);
 
     do_hook('mailbox_index_before');
+
+    if ($javascript_on && $compact_paginator) {
+        // Insert compact paginator javascript
+        echo "\n<!-- start of compact paginator javascript -->\n"
+            . "<script language=\"JavaScript\">\n"
+            . "function SubmitOnSelect(select, URL)\n"
+            . "{\n"
+            . "   URL += select.options[select.selectedIndex].value;\n"
+            . "   window.location.href = URL;\n"
+            . "}\n"
+            . "</script>\n"
+            . "<!-- end of compact paginator javascript -->\n";
+    }
+    mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $paginator_str);
 ?>
-<table border="0" width="100%" cellpadding="0" cellspacing="0">
-<tr>
-    <td>
-    <?php mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $paginator_str); ?>
-    </td>
-</tr>
 <tr><td height="5" bgcolor="<?php echo $color[4]; ?>"></td></tr>
 <tr>
     <td>
@@ -1040,38 +1143,34 @@ function showMessagesForMailbox($imapConnection, &$aMailbox) {
         <tr>
         <td>
             <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="<?php echo $color[5]; ?>">
-            <tr>
-                <td>
                 <?php
                     printHeader($aMailbox);
                     displayMessageArray($imapConnection, $aMailbox);
                 ?>
-                </td>
-            </tr>
             </table>
         </td>
         </tr>
     </table>
-    <?php
-        mail_message_listing_end($aMailbox['EXISTS'], $paginator_str, $msg_cnt_str);
-    ?>
     </td>
 </tr>
-</table>
+<?php
+    mail_message_listing_end($aMailbox, $paginator_str, $msg_cnt_str);
+?>
+</table></form>
 <?php
 
 }
 
 /**
-* Function to map an uid list with a msg header array by uid
-* The mapped headers are printed with printMessage
-* aMailbox parameters contains info about the page we are on, the
-* used search criteria, the number of messages to show
-*
-* @param resource $imapConnection socket handle to imap
-* @param array    $aMailbox array with required elements MSG_HEADERS, UIDSET, OFFSET, LIMIT
-* @return void
-**/
+ * Function to map an uid list with a msg header array by uid
+ * The mapped headers are printed with printMessage
+ * aMailbox parameters contains info about the page we are on, the
+ * used search criteria, the number of messages to show
+ *
+ * @param resource $imapConnection socket handle to imap
+ * @param array    $aMailbox array with required elements MSG_HEADERS, UIDSET, OFFSET, LIMIT
+ * @return void
+ **/
 function displayMessageArray($imapConnection, $aMailbox) {
     $iSetIndx    = $aMailbox['SETINDEX'];
     $aId         = $aMailbox['UIDSET'][$iSetIndx];
@@ -1123,15 +1222,15 @@ function displayMessageArray($imapConnection, $aMailbox) {
 }
 
 /**
-* Displays the standard message list header.
-*
-* To finish the table, you need to do a "</table></table>";
-*
-* @param resource $imapConnection
-* @param array    $aMailbox associative array with mailbox related information
-* @param string   $msg_cnt_str
-* @param string   $paginator Paginator string
-*/
+ * Displays the standard message list header.
+ *
+ * To finish the table, you need to do a "</table></table>";
+ *
+ * @param resource $imapConnection
+ * @param array    $aMailbox associative array with mailbox related information
+ * @param string   $msg_cnt_str
+ * @param string   $paginator Paginator string
+ */
 function mail_message_listing_beginning ($imapConnection,
                                          $aMailbox,
                                          $msg_cnt_str = '',
@@ -1142,13 +1241,12 @@ function mail_message_listing_beginning ($imapConnection,
 
     $php_self = $PHP_SELF;
 
-    $urlMailbox = urlencode($aMailbox['NAME']);
-
     if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
         $source_url = $regs[1];
     } else {
         $source_url = $php_self;
     }
+    $php_self = str_replace('&', '&amp;', $php_self);
 
     if (!isset($msg)) {
         $msg = '';
@@ -1169,8 +1267,8 @@ function mail_message_listing_beginning ($imapConnection,
             $sort = $aMailbox['SORT'] + SQSORT_THREAD;
         }
         $thread_link_str = '<small>[<a href="' . $source_url . '?srt='
-            . $sort . '&start_messages=1'
-            . '&mailbox=' . urlencode($aMailbox['NAME']) . '">' . $thread_name
+            . $sort . '&amp;start_messages=1'
+            . '&amp;mailbox=' . urlencode($aMailbox['NAME']) . '">' . $thread_name
             . '</a>]</small>';
     } else {
         $thread_link_str ='';
@@ -1184,29 +1282,30 @@ function mail_message_listing_beginning ($imapConnection,
 
     echo '<form name="' . $form_name . '" method="post" action="'.$php_self.'">' ."\n"
         . $moveFields;
+    echo '<table border="0" width="100%" cellpadding="0" cellspacing="0"><tr><td>';
 
     $button_str = '';
     // display flag buttons only if supported
     if ($show_flag_buttons  &&
         in_array('\\flagged',$aMailbox['PERMANENTFLAGS'], true) ) {
-        $button_str .= getButton('SUBMIT', 'markUnflagged', _("Unflag"));
-        $button_str .= getButton('SUBMIT', 'markFlagged',   _("Flag"));
+        $button_str .= getButton('submit', 'markUnflagged', _("Unflag"));
+        $button_str .= getButton('submit', 'markFlagged',   _("Flag"));
         $button_str .= "&nbsp;\n";
     }
     if (in_array('\\seen',$aMailbox['PERMANENTFLAGS'], true)) {
-        $button_str .= getButton('SUBMIT', 'markUnread', _("Unread"));
-        $button_str .= getButton('SUBMIT', 'markRead',   _("Read"));
+        $button_str .= getButton('submit', 'markUnread', _("Unread"));
+        $button_str .= getButton('submit', 'markRead',   _("Read"));
         $button_str .= "&nbsp;\n";
     }
-    $button_str .= getButton('SUBMIT', 'attache',_("Forward")) .
+    $button_str .= getButton('submit', 'attache',_("Forward")) .
                    "&nbsp;\n";
     if (in_array('\\deleted',$aMailbox['PERMANENTFLAGS'], true)) {
-        $button_str .= getButton('SUBMIT', 'delete',_("Delete"));
+        $button_str .= getButton('submit', 'delete',_("Delete"));
         $button_str .= '<input type="checkbox" name="bypass_trash" />' . _("Bypass Trash");
         $button_str .= "&nbsp;\n";
     }
     if (!$aMailbox['AUTO_EXPUNGE'] && $aMailbox['RIGHTS'] != 'READ-ONLY') {
-        $button_str .= getButton('SUBMIT', 'expungeButton',_("Expunge"))  .'&nbsp;' . _("mailbox") . "\n";
+        $button_str .= getButton('submit', 'expungeButton',_("Expunge"))  .'&nbsp;' . _("mailbox") . "\n";
         $button_str .= '&nbsp;';
     }
 ?>
@@ -1216,13 +1315,13 @@ function mail_message_listing_beginning ($imapConnection,
             <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1"  cellspacing="0">
             <tr>
                 <?php echo html_tag('td', '<small>' . $paginator . $thread_link_str . '</small>', 'left') . "\n"; ?>
-                <?php echo html_tag('td', '', 'center') . "\n"; ?>
+                <?php echo html_tag('td', '&nbsp;', 'center') . "\n"; ?>
                 <?php echo html_tag('td', '<small>' . $msg_cnt_str . '</small>', 'right') . "\n"; ?>
             </tr>
             </table>
         </td>
         </tr>
-        <tr width="100%" cellpadding="1"  cellspacing="0" border="0" bgcolor="<?php echo $color[0]; ?>">
+        <tr bgcolor="<?php echo $color[0]; ?>">
         <td>
             <table border="0" width="100%" cellpadding="1"  cellspacing="0">
             <tr>
@@ -1240,7 +1339,7 @@ function mail_message_listing_beginning ($imapConnection,
                         <select name="targetMailbox">
                             <?php echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes); ?>
                         </select></tt>&nbsp;
-                        <?php echo getButton('SUBMIT', 'moveButton',_("Move")); ?>
+                        <?php echo getButton('submit', 'moveButton',_("Move")); ?>
                     </small>
                 <?php
                 }
@@ -1251,22 +1350,25 @@ function mail_message_listing_beginning ($imapConnection,
         </td>
         </tr>
     </table>
+</td></tr>
 <?php
     do_hook('mailbox_form_before');
 }
 
 /**
-* Function to add the last row in a message list, it contains the paginator and info about
-* the number of messages.
-*
-* @param integer $num_msgs number of messages in a mailbox
-* @param string  $paginator_str Paginator string  [Prev | Next]  [ 1 2 3 ... 91 92 94 ]  [Show all]
-* @param string  $msg_cnt_str   Message count string Viewing Messages: 21 to 1861 (20 total)
-*/
-function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str) {
-global $color;
-if ($num_msgs) {
-    /* space between list and footer */
+ * Function to add the last row in a message list, it contains the paginator and info about
+ * the number of messages.
+ *
+ * @param array   $aMailbox associative array with mailbox related information
+ * @param string  $paginator_str Paginator string  [Prev | Next]  [ 1 2 3 ... 91 92 94 ]  [Show all]
+ * @param string  $msg_cnt_str   Message count string Viewing Messages: 21 to 1861 (20 total)
+ */
+function mail_message_listing_end($aMailbox, $paginator_str, $msg_cnt_str) {
+    global $color;
+
+    if ($aMailbox['EXISTS']) {
+        /* space between list and footer */
+
 ?>
 <tr><td height="5" bgcolor="<?php echo $color[4]; ?>" colspan="1"></td></tr>
 <tr>
@@ -1276,8 +1378,8 @@ if ($num_msgs) {
         <td>
             <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1"  cellspacing="0">
             <tr>
-                    <?php echo html_tag('td', '<small>' . $paginator_str . '</small>', 'left');  ?>
-                    <?php echo html_tag('td', '<small>' . $msg_cnt_str   . '</small>', 'right'); ?>
+                <?php echo html_tag('td', '<small>' . $paginator_str . '</small>', 'left');  ?>
+                <?php echo html_tag('td', '<small>' . $msg_cnt_str   . '</small>', 'right'); ?>
             </tr>
             </table>
         </td>
@@ -1290,14 +1392,13 @@ if ($num_msgs) {
     /* End of message-list table */
 
     do_hook('mailbox_index_after');
-    echo "</form>\n";
 }
 
 /**
-* Prints the table header for the messages list view
-*
-* @param array $aMailbox
-*/
+ * Prints the table header for the messages list view
+ *
+ * @param array $aMailbox
+ */
 function printHeader($aMailbox) {
     global $index_order, $internal_date_sort, $color;
 
@@ -1310,7 +1411,7 @@ function printHeader($aMailbox) {
     echo html_tag( 'tr' ,'' , 'center', $color[5] );
 
     /* calculate the width of the subject column based on the
-    * widths of the other columns */
+     * widths of the other columns */
     $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5);
     $subjectwidth = 100;
     foreach($index_order as $item) {
@@ -1323,7 +1424,7 @@ function printHeader($aMailbox) {
             echo html_tag( 'td',get_selectall_link($aMailbox) , '', '', 'width="1%"' );
             break;
         case 5: /* flags */
-            echo html_tag( 'td','' , '', '', 'width="1%"' );
+            echo html_tag( 'td','&nbsp;' , '', '', 'width="1%"' );
             break;
         case 2: /* from */
             if (handleAsSent($aMailbox['NAME'])) {
@@ -1342,7 +1443,7 @@ function printHeader($aMailbox) {
             echo "</td>\n";
             break;
         case 3: /* date */
-            echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' )
+            echo html_tag( 'td' ,'' , 'left', '', 'width="5%" style="white-space: nowrap;"' )
                 . '<b>' . _("Date") . '</b>';
             if ($showsort) {
                 if ($internal_date_sort) {
@@ -1362,7 +1463,7 @@ function printHeader($aMailbox) {
             echo "</td>\n";
             break;
         case 6: /* size */
-            echo html_tag( 'td', '', 'center','','width="5%" nowrap')
+            echo html_tag( 'td', '', 'center','','width="5%" style="white-space: nowrap;"')
                 . '<b>' . _("Size") . '</b>';
             if ($showsort) {
                 ShowSortButton($aMailbox, SQSORT_SIZE_ASC, SQSORT_SIZE_DEC);
@@ -1376,12 +1477,12 @@ function printHeader($aMailbox) {
 
 
 /**
-* This function shows the sort button. Isn't this a good comment?
-*
-* @param array $aMailbox
-* @param integer $Down
-* @param integer $Up
-*/
+ * This function shows the sort button. Isn't this a good comment?
+ *
+ * @param array $aMailbox
+ * @param integer $Down
+ * @param integer $Up
+ */
 function ShowSortButton($aMailbox, $Down, $Up ) {
     global $PHP_SELF;
 
@@ -1412,14 +1513,23 @@ function ShowSortButton($aMailbox, $Down, $Up ) {
 }
 
 /**
-* FIXME: Undocumented function
-*
-* @param array $aMailbox
-*/
+ * FIXME: Undocumented function
+ *
+ * @param array $aMailbox
+ */
 function get_selectall_link($aMailbox) {
-    global $checkall, $javascript_on;
+    global $checkall, $javascript_on, $fancy_index_highlite, $color;
     global $PHP_SELF;
 
+    // set this to an empty string to turn off extra 
+    // highlighting of checked rows
+    //
+    //$clickedColor = '';
+    if (!empty($color[16]))
+       $clickedColor = $color[16];
+    else
+       $clickedColor = $color[2];
+
     $result = '';
     if ($javascript_on) {
         $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $aMailbox['NAME']);
@@ -1427,37 +1537,67 @@ function get_selectall_link($aMailbox) {
         $form_name = "FormMsgs" . $safe_name;
         $result = '<script language="JavaScript" type="text/javascript">'
                 . "\n<!-- \n"
+                . "var orig_row_colors = new Array();\n"
                 . "function " . $func_name . "() {\n"
                 . "  for (var i = 0; i < document." . $form_name . ".elements.length; i++) {\n"
                 . "    if(document." . $form_name . ".elements[i].type == 'checkbox' && "
                 . "       document." . $form_name . ".elements[i].name.substring(0,3) == 'msg'){\n"
                 . "      document." . $form_name . ".elements[i].checked = "
                 . "        !(document." . $form_name . ".elements[i].checked);\n"
+                . ($fancy_index_highlite ? "      setPointer(document." . $form_name . ".elements[i].parentNode.parentNode, document." . $form_name . ".elements[i].name.substring(4, document." . $form_name . ".elements[i].name.length - 1), 'click', orig_row_colors[document." . $form_name . ".elements[i].name.substring(4, document." . $form_name . ".elements[i].name.length - 1)], orig_row_colors[document." . $form_name . ".elements[i].name.substring(4, document." . $form_name . ".elements[i].name.length - 1)], '" . $clickedColor . "');\n" : '')
                 . "    }\n"
                 . "  }\n"
                 . "}\n"
                 . "//-->\n"
                 . '</script>'
                 . '<input type="checkbox" name="toggleAll" title="'._("Toggle All").'" onclick="'.$func_name.'();" />';
-//                . <a href="javascript:void(0)" onClick="' . $func_name . '();">' . _("Toggle All")
+//                . <a href="javascript:void(0)" onclick="' . $func_name . '();">' . _("Toggle All")
 //                . "</a>\n";
     } else {
+        $result .= "<a href=\"$PHP_SELF";
+        // FIXME: why strpos() is used to detect presense of the symbol in the string.
+        // Function returns boolean value only when symbol is not found
         if (strpos($PHP_SELF, "?")) {
-            $result .= "<a href=\"$PHP_SELF&amp;mailbox=" . urlencode($aMailbox['NAME'])
-                    .  "&amp;startMessage=$aMailbox[PAGEOFFSET]&amp;srt=$aMailbox[SORT]&amp;checkall=";
+            $prefix = '&amp;';
         } else {
-            $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
-                    .  "&amp;startMessage=$aMailbox[PAGEOFFSET]&amp;srt=$aMailbox[SORT]&amp;checkall=";
+            $prefix = '?';
         }
+
+        // If variables are part of GET request, they are present in $PHP_SELF
+        // maybe other functions can be used instead of sqgetGlobalVar (like preg_match)
+        if (! sqgetGlobalVar('mailbox',$tmp,SQ_GET)) {
+            $result .= $prefix . 'mailbox=' . urlencode($aMailbox['NAME']);
+            $prefix = '&amp;';
+        }
+        if (! sqgetGlobalVar('startMessage',$tmp,SQ_GET)) {
+            $result .= $prefix . 'startMessage=' . $aMailbox['PAGEOFFSET'];
+            $prefix = '&amp;';
+        }
+        if (! sqgetGlobalVar('str',$tmp,SQ_GET)) {
+            $result .= $prefix . 'str=' . $aMailbox['SORT'];
+            $prefix = '&amp;';
+        }
+
         if (isset($checkall) && $checkall == '1') {
-            $result .= '0';
+            $checkall_val = '0';
         } else {
-            $result .= '1';
+            $checkall_val = '1';
+        }
+        if (! sqgetGlobalVar('checkall',$tmp,SQ_GET) ) {
+            $result .= $prefix . 'checkall=' . $checkall_val;
+            $prefix = '&amp;';
+        } else {
+            // checkall is already present in php_self. replace it
+            $result = preg_replace("/checkall=(\d)/","checkall=$checkall_val",$result);
         }
 
-        if (isset($aMailbox['SEARCH']) && $aMailbox['SEARCH'][0]) {
-            $result .= '&amp;where=' . urlencode($aMailbox['SEARCH'][0])
-                    .  '&amp;what=' .  urlencode($aMailbox['SEARCH'][1]);
+        // FIXME: I suspect that search pages use different variables in 1.5.1cvs
+        // and these variables are present in $PHP_SELF.
+        if (isset($aMailbox['SEARCH']) && isset($aMailbox['SEARCH'][0]) && ! sqgetGlobalVar('where',$tmp,SQ_GET)) {
+            $result .= '&amp;where=' . urlencode($aMailbox['SEARCH'][0]);
+            if (isset($aMailbox['SEARCH'][1]) && ! sqgetGlobalVar('what',$tmp,SQ_GET)) {
+                $result .= '&amp;what=' .  urlencode($aMailbox['SEARCH'][1]);
+            }
         }
         $result .= "\">";
         $result .= _("All");
@@ -1469,13 +1609,13 @@ function get_selectall_link($aMailbox) {
 }
 
 /**
-* This function computes the "Viewing Messages..." string.
-*
-* @param integer $start_msg first message number
-* @param integer $end_msg last message number
-* @param integer $num_msgs total number of message in folder
-* @return string
-*/
+ * This function computes the "Viewing Messages..." string.
+ *
+ * @param integer $start_msg first message number
+ * @param integer $end_msg last message number
+ * @param integer $num_msgs total number of message in folder
+ * @return string
+ */
 function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
     /* Compute the $msg_cnt_str. */
     $result = '';
@@ -1483,7 +1623,7 @@ function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
         $result = sprintf(_("Viewing Messages: %s to %s (%s total)"),
                         '<b>'.$start_msg.'</b>', '<b>'.$end_msg.'</b>', $num_msgs);
     } else if ($start_msg == $end_msg) {
-        $result = sprintf(_("Viewing Message: %s (1 total)"), '<b>'.$start_msg.'</b>');
+        $result = sprintf(_("Viewing Message: %s (%s total)"), '<b>'.$start_msg.'</b>', $num_msgs);
     } else {
         $result = '<br />';
     }
@@ -1492,14 +1632,14 @@ function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
 }
 
 /**
-* Generate a paginator link.
-*
-* @param mixed $box Mailbox name
-* @param mixed $start_msg Message Offset
-* @param mixed $use
-* @param string $text text used for paginator link
-* @return string
-*/
+ * Generate a paginator link.
+ *
+ * @param mixed $box Mailbox name
+ * @param mixed $start_msg Message Offset
+ * @param mixed $use
+ * @param string $text text used for paginator link
+ * @return string
+ */
 function get_paginator_link($box, $start_msg, $text) {
     sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
     $result = "<a href=\"$php_self?startMessage=$start_msg&amp;mailbox=$box\" "
@@ -1509,17 +1649,19 @@ function get_paginator_link($box, $start_msg, $text) {
 }
 
 /**
-* This function computes the paginator string.
-*
-* @param string  $box      mailbox name
-* @param integer $iOffset  offset in total number of messages
-* @param integer $iTotal   total number of messages
-* @param integer $iLimit   maximum number of messages to show on a page
-* @param bool    $bShowAll show all messages at once (non paginate mode)
-* @return string $result   paginate string with links to pages
-*/
+ * This function computes the paginator string.
+ *
+ * @param string  $box      mailbox name
+ * @param integer $iOffset  offset in total number of messages
+ * @param integer $iTotal   total number of messages
+ * @param integer $iLimit   maximum number of messages to show on a page
+ * @param bool    $bShowAll show all messages at once (non paginate mode)
+ * @return string $result   paginate string with links to pages
+ */
 function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll) {
-    global $username, $data_dir;
+    global $username, $data_dir, $javascript_on;
+    // page selector globals
+    global $page_selector, $page_selector_max, $compact_paginator;
     sqgetGlobalVar('PHP_SELF',$php_self,SQ_SERVER);
 
     /* Initialize paginator string chunks. */
@@ -1534,10 +1676,6 @@ function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll) {
     $spc = '&nbsp;';     /* This will be used as a space. */
     $sep = '|';          /* This will be used as a seperator. */
 
-    /* Get some paginator preference values. */
-    $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON);
-    $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX);
-
     /* Make sure that our start message number is not too big. */
     $iOffset = min($iOffset, $iTotal);
 
@@ -1547,130 +1685,147 @@ function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll) {
 
     if (!$bShowAll) {
         /* Compute the basic previous and next strings. */
-        if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
-            $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
-            $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
-        } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
-            $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
-            $nxt_str = _("Next");
-        } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
-            $prv_str = _("Previous");
-            $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
+        if ($compact_paginator) {
+            if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
+                $prv_str = get_paginator_link($box, $prev_grp, '<');
+                $nxt_str = get_paginator_link($box, $next_grp, '>');
+            } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
+                $prv_str = get_paginator_link($box, $prev_grp, '<');
+                $nxt_str = '>';
+            } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
+                $prv_str = '<';
+                $nxt_str = get_paginator_link($box, $next_grp, '>');
+            }
+        } else {
+            if (($next_grp <= $iTotal) && ($prev_grp >= 0)) {
+                $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
+                $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
+            } else if (($next_grp > $iTotal) && ($prev_grp >= 0)) {
+                $prv_str = get_paginator_link($box, $prev_grp, _("Previous"));
+                $nxt_str = _("Next");
+            } else if (($next_grp <= $iTotal) && ($prev_grp < 0)) {
+                $prv_str = _("Previous");
+                $nxt_str = get_paginator_link($box, $next_grp, _("Next"));
+            }
         }
 
         /* Page selector block. Following code computes page links. */
-        if ($iLimit != 0 && $pg_sel && ($iTotal > $iLimit)) {
+        if ($iLimit != 0 && $page_selector && ($iTotal > $iLimit)) {
             /* Most importantly, what is the current page!!! */
             $cur_pg = intval($iOffset / $iLimit) + 1;
 
             /* Compute total # of pages and # of paginator page links. */
             $tot_pgs = ceil($iTotal / $iLimit);  /* Total number of Pages */
-            $vis_pgs = min($pg_max, $tot_pgs - 1);   /* Visible Pages    */
 
-            /* Compute the size of the four quarters of the page links. */
+            if (!$compact_paginator) {
+                $vis_pgs = min($page_selector_max, $tot_pgs - 1);   /* Visible Pages    */
 
-            /* If we can, just show all the pages. */
-            if (($tot_pgs - 1) <= $pg_max) {
-                $q1_pgs = $cur_pg - 1;
-                $q2_pgs = $q3_pgs = 0;
-                $q4_pgs = $tot_pgs - $cur_pg;
+                /* Compute the size of the four quarters of the page links. */
 
-            /* Otherwise, compute some magic to choose the four quarters. */
-            } else {
-                /*
-                * Compute the magic base values. Added together,
-                * these values will always equal to the $pag_pgs.
-                * NOTE: These are DEFAULT values and do not take
-                * the current page into account. That is below.
-                */
-                $q1_pgs = floor($vis_pgs/4);
-                $q2_pgs = round($vis_pgs/4, 0);
-                $q3_pgs = ceil($vis_pgs/4);
-                $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
-
-                /* Adjust if the first quarter contains the current page. */
-                if (($cur_pg - $q1_pgs) < 1) {
-                    $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
+                /* If we can, just show all the pages. */
+                if (($tot_pgs - 1) <= $page_selector_max) {
                     $q1_pgs = $cur_pg - 1;
-                    $q2_pgs = 0;
-                    $q3_pgs += ceil($extra_pgs / 2);
-                    $q4_pgs += floor($extra_pgs / 2);
-
-                /* Adjust if the first and second quarters intersect. */
-                } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
-                    $extra_pgs = $q2_pgs;
-                    $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
-                    $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
-                    $q3_pgs += ceil($extra_pgs / 2);
-                    $q4_pgs += floor($extra_pgs / 2);
-
-                /* Adjust if the fourth quarter contains the current page. */
-                } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
-                    $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
-                    $q3_pgs = 0;
+                    $q2_pgs = $q3_pgs = 0;
                     $q4_pgs = $tot_pgs - $cur_pg;
-                    $q1_pgs += floor($extra_pgs / 2);
-                    $q2_pgs += ceil($extra_pgs / 2);
-
-                /* Adjust if the third and fourth quarter intersect. */
-                } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
-                    $extra_pgs = $q3_pgs;
-                    $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
-                    $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
-                    $q1_pgs += floor($extra_pgs / 2);
-                    $q2_pgs += ceil($extra_pgs / 2);
+
+                /* Otherwise, compute some magic to choose the four quarters. */
+                } else {
+                    /*
+                    * Compute the magic base values. Added together,
+                    * these values will always equal to the $pag_pgs.
+                    * NOTE: These are DEFAULT values and do not take
+                    * the current page into account. That is below.
+                    */
+                    $q1_pgs = floor($vis_pgs/4);
+                    $q2_pgs = round($vis_pgs/4, 0);
+                    $q3_pgs = ceil($vis_pgs/4);
+                    $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
+
+                    /* Adjust if the first quarter contains the current page. */
+                    if (($cur_pg - $q1_pgs) < 1) {
+                        $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
+                        $q1_pgs = $cur_pg - 1;
+                        $q2_pgs = 0;
+                        $q3_pgs += ceil($extra_pgs / 2);
+                        $q4_pgs += floor($extra_pgs / 2);
+
+                    /* Adjust if the first and second quarters intersect. */
+                    } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
+                        $extra_pgs = $q2_pgs;
+                        $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
+                        $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
+                        $q3_pgs += ceil($extra_pgs / 2);
+                        $q4_pgs += floor($extra_pgs / 2);
+
+                    /* Adjust if the fourth quarter contains the current page. */
+                    } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
+                        $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
+                        $q3_pgs = 0;
+                        $q4_pgs = $tot_pgs - $cur_pg;
+                        $q1_pgs += floor($extra_pgs / 2);
+                        $q2_pgs += ceil($extra_pgs / 2);
+
+                    /* Adjust if the third and fourth quarter intersect. */
+                    } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
+                        $extra_pgs = $q3_pgs;
+                        $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
+                        $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
+                        $q1_pgs += floor($extra_pgs / 2);
+                        $q2_pgs += ceil($extra_pgs / 2);
+                    }
                 }
-            }
 
-            /*
-            * I am leaving this debug code here, commented out, because
-            * it is a really nice way to see what the above code is doing.
-            * echo "qts =  $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
-            *    . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
-            */
+                /*
+                * I am leaving this debug code here, commented out, because
+                * it is a really nice way to see what the above code is doing.
+                * echo "qts =  $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
+                *    . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br />';
+                */
 
-            /* Print out the page links from the compute page quarters. */
+                /* Print out the page links from the compute page quarters. */
 
-            /* Start with the first quarter. */
-            if (($q1_pgs == 0) && ($cur_pg > 1)) {
-                $pg_str .= "...$spc";
-            } else {
-                for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
-                    $start = (($pg-1) * $iLimit) + 1;
-                    $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
-                }
-                if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
+                /* Start with the first quarter. */
+                if (($q1_pgs == 0) && ($cur_pg > 1)) {
                     $pg_str .= "...$spc";
+                } else {
+                    for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
+                        $start = (($pg-1) * $iLimit) + 1;
+                        $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
+                    }
+                    if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
+                        $pg_str .= "...$spc";
+                    }
                 }
-            }
 
-            /* Continue with the second quarter. */
-            for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
-                $start = (($pg-1) * $iLimit) + 1;
-                $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
-            }
+                /* Continue with the second quarter. */
+                for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
+                    $start = (($pg-1) * $iLimit) + 1;
+                    $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
+                }
 
-            /* Now print the current page. */
-            $pg_str .= $cur_pg . $spc;
+                /* Now print the current page. */
+                $pg_str .= $cur_pg . $spc;
 
-            /* Next comes the third quarter. */
-            for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
-                $start = (($pg-1) * $iLimit) + 1;
-                $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
-            }
+                /* Next comes the third quarter. */
+                for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
+                    $start = (($pg-1) * $iLimit) + 1;
+                    $pg_str .= get_paginator_link($box, $start, $pg) . $spc;
+                }
 
-            /* And last, print the forth quarter page links. */
-            if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
-                $pg_str .= "...$spc";
-            } else {
-                if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
+                /* And last, print the forth quarter page links. */
+                if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
                     $pg_str .= "...$spc";
-                }
-                for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
-                    $start = (($pg-1) * $iLimit) + 1;
-                    $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
+                } else {
+                    if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
+                        $pg_str .= "...$spc";
+                    }
+                    for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
+                        $start = (($pg-1) * $iLimit) + 1;
+                        $pg_str .= get_paginator_link($box, $start,$pg) . $spc;
+                    }
                 }
             }
+        $last_grp = (($tot_pgs - 1) * $iLimit) + 1;
         }
     } else {
         $pg_str = "<a href=\"$php_self?showall=0"
@@ -1680,9 +1835,9 @@ function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll) {
 
     /* Put all the pieces of the paginator string together. */
     /**
-    * Hairy code... But let's leave it like it is since I am not certain
-    * a different approach would be any easier to read. ;)
-    */
+     * Hairy code... But let's leave it like it is since I am not certain
+     * a different approach would be any easier to read. ;)
+     */
     $result = '';
     if ( $prv_str || $nxt_str ) {
 
@@ -1690,28 +1845,69 @@ function get_paginator_str($box, $iOffset, $iTotal, $iLimit, $bShowAll) {
         $all_str = "<a href=\"$php_self?showall=1"
                 . "&amp;startMessage=1&amp;mailbox=$box\" "
                 . ">" . _("Show All") . '</a>';
-        $result .= '[';
-        $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
-        $result .= ($nxt_str != '' ? $nxt_str : '');
-        $result .= ']' . $spc ;
-
     }
 
-    $result .= ($pg_str  != '' ? $spc . '['.$spc.$pg_str.']' .  $spc : '');
-    $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
+    if ($compact_paginator) {
+        if ( $prv_str || $nxt_str ) {
+            $result .= '[' . get_paginator_link($box, 1, '<<') . ']';
+            $result .= '[' . $prv_str . ']';
+
+            $pg_url = $php_self . '?mailbox=' . $box;
 
+            $result .= '[' . $nxt_str . ']';
+            $result .= '[' . get_paginator_link($box, $last_grp, '>>') . ']';
+
+            if ($page_selector) {
+                $result .= $spc . '<select name="startMessage"';
+                if ($javascript_on) {
+                    $result .= ' onchange="JavaScript:SubmitOnSelect'
+                        . '(this, \'' . $pg_url . '&amp;startMessage=\')"';
+                }
+                $result .='>';
+
+                for ($p = 0; $p < $tot_pgs; $p++) {
+                    $result .= '<option ';
+                    if (($p+1) == $cur_pg) $result .= 'selected ';
+                        $result .= 'value="' . (($p*$iLimit)+1) . '">'
+                         . ($p+1) . "/$tot_pgs" . '</option>';
+                }
+
+                $result .= '</select>';
+
+                if ($javascript_on) {
+                    $result .= '<noscript language="JavaScript">'
+                    . addSubmit(_("Go"))
+                    . '</noscript>';
+                } else {
+                    $result .= addSubmit(_("Go"));
+                }
+            }
+        }
+
+        $result .= ($pg_str  != '' ? '['.$pg_str.']' .  $spc : '');
+        $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
+    } else {
+        if ( $prv_str || $nxt_str ) {
+            $result .= '[';
+            $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
+            $result .= ($nxt_str != '' ? $nxt_str : '');
+            $result .= ']' . $spc ;
+        }
+
+        $result .= ($pg_str  != '' ? $spc . '['.$spc.$pg_str.']' .  $spc : '');
+        $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
+    }
     /* If the resulting string is blank, return a non-breaking space. */
     if ($result == '') {
         $result = '&nbsp;';
     }
-
     /* Return our final magical paginator string. */
     return ($result);
 }
 
 /**
-* FIXME: Undocumented function
-*/
+ * FIXME: Undocumented function
+ */
 function truncateWithEntities($subject, $trim_at)
 {
     $ent_strlen = strlen($subject);
@@ -1721,11 +1917,11 @@ function truncateWithEntities($subject, $trim_at)
     global $languages, $squirrelmail_language;
 
     /*
-    * see if this is entities-encoded string
-    * If so, Iterate through the whole string, find out
-    * the real number of characters, and if more
-    * than $trim_at, substr with an updated trim value.
-    */
+     * see if this is entities-encoded string
+     * If so, Iterate through the whole string, find out
+     * the real number of characters, and if more
+     * than $trim_at, substr with an updated trim value.
+     */
     $trim_val = $trim_at;
     $ent_offset = 0;
     $ent_loc = 0;
@@ -1745,16 +1941,16 @@ function truncateWithEntities($subject, $trim_at)
         return $subject;
 
     if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
-        function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
-        return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val);
+        function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth')) {
+        return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_strimwidth', $subject, $trim_val);
     }
 
-    return substr_replace($subject, '...', $trim_val);
+    return substr_replace($subject, '...', $trim_val + 1);
 }
 
 /**
-* FIXME: Undocumented function
-*/
+ * FIXME: Undocumented function
+ */
 function processSubject($subject, $threadlevel = 0) {
     /* Shouldn't ever happen -- caught too many times in the IMAP functions */
     if ($subject == '') {
@@ -1773,15 +1969,15 @@ function processSubject($subject, $threadlevel = 0) {
 
 
 /**
-* Creates button
-*
-* @deprecated see form functions available in 1.5.1 and 1.4.3.
-* @param string $type
-* @param string $name
-* @param string $value
-* @param string $js
-* @param bool $enabled
-*/
+ * Creates button
+ *
+ * @deprecated see form functions available in 1.5.1 and 1.4.3.
+ * @param string $type
+ * @param string $name
+ * @param string $value
+ * @param string $js
+ * @param bool $enabled
+ */
 function getButton($type, $name, $value, $js = '', $enabled = TRUE) {
     $disabled = ( $enabled ? '' : 'disabled ' );
     $js = ( $js ? $js.' ' : '' );
@@ -1793,23 +1989,23 @@ function getButton($type, $name, $value, $js = '', $enabled = TRUE) {
 }
 
 /**
-* Puts string into cell, aligns it and adds <small> tag
-*
-* @param string $string string
-* @param string $align alignment
-*/
+ * Puts string into cell, aligns it and adds <small> tag
+ *
+ * @param string $string string
+ * @param string $align alignment
+ */
 function getSmallStringCell($string, $align) {
     return html_tag('td',
                     '<small>' . $string . ':&nbsp; </small>',
                     $align,
                     '',
-                    'nowrap' );
+                    'style="white-space: nowrap;"' );
 }
 
 /**
-* This should go in imap_mailbox.php
-* @param string $mailbox
-*/
+ * This should go in imap_mailbox.php
+ * @param string $mailbox
+ */
 function handleAsSent($mailbox) {
     global $handleAsSent_result;
 
@@ -1867,11 +2063,8 @@ function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = ar
                 !in_array($aUid[0],$aMailbox['UIDSET'][$aMailbox['SETINDEX']])) {
                 break;
             }
-            // What kind of hook is this, can it be removed? Disabled for now because it can invalidate the cache
-            //if (!boolean_hook_function('move_messages_button_action', NULL, 1)) {
-                $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
-                $bExpunge = true;
-            //}
+            $aUpdatedMsgs = sqimap_msgs_list_delete($imapConnection, $mailbox, $aUid,$bypass_trash);
+            $bExpunge = true;
             break;
           case 'unsetDeleted':
           case 'setSeen':
@@ -1899,6 +2092,10 @@ function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = ar
                 $aMailbox['FORWARD_SESSION'] = $composesession;
             }
             break;
+          default:
+            // Hook for plugin buttons
+            do_hook_function('mailbox_display_button_action', $aUid);
+            break;
         }
         /**
          * Updates messages is an array containing the result of the untagged
@@ -1960,9 +2157,10 @@ function handleMessageListForm($imapConnection,&$aMailbox,$sButton='',$aUid = ar
                     $aMailbox['EXISTS'] -= (int) $iExpungedMessages;
                 }
                 // Change the startMessage number if the mailbox was changed
-                if (($aMailbox['PAGEOFFSET']+$iExpungedMessages-1) >= $aMailbox['EXISTS']) {
+                if (($aMailbox['PAGEOFFSET']-1) >= $aMailbox['EXISTS']) {
                     $aMailbox['PAGEOFFSET'] = ($aMailbox['PAGEOFFSET'] > $aMailbox['LIMIT']) ?
                         $aMailbox['PAGEOFFSET'] - $aMailbox['LIMIT'] : 1;
+                    $aMailbox['OFFSET'] = $aMailbox['PAGEOFFSET'] - 1 ;
                 }
             }
         }
@@ -2046,4 +2244,5 @@ function attachSelectedMessages($imapConnection,$aMsgHeaders) {
     return $composesession;
 }
 
+// vim: et ts=4
 ?>