<%@ Control Language="C#" AutoEventWireup="false" Inherits="Bitrix.UI.BXComponentTemplate" %>
<%@ Reference Control="~/bitrix/components/bitrix/system.login/component.ascx" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="Bitrix.Security" %>
<%@ Import Namespace="Bitrix.DataTypes" %>
<script runat="server">
	List<string> errors;
	
	public override string Title
	{
		get
		{
			return "Portal template";
		}
	}
	public new LoginComponent Component
	{
		get
		{
			return (LoginComponent)base.Component;
		}
	}
	
	protected void Login_Click(object sender, EventArgs e)
	{
		string login = Request.Form[UniqueID + "$login"] ?? "";
		string password = Request.Form[UniqueID + "$password"] ?? "";
		BXParamsBag<object> par = new BXParamsBag<object>();
		par.Add("Login", login);
		par.Add("Password", password);
		par.Add("Remember", true);

		errors = new List<string>();
		Component.ProcessCommand("login", par, errors);
	}

	protected void Logout_Click(object sender, EventArgs e)
	{
		errors = new List<string>();
		Component.ProcessCommand("logout", new BXParamsBag<object>(), errors);
	}
</script>
<% if (!BXIdentity.Current.IsAuthenticated) { %>
<asp:Panel runat="server" ID="Panel" DefaultButton="HiddenLoginButton"> 
	<table align="right" width="275" cellspacing="0" cellpadding="0" border="0">

		<% if (errors != null && errors.Count != 0) { %>
		<tr>
			<td valign="top" align="left" colspan="3"><span class="errortext"><%= Encode(string.Join(", ", errors.ToArray())) %></span></td>
		</tr>
		<% } %>

		<tr>
			<td valign="top" align="left">
				<label for="auth-user-login">Login:</label><br />
				<input type="text" name="<%= UniqueID %>$login" id="auth-user-login" maxlength="50" value="" tabindex="1" />
			</td>
			<td width="15">&nbsp;</td>
			<td width="120" valign="top" align="left">
				<label for="auth-user-pass">Password:</label><br />
				<input type="password" name="<%= UniqueID %>$password" id="auth-user-pass" maxlength="50" tabindex="2" />
			</td>
		</tr>
		<tr>
			<td valign="top" width="130" align="left" style="padding-top:8px;">
				<a href="<%= Encode(Component.PasswordRecoveryPath) %>">Forgot your password?</a><br />
				<% if (!string.IsNullOrEmpty(Component.RegisterPath)) { %>
				<a href="<%= Encode(Component.RegisterPath) %>">Register</a><br />
				<% } %>
			</td>
			<td width="15">&nbsp;</td>
			<td valign="top" width="120" align="right" style="padding-top:8px;">
				<div class="auth-button">
					<asp:LinkButton runat="server" ID="LoginButton" CssClass="button" TabIndex="4" OnClick="Login_Click"><span>Login</span></asp:LinkButton>
					<asp:Button runat="server" ID="HiddenLoginButton" OnClick="Login_Click" style="display: none" />
				</div>
			</td>
		</tr>
	</table>
</asp:Panel>
<% } else { %>
<%
	BXUser user = BXIdentity.Current.User;
	string userName = user.TextEncoder.Decode(user.GetDisplayName());   
%>
	<div class="auth-welcome">Hello, <%= Encode(userName) %></div>
	<div class="auth-links"><a href="<%= Encode(Component.ProfilePath) %>">Profile</a></div>
	<div class="auth-button">
		<asp:LinkButton runat="server" ID="LogoutButton" CssClass="button" OnClick="Logout_Click"><span>Logout</span></asp:LinkButton>
	</div>
<% } %>