<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;
<?php if ($useRichtext): ?>
        document.compose.editor.focus();
<?php else: ?>
        document.compose.message.focus();
<?php endif; ?>
    }
}
<?php endif;

$js = "var identities = new Array(\n";
foreach ($identities as $ident) {
    $js .= '    new Array(';
    $js .= '"' . str_replace("\n", ($useRichtext ? '<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 .= '"")';
        }
    }
    $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];
<?php if ($useRichtext): ?>
    var msg = document.compose.editor.getText().replace(/\r\n/g, '\n');
<?php else: ?>
    var msg = document.compose.message.value.replace(/\r\n/g, '\n');
<?php endif; ?>

<?php if ($useRichtext): ?>
    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];
        }
<?php if ($useRichtext): ?>
        document.compose.editor.setText(msg.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n'));
<?php else: ?>
        document.compose.message.value = msg.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n');
<?php endif; ?>
        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; ?>
    if (document.compose.save_sent_mail) {
        document.compose.save_sent_mail.checked = next[3];
    }
}

<?php if ($useRichtext): ?>
function richtextUpdateFields()
{
    // Copy richedit HTML to innerText of form element so it can be
    // posted as HTML
    document.compose.messageHTML.value = document.compose.editor.getXHTML();

    // Copy richedit innertext to innertext of form element so it can
    // be posted as plain text
    document.compose.message.value = document.compose.editor.getText();
}
<?php endif; ?>

var busyExpanding = false;
var searchFields  = new Array();
var searchValues  = new Array();
function expandField(field)
{
<?php if ($prefs->getValue('auto_expand')): ?>
    if (document.frames) {
        var iframe = document.frames['autoexpand'];
    } else if (document.getElementById) {
        var iframe = document.getElementById('autoexpand');
    } else {
        return;
    }
    if (!iframe) {
        return;
    }
    if (!field) {
        field = new Object();
        field.name  = searchFields.slice(0, 1);
        field.value = searchValues.slice(0, 1);
    }
    if (busyExpanding) {
       searchFields[searchFields.length] = field.name;
       searchValues[searchValues.length] = field.value;
       window.setTimeout('expandField()', 500);
       return;
    }
    busyExpanding = true;
    var doc;
    if (iframe.contentDocument) {
      doc = iframe.contentDocument;
    } else if (iframe.contentWindow) {
      doc = iframe.contentWindow.document;
    } else if (iframe.document) {
      doc = iframe.document;
    } else {
      return true;
    }
    var url = '<?php echo Horde::url(Horde::addParameter('expand.php', 'actionID', 'expand_addresses'), true) ?>';
    url += '<?php echo ini_get('arg_separator.output') ?>' + 'field_name=' + escape(field.name);
    url += '<?php echo ini_get('arg_separator.output') ?>' + 'field_value=' + escape(field.value);

    var status = document.getElementById('expanding' + field.name);
    status.style.visibility = "visible";

    doc.location.replace(url);
<?php endif; ?>
}

function uniqSubmit()
{
<?php if ($useRichtext): ?>
    richtextUpdateFields();
<?php endif; ?>
    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;
    document.compose.submitBtn7.disabled = true;
    if (document.compose.style && document.compose.style.cursor) {
        document.compose.style.cursor = "wait";
    }
    document.compose.submit();
}

// -->
</script>
