<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="Exception.jsp"%>
<%@page import="java.util.List"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.util.Properties"%>
<%@page import="com.fujitsu.iflow.model.util.QuickForm"%>
<%@page import="com.fujitsu.iflow.model.util.LoginAttemptRecord"%>
<%@page import="java.net.InetAddress"%>
<%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1

    /**********************************************
    * Please Read Before Working on this Page
    *
    * The "PasswordPanel" replaces a page requested with a password prompt.
    * User asks for a particular page, it is not there, so this gets displayed.
    * clicking login goes back to the same web address, but this time, since
    * you are logged in, it works.
    *
    * The nice thing about this paradigm is that the address seen in the
    * browser remain the same, and users can put in their favories without problem
    *
    * USAGE: At the top of every page, before any output is produced, 
    * you must have the  following:
    *
    * 1 request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    * 2 WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    * 3 if (wfSession == null) {
    * 4     %)(jsp:include page="PasswordPanel.jsp" flush="true"/)(%
    * 5     return;
    * 6 }
    *
    * line 1 is always needed
    * line 2 attempts to see if there is a current session
    * line 4 includes the PasswordPanel, put angle brackets in for the parens
    * line 5 return is needed to avoid getting anything else in the page.
    *
    ************************************************/
    //Resolve if the request came from remote host.
    String lclAddrss = InetAddress.getLocalHost().toString();
    String rmtAddrss = request.getRemoteAddr();
    if(lclAddrss.indexOf(rmtAddrss)>=0
        || rmtAddrss.indexOf("127.0.0.1")>=0 ){
        session.setAttribute("isRemoteCall","false");
    }
    else
         session.setAttribute("isRemoteCall","true");

    StringBuffer s1 = request.getRequestURL();
    s1.append("?");
    if (request.getQueryString() != null) {
        s1.append(request.getQueryString());
    }
    String goPage = s1.toString();

    String userName = (String) session.getAttribute("userName");
    String password = (String) session.getAttribute("password");
    if (userName == null) {
        userName = "";
    }

    String fileName = application.getRealPath("/").trim();
    if (fileName.endsWith(File.separator))
        fileName = fileName + "Taskmanager.conf";
    else
        fileName = fileName + File.separator + "Taskmanager.conf";
    Properties prop = new Properties();
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String line = null;
    for(line = in.readLine(); line != null; line = in.readLine())
    {
        int pos = line.indexOf("=");
        if(pos == -1 || line.charAt(1) == '#') {
            continue;
        }
        String name = line.substring(0,pos);
        String value = line.substring(pos+1);
        prop.setProperty(name.trim(),value.trim());
    }
    if (prop.isEmpty()) {
        throw new Exception("A properties file is expected for this application at the following location: "+fileName
         +"   A system administrator will need to place a configuration file with the right settings there.");
    }
    String hostName = prop.getProperty("HostName");
    if (hostName == null) {
        throw new Exception("A properties file at location: "+fileName
         +"  must have a setting for the name of the host ('HostName') that you are to log into.");
    }
    String serverName = prop.getProperty("ServerName");
    if (hostName == null) {
        throw new Exception("A properties file at location: "+fileName
         +"  must have a setting for the name of the server ('ServerName') that you are to log into.");
    }

    boolean isLiteEdition = false;
    String transportType = prop.getProperty("TWFTransportType");
    if (transportType!=null && transportType.equalsIgnoreCase("LE")) {
        isLiteEdition = true;
    }
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Interstage Business Process Manager | Login</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js">   
</script>
<script>
function loginChk()
{       	
    if( (loginForm.userName.value=="" ) || (loginForm.password.value=="") )
    {
        alert('Please enter the username and password');                 
	document.loginForm.userName.focus();
	return false;     	    
    }
    return true;
} 
</script>
</head>

<body class="bodyClass" onLoad="goforit();document.loginForm.userName.focus();">
<center>
<table width="100%" cellspacing="0" cellpadding="0" border="0" align="center">
    <tr>
    <td align="center"><jsp:include page="Top.jsp?DisplayConsole=false" flush="true"/> </td>
    </tr>
    <tr>
    <td>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
        <td><img src="images/clear.gif" width="1" height="27"></td>
        <td>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <td><img src="images/clear.gif" width="1" height="27"></td>
            <td>
            <table width="125" border="0" align="right" cellpadding="0" cellspacing="0">
                <tr>
                <td width="14"><img src="images/tab/left_circle.gif" width="14" height="27"></td>
                <td width="120" valign="middle" background="images/tab/but_main_bg.gif">
                <div align="center">
                <table cellpadding="0" cellspacing="0" border="0">
                  <tr>
                    <td>
                      <a href="help/devmgr/LogIn.html" class="help-cursor" target="helpwindow">
                      <img src="images/icons/question.gif" width="17" height="17" border="0"></a></td>
                    <td>&nbsp;
                      <a href="help/devmgr/LogIn.html" class="help-cursor" target="helpwindow">
                        <strong><font size="2" face="Arial, Helvetica, sans-serif">Help</font></strong></a>
                    </td>
                  </tr>
                </table>
                </div></td>
                <td width="14"><img src="images/tab/right_circle.gif" width="14" height="27"></td>
                </tr>
            </table>
        </td>
        </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr><td>
        <table width="182" border="0" cellspacing="0" cellpadding="0">
            <td width="25"><div align="right"><img src="images/tab/active_left.gif" width="5" height="25"></div></td>
            <td width="150" bgcolor="#FFFFFF" rowspan="2">
                <div align="center">
                    <table border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td><img src="images/icons/lock.gif" width="20" height="28"></td>
                        <td>&nbsp;&nbsp;<font color="#006699" size="2" face="Arial, Helvetica, sans-serif">
                          <strong>Login</strong></font>
                        </td>
                      </tr>
                    </table></td>
                </div>
            <td width="7"><img src="images/tab/active_right_only.gif" width="7" height="25"></td>
            </tr>
            <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            </tr>
        </table>
        </td><td valign="top">
        <table align="right" border="0" cellspacing="0" cellpadding="0">
            <td width="150"></td>
            <td width="7">&nbsp;</td>
            <td width="250" nowrap><div align="right"><small><font color='ffffff' face='verdana' size='1'>
                <span id="clock"></span></font></small></div></td>
            <td width="7">&nbsp;</td>
            </tr>
        </table>
        </td></tr>
    </table>
    </td>
    </tr>
    </table>
    </center>

<br/>

<center>
<table border="0" cellpadding="6" cellspacing="0">
<form action="LoginAction.jsp" method="post" name="loginForm"  onsubmit="return loginChk()">
<input type="hidden" name="go" value="<% QuickForm.writeHtml(out, goPage); %>">

<tr>
        <td align="center" colspan="3"><font class="arialfonts" color="#aaaaaa"> <b>Please
          login to&nbsp;</b></font> <font class="arialfonts"><b>
          <% QuickForm.writeHtml(out, hostName); %>
          </b></font><br/>
          <br/><br/></td>
</tr>
<tr>
    <td><font class="arialfonts">User ID</font></td>
    <td><b>&nbsp;:&nbsp;</b></td>
    <td><input class="TxtField" type="text" name="userName" value="<%QuickForm.writeHtml(out, userName);%>"></td>
</tr>
<% if (!isLiteEdition) { %>
<tr>
    <td><font class="arialfonts">Password</font></td>
    <td><b>&nbsp;:</b></td>
    <td><input class="TxtField" type="password" name="password"> </td>
</tr>
<% } %>
<tr>	
    <td></td>
    <td></td>
    <td>
        <table border="0" cellpadding="2" cellspacing="0">
        <tr valign="center" align="center"><td class=core-button-border>
        <input class="core-button" name="option"  type="submit" value="Login">
        </td>
        </tr>
        </table>
    </td>
</tr>

</form>

</table>
</center>

</body>
</html>
