    [% META title = 'Login' %]
<!-- BEGIN [% template.name %] -->
    [%
        # INCLUDE dumper.tt dump = env;
        # INCLUDE dumper.tt dump = c.cfg('settings');
        # INCLUDE dumper.tt dump = can_bypass_terms; 
        can_bypass_terms = 0; # simplified login form so not required
		
        IF user_profile; # INCLUDE dumper.tt dump = user_profile;
            username = env.SERVER_PORT.match(8000) AND NOT env.DB_SESSIONS
                ? user_profile.username
                : user_profile.first_name _ '.' _ user_profile.last_name;
        END;	
		
        settings = c.cfg('settings');
        centre = settings.item('_centre');
    %]
    
    [% BLOCK logout_msg %]
        <p>
            <span class="info">
            [% IF username %]
                You are logged in as [% username %]
            [% ELSE %]
                You are already logged in
            [% END %]
            </span>
            [ <a href="[% app_url %]/logout">logout</a> ]
        </p>
    [% END %]
    
    [% BLOCK submit; site.html.submit %]
      <!--[if IE]>
        <input type="button" name="Close" value="Close" onclick="closeMe()" />
      <![endif]-->						
    [% END %]

    [% BLOCK login_form %]
        <!-- Login form -->
        <form method="post" action="[% app_url %]" id="login_form" name="login_form"
                autocomplete="off">
            <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 colspan="2">
                        <input type="text" size="20" name="authen_username"
                            [% IF c.authen.is_login_timeout %]
                               value="[% username %]" [% site.html.readonly %]
                            [% END %] id="authen_loginfield" />
                    </td>
                </tr>
                <tr>
                    <td><b>Password</b>:</td>
                    <td>
                        <input type="password" id="authen_passwordfield"
                            name="authen_password" size="20" />
                    </td>
					<td>
                        [% IF ( c.authen.is_login_timeout OR can_bypass_terms );
                            PROCESS site/toggle.tt; # toggle needed for pwd reset
                            PROCESS submit; END; %]
					</td>
                </tr>
            </table>
          
            [% UNLESS ( c.authen.is_login_timeout OR can_bypass_terms );
                PROCESS site/toggle.tt link = 'terms and conditions'; %]
                <div class="indent"><span id="t-and-c">I agree to the
                    <a href="javascript:toggleview('terms','toggleterms')"
                       id="toggleterms">[% link %]</a> of use:</span>
                       [% PROCESS submit %]
            
                    <div id="terms" class="itemhidden">
                        [% INCLUDE site/terms_and_conditions.tt %]
                    </div>
                </div>
            [% END %]
    
            <div id="result" style="color: #f00"></div>
        </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>
    [% END %]
        
    <h2 class="header">[% settings.app_abbr %] Data Systems</h2>
	
	<!--
	<div class="notice" style="padding: 1em; font-weight: bold; font-size: larger">
		<span style="border: dotted 2px #f00; padding: 0.5em; background: #f5f5f5">
			<img src="/images/new.png" align="absmiddle">
			please take a moment to complete our <a target="_blank"
				href="http://www.surveymonkey.com/s/FJJBNLB">Report survey</a>
			<img src="/images/new.png" align="absmiddle">
		</span>
	</div>
	-->	
    
	[% IF c.authen.username; INCLUDE logout_msg; # already logged in %]
    [% ELSIF settings.offline %]
        <p class="warning">Logins temporarily disabled during essential maintenance.
            Please try again in a few minutes ....</p>

    [% ELSE;
        IF c.authen.is_login_timeout; INCLUDE logout_msg; %]
            <div class="warning indent">
                Idle timeout - please re-enter your login details:
            </div>
        [% ELSE;
            SET destination = c.query.param('destination') || destination_url;
            DEFAULT forgotten_pwd_class = 'itemhidden'; # overridden below
            msg = CAP_Messages.0; # if failure msg set in User::password_change()
            
            IF c.authen.login_attempts %]
                <div class="error">
                    Login failed (login attempt [% c.authen.login_attempts %]).
                    Possible reasons:
                </div>
                <div class="indent">- invalid username / password</div>
                <div class="indent">- expired user account</div>
                
                <div class="info">
                    If you have forgotten your account details, please
                    <a href="javascript:toggleview('form','toggleform')"
                        id="reset">reset your password</a> below:
                </div>
            [% ELSE %]
    			<p class="message">Please enter your login details:</p>
                <p class="indent">
                    <a href="javascript:toggleview('form','toggleform')"
                        id="reset">I've forgotten them, or account is inactive ....
                    </a>                
                </p>
            [% END %]

            [% IF msg.class.match('email_address'); # PROCESS dumper.tt dump = msg;
                email_address = msg.message; 
                forgotten_pwd_class = 'itemvisible'; # override default so it shows
            %]
                <p class="error indent">[% 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">
                    <div id="pwd-reset">
                        <p class="title">
                            Enter your email address:
                            <input type="text" id="email_address" name="email_address"
                                size="40" value="[% email_address %]" />
							[% IF two_stage_login # not using now have 1 stage login %]
                            <input type="hidden" name="access_token"
                                value="[% c.query.param('access_token') %]" />
							[% END %]
                            [% site.html.submit %]
                        </p>
                        <p>
                        [% UNLESS email_address %]
                            Your password will be reset and a new one e-mailed to you.
                        [% END %]
                        </p>
                    </div>
                </form>
            </div>
            
            [% IF c.authen.login_attempts %]
              <div class="info">Otherwise, you can apply for one
                <a href="/script/cgi/application_form.cgi?service=[% centre %]">here</a>.
              </div>
            [% END %]
        [% END %]
        
        [% INCLUDE login_form %]

        <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 %]        
	[% END %]

	<hr />

  [% UNLESS c.authen.is_login_timeout %]
    <table class="borderless">
      <tr>
        <td>
            <p class="reg">
                Application for [% settings.app_abbr %] user account:
				[% base_addr = url_base.replace(':8000', '') %]
                <a href="[% base_addr %]/script/cgi/application_form.cgi?service=[% centre %]">
                    <big>&#187;</big></a>
            </p>
            
            [% IF centre.match('leeds') %]
                <p>HMDS web-site: <a href="http://www.hmds.info/">www.hmds.info</a></p>
            [% END %]

            <p>
                Contact: <a href="mailto:[% settings.email_from %]">
                    [% settings.email_from %]</a>
                or [% settings.service_telno %] for assistance.
            </p>
            
            <p>[% PROCESS 'resources/menu/request_forms.tt' class = 'itemshow' %]</p>
           
            <p>Information on the <a href="javascript:toggleview('cookies','togglecookies')"
                id="toggletcookies">use of cookies</a> on this site.
            </p>
        
            <div id="cookies" class="itemhidden">
                [% INCLUDE site/cookies.tt %]
            </div>
        </td>
        [% # INCLUDE dumper.tt dump = limerick;
            IF limerick AND ( settings.offline OR devel_server )
        %]<td>[% INCLUDE site/limerick.tt %]</td>[% END %]
      </tr>
    </table>
  [% END %]
    
    <div align="right">
        <img style="border: none" src="/images/titanium_logo.png"
            alt="powered by Titanium" />
	</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 %] -->
