<?php if ($fail_message) { ?>
  <p><strong><?php echo $fail_message?></strong></p>
  <hr />
<?php } ?>
<br class="clear-floats" />

<?php 
$dbh = $request->getDbh();
if (READONLY) {
  echo asXML(HTML::h2("System is locked as read-only for maintenance"));
} elseif ($dbh->readonly) {
  echo asXML(HTML::h2("Database is locked as read-only for maintenance"));
}

if (DEBUG & _DEBUG_LOGIN) {
  $t = sprintf("  DEBUG: ALLOW_ANON_EDIT = %s, ALLOW_BOGO_LOGIN = %s, ALLOW_USER_PASSWORDS = %s, ENABLE_PAGEPERM = %s, ", 
	       ALLOW_ANON_EDIT ? "true" : "false",
	       ALLOW_BOGO_LOGIN ? "true" : "false",
	       ALLOW_USER_PASSWORDS ? "true" : "false",
               ENABLE_PAGEPERM ? "true" : "false");
  if (ALLOW_USER_PASSWORDS) {
    if ($fail_message) { // add for each method the exact failing reason.
      $t .= "  USER_AUTH_ORDER: ";
      foreach ($GLOBALS['USER_AUTH_ORDER'] as $auth) {
          $t .= " => $auth";
          if ($fail = @$GLOBALS['USER_AUTH_ERROR']["_".strtolower($auth)."passuser"])
              $t .= " ($fail)";
      }
    } else
	$t .= sprintf("  USER_AUTH_ORDER: %s", join(" => ", $GLOBALS['USER_AUTH_ORDER']));
        $t .= sprintf(",  USER_AUTH_POLICY: %s", USER_AUTH_POLICY)
            . sprintf(",  PASSWORD_LENGTH_MINIMUM: %s", PASSWORD_LENGTH_MINIMUM);
  }
  echo asXML(HTML::div(array('class' => 'debug'),$t));
  echo "<br />\n";
}
if (ALLOW_BOGO_LOGIN && $require_level <= WIKIAUTH_BOGO) {
  require_once("lib/BlockParser.php");
  // This sample covers French, Spanish, Italian, German, and probably others
  $examplechars = htmlentities("ÀñíçéàÖãæôßøü", ENT_QUOTES, 'UTF-8');
  // Japanese login requires utf-8 and ALLOW_NONWIKIWORD_LOGINS
  if (defined('ALLOW_NONWIKIWORD_LOGINS') && ALLOW_NONWIKIWORD_LOGINS) {
    // sample Japanese login name
    $examplechars = $examplechars . "天道あかね";
  }
  $t = asXML(TransformText(_("You may sign in using any [[Help:AddingPages|WikiWord]] as a user id. (Any characters in %s etc. may be used too). The user id will be used as a link in RecentChanges to your home page."), true));
  if (substr_count($t,"%s") == 1) {
      $s = @sprintf($t, $examplechars);
      // FIXME: Workaround gettext or sprintf problem
      if (empty($s)) echo $t;
      else echo sprintf($t, $examplechars);
  } else {
      echo $t;
  }
  /* On disabled DbPrefs (PagePrefs only) note the user to create his PersonalPage. */
  // don't understand why I put this in --rurban
  if (!isset($user->_prefs->_select)) {
    echo asXML(TransformText(_("Note: Only if you create your personal HomePage with same the pagename as your user id, you will be able to store your UserPreferences. Otherwise not.")));
  }
  if (empty($pass_required)) ?>
  <p><?php echo _("New users may use an empty password.") ?></p>
<?php } ?>
<form method="post" action="<?php echo $request->getPostURL() ?>" accept-charset="UTF-8" name="login">
<table>
<tr>
<td>
<fieldset>
<legend><?php echo _("Sign In") ?></legend>
<table>
<tr>
  <td class="align-right"><?php echo _("UserId:") ?></td>
  <td><input type="text" name="auth[userid]" size="12" maxlength="256" value="<?php echo $userid?>" tabindex="1" />
    <?php if (ENABLE_AUTH_OPENID) { ?>
    &nbsp;&nbsp;<?php echo _("or")?> <a href="http://openid.net/"><?php echo _("OpenID") ?></a>:&nbsp;<input type="text" id="inp-openid" name="openid_url" size="32" maxlength="256" value="" tabindex="3" />
    <?php } ?>
</td>
</tr>
<tr>
  <td class="align-right"><?php echo _("Password:") ?></td>
  <td><input type="password" name="auth[passwd]" size="12" maxlength="256" tabindex="2" /></td>
</tr>
<tr><td></td><td class="align-left">
  <?php echo Button("submit:", _("Sign In"), 'wikiaction')?>
  <?php if (!$require_level) { ?>
    <?php echo Button("submit:auth[cancel]", _("Cancel"), 'button')?>
  <?php } ?>
</td></tr>
</table>

<input type="hidden" name="auth[require_level]" value="<?php echo $require_level?>" />
<input type="hidden" name="auth[login]" value="1" />
<?php echo HiddenInputs($request->getArgs(), false, array('auth')) ?>
</fieldset>
</td>
</tr>
</table>
</form>

<script type="text/javascript">
<!-- // Really there's got to be a simpler way to do this....
function select_input (type) {
    var elements = document.forms['login'].elements;
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].type == type) {
            elements[i].focus();
            return;
        }
    }
}
select_input('<?php echo $userid ? "password" : "text" ?>');
//-->
</script>
