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

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 ?>
    }
}

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

$js = "var identities = new Array(\n";
foreach ($identities as $ident) {
    $js .= '    new Array(';
    $js .= '"' . str_replace("\n", ($rtemode ? '<br />' : '') . '\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 .= '"", ';
        }
    }

    if (isset($ident[4])) {
        $js .= '"' . $ident[4] . '"';
    } 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');

<?php if ($rtemode): ?>
    next[0] = next[0].replace(/^<br \/>\n/, '').replace(/ +/g, ' '); //.replace(/<br \/>/, '<BR>');
    last[0] = last[0].replace(/^<br \/>\n/, '').replace(/ +/g, ' '); //.replace(/<br \/>/, '<BR>');
<?php else: ?>
    next[0] = next[0].replace(/^\n/, '');
    last[0] = last[0].replace(/^\n/, '');
<?php endif; ?>

    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 could not 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; ?>
    document.compose.bcc.value = next[5];
    if (document.compose.save_sent_mail) {
        document.compose.save_sent_mail.checked = next[3];
    }
}

function uniqSubmit()
{
    now = new Date();
    document.compose.submitBtn1.disabled = true;
<?php if ($conf['user']['allow_folders']): ?>
    document.compose.submitBtn2.disabled = true;
<?php endif; ?>
    document.compose.submitBtn3.disabled = true;
    document.compose.submitBtn4.disabled = true;
<?php if ($conf['user']['allow_folders']): ?>
    document.compose.submitBtn5.disabled = true;
<?php endif; ?>
    document.compose.submitBtn6.disabled = true;
<?php if ($imp['file_upload']): ?>
    document.compose.submitBtn7.disabled = true;
<?php endif; ?>
    if (document.compose.style && document.compose.style.cursor) {
        document.compose.style.cursor = "wait";
    }
<?php if ($rtemode): ?>
    document.compose.onsubmit();
<?php endif; ?>
    document.compose.submit();
}

// -->
</script>
