<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ page import="net.sourceforge.jsurveyor.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<link rel='stylesheet' href='css_jsurveyor.css' />
<title>Login</title>
</head>
<body>
<jsp:useBean id="userLogin" class="net.sourceforge.jsurveyor.User">
	<jsp:setProperty name="userLogin" property="*" />
</jsp:useBean>
<jsp:useBean id="Data" class="net.sourceforge.jsurveyor.DataAccess">
</jsp:useBean>

<c:if test="${param.submitted && userLogin.userNameSet == true}">
<% 
	if (Data.existsUser(userLogin.getUserName())) {
		String writtenPassword = userLogin.getPassword();
		userLogin = Data.retrieveUser(userLogin.getUserName());

		if (userLogin.getPassword().equals(writtenPassword)) {
			session.setAttribute("name", userLogin.getUserName());
			session.setAttribute("loggedIn", "true");
			session.setAttribute("error", "");
			session.setAttribute("errorPosition", "nowhere");
		
		if ( (userLogin.getAccessControl()).getUserType().equals("root") ){
			%>
			<c:redirect url="root_user_list.jsp">
			</c:redirect>
			<%
		}
		// FIXME change the redirection to admin
		if ( (userLogin.getAccessControl()).getUserType().equals("admin") ){
			%>
			<c:redirect url="admin_main.jsp">
			</c:redirect>
			<%
		}
		// FIXME change redirection to normal start
		if ( (userLogin.getAccessControl()).getUserType().equals("normal") ){
			%>
			<c:redirect url="normal_questionnaire_list.jsp">
			</c:redirect>
			<%
		}
		
		} else {
		out.print("Incorrect password"); 
		}
	} else {
		out.print("User doesn't exits");
	}
	
%>
</c:if>

<form action="login.jsp" method="post">

<table style='width:30%;' align='center' valign='middle' border='0' cellspacing='0' cellpadding='1'>
	<tr>	<td height='10%'>&nbsp;</td>
	</tr>	
	<tr>
		<td colspan='2' align='center'><a href='http://jsurveyor.sf.net/'><img src='pics/jsurveyor_logo.png' alt='JSurveyor Logo' border='0' /></a></td>
	</tr>
	<tr>	<td height='10%' style='color:#FF0000;' align='center' colspan='2'>&nbsp;</td>
	</tr>
	<tr>
		<td class='mainmenu_headline'>Login</td>
	  	<td class='mainmenu_headline_emptyfield'>&nbsp;</td>
	</tr>
	<tr>
		<td colspan='2' class='mainmenu_blueframe'>
	    	<table border='0' cellspacing='0' cellpadding='0'>
			<tr>
		  	<td class='mainmenu_workframe'> <!-- Main workframe start -->
	        	<table border='0' cellspacing='0' cellpadding='0'> 
				<tr>
			    	<td>
					<input type='hidden' name='submitted' value='true' />
					<table border='0' style='text-align:center;'>
					 <tr>
					 	<td align='left'>Username: <br /><input type='text' class='textinput' name='userName' />
					 	</td>
					</tr>
					<tr>
					 	<td align='left'>Password: <br /><input type='password' class='textinput' name='password' />
				 		</td>
					</tr>
					<tr>
					 	<td align='right'><input type='submit' class='stdbutton' name='submit' value='Login' />
					 	</td>
					</tr>
					</table>
					</td>
			  	</tr>
			</table>
		       </td> 
		       </tr>
	       </table>
      	       </td>
	</tr>
</table>
</form>
</body>
</html>
