<script language="JavaScript" type="text/javascript">
<!--
<?php if (is_array($filters) && count($filters)): ?>
var fields = new Array(<?php
for ($i = 0; $i < count($filters); $i++) {
    if ($i > 0) { echo ', '; }
    echo 'new Array(';
    for ($j = 0; $j < count($filters[$i]['fields']); $j++) {
        if ($j > 0) { echo ', '; }
        echo '"' . $fieldsList[$filters[$i]['fields'][$j]] . '"';
    }
    echo ')';
}
?>
);

var strings = new Array(<?php
for ($i = 0; $i < count($filters); $i++) {
    if ($i > 0) { echo ', '; }
    echo '"' . addslashes($filters[$i]['text']) . '"';
}
?>
);

var actions = new Array(<?php
for ($i = 0; $i < count($filters); $i++) {
    if ($i > 0) { echo ', '; }
    echo '"' . $actionsList[$filters[$i]['action']] . '"';
}
?>
);

var folders = new Array(<?php
for ($i = 0; $i < count($filters); $i++) {
    if ($i > 0) { echo ', '; }
    echo ($filters[$i]['action'] == 'move') ? ('"' . addslashes($filters[$i]['folder']) . '"') : 'false';
}
?>
);
<?php endif; ?>

function selectRule(num)
{
    for (var i = 0; i < document.filters.elements['fields[]'].length; i++) {
        document.filters.elements['fields[]'][i].checked = false;
    }

    for (var i = 0; i < fields[num].length; i++) {
        document.filters.elements['fields[]'][fields[num][i]].checked = true;
    }

    document.filters.text.value = strings[num];
    document.filters.action[actions[num]].checked = true;

    if (folders[num] && document.filters.folder) {
        for (i = 0; i < document.filters.folder.length; i++) {
            if (document.filters.folder[i].value == folders[num]) {
                document.filters.folder.selectedIndex = i;
                break;
            }
        }
    }
    else if (document.filters.folder) {
        document.filters.folder.selectedIndex = 0;
    }

    document.filters.button.value = '<?php echo _("Modify") ?>';
    document.filters.actionID.value = '<?php echo FILTER_MODIFY ?>';
}

function resetForm()
{
    document.filters.reset();
    document.filters.actionID.value = '<?php echo FILTER_CREATE ?>';
    document.filters.button.value = '<?php echo _("Create") ?>';
    if (document.filters.number) {
        for (var i = 0; i < document.filters.number.length; i++) {
            document.filters.number[i].checked = false;
        }
    }
}

function handleErrors()
{
<?php if (!empty($_POST['fields'])): ?>
<?php for ($i = 0; $i < count($_POST['fields']); $i++): ?>
    document.filters.elements['fields[]'][<?php echo $fieldsList[$_POST['fields'][$i]]; ?>].checked = true;
<?php endfor; ?>
<?php endif; ?>
<?php if (!empty($_POST['text'])): ?>
    document.filters.text.value = '<?php echo addslashes($_POST['text']) ?>';
<?php endif; ?>
<?php if (!empty($_POST['action'])): ?>
    document.filters.action[<?php echo $actionsList[$_POST['action']] ?>].checked = true;
<?php endif; ?>
<?php if (!empty($_POST['folder'])): ?>
    if (document.filters.folder) {
        for (i = 0; i < document.filters.folder.length; i++) {
            if (document.filters.folder[i].value == '<?php echo addslashes($_POST['folder']) ?>') {
                document.filters.folder.selectedIndex = i;
                break;
            }
        }
    }
<?php endif; ?>
<?php if (isset($_POST['number']) && !empty($_POST['number'])): ?>
    document.filters.number[<?php echo $_POST['number'] ?>].checked = true;
    document.filters.button.value = '<?php echo _("Modify") ?>';
    document.filters.actionID.value = '<?php echo FILTER_MODIFY ?>';
<?php endif; ?>
}
//-->
</script>
