import java.io.*;
import java.util.*;
import java.lang.*;

import javax.servlet.*;
import javax.servlet.http.*;


public class verify extends HttpServlet
    implements SingleThreadModel
{
    String resultsDir;

    public void init(ServletConfig config)
        throws ServletException
    {
        super.init(config);
        resultsDir = getInitParameter("resultsDir");
        if (resultsDir == null) {
            Enumeration initParams = getInitParameterNames();
            System.err.println("The init parameters were: ");
            while (initParams.hasMoreElements()) {
                System.err.println(initParams.nextElement());
            }
            System.err.println("Should have seen one parameter name");
            throw new UnavailableException (this,
                "Not given a directory to write survey results!");
        }
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
        // first, set the "content type" header of the response
                res.setContentType("text/html");

                //Get the response's PrintWriter to return text to the client.
        PrintWriter toClient = res.getWriter();

        try
                {

                        String login="";
                        String password = "";
                        String errMsg = "";
                        char mKey[] = {'a','b','c','d','e','f'};
                        byte bKey[] = new byte[6];
                        String mPass = "";
                        boolean allow = false;

                        // Get client's form data & format it for the verification
                        Enumeration values = req.getParameterNames();
                        login = req.getParameterValues("Login")[0];
                        password = req.getParameterValues("Password")[0];

                        //Open the file for verifying the user
            String loginName = req.getParameterValues("verify")[0];
            FileInputStream inFile = new FileInputStream(resultsDir
                        + System.getProperty("file.separator")
                + loginName + ".txt");
            DataInputStream membersFile = new DataInputStream(inFile);

                        try
                        {
                                while(true)
                                {
                                        membersFile.readFully(bKey,0,6);
                                        for(int index=0;index<6;index++)
                                                mKey[index] = (char)bKey[index];
                                        String temp = new String(mKey);
                                        if (login.compareTo(temp)== 0)
                                        {
                                                mPass = membersFile.readLine();
                                                if (password.compareTo(mPass) == 0)
                                                        allow = true;
                                                else
                                                        errMsg = "You have entered the wrong password";
                                        }
                                        else
                                                mPass = membersFile.readLine();
                                }
                        }catch(EOFException e)
                        {
                }

                        //Close the file.
            membersFile.close();


                        // Respond to client with a thank you

                toClient.println("<html>");
                toClient.println("<head>");
                        if (allow)
                                toClient.println("<meta http-equiv=\"REFRESH\" content=\"3; URL=http://dl1487-6c6/oneshort/home.htm\">");
                        toClient.println("<FONT FACE=verdana,arial,helvetica SIZE=2>");
                        toClient.println("<title>OrionAuto - Navigation Bar</title>");
                        if (allow)
                        {
                                toClient.println("<script language=JavaScript>");
                                toClient.println("function loadFrames()");
                                toClient.println("{");
                                toClient.println("      parent.frames[0].document.location.href=\"http://dl1487-6c6/oneshort/steer.htm\"");
                                toClient.println("}");
                                toClient.println("</script>");
                        }
                        toClient.println("</head>");
                        if (allow)
                                toClient.println("<body onLoad=\"loadFrames()\">");
                        else
                                toClient.println("<body>");
                        toClient.println("<p>&nbsp;");
                        toClient.println("<p>&nbsp;");
                        if (allow)
                                toClient.println("<h3>Proceeding to the OneShort Web Site<br>");
                        else
                                toClient.println("<h3>You do not have authorization to the OneShort Web site<br>");
                        toClient.println("</body>");
                        toClient.println("</HTML>");
                }       catch(IOException e)
                {
            e.printStackTrace();
            toClient.println("A problem occured while recording your answers.  "
                        + "Please try again.");
        }

        // Close the writer; the response is done.
                toClient.close();
    }
}