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

function attachment_check(n)
{
    if (n.attachmentAction.value) {
        if (n.attachmentAction.value == 'add') {
<?php if (! $browser->hasQuirk('empty_file_input_value')): ?>
            if (n.file_upload.value == '') {
                alert('<?php echo addslashes(_("You must first specify the file to attach.")) ?>');
                n.file_upload.focus ();
                return false;
            } else {
                return true;
            }
<?php else: ?>
       return true;
<?php endif; ?>
        }

        if (n.attachmentAction.value == 'delete') {
            sel = false;

            var i = 0;

            for (i = 0; i < n.elements.length; i++) {
                if (n.elements[i].name == 'delattachments[]') {
                    if (n.elements[i].checked) {
                        sel = true;
                    }
                }
            }

            if (sel == false) {
                alert('<?php echo addslashes(_("You must first select the attachment(s) to delete.")) ?>');
                return false;
            } else {
                return true;
            }
        }
    }
}

function confirmCancel()
{
    if (window.confirm('<?php echo addslashes(_("Cancelling this message will permanently discard its contents.")) . '\n' . addslashes(_("Are you sure you want to do this?")) ?>')) {
        <?php echo $cancel_js ?>
    }
}

function change_selected(sel)
{
    if (!document.compose.elements[4 + 8 * sel + 1].checked) {
        document.compose.elements[4 + 8 * sel].checked = true;
    }
}

<?php if ($browser->isBrowser('msie')): ?>
function subjectTab()
{
    if (event.keyCode == 9 && !event.shiftKey) {
        event.returnValue = false;
        document.compose.message.focus();
    }
}
<?php endif; ?>

<?php
$js = "var identities = new Array(\n";
foreach ($identities as $ident) {
    $js .= '    new Array(';
    $js .= '"' . str_replace("\n", '\n', addslashes($ident[0])) . '", ';
    if ($ident[1]) {
        $js .= 'true, ';
    } else {
        $js .= 'false, ';
    }
    if ($conf['user']['select_sentmail_folder'] && !$prefs->isLocked('sent_mail_folder')) {
        if (isset($ident[2])) {
            $js .= $ident[2] . ', ';
        } else {
            $js .= 'null, ';
        }
    } else {
        if (isset($ident[2])) {
            $js .= '"\"' . $ident[2] . '\"", ';
        } else {
            $js .= '"", ';
        }
    }
    if ($ident[3]) {
        $js .= 'true, ';
    } else {
        $js .= 'false, ';
    }
    if ($conf['user']['select_sentmail_folder'] && !$prefs->isLocked('sent_mail_folder')) {
        $js .= 'null)';
    } else {
        if (isset($ident[2])) {
            $js .= '"\"' . IMP::displayFolder($ident[2]) . '\"")';
        } else {
            $js .= '"")';
        }
    }
    $js .= ",\n";
}
$js = substr($js, 0, -2) . "\n";
echo $js;
?>
);

function change_identity(id)
{
    var pos;

    var last = identities[document.compose.last_identity.value];
    var next = identities[id];
    var msg = document.compose.message.value.replace(/\r\n/g, '\n');

    next[0] = next[0].replace(/^\n/, '');
    last[0] = last[0].replace(/^\n/, '');

    if (last[1]) {
        pos = msg.indexOf(last[0]);
    } else {
        pos = msg.lastIndexOf(last[0]);
    }
    if (pos != -1) {
        if (next[1] == last[1]) {
            msg = msg.substring(0, pos) + next[0] + msg.substring(pos + last[0].length, msg.length);
        } else if (next[1]) {
            msg = next[0] + msg.substring(0, pos) + msg.substring(pos + last[0].length, msg.length);
        } else {
            msg = msg.substring(0, pos) + msg.substring(pos + last[0].length, msg.length) + next[0];
        }
        document.compose.message.value = msg.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n');
        document.compose.last_identity.value = id;
        window.status = '<?php echo addslashes(_("The signature was successfully replaced.")) ?>';
    } else {
        window.status = '<?php echo addslashes(_("The signature couldn't be replaced.")) ?>';
    }

<?php if ($conf['user']['select_sentmail_folder'] && !$prefs->isLocked('sent_mail_folder')): ?>
    document.compose.sent_mail_folder.selectedIndex = next[2];
<?php else: ?>
    if (document.getElementById && document.createTextNode) {
        folder_text = document.getElementById('sent_mail_folder');
        if (folder_text) {
            if (folder_text.firstChild) {
                folder_text.replaceChild(document.createTextNode(next[4]), folder_text.firstChild);
            } else {
                folder_text.appendChild(document.createTextNode(next[4]));
            }
        }
    } else if (document.all) {
        folder_text = document.all.sent_mail_folder;
        folder_text.innerText = next[4];
    }
<?php endif; ?>
    if (document.compose.save_sent_mail) {
        document.compose.save_sent_mail.checked = next[3];
    }
}

function timeoutReminder(first_reminder)
{
    if (first_reminder) {
        var msg = '<?php echo addslashes(_("Your session will expire in 5 minutes. Do you want to reload the page to prevent losing your work?")) ?>';
    } else {
        var msg = '<?php echo addslashes(_("Your session will expire in 1 minute. Do you want to reload the page to prevent losing your work?")) ?>';
    }
    if (window.confirm(msg)) {
        document.compose.actionID.value = '<?php echo REFRESH_COMPOSE ?>';
        uniqSubmit();
    }
}

window.setTimeout('timeoutReminder(true)', (<?php echo $timeout ?> - 300) * 1000);
window.setTimeout('timeoutReminder(false)', (<?php echo $timeout ?> - 60) * 1000);

//-->
</script>
