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

var cancelSubmit = false;
var prefsPage = <?php echo $prefsPage ?>;

var fields = new Array(
<?php for ($i = 0; $i < count($accounts); $i++): ?>
    new Array(
        "<?php echo addslashes($account->getValue('id', $i)) ?>",
        "<?php echo addslashes($account->getValue('protocol', $i)) ?>",
        "<?php echo addslashes($account->getValue('server', $i)) ?>",
        "<?php echo addslashes($account->getValue('username', $i)) ?>",
        "<?php echo addslashes($account->getValue('password', $i)) ?>",
        "<?php echo addslashes($account->getValue('rmailbox', $i)) ?>",
        "<?php echo addslashes($account->getValue('lmailbox', $i)) ?>",
        "<?php echo addslashes($account->getValue('onlynew', $i)) ?>",
        "<?php echo addslashes($account->getValue('del', $i)) ?>",
        "<?php echo addslashes($account->getValue('markseen', $i)) ?>",
        "<?php echo addslashes($account->getValue('loginfetch', $i)) ?>",
        "<?php echo addslashes($account->getValue('acctcolor', $i)) ?>"
    )<?php if ($i + 1 < count($accounts)) echo ','; ?>

<?php endfor; ?>
);

function newChoice()
{
    if (!prefsPage) {
        status_text = document.getElementById('stat');
        if (status_text) {
            status_text.replaceChild(document.createTextNode(' '), status_text.firstChild);
        }
    }
    var action = document.accounts.account.options[document.accounts.account.selectedIndex].value;
    if (action == -1) {
        if (prefsPage) {
            document.accounts.save.value = '<?php echo _("Create") ?>';
        }
        document.accounts.reset();
        clearForm();
    } else {
        if (prefsPage) {
            document.accounts.save.value = '<?php echo _("Change") ?>';
        }
        updateForm(action);
    }
}

function updateForm(account)
{
    if (prefsPage) {
        document.accounts.edit_account.value = account;
    }
    document.accounts.id.value =       fields[account][0];
    document.accounts.server.value =   fields[account][2];
    document.accounts.username.value = fields[account][3];
    document.accounts.password.value = fields[account][4];
    document.accounts.rmailbox.value = fields[account][5];
    document.accounts.protocol.selectedIndex = 0;
    for (var i = 0; i < document.accounts.protocol.length; i++) {
        if (document.accounts.protocol[i].value == fields[account][1]) {
            document.accounts.protocol.selectedIndex = i;
            break;
        }
    }
    document.accounts.lmailbox.selectedIndex = 0;
    for (var i = 0; i < document.accounts.lmailbox.length; i++) {
        if (document.accounts.lmailbox[i].value == fields[account][6]) {
            document.accounts.lmailbox.selectedIndex = i;
            break;
        }
    }
    document.accounts.onlynew.checked = (fields[account][7] == "1");
    document.accounts.del.checked = (fields[account][8] == "1");
    document.accounts.markseen.checked = (fields[account][9] == "1");
    document.accounts.loginfetch.checked = (fields[account][10] == "1");
    for (var i =0 ; i < document.accounts.acctcolor.length; i++) {
        if (document.accounts.acctcolor[i].value == fields[account][11]) {
            document.accounts.acctcolor[i].checked = true;
        }
    }
}

function clearForm()
{
    if (prefsPage) {
        document.accounts.edit_account.value = '';
    }
    document.accounts.id.value = '';
    document.accounts.server.value = '';
    document.accounts.username.value = '';
    document.accounts.password.value = '';
    document.accounts.rmailbox.value = '';
    document.accounts.lmailbox.selectedIndex = 0;
    document.accounts.protocol.selectedIndex = 0;
    document.accounts.account.selectedIndex = 0;
    document.accounts.onlynew.checked = false;
    document.accounts.del.checked = false;
    document.accounts.markseen.checked = false;
    document.accounts.loginfetch.checked = false;
    for (var i = 0; i < document.accounts.acctcolor.length; i++) {
        document.accounts.acctcolor[i].checked = false;
    }
    cancelSubmit = true;
}

function checkSubmit()
{
    if (!prefsPage) {
        status_text = document.getElementById('stat');
        if (status_text) {
            status_text.replaceChild(document.createTextNode('<?php echo _("Fetching... Please wait...")?>'), status_text.firstChild);
        }
    }
    cancel = cancelSubmit;
    cancelSubmit = false;
<?php if (($browser->getBrowser() == 'msie') && ($browser->getMajor() >= 4)): ?>
    window.event.returnValue = !cancel;
<?php endif; ?>
    return !cancel;
}

//-->
</script>
