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

var cancelSubmit = false;

var fields = new Array(
<?php for ($i = 0; $i < count($identities); $i++): ?>
<?php
    $folder = $identity->getValue('sent_mail_folder', $i);
    $folder = (empty($folder)) ? '' : IMP::preambleString() . $folder;
    $aliasaddr = $identity->getValue('alias_addr', $i);
    $tietoaddr = $identity->getValue('tieto_addr', $i);
?>
    new Array(
        "<?php echo addslashes($identity->getValue('id', $i)) ?>",
        "<?php echo addslashes($identity->getValue('fullname', $i)) ?>",
        "<?php echo addslashes($identity->getValue('from_addr', $i)) ?>",
        "<?php echo addslashes($identity->getValue('replyto_addr', $i)) ?>",
        "<?php echo (empty($aliasaddr)) ? '' : str_replace("\n", '\n', addslashes(@implode("\n", $aliasaddr))) ?>",
        "<?php echo (empty($tietoaddr)) ? '' : str_replace("\n", '\n', addslashes(implode("\n", $tietoaddr))) ?>",
        "<?php echo str_replace("\r", '', str_replace("\n", '\n', addslashes($identity->getValue('signature', $i)))) ?>",
        "<?php echo addslashes($identity->getValue('sig_dashes', $i)) ?>",
        "<?php echo addslashes($identity->getValue('sig_first', $i)) ?>",
        "<?php echo addslashes($identity->getValue('save_sent_mail', $i)) ?>",
        "<?php echo addslashes($folder) ?>"
    )<?php if ($i + 1 < count($identities)) echo ','; ?>

<?php endfor; ?>
);

function newChoice()
{
    var action = document.identities.identity.options[document.identities.identity.selectedIndex].value;

    if (action == -1) {
        document.identities.save.value = '<?php echo _('Create') ?>';
        document.identities.reset();
    } else {
        document.identities.save.value = '<?php echo _('Change') ?>';
        updateForm(action);
    }
}

function updateForm(identity)
{
<?php if (!$prefs->isLocked('default_identity')): ?>
    document.identities.edit_identity.value = identity;
    document.identities.id.value = fields[identity][0];
<?php endif; ?>
<?php if (!$prefs->isLocked('fullname')): ?>
    document.identities.fullname.value = fields[identity][1];
<?php endif; ?>
<?php if (!$prefs->isLocked('from_addr')): ?>
    document.identities.from_addr.value = fields[identity][2];
<?php endif; ?>
<?php if (!$prefs->isLocked('replyto_addr')): ?>
    document.identities.replyto_addr.value = fields[identity][3];
<?php endif; ?>
<?php if (!$prefs->isLocked('alias_addr')): ?>
    document.identities.alias_addr.value = fields[identity][4];
<?php endif; ?>
<?php if (!$prefs->isLocked('tieto_addr')): ?>
    document.identities.tieto_addr.value = fields[identity][5];
<?php endif; ?>
<?php if (!$prefs->isLocked('signature')): ?>
    document.identities.signature.value = fields[identity][6];
<?php endif; ?>
<?php if (!$prefs->isLocked('sig_dashes')): ?>
    if (fields[identity][7] == "1") document.identities.sig_dashes.checked = true;
    else document.identities.sig_dashes.checked = false;
<?php endif; ?>
<?php if (!$prefs->isLocked('sig_first')): ?>
    if (fields[identity][8] == "1") document.identities.sig_first.checked = true;
    else document.identities.sig_first.checked = false;
<?php endif; ?>
<?php if (!$prefs->isLocked('save_sent_mail')): ?>
    if (fields[identity][9] == "1") document.identities.save_sent_mail.checked = true;
    else document.identities.save_sent_mail.checked = false;
<?php endif; ?>
<?php if (!$prefs->isLocked('sent_mail_folder')): ?>
    document.identities.sent_mail.selectedIndex = 0;
    for (var i = 0; i < document.identities.sent_mail.length; i++) {
        if (document.identities.sent_mail[i].value == fields[identity][10]) {
            document.identities.sent_mail.selectedIndex = i;
            break;
        }
    }
<?php endif; ?>
}

function clearForm()
{
<?php if (!$prefs->isLocked('default_identity')): ?>
    document.identities.edit_identity.value = '';
    document.identities.id.value = '';
<?php endif; ?>
<?php if (!$prefs->isLocked('fullname')): ?>
    document.identities.fullname.value = '';
<?php endif; ?>
<?php if (!$prefs->isLocked('from_addr')): ?>
    document.identities.from_addr.value = '';
<?php endif; ?>
<?php if (!$prefs->isLocked('replyto_addr')): ?>
    document.identities.replyto_addr.value = '';
<?php endif; ?>
<?php if (!$prefs->isLocked('alias_addr')): ?>
    document.identities.alias_addr.value = '';
<?php endif; ?>
<?php if (!$prefs->isLocked('tieto_addr')): ?>
    document.identities.tieto_addr.value = '';
<?php endif; ?>
<?php if (!$prefs->isLocked('signature')): ?>
    document.identities.signature.value = '';
<?php endif; ?>
<?php if (!$prefs->isLocked('sig_dashes')): ?>
    document.identities.sig_dashes.checked = false;
<?php endif; ?>
<?php if (!$prefs->isLocked('sig_first')): ?>
    document.identities.sig_first.checked = false;
<?php endif; ?>
<?php if (!$prefs->isLocked('save_sent_mail')): ?>
    document.identities.save_sent_mail.checked = false;
<?php endif; ?>
<?php if (!$prefs->isLocked('sent_mail_folder')): ?>
    document.identities.sent_mail.selectedIndex = 0;
<?php endif; ?>
    document.identities.identity.selectedIndex = 0;
    cancelSubmit = true;
}

function checkSubmit()
{
    cancel = cancelSubmit;
    cancelSubmit = false;
<?php if ($browser->getBrowser() == 'msie' && $browser->getMajor() >= 4): ?>
    window.event.returnValue = !cancel;
<?php endif; ?>
    return !cancel;
}

function newFolderName()
{
    if (document.identities.sent_mail.selectedIndex == 2) {
        var folder = window.prompt('<?php echo addslashes(_("Enter the name for your new sent mail folder")) ?>', document.identities.sent_mail_new.value ? document.identities.sent_mail_new.value : '');
        if (folder != '' && folder != null) {
            document.identities.sent_mail_new.value = folder;
            document.identities.sent_mail[2].text = '<?php echo addslashes(_("Create a new sent mail folder")) ?> [' + document.identities.sent_mail_new.value + ']';
        }
    }

    if (document.identities.sent_mail.selectedIndex == 0) {
        document.identities.save_sent_mail.checked = 0;
    }
}

//-->
</script>
