/**
 * Format the address book into a format that is easy for template authors
 * to use
 * 
 * @param array $addresses all contacts as given by calling $abook->list_addr()
 * @return array
 * @author Steve Brown
 * @since 1.5.2
 */
function formatAddressList($addresses)
{
    if (!is_array($addresses) || count($addresses) == 0) {
        return array();
    }
    $contacts = array();
    while (list($undef, $row) = each($addresses)) {
        $contact = array('FirstName' => sm_encode_html_special_chars($row['firstname']), 'LastName' => sm_encode_html_special_chars($row['lastname']), 'FullName' => sm_encode_html_special_chars($row['name']), 'NickName' => sm_encode_html_special_chars($row['nickname']), 'Email' => sm_encode_html_special_chars($row['email']), 'FullAddress' => sm_encode_html_special_chars(AddressBook::full_address($row)), 'RawFullAddress' => AddressBook::full_address($row), 'Info' => sm_encode_html_special_chars($row['label']), 'Extra' => isset($row['extra']) ? $row['extra'] : NULL, 'Source' => sm_encode_html_special_chars($row['source']), 'JSEmail' => sm_encode_html_special_chars(addcslashes(AddressBook::full_address($row), "'"), ENT_QUOTES));
        $contacts[] = $contact;
    }
    return $contacts;
}
/**
 * List search results
 * @param array $res Array containing results of search
 * @param bool $includesource If true, adds backend column to address listing
 */
function addr_display_result($res, $includesource = true)
{
    global $color, $javascript_on, $PHP_SELF, $squirrelmail_language;
    if (sizeof($res) <= 0) {
        return;
    }
    echo addForm($PHP_SELF, 'POST', 'addrbook') . addHidden('html_addr_search_done', 'true');
    addr_insert_hidden();
    $line = 0;
    if ($javascript_on) {
        print '<script language="JavaScript" type="text/javascript">' . "\n<!-- \n" . "function CheckAll(ch) {\n" . "   for (var i = 0; i < document.addrbook.elements.length; i++) {\n" . "       if( document.addrbook.elements[i].type == 'checkbox' &&\n" . "           document.addrbook.elements[i].name.substr(0,16) == 'send_to_search['+ch ) {\n" . "           document.addrbook.elements[i].checked = !(document.addrbook.elements[i].checked);\n" . "       }\n" . "   }\n" . "}\n" . "//-->\n" . "</script>\n";
        $chk_all = '<a href="#" onClick="CheckAll(\'T\');">' . _("All") . '</a>&nbsp;<font color="' . $color[9] . '">' . _("To") . '</font>' . '&nbsp;&nbsp;' . '<a href="#" onClick="CheckAll(\'C\');">' . _("All") . '</a>&nbsp;<font color="' . $color[9] . '">' . _("Cc") . '</font>' . '&nbsp;&nbsp;' . '<a href="#" onClick="CheckAll(\'B\');">' . _("All") . '</a>';
    } else {
        // check_all links are implemented only in JavaScript. disable links in js=off environment.
        $chk_all = '';
    }
    echo html_tag('table', '', 'center', '', 'border="0" width="98%"') . html_tag('tr', '', '', $color[9]) . html_tag('th', '&nbsp;' . $chk_all, 'left') . html_tag('th', '&nbsp;' . _("Name"), 'left') . html_tag('th', '&nbsp;' . _("E-mail"), 'left') . html_tag('th', '&nbsp;' . _("Info"), 'left');
    if ($includesource) {
        echo html_tag('th', '&nbsp;' . _("Source"), 'left', '', 'width="10%"');
    }
    echo "</tr>\n";
    foreach ($res as $row) {
        $email = AddressBook::full_address($row);
        if ($line % 2) {
            $tr_bgcolor = $color[12];
        } else {
            $tr_bgcolor = $color[4];
        }
        if ($squirrelmail_language == 'ja_JP') {
            echo html_tag('tr', '', '', $tr_bgcolor, 'nowrap') . html_tag('td', '<input type="checkbox" name="send_to_search[T' . $line . ']" value = "' . htmlspecialchars($email) . '" />&nbsp;' . _("To") . '&nbsp;' . '<input type="checkbox" name="send_to_search[C' . $line . ']" value = "' . htmlspecialchars($email) . '" />&nbsp;' . _("Cc") . '&nbsp;' . '<input type="checkbox" name="send_to_search[B' . $line . ']" value = "' . htmlspecialchars($email) . '" />&nbsp;' . _("Bcc") . '&nbsp;', 'center', '', 'width="5%" nowrap') . html_tag('td', '&nbsp;' . htmlspecialchars($row['lastname']) . ' ' . htmlspecialchars($row['firstname']) . '&nbsp;', 'left', '', 'nowrap') . html_tag('td', '&nbsp;' . htmlspecialchars($row['email']) . '&nbsp;', 'left', '', 'nowrap') . html_tag('td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'nowrap');
        } else {
            echo html_tag('tr', '', '', $tr_bgcolor, 'nowrap') . html_tag('td', addCheckBox('send_to_search[T' . $line . ']', FALSE, $email) . '&nbsp;' . _("To") . '&nbsp;' . addCheckBox('send_to_search[C' . $line . ']', FALSE, $email) . '&nbsp;' . _("Cc") . '&nbsp;' . addCheckBox('send_to_search[B' . $line . ']', FALSE, $email) . '&nbsp;' . _("Bcc") . '&nbsp;', 'center', '', 'width="5%" nowrap') . html_tag('td', '&nbsp;' . htmlspecialchars($row['name']) . '&nbsp;', 'left', '', 'nowrap') . html_tag('td', '&nbsp;' . htmlspecialchars($row['email']) . '&nbsp;', 'left', '', 'nowrap') . html_tag('td', '&nbsp;' . htmlspecialchars($row['label']) . '&nbsp;', 'left', '', 'nowrap');
        }
        if ($includesource) {
            echo html_tag('td', '&nbsp;' . $row['source'] . '&nbsp;', 'left', '', 'nowrap');
        }
        echo "</tr>\n";
        $line++;
    }
    if ($includesource) {
        $td_colspan = '5';
    } else {
        $td_colspan = '4';
    }
    echo html_tag('tr', html_tag('td', '<input type="submit" name="addr_search_done" value="' . _("Use Addresses") . '" /> ' . '<input type="submit" name="addr_search_cancel" value="' . _("Cancel") . '" />', 'center', '', 'colspan="' . $td_colspan . '"')) . '</table>' . addHidden('html_addr_search_done', '1') . '</form>';
}
/**
 * List search results
 * @param array $res Array of search results
 * @param bool $includesource [Default=true]
 * @return void
 */
function display_result($res, $includesource = true)
{
    global $color;
    if (sizeof($res) <= 0) {
        return;
    }
    insert_javascript();
    $line = 0;
    echo html_tag('table', '', 'center', '', 'border="0" width="98%"') . html_tag('tr', '', '', $color[9]) . html_tag('th', '&nbsp;', 'left') . html_tag('th', '&nbsp;' . _("Name"), 'left') . html_tag('th', '&nbsp;' . _("E-mail"), 'left') . html_tag('th', '&nbsp;' . _("Info"), 'left');
    if ($includesource) {
        echo html_tag('th', '&nbsp;' . _("Source"), 'left', '', 'width="10%"');
    }
    echo "</tr>\n";
    while (list($undef, $row) = each($res)) {
        $email = htmlspecialchars(addcslashes(AddressBook::full_address($row), "'"), ENT_QUOTES);
        if ($line % 2) {
            $tr_bgcolor = $color[12];
        } else {
            $tr_bgcolor = $color[4];
        }
        echo html_tag('tr', '', '', $tr_bgcolor, 'nowrap') . html_tag('td', '<small><a href="javascript:to_address(' . "'" . $email . "');\">" . _("To") . "</a> | " . '<a href="javascript:cc_address(' . "'" . $email . "');\">" . _("Cc") . "</a> | " . '<a href="javascript:bcc_address(' . "'" . $email . "');\">" . _("Bcc") . "</a></small>", 'center', '', 'valign="top" width="5%" nowrap') . html_tag('td', '&nbsp;' . htmlspecialchars($row['name']), 'left', '', 'valign="top" nowrap') . html_tag('td', '&nbsp;' . '<a href="javascript:to_and_close(' . "'" . $email . "');\">" . htmlspecialchars($row['email']) . '</a>', 'left', '', 'valign="top"') . html_tag('td', htmlspecialchars($row['label']), 'left', '', 'valign="top" nowrap');
        if ($includesource) {
            echo html_tag('td', '&nbsp;' . $row['source'], 'left', '', 'valign="top" nowrap');
        }
        echo "</tr>\n";
        $line++;
    }
    echo '</table>';
}