    [% META title = 'Login' %]
<!-- BEGIN [% template.name %] -->
    [% 
        IF user_profile; # PROCESS dumper.tt dump = user_profile;
            username = c.query.url.match(8080) AND NOT env.DB_SESSIONS
                ? user_profile.username
                : user_profile.first_name _ '.' _ user_profile.last_name;
        END;
    %]
    
    [% BLOCK logout_msg %]
        <p>
            <span class="info">
            [% IF username %]
                You are logged in as <b>[% username %]</b>
            [% ELSE %]
                You are already logged in
            [% END %]
            </span>
            [ <a href="[% app_url %]/logout">logout</a> ]
        </p>
    [% END %]
    
    [% IF c.authen.username; INCLUDE logout_msg; # already logged in %]

    [% ELSE %]
        [% SET destination = c.query.param('destination') || destination_url %]

        [% IF c.authen.login_attempts %]
			<p class="error">Login failed (login attempt [% c.authen.login_attempts %])</p>
			<div class="bold">Possible reasons:</div>
			<div class="indent">- invalid username / password</div>
			<div class="indent">- expired user account</div>
			<p>If you have previously registered for a user account, please
				reset your password below. Otherwise, you can apply for one
				<a href="/">here</a>.
			</p>
        [% ELSIF c.authen.is_login_timeout; INCLUDE logout_msg; %]
            <div class="warning indent">
                Idle timeout - please re-enter your login details:
            </div>
        [% ELSE %]
			<div class="message">Please enter your login details:</div>
        [% END %]

	<script type="text/javascript">
		function closeMe() {
			window.open('','_parent','');
			window.close();
		}
	</script>
	
	[% # PROCESS dumper.tt dump = c.stash.REMOTE_ADDR # test Apache mod_rpaf working %]
	[% # PROCESS dumper.tt dump = CAP_Messages %]
	
    <!-- Login form -->
    <form method="post" autocomplete="off" action="[% app_url %]" name="login_form"
		  id="login_form">
        <input type="hidden" name="destination" value="[% destination %]" />
        <input type="hidden" name="access_token"
            value="[% c.query.param('access_token') %]" />
        
        <input type="hidden" name="rm" value="authen_login" />

        <table class="indent" id="login_form">
            <tr>
                <td><b>Username</b>:</td>
                <td>
                    <input type="text" id="authen_loginfield" name="authen_username"
                        [% IF c.authen.is_login_timeout %]
                           value="[% username %]" [% site.html.readonly %]
                        [% END %]
						size="20" />
                    [% # IF NOT remote_addr.match('163.160.21[45]') %]
                        <!-- <span class="blink red">
                            use &lt;first name&gt;.&lt;last name&gt; format
                        </span> -->
                    [% # END %]
                </td>
            </tr>
            <tr>
                <td><b>Password</b>:</td>
                <td>
                    <input type="password" id="authen_passwordfield"
						name="authen_password" size="20" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="right">
                    [% site.html.submit %]
					<!--[if IE]>
					<input type="button" name="Close" value="Close" onclick="closeMe()" />
					<![endif]-->
                </td>
            </tr>
        </table>

        <p id="result" style="color: #f00"></p>
    </form>

	<script type="text/javascript">
		function log(msg){
			document.getElementById('result').innerHTML = msg;
		}
		document.getElementById('authen_passwordfield').onkeypress = function(e){
			if(isCapslock(e)){
				log("WARNING: CAPS LOCK IS ON");
			}
            else {
                log("");
            }
		}
    </script>

    [% PROCESS site/toggle.tt link = 'Forgotten my password' %]

    <div style="padding: 10px">
        <p>
            <a href="javascript:toggleview('form','toggleform')" id="toggleform">[% link %]</a>
        </p>
    </div>

    [% DEFAULT forgotten_pwd_class = 'itemhidden' # overridden in block below %]

    [% # IF c.flash.has_key('email_address') # using MessageStack instead %]
    [%
		msg = CAP_Messages.0; # if failure msg set in User::password_change()
		IF msg.class == 'email_address'; # PROCESS dumper.tt dump = msg;
			email_address = msg.message; 
			forgotten_pwd_class = 'itemvisible'; # override default so it shows
	%]
		<p class="error">[% c.messages('login').email_not_found %]</p>		
    [% END %]

    <div class="[% forgotten_pwd_class %]" id="form">
        <form method="post" action="[% app_url %]/user/password_change" name="forgotten_pwd"
            id="forgotten_pwd">
            <p class="title">
                Enter your email address:
                <input type="text" id="email_address" name="email_address" size="40"
                    value="[% email_address %]" />
                <input type="hidden" name="access_token"
                    value="[% c.query.param('access_token') %]" />
                [% site.html.submit %]
            </p>
            <p>
            [% UNLESS email_address %]
                Your password will be reset and a new one e-mailed to you.
            [% END %]
            </p>
        </form>
    </div>
	[% END %]

	[% IF limerick; import(limerick) %]
		<div style="margin: 0 200px">
			<img src="/images/lear/[% img_src %].gif" width="[% width %]"
				height="[% height %]" alt="">			
			<p class="indent">[% lines.join | html_line_break %]</p>	
		</div>			
	[% END %]
	
	<div align="right">
        <!--<a href="http://cgi-app.org/" style="background-color: #fff; text-decoration: none">-->
            <img style="border: none" src="/images/titanium_logo.png" alt="powered by Titanium" />
        <!-- Start Trustico Site Seal Tag. Do Not Edit. -->
        <a target="_blank" href="http://www.trustico.co.uk/?seal=quickssl">
            <img alt="Secured By QuickSSL and Trustico"
                title="Secured By QuickSSL and Trustico"
                src="https://secure.trustico.com/quickssl/quickssl-115x55.gif"
                width="115" height="55" border="0">
        </a>
        <!-- End Trustico Site Seal Tag -->
	</div>

[% # set 'active' textbox - contains cursor:
    active = c.authen.is_login_timeout ? 'authen_password' : 'authen_username';
    PROCESS site/field_focus.tt form_name = 'login_form', field_name = active;
%]
<!-- END [% template.name %] -->
