<script language="JavaScript" type="text/javascript">
<!--

function AnySelected()
{
    for (i = 0; i < document.messages.elements.length; i++) {
        if (document.messages.elements[i].checked) return true;
    }
    return false;
}

function Submit(actID)
{
<?php if (strstr($imp['protocol'], 'pop3')): ?>
    if (window.confirm('<?php echo addslashes(_("Are you sure you wish to PERMANENTLY delete these messages?")) ?>')) {
<?php endif; ?>
    if (AnySelected()) {
        document.messages.actionID.value = actID;
        document.messages.submit();
    } else {
        window.alert('<?php echo addslashes(_("You must select at least one message first.")) ?>');
    }
<?php if (strstr($imp['protocol'], 'pop3')): ?>
    }
<?php endif; ?>
}

function toggle(isChecked)
{
    for (i = 0; i < document.messages.elements.length; i++) {
        document.messages.elements[i].checked = isChecked;
    }
}

function makeSelection(whichForm)
{
    if (whichForm == 1) {
        flag = document.select1.filter.options[document.select1.filter.selectedIndex].value;
    } else {
        flag = document.select2.filter.options[document.select2.filter.selectedIndex].value;
    }

    if (flag.substring(0, 1) == "!") {
        selectFlagged(parseInt(flag.substring(1)), false);
    } else {
        selectFlagged(parseInt(flag), true);
    }

    // reset the form
    if (whichForm == 1) {
        document.select1.reset();
    } else {
        document.select2.reset();
    }
}

<?php if ($conf['user']['allow_folders']): ?>
function updateFolders(whichForm)
{
    if ((whichForm == 1 && document.copymove1.targetMailbox.options[document.copymove1.targetMailbox.selectedIndex].value != "") ||
        (whichForm == 2 && document.copymove2.targetMailbox.options[document.copymove2.targetMailbox.selectedIndex].value != "")) {
        if (whichForm == 1) {
            document.copymove2.targetMailbox.selectedIndex = document.copymove1.targetMailbox.selectedIndex;
        } else {
            document.copymove1.targetMailbox.selectedIndex = document.copymove2.targetMailbox.selectedIndex;
        }
    }
} // updateFolders()

function Transfer(actID, whichForm)
{
    if (AnySelected()) {
        if (whichForm == 1) {
            document.messages.targetMbox.value = document.copymove1.targetMailbox.options[document.copymove1.targetMailbox.selectedIndex].value;
        } else {
            document.messages.targetMbox.value = document.copymove2.targetMailbox.options[document.copymove2.targetMailbox.selectedIndex].value;
        }

        // Check for a mailbox actually being selected.
        if (document.messages.targetMbox.value == '*new*') {
            var newFolder = window.prompt('<?php echo addslashes(_("You are copying/moving to a new folder.")) . '\n' . addslashes(_("Please enter a name for the new folder:")) ?>\n', '');
            if (newFolder != null && newFolder != '') {
                document.messages.newMbox.value = 1;
                document.messages.targetMbox.value = newFolder;
                document.messages.actionID.value = actID;
                document.messages.submit();
            }
        } else {
            if (document.messages.targetMbox.value == '') {
                window.alert('<?php echo addslashes(_("You must select a target mailbox first.")) ?>');
            } else {
                document.messages.actionID.value = actID;
                document.messages.submit();
            }
        }
    } else {
        window.alert('<?php echo addslashes(_("You must select at least one message first.")) ?>');
    }
}
<?php endif; ?>
<?php if (!$prefs->isLocked('filters')): ?>
function BlackList()
{
    if (AnySelected()) {
        document.messages.actionID.value = <?php echo IMP_BLACKLIST ?>;
        document.messages.submit();
    } else {
        window.alert('<?php echo addslashes(_("You must select at least one message first.")) ?>');
    }
}

<?php if ($show_whitelist): ?>
function WhiteList()
{
    if (AnySelected()) {
        document.messages.actionID.value = <?php echo IMP_WHITELIST ?>;
        document.messages.submit();
    } else {
        window.alert('<?php echo addslashes(_("You must select at least one message first.")) ?>');
    }
}
<?php endif; ?>
<?php endif; ?>

// Put everything reliant on IMAP flags in this section.
var Flags;

function selectFlagged(flag, val)
{
    shift = 0;
    for (var i = 0; i < document.messages.elements.length; i++) {
        while (document.messages.elements[i].name != "indices[]") {
            i++;
            shift++;
            if (!document.messages.elements[i]) {
                return;
            }
        }
        if (flag & Flags[i - shift]) {
            document.messages.elements[i].checked = val;
        } else {
            document.messages.elements[i].checked = !val;
        }
    }
}

<?php if (!strstr($imp['protocol'], 'pop3')): ?>
function flagMessages(whichForm)
{
    if ((whichForm == 1 && document.select1.flag.options[document.select1.flag.selectedIndex].value != "") ||
        (whichForm == 2 && document.select2.flag.options[document.select2.flag.selectedIndex].value != "")) {
        if (AnySelected()) {
            if (whichForm == 1) {
                document.messages.flag.value = document.select1.flag.options[document.select1.flag.selectedIndex].value;
            } else {
                document.messages.flag.value = document.select2.flag.options[document.select2.flag.selectedIndex].value;
            }

            document.messages.actionID.value = <?php echo FLAG_MESSAGES ?>;
            document.messages.submit();
        } else {
            if (whichForm == 1) {
                document.select1.flag.selectedIndex = 0;
            } else {
                document.select2.flag.selectedIndex = 0;
            }
            window.alert('<?php echo addslashes(_("You must select at least one message first.")) ?>');
        }
    }
}
<?php endif; ?>

//-->
</script>
