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

function setFocus()
{
    document.implogin.imapuser.focus();
}

function submit_login()
{
<?php if ($conf['server']['server_list'] == 'shown'): ?>
    if (document.implogin.server[document.implogin.server.selectedIndex].value.substr(0, 1) == "_") {
        return false;
    }
<?php endif; ?>
    if (document.implogin.imapuser.value == "") {
        alert('<?php echo addslashes(_("Please provide your username and password")) ?>');
        document.implogin.imapuser.focus();
        return false;
    } else if (document.implogin.pass.value == "") {
        alert('<?php echo addslashes(_("Please provide your username and password")) ?>');
        document.implogin.pass.focus();
        return false;
    } else {
        return true;
    }
}
//-->
</script>

<script language="JavaScript1.2" type="text/javascript">
<!--
function enter_key_trap(e)
{
    var keyPressed;

    if (document.layers) {
        keyPressed = String.fromCharCode(e.which);
    } else if (document.all) {
        keyPressed = String.fromCharCode(window.event.keyCode);
    } else if (document.getElementById) {
        keyPressed = String.fromCharCode(e.keyCode);
    }

    if ((keyPressed == "\r" || keyPressed == "\n") && (submit_login())) {
        document.implogin.submit();
    }
}
//-->
</script>

<form action="<?php echo Horde::applicationUrl('redirect.php') ?>" method="post" name="implogin">
<input type="hidden" name="actionID" value="<?php echo $actionID ?>" />
<input type="hidden" name="url" value="<?php echo htmlspecialchars(Horde::getFormData('url')) ?>" />
<input type="hidden" name="mailbox" value="<?php echo $prefs->getValue('mailbox') ?>" />
<?php
$args = IMP::getComposeArgs('array'); foreach ($args as $arg => $value): ?>
<input type="hidden" name="<?php echo htmlspecialchars($arg) ?>" value="<?php echo htmlspecialchars($value) ?>" />
<?php endforeach; ?>

<table align="center" border="0" width="300">
<tr><td align="center" colspan="2" class="header">
  <?php printf(_("Welcome to %s"), ($registry->getMethod('auth/login') == 'imp') ? $registry->getParam('name', 'horde') : $registry->getParam('name')) ?>
</td></tr>
        <?php if (!empty($reasons[$reason])): ?>
        <tr><td align="center" colspan="2" class="light"><p class="light"><?php echo $reasons[$reason] ?></p></td></tr>
        <?php endif; ?>
<tr>
    <td align="right" class="light"><b><?php echo _("Username") ?></b></td>
    <td align="left"><input type="text" tabindex="1" name="imapuser" value="<?php echo htmlspecialchars(Horde::getFormData('imapuser', getenv('REMOTE_USER'))) ?>" /></td>
    <?php if (!empty($conf['hooks']['vinfo']) && function_exists($conf['hooks']['vinfo'])): ?>
    <td align="left" class="light"><b>@<?php echo call_user_func($conf['hooks']['vinfo'],'vdomain') ?></b></td>
    <?php endif; ?>
</tr>
<tr>
    <td align="right" class="light"><b><?php echo _("Password") ?></b></td>
    <td align="left"><input type="password" tabindex="2" name="pass" /></td>
</tr>

<?php if ($conf['server']['server_list'] == 'hidden'): ?>
<tr>
    <td><input type="hidden" name="server" value="<?php echo $server_key ?>" /></td>
<?php elseif ($conf['server']['server_list'] == 'shown'): ?>
<tr>
    <td align="right" class="light"><b><?php echo _("Server") ?></b></td>
    <td class="light" align="left">
        <select tabindex="3" name="server"><?php echo $servers_list ?></select>
    </td>
<?php elseif ($conf['server']['change_server']): ?>
<tr>
    <td align="right" class="light"><b><?php echo _("Server") ?></b></td>
    <td align="left" class="light">
        <input type="hidden" name="namespace" value="<?php echo $namespace_value ?>" />
        <input type="hidden" name="maildomain" value="<?php echo $maildomain_value ?>" />
        <input type="text" tabindex="3" name="server" value="<?php echo Horde::getFormData('server', $server_value) ?>" />
        <?php if ($conf['server']['change_port']): ?>
        <input type="text" size="4" name="port" tabindex="3" value="<?php echo Horde::getFormData('port', $port_value) ?>" />
        <?php else: ?>
        <input type="hidden" name="port" value="<?php echo $port_value ?>" />
        <?php endif; ?>
        <?php if ($conf['server']['change_protocol']): ?>
        <select tabindex="3" name="protocol" onchange="updatePort();">
            <?php $protocol = Horde::getFormData('protocol', $protocol_value) ?>
            <option value="imap"<?php if ($protocol == 'imap') echo ' selected="selected"' ?>>IMAP</option>
            <option value="imap/notls"<?php if ($protocol == 'imap/notls') echo ' selected="selected"' ?>>IMAP, no TLS</option>
            <option value="imap/ssl"<?php if ($protocol == 'imap/ssl') echo ' selected="selected"' ?>>IMAP over SSL</option>
            <option value="imap/ssl/novalidate-cert"<?php if ($protocol == 'imap/ssl/novalidate-cert') echo ' selected="selected"' ?>>IMAP over SSL (self-signed)</option>
            <option value="pop3"<?php if ($protocol == 'pop3') echo ' selected="selected"' ?>>POP-3</option>
            <option value="pop3/ssl/novalidate-cert"<?php if ($protocol == 'pop3/ssl/novalidate-cert') echo ' selected="selected"' ?>>POP-3 over SSL (self-signed)</option>
        </select>
        <?php else: ?>
        <input type="hidden" name="protocol" value="<?php echo $protocol_value ?>" />
        <?php endif; ?>
    </td>
<?php else: ?>
<tr>
    <td>
        <input type="hidden" name="server" value="<?php echo $server_value ?>" />
        <input type="hidden" name="port" value="<?php echo $port_value ?>" />
        <input type="hidden" name="namespace" value="<?php echo $namespace_value ?>" />
        <input type="hidden" name="maildomain" value="<?php echo $maildomain_value ?>" />
        <input type="hidden" name="protocol" value="<?php echo $protocol_value ?>" />
        <input type="hidden" name="realm" value="<?php echo $realm_value ?>" />
    </td>
<?php endif; ?>

<?php if ($conf['server']['change_folders'] && $conf['server']['server_list'] == 'none'): ?>
</tr>
<tr>
    <td align="right" class="light"><b><?php echo _("Folder Prefix") ?></b></td>
    <td align="left"><input type="text" tabindex="3" name="folders" value="<?php echo Horde::getFormData('folders', $folders_value) ?>" /></td>
</tr>
<?php else: ?>
    <td><input type="hidden" name="folders" value="<?php echo $folders_value ?>" /></td>
</tr>
<?php endif; ?>

<?php if (!$prefs->isLocked('language')): ?>
<tr>
    <td align="right" class="light"><b><?php echo _("Language") ?></b></td>
    <td align="left" class="light"><?php echo $langs ?></td>
</tr>
<?php endif; ?>

<tr>
    <td>&nbsp;</td>
    <td align="left" class="light"><input type="submit" class="button" name="button" tabindex="4" value="<?php echo _("Log in") ?>" onclick="return submit_login();" /></td>
</tr>

</table>
</form>

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

function updatePort()
{
<?php if ($conf['server']['change_port']): ?>
    if (document.implogin.protocol.options[document.implogin.protocol.selectedIndex].value == 'imap') {
        document.implogin.port.value = 143;
    } else if (document.implogin.protocol.options[document.implogin.protocol.selectedIndex].value == 'imap/ssl') {
        document.implogin.port.value = 993;
    } else if (document.implogin.protocol.options[document.implogin.protocol.selectedIndex].value == 'imap/ssl/novalidate-cert') {
        document.implogin.port.value = 993;
    } else if (document.implogin.protocol.options[document.implogin.protocol.selectedIndex].value == 'pop3') {
        document.implogin.port.value = 110;
    } else if (document.implogin.protocol.options[document.implogin.protocol.selectedIndex].value == 'pop3/ssl') {
        document.implogin.port.value = 995;
    } else if (document.implogin.protocol.options[document.implogin.protocol.selectedIndex].value == 'pop3/ssl/novalidate-cert') {
        document.implogin.port.value = 995;
    }
<?php endif; ?>
}

<?php if (!$prefs->isLocked('language')): ?>
function selectLang()
{
    // We need to reload the login page here, but only if the user hasn't
    // already entered a username and password.
    if (document.implogin.imapuser.value == '' &&
        document.implogin.pass.value == '') {
        var lang_page = 'login.php?new_lang=' + document.implogin.new_lang[document.implogin.new_lang.selectedIndex].value;
<?php
$url = Horde::getFormData('url');
if (!empty($url)):
?>
        lang_page += '&url=<?php echo urlencode($url) ?>';
<?php endif; ?>
        self.location = lang_page;
    }
}
<?php endif; ?>

//-->
</script>

<script language="JavaScript1.2" type="text/javascript">
<!--
// Setup the enter keytrap code
if (window.document.captureEvents != null) {
    window.document.captureEvents(Event.KEYPRESS);
    window.document.onkeypress = enter_key_trap;
}
//-->
</script>
