%#============================================================================
%# ePortal - WEB Based daily organizer
%# Author - S.Rusakov <rusakov_sa@users.sourceforge.net>
%#
%# Copyright (c) 2000-2003 Sergey Rusakov.  All rights reserved.
%# This program is free software; you can redistribute it
%# and/or modify it under the same terms as Perl itself.
%#
%#
%#----------------------------------------------------------------------------
<& /message.mc &>
&nbsp;
<% $dlg->dialog_start( width => 300, title => pick_lang(
                                      rus => "Регистрация в системе",
                                      eng => "Login to ePortal") ) %>
<% $dlg->field("username") %>
<% $dlg->field("password") %>
<% $dlg->field("savepassword") %>
<% $dlg->buttons(ok_label => pick_lang(rus => "Зарегистрироваться!", eng => "Login!")) %>
<% $dlg->dialog_end %>

&nbsp;
<hr>
<p>
	<% pick_lang(rus => q{
		<a href="/login-faq.htm">Здесь</a> находится список часто задаваемых
		вопросов, касающихся регистрации на информационном сервере.
	}, eng => q{
    <a href="/login-faq.htm">Here</a> is Frequently Asked Questions
		about login process.
	}) %>

<p>
<& /inset.mc &>







%#=== @METAGS onStartRequest ====================================================
<%method onStartRequest><%perl>
  $obj = new ePortal::ThePersistent::Dual(Attributes => {
        username => {
                    label => {rus => 'Ваше имя', eng => 'Login name'},
                    size => 10,
        },
        password => {
                    label => {rus => 'Пароль', eng => 'Password'},
                    size => 10,
                    fieldtype => 'password',
        },
        savepassword => {
                    label => {rus => "Запомнить пароль", eng => 'Remember me'},
                    dtype => 'YesNo',
                    default => 1,
        },
    });
  $dlg = new ePortal::HTML::Dialog( obj => $obj);

  my $location = try {
    $dlg->handle_request( );
  } catch ePortal::Exception::DataNotValid with {
    my $E = shift;
    $session{ErrorMessage} = $E->text;
    '';
  };

	# Handle CANCEL
	if ($location and $dlg->isButtonPressed("cancel")) {
    return $location;
	}

	# Handle OK
	my($username, $reason);
	if ($dlg->isButtonPressed("ok")) {
    use locale;
    $obj->username( lc $obj->username);
    ($username, $reason) = $ePortal->CheckUserAccount(
      username => $obj->username,
      password => $obj->password);

		if ($username) {
      $m->scomp('/pv/send_auth_cookie.mc', username => $username, savepassword => $obj->savepassword);
			$location = "/index.htm" if $location =~ /login\.htm/;
			return $location if $location;
		} else {
      $m->scomp('/pv/send_auth_cookie.mc');
		}
	}

	my %reasons = (
		bad_user 			=> pick_lang(
        rus => "Пользователь с таким именем не существует",
				eng => "Bad user name"),
    bad_password  => pick_lang(
				rus => "Вы ввели неправильный пароль",
				eng => "Bad password"),
		md5_changed 	=> pick_lang(
				rus => "Полученная информация не достоверна",
				eng => "MD5 checksum incorrect"),
		ip_changed		=> pick_lang(
				rus => "Адрес компьютера изменился",
				eng => "Client TCP/IP address changed"),
		no_user				=> pick_lang(
				rus => "Неизвестный пользователь или не указано имя пользователя",
				eng => "No user name or user unknown"),
		disabled			=> pick_lang(
				rus => "Бюджет пользователя отключен",
				eng => "User is disabled"),
		system_error	=> pick_lang(
				rus => "Системная ошибка",
				eng => "System error"),
		"" => "",
	);
	if (exists $reasons{$reason}) {
		$session{ErrorMessage} = $reasons{$reason};
	} else {
		$session{ErrorMessage} = pick_lang(
				rus => "Неизвестная причина отказа",
				eng => "Unknown error occured");
	}

</%perl></%method>


%#=== @METAGS attr ====================================================
<%attr>
Title => {rus => "Регистрация на сервере", eng => "Login to server"}
</%attr>


%#=== @metags once =========================================================
<%once>
my ($dlg, $obj);
</%once>

%#=== @metags cleanup =========================================================
<%cleanup>
($dlg, $obj) = ();
</%cleanup>

