<?php
if (empty($FORMNAME))
    $FORMNAME = 'signin';
else
    $NOFORM = true;
?>
<?php if (empty($NOFORM)) { ?>
  <form method="post" action="<?php echo $request->getPostURL() ?>" accept-charset="UTF-8" name="<?php echo $FORMNAME?>">
  <?php echo HiddenInputs($request->getArgs(), false, array('auth')) ?>
<?php } ?>
<?php 
if ($user->isAuthenticated()) {
    $Sep = $WikiTheme->getButtonSeparator();
    $SignOutB = $WikiTheme->makeButton(_("Sign Out"), "javascript:SignOut();", 'wikiaction');
    $SignOutJS = JavaScript('    document.write(\'<input type="hidden" name="auth[logout]" value="0" />\');
    function SignOut() { 
      var form = document.forms["'.$FORMNAME.'"];
      form.elements["auth[logout]"].value = 1;
      form.submit();
    }
    document.write(\''.AsXML($SignOutB).'\');');
?>
  <?php echo fmt("Authenticated as %s", WikiLink($user->getAuthenticatedId(), 'auto')) ?>
  <?php echo $SEP?><?php echo $SignOutJS?>
  <noscript><?php echo Button("submit:auth[logout]", _("Sign Out"), 'wikiaction') ?></noscript>
<?php } else {
    $SignInInput = HTML::input(array('type' => 'text',
				     'name' => "auth[userid]",
				     'size' => 12,
				     'maxlength' => 256,
				     'onchange' => "SignIn(this.form,false);"));
    $SignInInput->addTooltip(_("Enter your UserId to sign in"));
    $userid = $user->getId();
    if ($user->isSignedIn()) {
        echo sprintf(_("You are signed but not authenticated as %s. "), 
		   asXml(HTML::a(array(
				       'href'=>'#',
				       'title' => sprintf(_("Click to authenticate as %s"),$userid),
				       'onclick'=>'SignIn(document.forms["'.$FORMNAME.'"],"'.$userid.'");'),
				 $userid)));
    }
    $editing_policy = (defined('EDITING_POLICY') and $request->_dbi->isWikiPage(EDITING_POLICY));
    $SignInJS = JavaScript('
     function SignIn(form,userid) {'.($editing_policy ? '
      if (window.focus) {
        window.open("' . WikiUrl(EDITING_POLICY) . '",<!-- url -->
                    "PleaseReadThisFirst",<!-- window name -->
                    "width=600,height=430,scrollbars=yes,resizable=yes,toolbar=yes");
      }':'').'
      if (userid) {
        form.elements["auth[userid]"].value = userid;
      }
      form.submit();
    }');
?>
  <?php echo $SignInJS ?>
  <?php echo Button("submit:auth[login]", _("Sign in as:"), 'wikiaction')?>
  <?php echo $SignInInput ?>
<?php } ?>
<?php if (empty($NOFORM)) { ?>
  </form>
<?php } ?>
