<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%	boolean showTag = false;
	if (request.getSession().getAttribute("username")!=null) showTag = true;

	boolean Proc = false;
	if (request.getParameter("password")!=null) Proc = true;
	if (showTag) Proc = false;

if(Proc)
{
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	String loginrecord = null;
	int separatorIndex = 0;
	String recordUsername = null;
	String recordPassword = null;
	int recordLength = 0;
	boolean matchTag = false;
	
	try{
		FileReader fr = new FileReader("/opt/admin.txt");
		BufferedReader br = new BufferedReader(fr);
		loginrecord = new String();
		while((loginrecord=br.readLine())!=null && !matchTag){
			recordLength = loginrecord.length();
			separatorIndex = loginrecord.indexOf(":");
			recordUsername = loginrecord.substring(0, separatorIndex);
			recordPassword = loginrecord.substring(separatorIndex+1,recordLength);
			if(recordUsername.compareTo(username)==0){
				if((recordPassword.compareTo(password)==0)) matchTag = true;
			} 
		}
		if(matchTag){
			FileWriter fw = new FileWriter("/opt/admin.log",true);
			BufferedWriter bw = new BufferedWriter(fw);
			Date dateToday = new Date();
			String logStr = recordUsername+":login:"+dateToday;
			bw.write(logStr);
			bw.newLine();
			bw.close();
			bw = null;
			fw = null;
		}
		fr = null;
		br = null;
		
	}catch(Exception e){
		out.println("Error!"+e.getMessage());
	}
		
	if(matchTag){
		request.getSession().setAttribute("username", username);
		response.sendRedirect("index.jsp");
	} else {
		response.sendRedirect("loginform.jsp?err=1");
	}

}
else
{
%>
<html>
<head>
<link rel="shortcut icon" type="image/ico" href="images/favicon.ico" />
<style>
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	background-color: white;
}
body,td,input,select,textarea { color: gray;font-size:12px;font-family: verdana;}
form { margin:0px}
textarea { border: 1px solid #CCCCCC; width:80%; background-image:url(images/popo.gif); background-position:bottom right; background-repeat:no-repeat}
pre { margin: 0px}

A { TEXT-DECORATION: none}
A:link { COLOR: #2455BF}
A:visited { COLOR: #2455BF}
A:hover { COLOR: #000000}
A:active { COLOR: #000000}

A.head { font-size:14px; font-weight:bold}
A.head:link { COLOR: #999999}
A.head:visited { COLOR: #999999}
A.head:hover { COLOR: #9E0B0E}
A.head:active { COLOR: #9E0B0E}

A.left { font-size:13px}
A.left:link { COLOR: #999999}
A.left:visited { COLOR: #999999}
A.left:hover { COLOR: #000000}
A.left:active { COLOR: #000000}

A.white { font-size:12px; font-weight:normal}
A.white:link { COLOR: #FFFFFF}
A.white:visited { COLOR: #FFFFFF}
A.white:hover { COLOR: #FF0000}
A.white:active { COLOR: #FF0000}

.nav {
	width: 100px;
	border-left: 1px solid #DCDCDC;
}
.nav1 {
	background-color:#FFFFFF;
	width: 100px;
        border-left: 1px solid #DCDCDC;
}
.navColor { background-color:#9E0B0E; color:#FFFFFF; font-weight:bold; text-align:center}
.topline { background-color:#000000}
.shadow { background-color:#B5B5B5}
.left_title { background-color:#A69784}
.left_line {
	border-right: 1px solid #E8E8E8;
	padding-left: 10px;
	padding-right: 10px;
}
.menubg { background-color:#F2F2F2}
.menu_margin { margin:10px; background-color: #FFFFFF;}	
.menu1 {
	background-color: #F5F0E9;
	text-align: center;
	height: 28px;
	font-size: 13px;
}
.menu2 {
	background-color: #FFFFFF;
	text-align: center;
	height: 28px;
	font-size: 13px;
}
.main_pad {
	padding-left: 10px;
	padding-right: 10px;
}
.main_topline  { background-color: #F2F2F2 }
.table_border  { background-color: #F2F2F2 }

.table_bg { background-color: #FFFFFF; }
.table_bghot {
	background-color: #FFFD9D;
}

.table_title { font-weight: bold; font-size:13px}

.table_border2 { background-color: #C0C0C0 }
.table_title2 {
	font-size: 13px;
	font-weight: bold;
	background-color: #F2F2F2;
}
.sch_date1, .sch_overtime {
	background-color:#E4F6FF;
}
.sch_date2 {
	color : red
}
.sch_date3 {
	background-color:#E4F6FF;
	color : red
}
.sch_delay {
	background-color:#FF0000
}
.sch_memo {
}
.sch_log {
	margin: 10px;
}
.button {
	background-color:#F2F2F2; 
	border-top: 1px solid #F7F7F7; 
	border-left: 1px solid #F7F7F7; 
	border-bottom: 1px solid #CECECE;
	border-right: 1px solid #CECECE;
}
.input { border: 1px solid #CCCCCC}
.stest { font-size:9px}
.title { color:black; font-size:14px; font-weight:bold}
.red { 
	color: red;
	font-weight: bold;
}
.rolelist {
	padding: 5px;
	background-color: #E4F6FF;
}
.error {
	color: red;
	padding-top: 80px;
	text-align: center;
}
</style>
</head>
<title>Iweb Login</title>
<body>
<div style="text-align: center;"><img src="/iweb/images/pw-header.jpg"></div>
<%
if(showTag)
{%>
	<center>Вы уже зарегистрировались? Пожалуйста вернитесь.</center> 
<%
} else { 
	String strErr = "";
	if(request.getParameter("err")!=null && request.getParameter("err").equals("50")) strErr = "Login ou senha inválida!";%>
   <div style="text-align: center;">Введите имя пользователя и пароль:</div>
   </tr> 
	<tr><td colspan="2">&nbsp;</td></tr>
	<tr>
		<td width="5%"></td>
		<td width="95%">
		<form name="login" action="loginform.jsp">
			<table width="250px" align="center" border="0" cellspacing="0" cellpadding="2">
				<%	if(!strErr.equals("")){ %>
				<tr bgcolor="#FFFFFF"><td colspan="2" class="ver_10_red"><%=strErr%></td></tr><%}%> 
                <tr>
                    <td>
                    <div style="text-align: center;">
                    Имя пользователя:&nbsp;<input type="text" name="username" value="" size="25" />
                    Пароль:&nbsp;<input type="password" name="password" value="" size="25" />
                    </div><br>
                    <div style="text-align: center;">
                    <input type="submit" name="button1" value="Logar">&nbsp;<input type="reset" name="button1" value="Limpar">
                    </div>
                    </td>
				</tr>
			</table>
		</form>
		</td>
	</tr>   
<%
}%> 
</table>
<%@include file="../include/foot.jsp"%>   
</body>
</html>
<%
}%>
