<?php
/**********************************************************************
 *
 * set of functions that generate html. most will require some get*
 * function that retrieves data and returns it in array form
 * these function want to go into there own include file
***********************************************************************/

function genPrologue()
{
$html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
. "			\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
. "		<html>\n"
. "			<head>\n"
. "				<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n"
. "				<meta name=\"author\" content=\"ASF\" /><meta name=\"email\" content=\"apache@apache.org\" />\n"
. "				<title>Welcome! - The MCUDL Player Site</title>\n"
. "			</head>\n"
. "			<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#525D76\">				\n"
. "				<table bgcolor=\"#ffffff\" border=\"0\" width=\"100%\" cellspacing=\"0\">\n"
. "					<tr>\n"
. "						<td align=\"center\" valign=\"top\" >\n"
. "							<a href=\"http://www.mcudl.org/\">\n"
. "								<img src=\"../images/banners/mcudlsmbanner.gif\" alt=\"MCUDL Player Site\" align=\"center\" border=\"0\"/>\n"
. "							</a>\n"
. "						</td>\n"
. "					</tr>\n"
. "				</table>\n"
. "				<table bgcolor=\"white\" border=\"0\" width=\"100%\" cellspacing=\"4\">\n"
. "					<!-- <tr><td colspan=\"2\"><hr noshade=\"noshade\" size=\"1\"/></td></tr> -->";

	print $html;
}

function genEpilogue()
{
	print ("</table> </body> </html>");
}

function genInputField ($type, $name, $size, $value)
{
 
  $f = "<input type=\"" . $type . "\" name=\"". $name . "\" ";

  if ($value != null)
	 $f .= " value=\"" . $value . "\"";

  if ($size != null)
	 $f .= " size=\"" . $size . "\"";

  $f .= "/>";

  print $f;

}

function genTeamTable()
{
	$aTeams		= array();
	$draftTeams	= array();

	getTeamSet("A", &$aTeams);
	$aCount	= sizeof($aTeams);

	getTeamSet("Draft", &$draftTeams);
	$draftCount	= sizeof($draftTeams);

   print ("<table width=\"100%\">\n");
   print ("<thead><tr>"
   			. "<th>Draft</th>"
			. "<th>A League</th>"
			. "</tr></thead>");

	$rowCount	= 0;

	while ($rowCount < $draftCount || $rowCount < $aCount)
	{
		print ("<tr>");
		if ($rowCount < $draftCount)
		{
			print ("<td align=\center\"><a href=\"showteamupdate.php?team_id="
			  . $draftTeams[$rowCount]["id"] . "\">" . $draftTeams[$rowCount]["name"] ."</a></td>");
		}
		else
			print ("<td>&nbsp;</td>");

		if ($rowCount < $aCount)
		{
			print ("<td align=\center\"><a href=\"showteamupdate.php?team_id="
			  . $aTeams[$rowCount]["id"] . "\">" . $aTeams[$rowCount]["name"] ."</a></td>");
		}
		else
			print ("<td>&nbsp;</td>");

		print ("</tr>");

		$rowCount	+= 1;
	}

	print ("</table>");
}

function genDivisionStandings($division)
{
 GLOBAL $dbserver, $dbuser, $dbpwd, $dbprefix, $database;

	$link = mysql_connect ($dbserver, $dbuser, $dbpwd)
		or die ("Could not connect");

	$q = "SELECT id, name, wins, losses, draws "
			. " from $database.team t "
			. " where division = '" . $division . "'"
			. " order by wins DESC, losses, draws";
	
	$AResult = mysql_query($q, $link)
		or die("Query Failed In genTeamInfo:[" . $q . "]");

   print ("<table>\n"
			. "<tr><td COLSPAN=\"4\" align=\"center\"><b>$division League Standings</b></td>"
   			. "<tr>"
			. "<td>Team</td>"
			. "<td>Wins</td>"
			. "<td>Losses</td>"
			. "<td>Draws</td>"
			. "</tr>");

   while ($row = mysql_fetch_array ($AResult)) {
		$teamId = $row[0];
		$name = $row[1];
		$wins = $row[2];
		$losses = $row[3];
		$draws = $row[4];

		print ("<tr>"
				. "<td align=\"left\"><em>$name</em></td>"
				. "<td align=\"center\">$wins</td>"
				. "<td align=\"center\">$losses</td>"
				. "<td align=\"center\">$draws</td>"
				. "</tr>");
	}

	print ("</table>");
		
	mysql_free_result ($AResult);
	mysql_close ($link);
}

function genTeamInfo($teamId)
{
 GLOBAL $dbserver, $dbuser, $dbpwd, $dbprefix;

	$link = mysql_connect ($dbserver, $dbuser, $dbpwd)
		or die ("Could not connect");

	$q = "SELECT id, name, wins, losses, draws from " . $dbprefix . "team";

	if ($teamId != null)
		  $q	.= " where id = " . "$teamId";

	$q	.= " order by wins DESC, losses, draws";
	
	$result = mysql_query($q, $link)
		or die("Query Failed In genTeamInfo:[" . $q . "]");

   print ("<table>\n"
   			. "<thead><tr>"
			. "<th>Team</th>"
			. "<th>Wins</th>"
			. "<th>Losses</th>"
			. "<th>Draws</th>"
			. "<th>&nbsp;</th>"
			. "</tr></thead>");

   while ($row = mysql_fetch_array ($result)) {
		$teamId = $row[0];
		$name = $row[1];
		$wins = $row[2];
		$losses = $row[3];
		$draws = $row[4];

		print ("<tr>"
				. "<td>$name</td>"
				. "<td>$wins</td>"
				. "<td>$losses</td>"
				. "<td>$draws</td>"
				. "<td><a href=\"showteamroster.php?team_id=$teamId\">Roster</a></td>"
				. "</tr>");
	}

	print ("</table>");
		
	mysql_free_result ($result);
	mysql_close ($link);
}

function genGameStats($gameId, $teamId, $playerId)
{
	genGameStatsForUpdate($gameId, $teamId, $playerId);
}

function genGameStatsForUpdate($gameId, $teamId, $playerId)
{
	$game	= array();
	getGameInfo($gameId, &$game);

	$stats	= array();
	getGameStats($gameId, $teamId, $playerId, &$stats);

	if ($game["homeTeamId"] == $teamId)
	{
		$userTeam	= $game["homeTeam"];
		$otherTeam	= $game["awayTeam"];
	}
	else
	{
		$userTeam	= $game["awayTeam"];
		$otherTeam	= $game["homeTeam"];
	}

	print ("<form method=\"POST\" name=\"gameStatFrm\" action=\"addGameStats.php\">\n"
			. "<table cellpadding=\"0\" border=\"0\" > "
			. "<tr><td colspan=\"4\" align=\"center\">Player Stats For <br>"
			. "<b>$userTeam</b> On Game " . $game["date"] . "  vs. <em> $otherTeam </em><br></td>"
			. "</tr>"
			. "<tr><td>&nbsp;</td></tr>"
			. "<tr>"
				. "<td><b>Player</b></td>"
				. "<td><b>Goals</b></td>"
				. "<td><b>Assists</b></td>"
				. "<td><b>Blocks</b></td>"
			. "</tr>"
			. "\n");

   $count	= 0;
   foreach ($stats AS $stat)
   {
	   print ("<tr>");

		$playerId	= $stat["playerId"];
		$firstName	= $stat["firstName"];
		$lastName	= $stat["lastName"];
		$goals		= $stat["goals"];
		$assists	= $stat["assists"];
		$blocks		= $stat["blocks"];

			// need to figure this out for insert/update
		if ($stat["statId"] == null)
			$statId	= "null";
		else
			$statId=$stat["statId"];;

		print ("<td align=\"left\" nowrap=\"nowrap\" >");
			print ("<em>$firstName $lastName :</em>");
			genInputField("hidden", "playerId" . "$count", "1", $playerId);
			genInputField("hidden", "statId" . "$count", "1", $statId);
		print("</td>\n");
		print ("<td align=\"center\">\n");
			genInputField("text", "goals" . "$count", "2", $goals);
		print("</td>\n");
		print ("<td align=\"center\">");
			genInputField("text", "assists" . "$count", "2", $assists);
		print("</td>\n");
		print ("<td align=\"center\">");
			genInputField("text", "blocks" . "$count", "2", $blocks);
		print("</td>\n");

		print("</tr>\n");

		$count	+= 1;
	}

	print ("<tr><td>");
	genInputField("submit", "Save", "1", "Save");
	genInputField("hidden", "game_id", "1", $gameId);
	genInputField("hidden", "team_id", "1", $teamId);
	genInputField("hidden", "playerCount", "1", "$count");

	print ("</td></tr>\n");

	print ("</table></form>");
}

function genTeamRosterUpdate($teamId)
{
	$roster	= array();
	getTeamRoster($teamId, &$roster);

   print ("<table align=\"center\">\n");
   foreach ($roster AS $player)
   {
		$playerId	= $player["playerId"];
		$firstName	= $player["firstName"];
		$lastName	= $player["lastName"];
		$isCaptain	= $player["isCaptain"];

		$captainText	= " ";
		if ($isCaptain == 'Y')
			$captainText = "[Captain]";

		print ("<tr><td align\"center\"><b>$firstName $lastName $captainText </b><a href=\"removePlayer.php?player_id=$playerId&team_id=$teamId\">Remove</a></td></tr>");
	}

	print ("</table>");
}


function genAvailablePlayersSelection($fieldName, $division, $captainsOnly = false)
{
	$players	= array();
	getAvailablePlayers($captainsOnly, $division, &$players);

	print ("<select name=\"$fieldName\" >");

		// empty option
	print ("<option value=\"\">Select Player</option>");

   foreach ($players AS $player)
   {
		$playerId	= $player["playerId"];
		$firstName	= $player["firstName"];
		$lastName	= $player["lastName"];

		print ("<option value=\"$playerId\">$lastName, $firstName</option>");
	}

	print("</select>");
}

function genTeamRoster($teamId)
{
	$players	= array();
	getTeamRoster($teamId, &$players);

   $r	= 0;
   print ("<table>\n");
   print ("<tr>");
  
   print ("<td>Last Name</td>".
                "<td>First Name</td>".
                "<td>Email Address</td>".
                "<td>Nickname</td>".                
                "<td>Street</td>".
                "<td>City</td>".
                "<td>State</td>".
                "<td>Zip</td>".
                "<td>Home Phone</td>".
                "<td>Work Phone</td>".
                "<td>Paid Dues</td>");

   print ("</tr>");
                   
                    
   foreach ($players AS $player)
   {
	 $firstName		= $player["firstName"];
	 $lastName		= $player["lastName"];
	 $nickName		= $player["nickName"];
	 $isCaptain		= $player["isCaptain"];
	 $street		= $player["street"];
	 $city			= $player["city"];
	 $state			= $player["state"]; 
	 $zipcode		= $player["zipcode"];
	 $homePhone		= $player["homePhone"];
	 $workPhone		= $player["workPhone"];
	 $emailAddress	= $player["emailAddress"];
	 $isPaid                = $player["isPaid"];
		$captainText	= "";
		if ($isCaptain == 'Y')
			$captainText = "[Captain] ";

	 $color	= "white";
	 if ($r % 2 == 0)
	 	$color	= "gray";

	 print ("<tr bgcolor=\"$color\">");
	 print ("<td>$captainText" . "$firstName</td>");
	 print ("<td>$lastName</td>");
		print ("<td>$emailAddress</td>");
	 print ("<td>$nickName</td>");
	 print ("<td>$street</td>");
	 print ("<td>$city</td>");
	 print ("<td>$state</td>");
	 print ("<td>$zipcode</td>");
	 print ("<td>$homePhone</td>");
	 print ("<td>$workPhone</td>");
	 print ("<td>$isPaid</td>");
         
         print ("</tr>");
         
	$r	+= 1;
	}

	print ("</table>");
}

function genLeagueRoster()
{
	$players	= array();
	getTeamRoster(null, &$players);

	$r	= 0;
	print ("<table border=\"1\">\n");
	print ("<tr>");
	print ("<td>Team</td>".
			"<td>Last Name</td>".
			"<td>First Name</td>".
			"<td>Street</td>".
			"<td>City</td>".
			"<td>State</td>".
			"<td>Zip</td>".
			"<td>Paid Dues</td>");
	print ("</tr>");

	foreach ($players AS $player)
	{
		$firstName		= $player["firstName"];
		$lastName		= $player["lastName"];
		$nickName		= $player["nickName"];
		$isCaptain		= $player["isCaptain"];
		$street			= $player["street"];
		$city			= $player["city"];
		$state			= $player["state"]; 
		$zipcode		= $player["zipcode"];
		$homePhone		= $player["homePhone"];
		$workPhone		= $player["workPhone"];
		$emailAddress	= $player["emailAddress"];
		$isPaid			= $player["isPaid"];
		$teamName		= $player["teamName"];

		$captainText	= "";
		if ($isCaptain == 'Y')
			$captainText = "[Captain] ";

		$color	= "white";
		if ($r % 2 == 0)
			$color	= "gray";

		print ("<tr bgcolor=\"$color\">");
		print ("<td>$teamName</td>");
		print ("<td>$captainText" . "$lastName</td>");
		print ("<td>$firstName</td>");
		print ("<td>$street</td>");
		print ("<td>$city</td>");
		print ("<td>$state</td>");
		print ("<td>$zipcode</td>");
		print ("<td>$isPaid</td>");
		print ("</tr>");

		$r	+= 1;
	}

	print ("</table>");
}

function genTeamSelection($fieldName, $division)
{
	$teams	= array();
	getTeamSet($division, &$teams);

	print ("<select name=\"$fieldName\" >");

		// empty option
	print ("<option value=\"\">Select Team</option>");

   foreach ($teams AS $team)
   {
		$teamId	= $team["id"];
		$name	= $team["name"];

		print ("<option value=\"$teamId\">$name</option>");
	}

	print("</select>");
}

function genUnplayedDivisionGames($division)
{
	$games	= array();
	getDivisionGames($division, false, &$games);

	print ("<table> "
			. "<thead><tr>"
				. "<th>Date</th>"
				. "<th>Home</th>"
				. "<th>Away</th>"
				. "<th>Field</th>"
			. "</tr></thead>");

	foreach ($games AS $game)
	{
		print ("<tr>");
		print ("<td>" . $game["date"] . "</td>");
		print ("<td>" . $game["homeTeam"] . "</td>");
		print ("<td>" . $game["awayTeam"] . "</td>");
		print ("<td>" . $game["field"] . "</td>");

		print ("</tr>");

	}
	print ("</table>");
}

function genTeamGameResults($team, $forCaptain)
{
	$games	= array();
	getTeamGames($team, true, &$games);

	print ("<table bgcolor=\"#F879FA\" cellspacing=\"4\" border=\"0\" bordercolorlight=\"#F879FA\" bordercolordark=\"#F879FA\"> "
			. "<thead><tr>"
				. "<th>Date</th>"
				. "<th>Home</th>"
				. "<th>Away</th>"
				. "<th>Field</th>"
				. "<th>&nbsp;</th>"
				. "<th>&nbsp;</th>"
			. "</tr></thead>");

	foreach ($games AS $game)
	{
		print ("<tr>");
		print ("<td valign=\"center\">" . $game["date"] . "</td>");
		print ("<td valign=\"center\">" . $game["homeTeam"] . ":<b>".$game["homeScore"] . "</b></td>");
		print ("<td valign=\"center\">" . $game["awayTeam"] . ":<b>".$game["awayScore"] . "</b></td>");
		print ("<td valign=\"center\">" . $game["field"] . "</td>");

		if ($forCaptain == true)
		{
			print("<td><a href=\"clearGameScore.php?game_id=" . $game["id"]
					. "&team_id=" . $team
					. "&home_team_id=" . $game["homeTeamId"]
					. "&home_score=" . $game["homeScore"]
					. "&away_team_id=" . $game["awayTeamId"]
					. "&away_score=" . $game["awayScore"]
					. "\">Clear</a></td>");

			print("<td><a href=\"showaddplayerstats.php?game_id="
					. $game["id"]
					. "&team_id=$team"
					. "\">Enter Player Stats</a></td>");
		}

		print ("</tr>");

	}
	print ("</table>");
}

function genTeamGameResultUpdate($team)
{
	$games	= array();
	getTeamGames($team, false, &$games);

	print ("<form method=\"POST\" name=\"gameResultFrm\" action=\"addGameResults.php\"><table> "
			. "<thead><tr>"
				. "<th>Game</th>"
				. "<th>Home Score</th>"
				. "<th>Away Score</th>"
				. "<th>&nbsp;</th>"
				. "<th>&nbsp;</th>"
			. "</tr></thead>");

	$gameCount = 0;
	foreach ($games AS $game)
		{
		print ("<tr>");
		print ("<td><b>" . $game["date"] . "</b>&nbsp;<em>" . $game["homeTeam"] . " vs " . $game["awayTeam"] . "</em></td>");
		
		print ("<td>");
			print ("<b>" . $game["homeTeam"] . "</b>&nbsp;");
			genInputField("text", "homeScore" . "$gameCount", "2", $game["homeScore"]);
		print("</td>");

		print ("<td>");
			print ("<b>" . $game["awayTeam"] . "</b>&nbsp;");
			genInputField("text", "awayScore". "$gameCount", "2", $game["awayScore"] );
			print("</td>");
			print("<td>");
				 genInputField("hidden", "gameId" . "$gameCount", 1,  $game["id"]);
				 genInputField("hidden", "team_id" . "$gameCount", 1,  $team);
				 genInputField("hidden", "home_team_id" . "$gameCount", 1,  $game["homeTeamId"]);
				 genInputField("hidden", "away_team_id" . "$gameCount", 1,  $game["awayTeamId"]);
			print("</td>");
			print ("</tr>\n");
			$gameCount += 1;
	}
	   
		print("<tr>");
		print("<td>");
		genInputField("hidden", "gameCount", "1", "$gameCount");
		genInputField("hidden", "team_id", "1", "$team");
		genInputField("submit", "saveGame", "1", "Save");
	print ("</table></form>");
}

function genUnpaidPlayersForUpdate($readOnly)
{
	$players	= array();

	getUnpaidPlayers(&$players);

	print ("<form method=\"POST\" name=\"playerAdminFrm\" action=\"updatePlayerAdmin.php\">\n"
			. "<table cellpadding=\"0\" border=\"0\" > "
			. "<tr><td>&nbsp;</td></tr>"
			. "<tr>"
				. "<td><b>Last Name</b></td>"
				. "<td><b>First Name</b></td>"
				. "<td><b>Is Paid</b></td>"
				. "<td><b>Provided Waiver</b></td>"
			. "</tr>"
			. "\n");

   $count	= 0;
   foreach ($players AS $player)
   {
	   print ("<tr>");

		$playerId	= $player["playerId"];
		$firstName	= $player["firstName"];
		$lastName	= $player["lastName"];
		$isPaid		= $player["isPaid"];
		$providedWaiver	= $player["providedWaiver"];


		print ("<td align=\"right\" nowrap=\"nowrap\" >");
		        genInputField("hidden", "playerId" . "$count", "1", $playerId);
		print ("<b>$lastName</b>" );
		print("</td>\n");

		print ("<td align=\"right\">"
			. "<b>$firstName</b>");
		print("</td>\n");

		print ("<td align=\"center\">");
                if ($isPaid == 'Y')
		    print ("<input checked type=\"checkbox\" name=\"isPaid" . $count . "\"/>");
		else
		    print ("<input type=\"checkbox\" name=\"isPaid" . $count . "\"/>");
		print("</td>\n");

		print ("<td align=\"center\">");
                if ($providedWaiver == 'Y')
		    print ("<input checked type=\"checkbox\" name=\"providedWaiver" . $count . "\"/>");
		else
		    print ("<input type=\"checkbox\" name=\"providedWaiver" . $count . "\"/>");
		print("</td>\n");

		print("</tr>\n");

		$count	+= 1;
	}

	print ("<tr><td>");
if (!$readOnly) {
	genInputField("submit", "Save", "1", "Save");
	genInputField("hidden", "playerCount", "1", "$count");
}

	print ("</td></tr>\n");

	print ("</table></form>");
}
?>
