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

var tooMany = '<?php echo addslashes(_("Please select only one folder for this operation.")) ?>';
var delConfirm = '<?php echo addslashes(_("All messages in the folder(s) will be lost!")) . '\n' . addslashes(_("Are you completely sure you wish to continue?")) ?>\n';
var downloadConfirm = '<?php echo addslashes(_("All messages in the following folder(s) will be downloaded into one MBOX file:")) ?>';
var downloadSure = '<?php echo addslashes(_("This may take some time. Are you sure you want to continue?")) ?>';
var delList = '<?php echo addslashes(_("You are attempting to delete the following folders:")) ?>';
var delSure = '<?php echo addslashes(_("Are you SURE you want to continue?")) ?>';
var displayNames;

function buttonCount()
{
    numSel = 0;
    for (var i = 0; i < document.fmanager.elements.length; i++) {
        if (document.fmanager.elements[i].checked) {
            numSel++;
        }
    }
    return numSel;
}

function getSelectedFolders()
{
    var sel = "";
    var folder = 0;
    for (var a = 0; a < document.fmanager.elements.length; a++) {
        if (document.fmanager.elements[a].name == 'folder_list[]') {
            if (document.fmanager.elements[a].checked == 1) {
                sel = sel + displayNames[folder] + "\n";
            }
            folder++;
        }
    }

    if (sel.charAt(sel.length - 1) == "\n") {
        sel = sel.substring(0, sel.length - 1);
    }

    return sel;
}

function chooseAction(i)
{
    if (i == 0)
        var action = document.fmanager.action_choose0.options[document.fmanager.action_choose0.selectedIndex].value;
    else
        var action = document.fmanager.action_choose1.options[document.fmanager.action_choose1.selectedIndex].value;

    if (action == <?php echo CREATE_FOLDER ?>) {
        createMailbox();
    } else if (buttonCount() == 0) {
        alert('<?php echo addslashes(_("Please select a folder before you perform any actions.")) ?>');
    } else if (action == <?php echo DELETE_FOLDER ?>) {
        deleteMailbox();
    } else if (action == <?php echo RENAME_FOLDER ?>) {
        renameMailbox();
    } else if (action == <?php echo SUBSCRIBE_FOLDER ?>) {
        submitAction(<?php echo SUBSCRIBE_FOLDER  ?>);
    } else if (action == <?php echo UNSUBSCRIBE_FOLDER ?>) {
        submitAction(<?php echo UNSUBSCRIBE_FOLDER ?>);
    } else if (action == <?php echo POLL_FOLDER ?>) {
        submitAction(<?php echo POLL_FOLDER ?>);
    } else if (action == <?php echo NOPOLL_FOLDER ?>) {
        submitAction(<?php echo NOPOLL_FOLDER ?>);
    } else if (action == <?php echo DOWNLOAD_FOLDER ?>) {
        downloadMailbox();
    } else if (action == <?php echo EMPTY_MAILBOX ?>) {
        if (window.confirm('<?php echo addslashes(_("Are you sure you wish to PERMANENTLY delete these messages?")) ?>')) {
            submitAction(<?php echo EMPTY_MAILBOX ?>);
        }
    }
}

function submitAction(a)
{
    document.fmanager.actionID.value = a;
    document.fmanager.submit();
}

function changeRefresh()
{
    if (document.fmanager.refresh_choose.selectedIndex != 0) {
        document.fmanager.refresh_time.value = document.fmanager.refresh_choose.options[document.fmanager.refresh_choose.selectedIndex].value;
        document.fmanager.submit();
    }
}

function createMailbox()
{
    var count = buttonCount();
    if (count > 1) {
        window.alert(tooMany);
        return;
    }

    if (count == 1) {
        var mbox = window.prompt('<?php echo addslashes(_("You are creating a sub-folder to ")) ?>' + getSelectedFolders() + '.\n<?php echo addslashes(_("Please enter the name of the new folder:")) ?>\n', '');
    } else {
        var mbox = window.prompt('<?php echo addslashes(_("You are creating a top-level folder.")) . '\n' . addslashes(_("Please enter the name of the new folder:")) ?>\n', '');
    }

    if (mbox != null && mbox != '') {
        document.fmanager.new_mailbox.value = mbox;
        document.fmanager.actionID.value = <?php echo CREATE_FOLDER ?>;
        document.fmanager.submit();
    }
}

function deleteMailbox()
{
    if (window.confirm(delList + "\n" + getSelectedFolders() + "\n" + delSure)) {
        if (window.confirm(delConfirm)) {
            document.fmanager.actionID.value = <?php echo DELETE_FOLDER ?>;
            document.fmanager.submit();
        }
    }
}

function downloadMailbox()
{
    var count = buttonCount();
    if (window.confirm(downloadConfirm + "\n" + getSelectedFolders() + "\n" + downloadSure)) {
        document.fmanager.actionID.value = <?php echo DOWNLOAD_FOLDER ?>;
        document.fmanager.submit();
    }
}

function renameMailbox()
{
    newnames = '';
    oldnames = '';
    for (var i = 0; i < document.fmanager.elements.length; i++) {
        if (document.fmanager.elements[i].type == "checkbox" && document.fmanager.elements[i].checked) {
            tmp = window.prompt('<?php echo addslashes(_("You are renaming the folder: ")) ?>' +
                                displayNames[i - 1] + "\n" +
                                '<?php echo addslashes(_("Please enter the new name:")) ?>', displayNames[i - 1]);
            if (tmp == null || tmp == '') {
                return false;
            }
            newnames = newnames + tmp + "\n";
            oldnames = oldnames + document.fmanager.elements[i].value + "\n";
        }
    }
    if (newnames.charAt(newnames.length - 1) == "\n") {
        newnames = newnames.substring(0, newnames.length - 1);
    }
    if (oldnames.charAt(oldnames.length - 1) == "\n") {
        oldnames = oldnames.substring(0, oldnames.length - 1);
    }
    document.fmanager.new_names.value = newnames;
    document.fmanager.old_names.value = oldnames;
    document.fmanager.actionID.value = <?php echo RENAME_FOLDER ?>;
    document.fmanager.submit();
    return true;
}

//-->
</script>
