/*
 *	EasyScout Boy Scouts of America tracking software.
 *	http://www.easyscout.org/
 *
 *	Copyright (C) 2000 Conan C. Albrecht
 *	conan@warp.cx
 *
 *	This program is released under the GNU license so tech-savvy
 *	Boy Scouts and others can enhance the program for the use
 *	of all.  Please e-mail changes to my address above so I can
 *	reintegrate into the main program at http://www.easyscout.org/.
 *	This way, all users mutually benefit one another in the spirit of
 *	scouting.
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version 2
 *	of the License, or (at your option) any later version.
 *
 * 	Any and all changes to the program code must be documented with
 *  date, author name and e-mail, and a short description.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package cx.warp.scout;

import cx.warp.scout.lookup.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;

/** The scout user interface methods */
public class ScoutUI {
	
	public static final String[] SCREENS = { "Menu", "Trail To Eagle", "Advancement", "Merit Badges", "Activities", "Leadership", "Personal" };
	protected static final int MENU = 0;
	protected static final int TRAIL = 1;
	protected static final int ADVANCEMENT = 2;
	protected static final int MERITBADGES = 3;
	protected static final int ACTIVITIES = 4;
	protected static final int LEADERSHIP = 5;
	protected static final int PERSONAL = 6; 

	/** Returns a scout given the scout id, or null if no scout exists with the given id */
	public static Scout getScout(HttpServletRequest request, Session session) throws DataException {
		if (session != null) {
			// is it a scout login?
			if (session.isScout()) {
				return session.getScout();
			}else{
				Troop troop = session.getTroop();
				if (troop != null) {
					return troop.getScout(request.getParameter("scoutid"));
				}//if
			}//if
		}//if
		return null;
	}//getScout

	/** Shows the create new scout screen */
	public static void createScreen(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Scout scout = getScout(request, session);		
		
		// get parameters from the scout, if they exist
		String id = "new";
		String fname = "";
		String mname = "";
		String lname = "";
		Date birthdate = new Date();
		String address = "";
		String phone = "";
		String email = "";
		String registration = "";
		String notes = "";
		String password = "";
		String patrol = "";
		String contact = 
		  "Father: \n" +
	    "Phone: \n" +
			"Mother: \n" +
	    "Phone: \n";
		String emergency = "Parents";
		String medicalnotes = 
			"Medications: \n" +
			"Allergies: \n" +
		  "Physician: \n" +
	    "Phone: \n" +
			"Insurance Plan Information: \n" +
			"Illness/Injury History:\n";
		if (scout != null) {
			if (scout.getFirstName() != null) fname = scout.getFirstName();
			if (scout.getMiddleName() != null) mname = scout.getMiddleName();
			if (scout.getLastName() != null) lname = scout.getLastName();
			if (scout.getBirthDate() != null) birthdate = scout.getBirthDate();
			if (scout.getAddress() != null) address = scout.getAddress();
			if (scout.getPhone() != null) phone = scout.getPhone();
			if (scout.getEmail() != null) email = scout.getEmail();
			if (scout.getRegistration() != null) registration = scout.getRegistration();
			if (scout.getNotes() != null) notes = scout.getNotes();
			if (scout.getID() != null) id = scout.getID();
			if (scout.getContact() != null) contact = scout.getContact();
			if (scout.getEmergency() != null) emergency = scout.getEmergency();
			if (scout.getMedicalNotes() != null) medicalnotes = scout.getMedicalNotes();
			if (scout.getPassword() != null) password = scout.getPassword();
		  if (scout.getPatrol() != null) patrol = scout.getPatrol();
		}else{
			Utils.printSection(out, "CREATE NEW SCOUT");
			out.println("<p>");
		}//if
		
		if (session.isScout()) {
			out.println("You can change some personal information on this page.  For modifications to read-only");
			out.println("fields, please contact your Scoutmaster.<p><hr><p>");
		}//if
	
		out.println("<form action=" + Utils.servletdir() + " method=post>");
		out.println("<input type=hidden name=action value=createscout>");
		out.println("<input type=hidden name=z value=" + session.getZ() + ">");
		out.println("<input type=hidden name=scoutid value=" + id + ">");
		out.println("<table border=0>");
		out.println("<tr>");
		out.println("<td>" + Utils.font() + "First Name:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font() + "<input type=text name=fname size=30 value=\"" + fname + "\"></td>");
		}else{
			out.println("<td>" + Utils.font() + TroopUI.tableCell(fname) + "</td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Middle Name:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font() + "<input type=text name=mname size=30 value=\"" + mname + "\"></td>");
		}else{
			out.println("<td>" + Utils.font() + TroopUI.tableCell(mname) + "</td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Last Name:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font() + "<input type=text name=lname size=30 value=\"" + lname + "\"></td>");
		}else{
			out.println("<td>" + Utils.font() + TroopUI.tableCell(lname) + "</td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Birthdate:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font());
			Utils.printDateField(out, "", birthdate);
			out.println("</td>");
		}else{
			out.println("<td>" + Utils.font() + Utils.format(birthdate) + "</td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td valign=top>" + Utils.font() + "Address:</td>");
		out.println("<td>" + Utils.font() + "<textarea name=address rows=3 cols=30>" + address + "</textarea></td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Phone Number:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=phone size=30 value=\"" + phone + "\"></td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "E-Mail:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=email size=30 value=\"" + email  + "\"></td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Patrol:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font() + "<input type=text name=patrol size=30 value=\"" + patrol  + "\"></td>");
		}else{
			out.println("<td>" + Utils.font() + TroopUI.tableCell(patrol) + "</td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td valign=top>" + Utils.font() + "Adult Contacts:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font() + "<textarea name=contact rows=5 cols=50>" + contact + "</textarea></td>");
		}else{
			out.println("<td>" + Utils.font() + TroopUI.tableCell(contact) + "</td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Registration Number:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font() + "<input type=text name=registration size=30 value=\"" +registration + "\"></td>");
		}else{
			out.println("<td>" + Utils.font() + TroopUI.tableCell(registration) + "</td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td valign=top>" + Utils.font() + "Medical Information:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font() + "<textarea name=medical rows=8 cols=50>" + medicalnotes + "</textarea></td>");
		}else{
			out.println("<td>" + Utils.font() + TroopUI.tableCell(medicalnotes) + "</td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td valign=top>" + Utils.font() + "In Case Of Emergency, Contact:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font() + "<textarea name=emergency rows=3 cols=30>" + emergency + "</textarea></td>");
		}else{
			out.println("<td>" + Utils.font() + TroopUI.tableCell(emergency) + "</td>");
		}//if
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Password:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=password size=30 value=\"" + password + "\"></td>");
		out.println("</tr><tr>");
		out.println("<td valign=top>" + Utils.font() + "Notes:</td>");
		if (session.isOwner()) {
			out.println("<td>" + Utils.font() + "<textarea name=notes rows=8 cols=50 wrap=virtual>" + notes + "</textarea></td>");
		}else{
			out.println("<td>" + Utils.font() + TroopUI.tableCell(notes) + "</td>");
		}//if

		if (scout == null) {
			out.println("</tr><tr>");
			out.println("<td>" + Utils.font() + "Requirements Version:</td>");
			out.println("<td>" + Utils.font());
			out.println("<select name=version>");
			for (Enumeration enum = XMLLookup.getRankVersions().elements(); enum.hasMoreElements(); ) {
				String s = (String)enum.nextElement();
				out.print("<option");
				if (!enum.hasMoreElements()) {
					out.print(" selected");
				}//if
				out.println(">" + s + "</option>");
			}//for
			out.println("</select>");
		  out.println("</td>");
		}//if
		if (session.isWrite()) {
			out.println("</tr><tr>");
			out.println("<td>" + Utils.font() + "&nbsp;</td>");
			out.println("<td>" + Utils.font() + "<input type=submit value=Submit></td>");
		}//if
		out.println("</tr>");
		out.println("</table>");
		out.println("</form>");
		
	}//createScreen
	
	/** Creates/Updates a Scout */
	public static void createScout(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Troop troop = session.getTroop();
		
		// find or create the new scout
		Scout scout = getScout(request, session);
		if (scout == null) { // i.e. create a new one
			scout = troop.createScout(request.getParameter("version"));
		}//if
		
		// update the scout's information
		scout.setAddress(request.getParameter("address"));
		scout.setPhone(request.getParameter("phone"));
		scout.setEmail(request.getParameter("email"));
		if (session.isOwner()) { // owner-changeable fields
			scout.setFirstName(request.getParameter("fname"));
			scout.setMiddleName(request.getParameter("mname"));
			scout.setLastName(request.getParameter("lname"));
			scout.setBirthDate(Utils.parseDateField(request, ""));
			scout.setRegistration(request.getParameter("registration"));
			scout.setNotes(request.getParameter("notes"));
			scout.setContact(request.getParameter("contact"));
			scout.setEmergency(request.getParameter("emergency"));
			scout.setPatrol(request.getParameter("patrol"));
			scout.setMedicalNotes(request.getParameter("medical"));
		}//if isOwner
		
		// set the password
		String pass = request.getParameter("password");
		if (pass != null && !pass.equals("")) { // step through and make sure no other scouts have this password
			if (troop.getPassword().equals(pass)) {
				pass = null;
			}//if
			for (Enumeration enum = troop.getScouts(); enum.hasMoreElements(); ) {
				Scout scout2 = (Scout)enum.nextElement();
				if (!scout.getID().equals(scout2.getID()) && pass.equals(scout2.getPassword())) {
					pass = null;
					break;
				}//if
			}//for
			if (pass == null) { // we found a duplicate
				Utils.error(out, "The requested password is not currently available.  Password left unchanged.");
			}//if
		}//if
		if (pass != null) {
			scout.setPassword(pass);
		}//if
	}//createScout
	
	/** The main scout detail screen */
	public static void scoutDetail(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Scout scout = getScout(request, session);
		if (scout == null) {
			out.println("Error locating scout!");
			return;
		}//if
		
		// headers
		Utils.printSection(out, "SCOUT: " + scout.getName());
		
		out.println("<table cellspacing=0 cellpadding=0 border=0 width=100%><tr>");
		int current = MENU;
		try {
			current = Integer.parseInt(request.getParameter("screen")); 
		}catch (Exception e) { }
		out.println("<td align=right>" + Utils.font());
		out.println("<form name=menu action=" + Utils.servletdir() + " method=post>");
		out.println("<input type=hidden name=z value=" + session.getZ() + ">");
		out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
		out.println("<input type=hidden name=action value=scoutdetail>");
		out.println("Page:&nbsp;<font size=-1><select name=screen onChange=\"javascript:document.menu.submit();\">");
		for (int i = 0; i < SCREENS.length; i++) {
			if (i != ADVANCEMENT || (session.isOwner() && session.isWrite())) {
				out.print("<option value=" + String.valueOf(i));
				if (i == current) {
					out.print(" selected");
				}//if
				out.println(">" + SCREENS[i] + "</option>");
			}//if
		}//for i
		out.println("</select>");
		out.println("<input type=image border=0 src=\"" + Utils.imagedir() + "check.gif\">");
		out.println("</form>");
		out.println("</td></tr></table>");
		
		//switch based upon the current screen
		if (current == MENU) {
			menu(request, out, session);
		}else if (current == TRAIL) {
			status(request, out, session);
		}else if (current == ADVANCEMENT && session.isOwner() && session.isWrite()) {
			advancement(request, out, session);
		}else if (current == MERITBADGES) {
			meritBadges(request, out, session);
		}else if (current == ACTIVITIES) {
			activities(request, out, session);
		}else if (current == LEADERSHIP) {
			leadership(request, out, session);
		}else if (current == PERSONAL) {
			personal(request, out, session);
		}//if
		
	}//scoutDetail
	
	/** Prints the opening menu */
	public static void menu(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		// show the alternative menu if this is a scout
		if (session.isScout()) {
			menuScout(request, out, session);
			return;
		}//if
		
		// change the text if this is a read-only session
		String title = "Edit";
		if (session.isReadOnly()) {
			title = "View";
		}//if
		
		// this is the owner menu
		Scout scout = getScout(request, session);
		out.println("<font size=+1 color=#" + Utils.COLORS[4] + ">You are editing " + scout.getName() + "'s records:</font>");
		out.println("<p>");
		out.println("<font size=-1><i>Hint:</i> You can also access all of the menu options below from the \"Page\" drop-down box");
		out.println("in the upper right corner.  This drop-down appears in all pages to allow easy navigation through");
		out.println(scout.getFirstName() + "'s records.</font>");
		out.println("<p>");
		out.println("<center>");
		out.println("<font size=+1><i>What Would You Like To Do?</font>");
		out.println("<p>");
		
		out.println("<p>");
		out.println("<table border=0 cellspacing=10>");
		
		out.println("<tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "bsa6.gif\">");
		out.println("</td><td valign=top>" + Utils.font());
		printMenuItem(request, out, session, scout, "View " + scout.getFirstName() + "'s Trail To Eagle", TRAIL);
		out.println("<br><font size=-1>");
		out.println("Shows a summary of a Scout's advancement towards Eagle and gives a ");
	  out.println("high level view of what should be worked for the next rank.");
		out.println("</td>");
		
		if (session.isWrite()) {
			out.println("</tr><tr>");
			out.println("<td valign=top>");
			out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "flag.gif\">");
			out.println("</td><td valign=top>" + Utils.font());
			printMenuItem(request, out, session, scout, "Pass Off Requirements and/or Advance To New Rank", ADVANCEMENT);
			out.println("<br><font size=-1>");
			out.println("Lists all ranks (Scout through Eagle Palms) and allows you to check off");
			out.println("requirements for each rank.");
			out.println("</td>");
		}//if
		
		out.println("</tr><tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "records.gif\">");
		out.println("</td><td valign=top>" + Utils.font());
		printMenuItem(request, out, session, scout,title + " Earned Merit Badges", MERITBADGES);
		out.println("<br><font size=-1>");
		out.println(title + "s merit badges a Scout has earned, including the date it was passed off and the counselor");
		out.println("the Scout worked with.");
		out.println("</td>");

		out.println("</tr><tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "open.gif\">");
		out.println("</td><td valign=top>" + Utils.font());
		printMenuItem(request, out, session, scout, title + " Activities " + scout.getFirstName() + " Attended", ACTIVITIES); 
		out.println("<br><font size=-1>");
		if (session.isOwner()) {
			out.println("Views all activities a Scout has attended and allows you to record new activities.");
		}else{
			out.println("Views all activities a Scout has attended.");
		}//if
		out.println("</td>");

		out.println("</tr><tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "leader.gif\">");
		out.println("</td><td valign=top>" + Utils.font());
		printMenuItem(request, out, session, scout, title + " " + scout.getFirstName() + "'s Leadership Positions", LEADERSHIP);
		out.println("<br><font size=-1>");
		out.print("Shows a history of the leadership positions a Scout has held");
		if (session.isOwner()) {
	  	out.print("and allows you to record new positions a Scout serves in");
		}//if
		out.println(".</td>");

		out.println("</tr><tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "all.gif\">");
		out.println("</td><td valign=top>" + Utils.font());
		printMenuItem(request, out, session, scout, title + " Personal Information", PERSONAL);
		out.println("<br><font size=-1>");
		out.println(title + "s personal information about a Scout, such as name, phone, e-mail, address, etc.");
		out.println("</td>");
		out.println("</tr>");
		out.println("</table>");
		out.println("</font>");
		out.println("</center>");
		
	}//menu
	
	/** Prints the menu for scout logins */
	public static void menuScout(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Scout scout = session.getScout();
		out.println("<font size=+1 color=#" + Utils.COLORS[4] + ">You are viewing " + scout.getName() + "'s records:</font>");
		out.println("<p>");
		out.println("<font size=-1><i>Hint:</i> You can also access all of the menu options below from the \"Page\" drop-down box");
		out.println("in the upper right corner.  This drop-down appears in all pages to allow easy navigation through");
		out.println("your records.</font>");
		out.println("<p>");
		out.println("<center>");
		out.println("<font size=+1><i>What Would You Like To Do?</font>");
		out.println("<p>");
		
		out.println("<p>");
		out.println("<table border=0 cellspacing=10>");
		out.println("<tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "bsa6.gif\">");
		out.println("</td><td valign=top>" + Utils.font());

		printMenuItem(request, out, session, scout, "View The Trail To Eagle", TRAIL);
		out.println("<br><font size=-1>");
		out.println("Shows a summary of your advancement towards Eagle and gives a ");
	  out.println("high level view of what should be worked for the next rank.");
		out.println("</td>");
		out.println("</tr><tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "flag.gif\">");
		out.println("</td><td valign=top>" + Utils.font());

		printMenuItem(request, out, session, scout, "View Earned Merit Badges", MERITBADGES);
		out.println("<br><font size=-1>");
		out.println("Shows the merit badges you have earned.");
		out.println("the Scout worked with.");
		out.println("</td>");
		out.println("</tr><tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "open.gif\">");
		out.println("</td><td valign=top>" + Utils.font());

		printMenuItem(request, out, session, scout, "View Activities", ACTIVITIES); 
		out.println("<br><font size=-1>");
		out.println("Shows the activities you have attended.");
		out.println("</td>");
		out.println("</tr><tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "leader.gif\">");
		out.println("</td><td valign=top>" + Utils.font());

		printMenuItem(request, out, session, scout, "View Your Leadership Positions", LEADERSHIP);
		out.println("<br><font size=-1>");
		out.println("Shows a history of the leadership positions you have held.");
		out.println("</td>");
		out.println("</tr><tr>");
		out.println("<td valign=top>");
		out.println("<img alt=\"--\" border=0 src=\"" + Utils.imagedir() + "all.gif\">");
		out.println("</td><td valign=top>" + Utils.font());
		
		printMenuItem(request, out, session, scout, "View Personal Information and Edit Password", PERSONAL);
		out.println("<br><font size=-1>");
		out.println("Views your personal information and allows you to edit your EasyScout password.");
		out.println("</td>");
		out.println("</tr>");
		out.println("</table>");
		out.println("</font>");
		out.println("</center>");
 	}//menuScouts
	
	/** Prints a menu item */
	public static void printMenuItem(HttpServletRequest request, PrintWriter out, Session session, Scout scout, String title, int screen) throws IOException, DataException {
		out.println("<a href=\"" + Utils.servletdir() +
		            "?z=" + session.getZ() +
								"&scoutid=" + scout.getID() +
								"&action=scoutdetail" +
								"&screen=" + String.valueOf(screen) +
								"\">" +
								title +
								"</a>");
	}//printMenuItem

	/** Prints the merit badges for a scout */
	public static void meritBadges(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Scout scout = getScout(request, session);
		
		// save any changes to activities
		String subaction = request.getParameter("subaction");
		if (session.isOwner() && session.isWrite()) {
			if (subaction != null && subaction.equals("create")) {
				String id = request.getParameter("badgeid");
				MeritBadge badge = scout.getMeritBadge(id);
				if (badge == null) {
					badge = scout.createMeritBadge();
				}//if
				String badgename = request.getParameter("name");
				badge.setRequired(XMLLookup.isRequired(badgename));
				badge.setName(badgename);
				badge.setEarnedDate(Utils.parseDateField(request, ""));
				badge.setCounselor(request.getParameter("counselor"));
			
			}else if (subaction != null && subaction.equals("edit")) {
				String id = request.getParameter("badgeid");
				MeritBadge badge = scout.getMeritBadge(id);
				if (badge != null) {
					out.println("<font size=+1>Edit Merit Badge:</font><p>");
					createBadgeScreen(request, out, session, badge);
					out.println("<p>");
					out.println("<form action=" + Utils.servletdir() + " method=post>");
					out.println("<input type=hidden name=action value=scoutdetail>");
					out.println("<input type=hidden name=subaction value=delete>");
					out.println("<input type=hidden name=screen value=" + String.valueOf(MERITBADGES) + ">");
					out.println("<input type=hidden name=z value=" + session.getZ() + ">");
					out.println("<input type=hidden name=badgeid value=" + id + ">");
					out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
					out.println("<input type=submit value=\"Delete Merit Badge\">");
					out.println("</form>");
					return;
				}else {
					out.println("<font size=+1>Add Merit Badge:</font><p>");
					createBadgeScreen(request, out, session, null);
					return;
				}//if
			
			}else if (subaction != null && subaction.equals("delete")) {
				String id = request.getParameter("badgeid");
				scout.removeMeritBadge(id);			
		
			}//if
			
		}//if isOwner()
		
		// show the current badges
		out.println("<table border=0 width=100% cellpadding=0 cellspacing=0>");
		out.println("<tr>");
		out.println("<th>" + Utils.font() + "Merit Badge</th>");
		out.println("<th>" + Utils.font() + "Date</th>");
		out.println("<th>" + Utils.font() + "Required?</th>");
		out.println("<th>" + Utils.font() + "Counselor</th>");
		out.println("</tr>");
		MeritBadge badge;
		for (Enumeration enum = scout.getMeritBadges(); enum.hasMoreElements(); ) {
			badge = (MeritBadge)enum.nextElement();
			out.println("<tr>");
			out.println("<td>" + Utils.font());
			if (session.isOwner() && session.isWrite()) {
				out.println("<a href=\"" + Utils.servletdir() + "?z=" + session.getZ() + "&scoutid=" + scout.getID() + "&screen=" + String.valueOf(MERITBADGES) + "&action=scoutdetail&subaction=edit&badgeid=" + badge.getID() + "\">");
				out.println(badge.getName() + "</a>");
			}else{
				out.println(badge.getName());
			}//if
			out.println("</td>");
			out.println("<td>" + Utils.font() + Utils.format(badge.getEarnedDate()) + "</td>");
			if (badge.isRequired()) {
				out.println("<td>" + Utils.font() + "Yes</td>");
			}else{
				out.println("<td>" + Utils.font() + "&nbsp;</td>");
			}//if
			out.println("<td>" + Utils.font() + badge.getCounselor() + "</td>");
			out.println("</tr>");
		}//for
		out.println("</table>");
		
		// show the add merit badge form
		if (session.isOwner() && session.isWrite()) {
			out.println("&nbsp;<p>");
			out.println("<a href=\"" + Utils.servletdir() + 
			  "?action=scoutdetail" +
				"&subaction=edit" +
				"&screen=" + String.valueOf(MERITBADGES) +
				"&z=" + session.getZ() +
				"&scoutid=" + scout.getID() +
			  "\">Add Merit Badge</a>");
		}//if isOwner();
	}//meritbadges
	
	/** Create new merit badge screen */
	public static void createBadgeScreen(HttpServletRequest request, PrintWriter out, Session session, MeritBadge badge) throws IOException, DataException {
		String fid = "new";
		String fname = "";
		Date fdate = new Date();
		String fcounselor = "";
		if (badge != null) {
			fid = badge.getID();
			fname = badge.getName();
			fdate = badge.getEarnedDate();
			fcounselor = badge.getCounselor();
		}//if	
	
		out.println("<form action=" + Utils.servletdir() + " method=post>");
		out.println("<input type=hidden name=action value=scoutdetail>");
		out.println("<input type=hidden name=subaction value=create>");
		out.println("<input type=hidden name=screen value=" + String.valueOf(MERITBADGES) + ">");
		out.println("<input type=hidden name=z value=" + session.getZ() + ">");
		out.println("<input type=hidden name=badgeid value=" + fid + ">");
		out.println("<input type=hidden name=scoutid value=" + request.getParameter("scoutid") + ">");
		out.println("<table border=0>");
		out.println("<tr>");
		out.println("<td>" + Utils.font() + "Merit Badge:</td>");
		out.println("<td>" + Utils.font());
		Utils.printMeritBadges(out, "name", fname);
		out.println("</td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Date Earned:</td>");
		out.println("<td>" + Utils.font());
		Utils.printDateField(out, "", fdate);
		out.println("</td>");
		out.println("</tr><tr>");
		out.println("<td valign=top>" + Utils.font() + "Counselor:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=counselor size=30 value=\"" + fcounselor + "\"></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>" + Utils.font() + "&nbsp;</td>");
		out.println("<td>" + Utils.font() + "<input type=submit value=Submit></td>");
		out.println("</tr>");
		out.println("</table>");
		out.println("</form>");		
	}//createScreen
	
	/** Prints the activities a scout has been on */
	public static void activities(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Scout scout = getScout(request, session);

		// save any changes to activities
		if (session.isOwner() && session.isWrite()) {
			String subaction = request.getParameter("subaction");
			if (subaction != null && subaction.equals("create")) {
				String id = request.getParameter("activityid");
				Activity activity = scout.getActivity(id);
				if (activity == null) {
					activity = scout.createActivity();
				}//if
				activity.setName(request.getParameter("name"));
				activity.setDate(Utils.parseDateField(request, ""));
				activity.setDescription(request.getParameter("description"));
			
			}else if (subaction != null && subaction.equals("edit")) {
				String id = request.getParameter("activityid");
				Activity activity = scout.getActivity(id);
				if (activity != null) {
					out.println("<font size=+1>Edit Activity:</font><p>");
					createActivityScreen(request, out, session, activity);
					out.println("<p>");
					out.println("<form action=" + Utils.servletdir() + " method=post>");
					out.println("<input type=hidden name=action value=scoutdetail>");
					out.println("<input type=hidden name=subaction value=delete>");
					out.println("<input type=hidden name=screen value=" + String.valueOf(ACTIVITIES) + ">");
					out.println("<input type=hidden name=z value=" + session.getZ() + ">");
					out.println("<input type=hidden name=activityid value=" + id + ">");
					out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
					out.println("<input type=submit value=\"Delete Activity\">");
					out.println("</form>");
					return;
				}else{
					out.println("<font size=+1>Add Activity:</font><p>");
					createActivityScreen(request, out, session, null);
					return;
				}//if

			}else if (subaction != null && subaction.equals("delete")) {
				String id = request.getParameter("activityid");
				scout.removeActivity(id);			
				
			}//if
		
		}//if isOwner();
		
		// show the current activities
		out.println("<table border=0 width=100% cellpadding=0 cellspacing=0>");
		out.println("<tr>");
		out.println("<th>" + Utils.font() + "Activity</th>");
		out.println("<th>" + Utils.font() + "Date</th>");
		out.println("<th>" + Utils.font() + "Description / Type</th>");
		out.println("</tr>");
		Activity activity;
		for (Enumeration enum = scout.getActivities(); enum.hasMoreElements(); ) {
			activity = (Activity)enum.nextElement();
			out.println("<tr>");
			out.println("<td>" + Utils.font());
			if (session.isOwner() && session.isWrite()) {
				out.println("<a href=\"" + Utils.servletdir() + "?z=" + session.getZ() + "&scoutid=" + scout.getID() + "&screen=" + String.valueOf(ACTIVITIES) + "&action=scoutdetail&subaction=edit&activityid=" + activity.getID() + "\">");
				out.println(activity.getName() + "</a>");
			}else{
				out.println(activity.getName());
			}//if
			out.println("</td>");
			out.println("<td>" + Utils.font() + Utils.format(activity.getDate()) + "</td>");
			out.println("<td>" + Utils.font() + activity.getDescription() + "</td>");
		}//for
		out.println("</table>");
		
		// show the add activity form
		if (session.isOwner() && session.isWrite()) {
			out.println("&nbsp;<p>");
			out.println("<a href=\"" + Utils.servletdir() + 
			  "?action=scoutdetail" +
				"&subaction=edit" +
				"&screen=" + String.valueOf(ACTIVITIES) +
				"&z=" + session.getZ() +
				"&scoutid=" + scout.getID() +
			  "\">Add Activity</a>");
		}//if
	}//activities

	/** Create new activity screen */
	public static void createActivityScreen(HttpServletRequest request, PrintWriter out, Session session, Activity activity) throws IOException, DataException {
		String fid = "new";
		String fname = "";
		Date fdate = new Date();
		String fdescription = "";
		if (activity != null) {
			fid = activity.getID();
			fname = activity.getName();
			fdate = activity.getDate();
			fdescription = activity.getDescription();			
		}//if	
	
		out.println("<form action=" + Utils.servletdir() + " method=post>");
		out.println("<input type=hidden name=action value=scoutdetail>");
		out.println("<input type=hidden name=subaction value=create>");
		out.println("<input type=hidden name=screen value=" + String.valueOf(ACTIVITIES) + ">");
		out.println("<input type=hidden name=z value=" + session.getZ() + ">");
		out.println("<input type=hidden name=activityid value=" + fid + ">");
		out.println("<input type=hidden name=scoutid value=" + request.getParameter("scoutid") + ">");
		out.println("<table border=0>");
		out.println("<tr>");
		out.println("<td>" + Utils.font() + "Name/Type:</td>");
		out.println("<td>" + Utils.font() + "<input type=text name=name size=30 value=\"" + fname + "\"></td>");
		out.println("</tr><tr>");
		out.println("<td>" + Utils.font() + "Date:</td>");
		out.println("<td>" + Utils.font());
		Utils.printDateField(out, "", fdate);
		out.println("</td>");
		out.println("</tr><tr>");
		out.println("<td valign=top>" + Utils.font() + "Description/Notes:</td>");
		out.println("<td>" + Utils.font() + "<textarea name=description cols=50 rows=10>" + fdescription + "</textarea></td>");
		out.println("</tr>");
		out.println("<tr>");
		out.println("<td>" + Utils.font() + "&nbsp;</td>");
		out.println("<td>" + Utils.font() + "<input type=submit value=Submit></td>");
		out.println("</tr>");
		out.println("</table>");
		out.println("</form>");		
	}//createScreen
	
	/** Prints the leadership of a scout */
	public static void leadership(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Scout scout = getScout(request, session);

		// save any changes to leadership
		if (session.isOwner() && session.isWrite()) {
			String subaction = request.getParameter("subaction");
			if (subaction != null && subaction.equals("create")) {
				String id = request.getParameter("positionid");
				Position pos = scout.getPosition(id);
				if (pos == null) {
					pos = scout.createPosition();
				}//if
				String posname = request.getParameter("position");
				if (posname != null && posname.equals("Other")) {
					posname = request.getParameter("otherposition");
				}///if
				pos.setName(posname);
				pos.setStartDate(Utils.parseDateField(request, "1"));
				String endenabled = request.getParameter("endenabled");
				if (endenabled != null && endenabled.equalsIgnoreCase("on")) {
					pos.setEndDate(Utils.parseDateField(request, "2"));
				}//if
			
			}else if (subaction != null && subaction.equals("edit")) {
				String id = request.getParameter("positionid");
				Position pos = scout.getPosition(id);
				if (pos != null) {
					out.println("<font size=+1>Edit Leadership Position:</font><p>");
					createPositionScreen(request, out, session, pos);
					out.println("<p>");
					out.println("<form action=" + Utils.servletdir() + " method=post>");
					out.println("<input type=hidden name=action value=scoutdetail>");
					out.println("<input type=hidden name=subaction value=delete>");
					out.println("<input type=hidden name=screen value=" + String.valueOf(LEADERSHIP) + ">");
					out.println("<input type=hidden name=z value=" + session.getZ() + ">");
					out.println("<input type=hidden name=positionid value=" + id + ">");
					out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
					out.println("<input type=submit value=\"Remove Leadership Position\">");
					out.println("</form>");
					return;
				}else{
					out.println("<font size=+1>Add Leadership Position:</font><p>");
					createPositionScreen(request, out, session, null);
					return;
				}//if

			}else if (subaction != null && subaction.equals("delete")) {
				String id = request.getParameter("positionid");
				scout.removePosition(id);			
			
			}//if
			
		}//if

	  // show the current positions
		out.println("<table border=0 width=100% cellpadding=0 cellspacing=0>");
		out.println("<tr>");
		out.println("<th>" + Utils.font() + "Position</th>");
		out.println("<th>" + Utils.font() + "Start Date</th>");
		out.println("<th>" + Utils.font() + "End Date</th>");
		out.println("</tr>");
		Position position;
		for (Enumeration enum = scout.getPositions(); enum.hasMoreElements(); ) {
			position = (Position)enum.nextElement();
			out.println("<tr>");
			out.println("<td>" + Utils.font());
			if (session.isOwner() && session.isWrite()) {
				out.println("<a href=\"" + Utils.servletdir() + "?z=" + request.getParameter("z") + "&scoutid=" + scout.getID() + "&screen=" + String.valueOf(LEADERSHIP) + "&action=scoutdetail&subaction=edit&positionid=" + position.getID() + "\">");
			  out.println(position.getName() + "</a></td>");
			}else{
				out.println(position.getName());
			}//if
			out.println("<td>" + Utils.font() + Utils.format(position.getStartDate()) + "</td>");
			if (position.getEndDate() == null) {
				out.println("<td>" + Utils.font() + "Current</td>");
			}else{
				out.println("<td>" + Utils.font() + Utils.format(position.getEndDate()) + "</td>");
			}//if
			out.println("</tr>");
		}//for
		out.println("</table>");
		out.println("&nbsp;<p>");
		
		// show the add position form
		if (session.isOwner() && session.isWrite()) {
			out.println("<a href=\"" + Utils.servletdir() + 
			  "?action=scoutdetail" +
				"&subaction=edit" +
				"&screen=" + String.valueOf(LEADERSHIP) +
				"&z=" + session.getZ() +
				"&scoutid=" + scout.getID() +
			  "\">Add Leadership Position</a>");
		}//if
	}//leadership
	
	/** Create new activity screen */
	public static void createPositionScreen(HttpServletRequest request, PrintWriter out, Session session, Position position) throws IOException, DataException {
		String id = "new";
		Vector posVec = XMLLookup.getLeadershipPositions();
		String pos = (String)posVec.firstElement();
		Date start = new Date();
		Date end = new Date();
		String endenabled = "";
		if (position != null) {
			id = position.getID();
			pos = position.getName();
			start = position.getStartDate();
			if (position.getEndDate() != null) {
				endenabled = " CHECKED";
				end = position.getEndDate();
			}//if
		}//if
	
	
		out.println("<form action=" + Utils.servletdir() + " method=post>");
		out.println("<input type=hidden name=action value=scoutdetail>");
		out.println("<input type=hidden name=subaction value=create>");
		out.println("<input type=hidden name=screen value=" + String.valueOf(LEADERSHIP) + ">");
		out.println("<input type=hidden name=z value=" + session.getZ() + ">");
		out.println("<input type=hidden name=positionid value=" + id + ">");
		out.println("<input type=hidden name=scoutid value=" + request.getParameter("scoutid") + ">");
		out.println("<table border=0>");
		out.println("<tr>");
		out.println("<td valign=top>" + Utils.font() + "Position:</td>");
		out.println("<td valign=top>" + Utils.font() + "<select name=position>");
		int selected = -1;
		for (int i = 0; i < posVec.size(); i++) {
			String posSt = (String)posVec.elementAt(i);
			out.print("<option");
			if ((position == null && selected < 0) || pos.equals(posSt) || (selected < 0 && i == posVec.size() - 1)) {
				out.print(" selected");
				selected = i;
			}//if
		  out.println(">" + posSt + "</option>");
		}//for i
		out.println("</select><br>");
		out.print("If Other: <input type=text name=otherposition size=30");
		if (selected == posVec.size() - 1) {
			out.print(" value=\"" + pos + "\"");
		}//if
		out.println(">");
		out.println("<br>&nbsp;</td>");
		out.println("</tr><tr>");
		
		out.println("<td valign=top>" + Utils.font() + "Start Date:</td>");
		out.print("<td valign=top>" + Utils.font());
		Utils.printDateField(out, "1", start);
		out.println("<br>&nbsp;</td>");
		out.println("</tr><tr>");

		out.println("<td valign=top>" + Utils.font() + "End Date:</td>");
		out.println("<td>" + Utils.font());
		out.println("<input type=checkbox name=endenabled " + endenabled + ">Check here if the Scout has finished serving in this position.<br>");
		Utils.printDateField(out, "2", end);
		out.println("</td>");
		out.println("</tr><tr>");
		
		out.println("<tr>");
		out.println("<td>&nbsp;</td>");
		out.println("<td>" + Utils.font() + "<input type=submit value=Submit></td>");
		out.println("</tr>");
		out.println("</table>");
		out.println("</form>");		
	}//createScreen

	
	/** Prints the personal info of a scout */
	public static void personal(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		createScreen(request, out, session);
	}//personal


	/** Prints the advancment page */
	public static void advancement(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Scout scout = getScout(request, session);
		
		int rankIndex = -1;
		try {
			rankIndex = Integer.parseInt(request.getParameter("rank"));
		}catch (Exception e) { }
		
		// determine if we are supposed to pass the scout off on anything
		String subaction = request.getParameter("subaction");
		if (subaction != null && subaction.equals("pass")) {
			try {
				int index = Integer.parseInt(request.getParameter("requirement"));
				Rank r = scout.getRank(rankIndex);
				Requirement req = r.getRequirement(index);
				String pass = request.getParameter("pass");
				if (pass != null && pass.equals("1")) {
					req.setPassed(Utils.parseDateField(request, ""));
				}else{
					req.setPassed(null);
				}//if
				if (r.isCompleted()) {
					if (promoteDialog(request, out, session, r)) {
						return;
					}//if
				}//if
				scout.save();  // explicitly save since ranks & requirements don't save automatically (it was too hard to code)
			}catch (NumberFormatException e) { 
			}catch (NullPointerException e) { }
			
		}else if (subaction != null && subaction.equals("promote")) {
			Rank r = scout.getRank(rankIndex);
			Date d = Utils.parseDateField(request, "");
			for (Enumeration enum = r.getRequirements(); enum.hasMoreElements(); ) {
				Requirement requirement = (Requirement)enum.nextElement();
				if (requirement.getPassed() == null) {
					requirement.setPassed(d);
				}//if
			}//for
			scout.save();  // explicitly save since ranks & requirements don't save automatically (it was too hard to code)
			if (promoteDialog(request, out, session, r)) {
				return;
			}//if
			
		}else if (subaction != null && subaction.equals("promoteconfirm")) {
			String submit = request.getParameter("answer");
			if (submit != null && submit.equals("Yes")) {
				Rank r = scout.getRank(rankIndex);
				scout.setRank(r.getName());
				scout.save();  // explicitly save since ranks & requirements don't save automatically (it was too hard to code)
			}//if
			
		}else if (subaction != null && subaction.equals("award")) {
			Rank r = scout.getRank(rankIndex);
			Date d = Utils.parseDateField(request, "");
			r.setAwarded(d);
			scout.save();  // explicitly save since ranks & requirements don't save automatically (it was too hard to code)
			
		}else if (subaction != null && subaction.equals("awardclear")) {
			Rank r = scout.getRank(rankIndex);
			r.setAwarded(null);
			scout.save();  // explicitly save since ranks & requirements don't save automatically (it was too hard to code)
			
		}//if

		// rank summary
		Rank current = scout.getRank();
		Rank next = scout.getNextRank();
		out.println("<table border=0 width=100% cellspacing=0 cellpadding=0>");
		out.println("<tr>");
		out.println("<th>" + Utils.font() + "Rank</th>");
		out.println("<th>" + Utils.font() + "Completed</th>");
		out.println("<th>" + Utils.font() + "Awarded</th>");
		out.println("</tr>");
		Enumeration enum = scout.getRanks();
		enum.nextElement(); // skip past pre-scout
		int index = 1; // 1 since we skip pre-scout
		while (enum.hasMoreElements()) {
			Rank r = (Rank)enum.nextElement();
			
			// print out the rank information
			out.println("<tr>");
			out.println("<td>" + Utils.font());
			out.println("<a href=\"" + Utils.servletdir() +
			  "?z=" + session.getZ() +
				"&action=scoutdetail" +
				"&screen=" + String.valueOf(ADVANCEMENT) +
				"&scoutid=" + scout.getID() +
					"&rank=" + String.valueOf(index) +
			  "\">" + r.getName() + "</a>");
			if (current != null && r.getName().equals(current.getName())) {
				out.println("<img src=\"" + Utils.imagedir() + "bsa6.gif\" alt=\"(current)\">");
			}//if
			out.println("</td>");
			if (r.isCompleted()) {
				out.println("<td>" + Utils.font() + Utils.format(r.getCompletedDate()) + "</td>");
			}else{
				out.println("<td>" + Utils.font() + r.getCompletedCount() + "/" + r.getRequirementCount() + " requirements</td>");
			}//if
			if (r.getAwarded() != null) {
				out.println("<td><center>" + Utils.font());
				out.println("<form action=" + Utils.servletdir() + " method=post>");
				out.println("<input type=hidden name=action value=scoutdetail>");
				out.println("<input type=hidden name=subaction value=awardclear>");
				out.println("<input type=hidden name=screen value=" + String.valueOf(ADVANCEMENT) + ">");
				out.println("<input type=hidden name=rank value=" + String.valueOf(index) + ">");
				out.println("<input type=hidden name=z value=" + session.getZ() + ">");
				out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
			  out.print  (Utils.format(r.getAwarded()) + "&nbsp;");
				out.println("<input type=image border=0 src=\"" + Utils.imagedir() + "x.gif\">");
				out.println("</form>");
			  out.println("</td>");
			}else if (r.getAwarded() == null && r.isCompleted()) {
				out.println("<td><center>" + Utils.font());
				out.println("<form action=" + Utils.servletdir() + " method=post>");
				out.println("<input type=hidden name=action value=scoutdetail>");
				out.println("<input type=hidden name=subaction value=award>");
				out.println("<input type=hidden name=screen value=" + String.valueOf(ADVANCEMENT) + ">");
				out.println("<input type=hidden name=rank value=" + String.valueOf(index) + ">");
				out.println("<input type=hidden name=z value=" + session.getZ() + ">");
				out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
				Utils.printDateField(out, "", new Date());
				out.println("<input type=submit value=Award>");
				out.println("</form>");
				out.println("</td>");
			}else{
				out.println("<td>" + Utils.font() + "&nbsp;</td>");
			}//if
				
			out.println("</tr>");
			index++;
		}//for enum
		out.println("</table>");
		out.println("<p>");
	
		// print rank detail
		Rank rank = scout.getRank();
		if (rankIndex >= 0) {
			rank = scout.getRank(rankIndex);
		}//if
		if (next != null && (rank == null || rank.getName().equals(next.getName()))) {
			rank = next;
		}//if
		if (rank.getRequirementCount() > 0) {
			Utils.printSection(out, rank.getName(), Utils.COLORS[7]);
			String rankIndexSt = String.valueOf(scout.getRankIndex(rank.getName()));
			out.println("<table border=0 width=100% cellspacing=0 cellpadding=0>");
			out.println("<tr>");
			out.println("<th>" + Utils.font() + "Requirement</th>");
			out.println("<th>" + Utils.font() + "Passed</th>");
			index = 0;
			for (Enumeration enum2 = rank.getRequirements(); enum2.hasMoreElements(); ) {
				Requirement requirement = (Requirement)enum2.nextElement();
				out.println("<tr>");
				out.println("<td>" + Utils.font() + requirement.getText() + "</td>");
				if (requirement.getPassed() == null) {
					out.println("<td>" + Utils.font());
					out.println("<form action=" + Utils.servletdir() + " method=post>");
					out.println("<input type=hidden name=action value=scoutdetail>");
					out.println("<input type=hidden name=subaction value=pass>");
					out.println("<input type=hidden name=pass value=1>");
					out.println("<input type=hidden name=screen value=" + String.valueOf(ADVANCEMENT) + ">");
					out.println("<input type=hidden name=rank value=" + rankIndexSt + ">");
					out.println("<input type=hidden name=z value=" + session.getZ() + ">");
					out.println("<input type=hidden name=requirement value=" + String.valueOf(index) + ">");
					out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
					Utils.printDateField(out, "", new Date());
					out.println("<input type=image border=0 src=\"" + Utils.imagedir() + "go.gif\">");
					out.println("</form>");
					out.println("</td>");
				}else{
					out.println("<td>" + Utils.font());
					out.println("<center>");
					out.println("<form action=" + Utils.servletdir() + " method=post>");
					out.println("<input type=hidden name=action value=scoutdetail>");
					out.println("<input type=hidden name=subaction value=pass>");
					out.println("<input type=hidden name=rank value=" + rankIndexSt + ">");
					out.println("<input type=hidden name=pass value=0>");
					out.println("<input type=hidden name=screen value=" + String.valueOf(ADVANCEMENT) + ">");
					out.println("<input type=hidden name=z value=" + session.getZ() + ">");
					out.println("<input type=hidden name=requirement value=" + String.valueOf(index) + ">");
					out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
					out.print  (Utils.format(requirement.getPassed()) + "&nbsp;");
					out.println("<input type=image border=0 src=\"" + Utils.imagedir() + "x.gif\">");
					out.println("</form>");
				  out.println("</td>");
				}//if
				out.println("</tr>");
				index++;
			}//for
			out.println("</table>");
		
			// promotion for this rank
			if (!rank.isCompleted()) {
				out.println("<form action=" + Utils.servletdir() + " method=post>");
				out.println("<input type=hidden name=action value=scoutdetail>");
				out.println("<input type=hidden name=subaction value=promote>");
				out.println("<input type=hidden name=rank value=" + rankIndexSt + ">");
				out.println("<input type=hidden name=screen value=" + String.valueOf(ADVANCEMENT) + ">");
				out.println("<input type=hidden name=z value=" + session.getZ() + ">");
				out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
				out.println("<input type=submit value=\"Pass Off All Requirements\">");
				out.println("as of");
				Utils.printDateField(out, "", new Date());
				out.println("</form>");
				out.println("<p>");
			}//if

			// change rank to current rank
			if (!rank.getName().equals(current.getName())) {
				out.println("<form action=" + Utils.servletdir() + " method=post>");
				out.println("<input type=hidden name=action value=scoutdetail>");
				out.println("<input type=hidden name=subaction value=promoteconfirm>");
				out.println("<input type=hidden name=rank value=" + rankIndexSt + ">");
				out.println("<input type=hidden name=screen value=" + String.valueOf(ADVANCEMENT) + ">");
				out.println("<input type=hidden name=z value=" + session.getZ() + ">");
				out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
				out.println("<input type=hidden name=answer value=Yes>");
				out.println("<input type=submit value=\"Change Rank To " + rank.getName() +"\">");
				out.println("</form>");
			}//if
			
		}//if
		
	}//advancement
	
	/** Asks the user if the scout should be promoted to the next rank */
	public static boolean promoteDialog(HttpServletRequest request, PrintWriter out, Session session, Rank rank) throws IOException, DataException {
		Scout scout = getScout(request, session);
		int index = scout.getRankIndex(rank.getName());
		int index2 = scout.getRankIndex(scout.getRank().getName());
		if (index > index2) {
			out.println("<font size=+1>" + scout.getName() + " has completed the requirements for " + rank.getName());
	  	out.println("<p>");
			out.println("Do you want to promote him to " + rank.getName() + "?");
			out.println("<p>");
			out.println("<center>");
			out.println("<form action=" + Utils.servletdir() + " method=post>");
			out.println("<input type=hidden name=action value=scoutdetail>");
			out.println("<input type=hidden name=subaction value=promoteconfirm>");
			out.println("<input type=hidden name=rank value=" + String.valueOf(index) + ">");
			out.println("<input type=hidden name=screen value=" + String.valueOf(ADVANCEMENT) + ">");
			out.println("<input type=hidden name=z value=" + session.getZ() + ">");
			out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
			out.println("<input type=submit name=answer value=Yes>");
			out.println("<input type=submit name=answer value=No>");
			out.println("</center>");
		}else{
			return false;
		}//if
		
		return true;
	}//promote

	/** Prints the status of a scout */
	public static void status(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Scout scout = getScout(request, session);
		
		// get the right rank
		Rank next = scout.getNextRank();
		try { // did we specify one?
			next = scout.getRank(Integer.parseInt(request.getParameter("rankid")));
		}catch (Exception e) { }
		if (next == null) {
			next = scout.getRank();
		}//if
		
		// header
		out.println("<center>");
		out.println("<table border=0 cellspacing=10>");
		out.println("<tr>");
		String rankimg = Utils.imagedir() + Utils.replace(next.getName(), " ", "").toLowerCase() + ".gif"; 
		out.println("<td><img src=\"" + rankimg + "\"></td>");
		out.println("<td><center>" + Utils.font());
	  out.println("<font size=+2>The Trail To Eagle</font><br>");
		// change view
		out.println("<font size=-1>");
		out.println("<form name=viewrank action=" + Utils.servletdir() + " method=post>");
		out.println("<input type=hidden name=action value=scoutdetail>");
		out.println("<input type=hidden name=screen value=" + String.valueOf(TRAIL) + ">");
		out.println("<input type=hidden name=z value=" + session.getZ() + ">");
		out.println("<input type=hidden name=scoutid value=" + scout.getID() + ">");
		out.println("<select name=rankid onChange=\"javascript:document.viewrank.submit();\">");
		int index = 0;
		for (Enumeration enum = scout.getRanks(); enum.hasMoreElements(); ) {
			Rank rank = (Rank)enum.nextElement();
			out.print("<option");
			if (rank.getName().equals(next.getName())) {
				out.print(" selected");
			}//if
			out.println(" value=" + String.valueOf(index) + ">" + rank.getName() + "</option>");
			index++;
		}//for
		out.println("</select>");
		out.println("<input type=image border=0 src=\"" + Utils.imagedir() + "check.gif\">");
		out.println("</form>");
		out.println("</font>");
		out.println("</td>");
		out.println("<td><img src=\"" + Utils.imagedir() + "Eagle2.gif\"></td>");
		out.println("</tr>");
		out.println("</table>");
		out.println("</center>");
		
		// rank
		if (next != null) {
			out.println("<p>");
			Utils.printSection(out, "Rank: " + next.getName(), Utils.COLORS[7]);
		  out.println("<table border=0 cellspacing=10>");
			for (Enumeration enum = next.getRequirements(); enum.hasMoreElements(); ) {
				Requirement requirement = (Requirement)enum.nextElement();
				out.println("<tr>");
				out.println("<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>");
				Date passed = requirement.getPassed();
				if (passed == null) {
					out.println("<td>");
				  out.println(Utils.font() + requirement.getText());
					out.println("</td>");
					out.println("<td>");
					out.println("<font color=#" + Utils.COLORS[4] +">");
				  out.println(Utils.font() + "To&nbsp;Do");
					out.println("</td>");
				}else{
					out.println("<td>");
				  out.println(Utils.font() + requirement.getText());
					out.println("</td>");
					out.println("<td>");
					out.println("<font color=#" + Utils.COLORS[2] +">");
				  out.println(Utils.font() + "Passed&nbsp;on " + Utils.format(passed));
					out.println("</td>");
				}//if
				out.println("</tr>");
			}//for enum
			out.println("</table>");
		}//if
		
		// merit badges
		int required = 0;
		int badges = 0;
		for (Enumeration enum = scout.getMeritBadges(); enum.hasMoreElements(); ) {
			MeritBadge badge = (MeritBadge)enum.nextElement();
			if (badge.isRequired()) {
				required++;
			}//if
			badges++;
		}//for
		out.println("<p>");
		Utils.printSection(out, "Merit Badges For " + next.getName() + " Rank", Utils.COLORS[7]);
	  out.println("<table border=0 cellspacing=10>");
		
		// header row
		out.println("<tr>");
		out.println("<th></th>");
		out.println("<th></th>");
		out.println("<td valign=top><center>" + Utils.font() + "Earned</td>");
		if (next != null) {
			out.println("<td valign=top><center>" + Utils.font() + "Required</td>");
			out.println("<td valign=top><center>" + Utils.font() + "Missing</td>");
		}//if
		out.println("</tr>");
		
		// required merit badges row
		out.println("<tr>");
		out.println("<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>");
		out.println("<td>" + Utils.font() + "Required Merit Badges</td>");
		out.println("<td>" + Utils.font() + "<center>" + String.valueOf(required) + "</td>");
		if (next != null) {
			out.println("<td>" + Utils.font() + "<center>" + String.valueOf(next.getRequiredCount()) + "</td>");
			int missing = next.getRequiredCount() - required;
			if (missing > 0) {
				out.println("<td>" + Utils.font() + "<center><font color=#" + Utils.COLORS[4] + ">" + String.valueOf(missing) + "</td>");
			}else{
				out.println("<td>" + Utils.font() + "<center><font color=#" + Utils.COLORS[2] + ">" + "0</td>");
			}//if
		}//if
		out.println("</tr>");
		
		// total merit badges
		out.println("<tr>");
		out.println("<td>&nbsp;&nbsp;&nbsp;&nbsp;</td>");
		out.println("<td>" + Utils.font() + "Total Merit Badges</td>");
		out.println("<td>" + Utils.font() + "<center>" + String.valueOf(badges) + "</td>");
		if (next != null) {
			out.println("<td>" + Utils.font() + "<center>" + String.valueOf(next.getMeritBadgeCount()) + "</td>");
			int missing = next.getMeritBadgeCount() - badges;
			if (missing > 0) {
				out.println("<td>" + Utils.font() + "<center><font color=#" + Utils.COLORS[4] + ">" + String.valueOf(missing) + "</td>");
			}else{
				out.println("<td>" + Utils.font() + "<center><font color=#" + Utils.COLORS[2] + ">" + "0</td>");
			}//if
		}//if
		out.println("</tr>");
		out.println("</table>");
		
		
	}//status
	
	/** Prints the delete scout link */
	public static void printDeleteLink(HttpServletRequest request, PrintWriter out, Session session, String text, String scoutid) throws IOException, DataException {
			out.println("<a href=\"javascript:confirmURL('Delete this scout (and ALL related data) from troop records?','" + 
			  Utils.servletdir() + 
			  "?z=" + session.getZ() +
				"&scoutid=" + scoutid +
				"&action=scoutdelete" +
				"')\">" + text + "</a>");
	}//printDeleteLink

	/** Deletes a scout */
	public static void deleteScout(HttpServletRequest request, PrintWriter out, Session session) throws IOException, DataException {
		Troop troop = session.getTroop();
		
		// find or create the new leader
		String id = request.getParameter("scoutid");
		if (id == null || !troop.deleteScout(id)) { 
			Utils.error(out, "Could not delete scout from DB.");
		}//if
	}//deleteLeader		
	
}//ScoutUI
