//ChangePassword.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class ChangePassword extends HttpServlet
{
	// define a few things we'll need
	private HttpSession session = null;
	private Connection conn = null;
	private Configure cfg = new Configure();
	
	public void init (ServletConfig config)
		throws ServletException
	{
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection (cfg.dbPath, cfg.dbUsername, cfg.dbPassword);
		}

		catch (SQLException sqe)
		{
			sqe.printStackTrace();
		}

		catch (ClassNotFoundException ce)
		{
			System.out.println(ce);
		}
		System.out.println("ChangePassword: Init Successful");
	}
	
	
	public void doGet (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		PrintWriter out = res.getWriter();
		try
		{
		session = req.getSession (true);
		String username = req.getParameter ("username");
		String password = req.getParameter ("password");
		session.putValue (session.getId(), username);
		session = null;
		res.setContentType ("text/html");


		out.println("<html><head><title>Change Password</title></head><body bgcolor=#ffffff>"+
		"<h1>Change Password for "+username+"</h1><form action=\""+cfg.ServletDir+
		"ChangePassword\" method=\"POST\">"+
		
		"<input type=\"HIDDEN\" name=\"username\" value=\""+username+"\"><table border=0>"+
		"<tr><td>Old Password</td><td><input type=\"PASSWORD\" size=20 name=\"oldpassword\"></td></tr>"+
		"<tr><td>New Password</td><td><input type=\"PASSWORD\" size=20 name=\"password1\"></td></tr>"+
		"<tr><td>Confirm</td><td><input type=\"PASSWORD\" size=20 name=\"password2\"></td></tr>"+
		"<tr><td colspan=2><input type=\"submit\" value=\"Change Password\"></td></tr>"+
		"</table></form></body></html>");
		}
		catch (Exception e)
		{
			out.println(e);
		}
	}
	
	public void doPost (HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException
	{
		session = req.getSession (true);
		PrintWriter out = res.getWriter();
		String username = req.getParameter ("username");
		String oldpassword = req.getParameter ("oldpassword");
		String newpassword = req.getParameter ("password1");
		session.putValue (session.getId(), username);
		session = null;

		System.out.println(username+"-"+oldpassword+"-"+newpassword);
		
		res.setContentType ("text/html");
		try
		{
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery ("SELECT passwd from LOGIN WHERE name='"+username+"'");
						
			String temp="";
			while(rs.next())
				temp=rs.getString(1);	
			if (oldpassword.equals (temp))
			{
				stmt = conn.createStatement();
				stmt.executeQuery("update LOGIN set passwd='"+newpassword+"' where name='"+
				username+"'");
				out.println("<html><head><title>Your Password has been changed</title></head>"+
				"<body bgcolor=#ffffff><h2>Your Password has been changed</h2>"+
				"<form action=\""+cfg.ServletDir+"Complaintee\" method=\"POST\">"+
				"<input type=\"hidden\" name=\"username\" value=\""+username+"\">"+
				"<input type=\"hidden\" name=\"password\" value=\""+newpassword+"\">"+
				"<input type=\"submit\" value=\"Main Page\"></form></body></html>");
			}
			else
			{
				out.println("<html><head><title>Incorrect Password</title></head>"+
				"<body><h1>Old Password supplied is incorrect. Please Try again</h1></body></html>");
			}
		}
		catch (Exception e)
		{
			out.println("<html><head><title>Error Updating Password</title></head><body>"+
			"<h1>Error Updating Password</h1><h3>"+e+"</h3></body></html>");
		}
	}
}

