<?php
$chosenCategories = explode(';', $prefs->getValue('summary_categories'));
if ($chosenCategories != array('')) {
    foreach ($chosenCategories as $chosenColumn) {
        list($categories[],) = explode(',', $chosenColumn);
    }
} else {
    $categories = array();
}

$js = "var categories = new Array();\n";

require_once 'Horde/Prefs/CategoryManager.php';
$cManager = &new Prefs_CategoryManager();
$column_count = 0;
foreach ($cManager->get() as $id => $name) {
    if (in_array($id, $categories)) {
        $js .= "categories[$column_count] = new Array('$id', '$name', true);\n";
	} else {
        $js .= "categories[$column_count] = new Array('$id', '$name', false);\n";
    }
    $column_count++;
}

?>
<script language="JavaScript" type="text/javascript">
<!--
<?php echo $js ?>

function deselectHeaders()
{
    document.prefs.unselected_categories[0].selected = false;
    document.prefs.selected_categories[0].selected = false;
}

function resetHidden()
{
    var tmp = '';
    var first = '';
    var f = document.prefs;
    var i = 0;
    for (var j = 1; j < f.selected_categories.length; j++) {
        if (f.selected_categories[j].value) {
            tmp += first + f.selected_categories[j].value + ',' + i;
            first = ';';
            i++;
        }
    }
    document.prefs.summary_categories.value = tmp;
}

function addColumn()
{
    var f = document.prefs;

    for (i = 1; i < f.unselected_categories.length; i++) {
        if (f.unselected_categories[i].selected) {
            for (var j = 1; j < categories.length; j++) {
                if (categories[j][0] == f.unselected_categories[i].value) {
                    categories[j][2] = true;
                }
            }
            f.selected_categories[f.selected_categories.length] = new Option(f.unselected_categories[i].text, f.unselected_categories[i].value);
            f.unselected_categories[i] = null;
            i--;
        }
    }

    resetHidden();
}

function removeColumn()
{
    var f = document.prefs;

    for (i = 1; i < f.selected_categories.length; i++) {
        if (f.selected_categories[i].selected) {
            for (var j = 1; j < categories.length; j++) {
                if (categories[j][0] == f.selected_categories[i].value) {
                    categories[j][2] = false;
                }
            }
            f.unselected_categories[f.unselected_categories.length] = new Option(f.selected_categories[i].text, f.selected_categories[i].value)
            f.selected_categories[i] = null;
            i--;
        }
    }

    resetHidden();
}
// -->
</script>
<?php echo _("Choose the memo categories to list in the summary view.") ?><br />
<input type="hidden" name="summary_categories" value="<?php echo $prefs->getValue('summary_categories') ?>" />
<table>
  <tr>
    <td>
      <select name="unselected_categories" multiple="multiple" size="10" width="30" onchange="deselectHeaders();">
        <option value=""><?php echo _("Available Categories:") ?></option>
      </select>
    </td>
    <td>
      <a href="" onclick="addColumn(); return false;"><?php echo Horde::img('rhand.png', _("Add category"), '', $registry->getImageDir('horde')) ?></a>
      <br />
      <a href="" onclick="removeColumn(); return false;"><?php echo Horde::img('lhand.png', _("Remove category"), '', $registry->getImageDir('horde')) ?></a>
    </td>
    <td>
      <select name="selected_categories" multiple="multiple" size="10" width="30" onchange="deselectHeaders();">
        <option value=""><?php echo _("Selected Categories:") ?></option>
      </select>
    </td>
  </tr>
</table>
<script language="JavaScript" type="text/javascript">
<!--
var f = document.prefs;
var fieldString = '';

while (f.unselected_categories.length > 1) {
    f.unselected_categories.options[f.unselected_categories.length - 1] = null;
}
while (f.selected_categories.length > 1) {
    f.selected_categories.options[f.selected_categories.length - 1] = null;
}

var selected = new Array();
var unselected = new Array();
for (var i = 0; i < categories.length; i++) {
    if (categories[i][2]) {
        selected[selected.length] = new Array(categories[i][1], categories[i][0]);
    } else {
        unselected[unselected.length] = new Array(categories[i][1], categories[i][0]);
    }
}
for (i = 0; i < selected.length; i++) {
    f.selected_categories.options[i + 1] = new Option(selected[i][0], selected[i][1]);
}
for (i = 0; i < unselected.length; i++) {
    f.unselected_categories.options[i + 1] = new Option(unselected[i][0], unselected[i][1]);
}

//-->
</script>
