<?php

  include("html_functions.php");



  ################## PAGE INITIALIZATION #########################

  $lgid=$_SESSION['lgid'];

  $lgdate=$_SESSION['lgdate'];

  $lgpath=$_SESSION['lgpath'];



  if (($_SESSION['user_team_id']>0)&&(!isset($_GET['team_id'])))

  {

	  if ($_SESSION['user_league_id']==$lgid) {$team_id=$_SESSION['user_team_id'];}

	  else

	  {

		  include($_SESSION['adminpath']."/dbopen.php");

		  $query="SELECT team_id FROM teams WHERE parent_team_id=".$_SESSION['user_team_id']." AND league_id=$lgid";

		  $result=mysqli_query($db,$query);

		  if (mysqli_num_rows($result))

		  {

			  $row=mysqli_fetch_array($result);

			  $team_id=$row['team_id'];

		  }

		  mysqli_close($db);

	  }

  }

  elseif (isset($_GET['team_id'])) {$team_id=$_GET['team_id'];}

  

  # Title Bar

  $title='Box Scores';



  page_head($title,$_SESSION['lgpath']);

  echo " <div id='topbar'>\n";

  main_titlebar($lgid,$_SESSION['lgpath'],$title,$team_id);

  nav_toolbar("season","Season");

  echo " </div>   <!-- END topbar DIV -->\n";

  echo " <div id='contentpane'>\n";





  ################## START OF PAGE CONTENT #########################

  $simLen=get_sim_length($_SESSION['adminpath'],$lgid);



  include($_SESSION['adminpath']."/dbopen.php");



  ##### Show Selection Form #####

  $query="SELECT team_id,abbr,name,nickname,logo_file_name FROM teams WHERE league_id=$lgid AND allstar_team=0 ORDER BY name,nickname;";

  $result=mysqli_query($db,$query);

   

  echo "<div class='textbox'>\n";

  echo " <table cellpadding=2 cellspacing=0 border=0>\n";

  echo "  <tr class='title'><td>Select Team</td></tr>\n";

  echo "  <tr><td>\n";

  echo "   <form method='get' action='boxscores.php' class='inline'>\n";

  echo "    <select name='team_id'>\n";

  $fnd=0;

  $minTm=0;

  while ($row=mysqli_fetch_array($result))

  {

    	  $tid=$row['team_id'];

	  if ($minTm==0) {$minTm=$tid;}

    	  $tname=$row['name']." ".$row['nickname'];

	  $tname=str_replace(".","",$tname);

    	  $teams[$tid]['name']=$tname;

	  $teams[$tid]['logo']=urlencode($row['logo_file_name']);

    	  $teams[$tid]['w']=0;

	  $teams[$tid]['l']=0;

    	  $teams[$tid]['hw']=0;

	  $teams[$tid]['hl']=0;

	  $teams[$tid]['rw']=0;

    	  $teams[$tid]['rl']=0;

	  $teamcity[$tid]=$row['name'];

    	  $tabbr[$tid]=$row['abbr'];

	  echo "     <option value=$tid";

    	  if ($tid==$team_id) {echo " selected";$fnd=1;}

	  echo ">$tname</option>\n";

  }

  if ($fnd==0) {$team_id=$minTm;}

  echo "    </select>\n";

  echo "    <input type='submit' value='Go' style='float:left;' />\n";

  echo "   </form>\n";

  echo "  </td></tr>\n";

  echo " </table>\n";  

  echo "</div>\n";



  ## Results Toggle

  echo "<div class='textbox' style='float:right;clear:right;border:0;'>\n";

  echo "    <form method='post' action='admin_login_functions.php'>\n";

  echo "     <input type='hidden' name='formName' value='toggleResults'>\n";

  echo "     <input type='hidden' name='user_id' value='".$_SESSION['user_id']."'>\n";

  echo "     <label for='hideScores'>Hide Results</label>\n";

  echo "     <input type='checkbox' name='hideScores' id='hideScores' value=1 ";

  if ($_SESSION['hideScores']==1) {echo "checked";}

  echo " onclick='this.form.submit();' />\n";

  echo "    </form>\n";

  echo "</div>\n";



  ## Verify team ID

  if ($team_id=="")

  {

	  ### END OF PAGE

	  echo " </div>   <!-- END contentpane DIV -->\n";

	  page_foot($_SESSION['lgpath']);

	  exit;

  }



  ##### Get Games #####

  $gList="";

  $pList="";

  $query="SELECT game_id,games.date,home_team,away_team,innings,runs0,runs1,hits0,hits1,errors0,errors1,winning_pitcher,losing_pitcher,save_pitcher,if(home_team=$team_id,if(runs1>runs0,1,0),if(runs0>runs1,1,0)) as win,if(away_team=$team_id,if(runs1>runs0,1,0),if(runs0>runs1,1,0)) as loss FROM games WHERE DATEDIFF('$lgdate',games.date)<$simLen AND played=1 AND (home_team=$team_id OR away_team=$team_id) ORDER BY games.date";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $gid=$row['game_id'];

		  $gdate=$row['date'];



		  $tWins=$tWins+$row['win'];

		  $tLosses=$tLosses+$row['loss'];





		  $games[$gid][$gdate]=1;

		  $gList.=",$gid";



		  $games[$gid]['date']=$gdate;

		  $games[$gid]['hid']=$row['home_team'];

		  $games[$gid]['aid']=$row['away_team'];

		  $games[$gid]['innings']=$row['innings'];

		  $games[$gid]['runs0']=$row['runs0'];

		  $games[$gid]['runs1']=$row['runs1'];

		  $games[$gid]['hits0']=$row['hits0'];

		  $games[$gid]['hits1']=$row['hits1'];

		  $games[$gid]['errors0']=$row['errors0'];

		  $games[$gid]['errors1']=$row['errors1'];

		  if ($row['win']==1) {$games[$gid]['dec']="W";}

		  else {$games[$gid]['dec']="L";}



		  $wp=$row['winning_pitcher'];

		  $lp=$row['losing_pitcher'];

		  $sp=$row['save_pitcher'];

		  $games[$gid]['winning_pitcher']=$wp;

		  $games[$gid]['losing_pitcher']=$lp;

		  $games[$gid]['save_pitcher']=$sp;



		  $players[$wp]['fnd']=1;

		  $players[$lp]['fnd']=1;

		  $players[$sp]['fnd']=1;

	  }

  }

  $gList=trim($gList,",");



  if ($gList=="") {$gList=-999;}



  ##### Get Home Runs #####

  $query="SELECT player_id,team_id,game_id,hr FROM players_game_batting WHERE hr>0 AND game_id IN ($gList)";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result))

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $pid=$row['player_id'];

		  $tid=$row['team_id'];

		  $gid=$row['game_id'];

		  $hr=$row['hr'];



		  $players[$pid]['fnd']=1;



		  $games[$gid]['hrs'][$tid][$pid]=$hr;

	  }

  }



  ##### Get Player Names #####

  if (isset($players))

  {

	  foreach ($players as $pid => $val) {$pList.=",$pid";}

	  $pList=trim($pList,",");

	

	  $query="SELECT player_id,first_name,last_name FROM players WHERE player_id IN ($pList)";

	  if ($_GET['showq']==1) {echo "$query<br/>\n";}

	  $result=mysqli_query($db,$query);

	  if (mysqli_num_rows($result))

	  {

		  while ($row=mysqli_fetch_array($result))

		  {

			  $pid=$row['player_id'];

			  $fi=$row['first_name'];

			  $fi=$fi[0];

			  $name=$fi.". ".$row['last_name'];

			  $players[$pid]['name']=$name;

		  }

	  }

  }



  ##### Get Inning-by-Inning Scores #####

  $query="SELECT game_id,team,inning,score FROM games_score WHERE game_id IN ($gList) ORDER BY game_id,team,inning";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  if (mysqli_num_rows($result))

  {

	  $prevGid=-1;

	  while ($row=mysqli_fetch_array($result))

	  {

		  $gid=$row['game_id'];

		  $tid=$row['team'];

		  $inn=$row['inning'];



		  $score=$row['score'];



		  if ($inn<=($games[$gid]['innings']-12)) {continue;}

	

		  if ($tid==0)

		  {

			  if ($prevGid!=$gid) {$innText[$gid]['inn']="<td class='hc' width=18>&#160;</td>";}

			  $innText[$gid]['inn'].="<td class='hc' width=15>$inn</td>";

		  }

		  elseif (($games[$gid]['runs1']>$games[$gid]['runs0'])&&($row['inning']>8)&&($row['inning']==$games[$gid]['innings'])&&($score==0)) {$score="X";}





		  $innText[$gid][$tid]=$innText[$gid][$tid]."<td class='ic' width=15>";

		  if ($_SESSION['hideScores']==1) {$innText[$gid][$tid]=$innText[$gid][$tid]."<span title='$score'>?</span>";}

		  else {$innText[$gid][$tid]=$innText[$gid][$tid]."$score";}

		  $innText[$gid][$tid]=$innText[$gid][$tid]."</td>";



		  $prevGid=$gid;

	  }

  }

  

  ##### Get Games w/ Great Ind Performances #####

  # Batting Performances

  $query="SELECT game_id,pgb.player_id,pgb.team_id,first_name,last_name,h,d,t,hr,rbi,sb FROM players_game_batting as pgb,players as p WHERE pgb.player_id=p.player_id AND pgb.league_id=".$lgid." AND (hr>2 OR h>4 OR rbi>5 or sb>2 OR ((h-d-t-hr)>0 AND d>0 AND t>0 AND hr>0)) AND game_id IN ($gList);";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  while ($row=mysqli_fetch_array($result))

  {

    	  $gid=$row['game_id'];

	  $pid=$row['player_id'];

    	  $tid=$row['team_id'];

	  $fi=$row['first_name'];

    	  $fi=$fi[0];

	  $name=$fi.". ".$row['last_name'];

    	  $h=$row['h'];

	  $d=$row['d'];

    	  $t=$row['t'];

	  $hr=$row['hr'];

	  $s=$h-$d-$t-$hr;

    	  $rbi=$row['rbi'];

	  $sb=$row['sb'];



	  if ($hr>2) 

   	  {

		  if ($_SESSION['hideScores']==1) {$games[$gid]['note']="<span title=\"$name belts $hr HR's\">???</span>";}

		  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> belts $hr HR's";}

   	  }

	  if ((($h>4)&&($tid==$team_id))||($h>5))

   	  {

		  if ($_SESSION['hideScores']==1) {$games[$gid]['note']="<span title=\"$name knocks $h hits\">???</span>";}

		  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> knocks $h hits";}

   	  }

	  if ((($rbi>5)&&($tid==$team_id))||($rbi>7))

   	  {

		  if ($_SESSION['hideScores']==1) {$games[$gid]['note']="<span title=\"$name plates $rbi\">???</span>";}

		  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> plates $rbi";}

   	  }

	  if ((($sb>2)&&($tid==$team_id))||($sb>3))

   	  {

		  if ($_SESSION['hideScores']==1) {$games[$gid]['note']="<span title=\"$name steals $sb bases\">???</span>";}

		  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> steals $sb bases";}

   	  }

	  if (($s>0)&&($d>0)&&($t>0)&&($hr>0))

   	  {

		  if ($_SESSION['hideScores']==1) {$games[$gid]['note']="<span title=\"$name hits for the cycle\">???</span>";}

		  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> hits for the cycle";}

   	  }

  }



  # Pitching Performances

  $query="SELECT game_id,pgp.player_id,pgp.team_id,first_name,last_name,k,(ip*3+ipf)/3 as ip,ha,cg,sho FROM players_game_pitching_stats as pgp,players as p WHERE pgp.player_id=p.player_id AND pgp.league_id=".$lgid." AND (k>9 OR ((ip*3+ipf)/3)>9 OR (ha=0 AND ((ip*3+ipf)/3)>7) OR (ha<3 AND cg=1 AND sho=1)) AND game_id IN ($gList);";

  if ($_GET['showq']==1) {echo "$query<br/>\n";}

  $result=mysqli_query($db,$query);

  while ($row=mysqli_fetch_array($result))

  {

    	  $gid=$row['game_id'];

	  $pid=$row['player_id'];

    	  $tid=$row['team_id'];

	  $fi=$row['first_name'];

    	  $fi=$fi[0];

	  $name=$fi.". ".$row['last_name'];

    	  $k=$row['k'];

	  $ip=$row['ip'];

    	  $ha=$row['ha'];

	  $cg=$row['cg'];

    	  $sho=$row['sho'];

	  if (floor($ip)==$ip) {$dispIP=round($ip,0);} else {$dispIP=round(floor($ip),0)." ".round((3*($ip-floor($ip))),0)."/3";}



	  if ((($k>9)&&($tid==$team_id))||($k>14))

	  {

		  if ($_SESSION['hideScores']==1) {$games[$gid]['note']="<span title=\"$name strikes out $k\">???</span>";}

		  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> strikes out $k";}

	  }

    	  if ($ip>9)

	  {

		  if ($_SESSION['hideScores']==1) {$games[$gid]['note']="<span title=\"$name goes ".$dispIP." innings\">???</span>";}

		  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> goes ".$dispIP." innings";}

   	  }

	  if (($ha==0)&&($ip>7))

	  {

		  if ($cg==1) 

		  {

			  if ($_SESSION['hideScores']==1) {$gnote="<span title=\"$name pitches a no-hitter\">???</span>";}

			  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> pitches a no-hitter";}

		  }

		  else 

		  {

			  if ($_SESSION['hideScores']==1) {$games[$gid]['note']="<span title=\"$name fails to allow a hit\">???</span>";}

			  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> fails to allow a hit";}

		  }

	  }

	  if (($ha<3)&&($cg==1)&&($sho==1)&&($ha!=0))

	  {

		  if ($_SESSION['hideScores']==1) {$gnote="<span title=\"$name pitches a $ha-hit shutout\">???</span>";}

		  else {$games[$gid]['note']=$games[$gid]['note'].", <a href='./player.php?player_id=$pid'>$name</a> pitches a $ha-hit shutout";}

	  }

  }



  ##### Display Past Games #####

  if ($_SESSION['hideScores']==1)

  {

	  $tWins="<span title='$tWins'>??</span>";

	  $tLosses="<span title='$tLosses'>??</span>";

  }



  echo "<div style='float:left;margin:0px;padding:0px;width:935px;'>\n";    # Container Div



  echo " <div id='first' class='boxscores'><table cellpadding=2 cellspacing=0 border=0 style='border:0;margin:0;width:100%;'><tr class='title'><td>Last Sim Results ($tWins-$tLosses)</td></tr></table>\n";



  if (isset($games))

  {

	  foreach ($games as $gid => $val)

	  {

		  $hid=$games[$gid]['hid'];

		  $hname=$teams[$hid]['name'];

		  $aid=$games[$gid]['aid'];

		  $aname=$teams[$aid]['name'];

		  $inns=$games[$gid]['innings'];

	

		  $gRes=$games[$gid]['dec'];

		  if ($gRes=="W")

		  {

			  if ($_SESSION['hideScores']==1) {$gRes="<span style='font-weight:bold;padding-right:1px;color:#000099;' title='Win'>???</span>: ";}

			  else {$gRes="<span style='font-weight:bold;padding-right:1px;color:#009900;'>Win</span>: ";}

		  }

		  else 

		  {

			  if ($_SESSION['hideScores']==1) {$gRes="<span style='font-weight:bold;padding-right:1px;color:#000099;' title='Loss'>???</span>: ";}

			  else {$gRes="<span style='font-weight:bold;padding-right:1px;color:#990000;'>Loss</span>: ";}

		  }



		  # Inning-by-Inning

		  $inntxt=$innText[$gid]['inn'];

		  $ainn=$innText[$gid][0];

		  $hinn=$innText[$gid][1];

	  

		  for ($i=0;$i<(12-$inns);$i++)

		  {

			  $inntxt.="<td class='hc' width=15>&#160;</td>";

			  $ainn.="<td class='ic' width=15>&#160;</td>";

			  $hinn.="<td class='ic' width=15>&#160;</td>";

		  }

		  $inntxt.="<td class='hc' width=15>R</td><td class='hc' width=15>H</td><td class='hc' width=15>E</td>";



		  if ($_SESSION['hideScores']==1)

		  {

			  $games[$gid]['runs0']="<span title='".$games[$gid]['runs0']."'>?</span>";

			  $games[$gid]['runs1']="<span title='".$games[$gid]['runs1']."'>?</span>";

			  $games[$gid]['hits0']="<span title='".$games[$gid]['hits0']."'>?</span>";

			  $games[$gid]['hits1']="<span title='".$games[$gid]['hits1']."'>?</span>";

			  $games[$gid]['errors0']="<span title='".$games[$gid]['errors0']."'>?</span>";

			  $games[$gid]['errors1']="<span title='".$games[$gid]['errors1']."'>?</span>";

		  }

		  $ainn.="<td class='icgb' width=15>".$games[$gid]['runs0']."</td><td class='ic' width=15>".$games[$gid]['hits0']."</td><td class='ic' width=15>".$games[$gid]['errors0']."</td>";

		  $hinn.="<td class='icgb' width=15>".$games[$gid]['runs1']."</td><td class='ic' width=15>".$games[$gid]['hits1']."</td><td class='ic' width=15>".$games[$gid]['errors1']."</td>";

	

		  # Pitchers

		  $wp=$games[$gid]['winning_pitcher'];

		  $lp=$games[$gid]['losing_pitcher'];

		  $sp=$games[$gid]['save_pitcher'];

	  

		  $ptxt="W: <a href='./player.php?player_id=$wp'>".$players[$wp]['name']."</a> L: <a href='./player.php?player_id=$lp'>".$players[$lp]['name']."</a>";

		  if ($sp!=0) {$ptxt.=" S: <a href='./player.php?player_id=$sp'>".$players[$sp]['name']."</a>";}

		  if ($_SESSION['hideScores']==1)

		  {

			  $ptxt="<span title=\"W: ".$players[$wp]['name']." L: ".$players[$lp]['name'];

			  if ($sp!=0) {$ptxt.=" S: ".$players[$sp]['name'];}

			  $ptxt.="\">W: ?? L: ?? S: ??</span>";

		  }

		  

		  # HR Text

		  unset($hrtxt);

		  if (isset($games[$gid]['hrs'][$aid]))

		  {

			  $hrtxt[$aid]=$tabbr[$aid].": ";

			  $hrCnt=0;

			  foreach ($games[$gid]['hrs'][$aid] as $pid => $hr)

			  {

				  if ($hrCnt>0) {$hrtxt[$aid].=", ";}

				  if ($_SESSION['hideScores']==1) {$hrtxt[$aid].=$players[$pid]['name'];}

				  else {$hrtxt[$aid].="<a href='./player.php?player_id=$pid'>".$players[$pid]['name']."</a>";}

				  if ($hr>1) {$hrtxt[$aid].=" ($hr)";}

				  $hrCnt++;

			  }

		  }

		  if (isset($games[$gid]['hrs'][$hid]))

		  {

			  $hrtxt[$hid]=$tabbr[$hid].": ";

			  $hrCnt=0;

			  foreach ($games[$gid]['hrs'][$hid] as $pid => $hr)

			  {

				  if ($hrCnt>0) {$hrtxt[$hid].=", ";}

				  if ($_SESSION['hideScores']==1) {$hrtxt[$hid].=$players[$pid]['name'];}

				  else {$hrtxt[$hid].="<a href='./player.php?player_id=$pid'>".$players[$pid]['name']."</a>";}

				  if ($hr>1) {$hrtxt[$hid].=" ($hr)";}

				  $hrCnt++;

			  }

		  }

		  if ((isset($games[$gid]['hrs'][$aid]))||(isset($games[$gid]['hrs'][$hid])))

		  {

			  if ($_SESSION['hideScores']==1) {$hrNote="HR - <span title=\"".$hrtxt[$aid]." ".$hrtxt[$hid]."\">???</span>";}

			  else {$hrNote="HR - ".$hrtxt[$aid]." ".$hrtxt[$hid];}

		  }

		  else

		  {

			  if ($_SESSION['hideScores']==1) {$hrNote="HR - <span title=\"None\">???</span>";}

			  else {$hrNote="";}

		  }

		  

		  # Game Note

		  if (isset($games[$gid]['note']))

		  {

			  $gnote=$games[$gid]['note'];

			  $gnote=trim($gnote,", ");

			  $gnote="<br />Notes: ".$gnote;

		  }

		  else {$gnote="";}

		

		  unset($gdate);

		  $gDate=new Date($games[$gid]['date']);

		  echo " <table cellspacing=0 cellpadding=0 style='border:0px;width:430px;margin:10px;'>\n";

		  echo "  <tr>\n";

		  echo "   <td class='hl'>\n";

		  echo "    $gRes\n";

		  echo "    ".$gDate->format("M j, Y");

		  echo ": <a href='$lgpath/box_scores/game_box_$gid.html'>Box Score</a>";

		  echo " | <a href='$lgpath/game_logs/log_$gid.html'>Game Log</a>";

		  echo " | <a href='./matchups.php?team_id1=$hid&team_id2=$aid'>Season Series</a>";

		  echo "   </td>\n";

		  echo "  </tr>\n";

		  echo "  <tr>\n";

		  echo "   <td>\n";

		  echo "    <table cellpadding=0 cellspacing=0 style='border:1px black solid;width:430px;margin-top:2px;margin-left:0px;'>\n";

		  echo "     <tr>\n";

		  echo "      <td style='padding:1px;width:44px;border-right:1px solid #999999;'>\n";

		  echo "       <img src='$lgpath/images/team_logos/".str_replace(".png","_40.png",$teams[$aid]['logo'])."' width=40 height=40><br>\n";

		  echo "       <img src='$lgpath/images/team_logos/".str_replace(".png","_40.png",$teams[$hid]['logo'])."' width=40 height=40>\n";

		  echo "      </td>\n";

		  echo "      <td valign='top' style='padding:0px;margin:0px'>\n";

		  echo "       <table cellspacing=0 cellpadding=1 style='width:100%;margin:0px;border:0px'>\n";

		  echo "        <tr>$inntxt</tr>\n";

		  echo "        <tr><td class='gl'><a href='$lgpath/teams/team_$aid.html'>".$teamcity[$aid]."</a></td>";

		  echo "$ainn</tr>\n";

		  echo "        <tr><td class='gl'><a href='$lgpath/teams/team_$hid.html'>".$teamcity[$hid]."</a></td>";

		  echo "$hinn</tr>\n";

		  echo "        <tr><td colspan=16 class='gl' style='padding:6px 4px 4px 4px;'>".$ptxt."<br />".$hrNote.$gnote."</td></tr>\n";

		  echo "       </table>\n";

		  echo "      </td>\n";

		  echo "     </tr>\n";

		  echo "    </table>\n";

		  echo "   </td>\n";

		  echo "  </tr>\n";

		  echo " </table>\n";

	  }

  }

  else {echo "<p>No games played</p>\n";}



  echo " </div>\n";



  ##### Get Team Situational Records #####

  $query="SELECT game_id,home_team,away_team,runs0,runs1 FROM games WHERE game_type=0 AND league_id=$lgid AND played=1;";

  $result=mysqli_query($db,$query);

  while ($row=mysqli_fetch_array($result))

  {

    	  $hid=$row['home_team'];

	  $aid=$row['away_team'];

    	  if ($row['runs0']>$row['runs1'])

	  {

	 	  $teams[$aid]['w']=$teams[$aid]['w']+1;

		  $teams[$aid]['rw']=$teams[$aid]['rw']+1;

	 	  $teams[$hid]['l']=$teams[$hid]['l']+1;

		  $teams[$hid]['hl']=$teams[$hid]['hl']+1;

	 	  $teams[$aid]['wVs'][$hid]=$teams[$aid]['wVs'][$hid]+1;

	  }

   	  else

	  {

	 	  $teams[$aid]['l']=$teams[$aid]['l']+1;

		  $teams[$aid]['rl']=$teams[$aid]['rl']+1;

	 	  $teams[$hid]['w']=$teams[$hid]['w']+1;

		  $teams[$hid]['hw']=$teams[$hid]['hw']+1;

	 	  $teams[$hid]['wVs'][$aid]=$teams[$hid]['wVs'][$aid]+1;

	  }

  }



  ##### Get Upcoming Schedule #####

  $query="SELECT game_id,home_team,away_team,games.date,time FROM games WHERE played=0 AND DATEDIFF('".$lgdate."',games.date)<=0 AND DATEDIFF('".$lgdate."',games.date)>-".($simLen-1)." AND (home_team=".$team_id." OR away_team=".$team_id.") ORDER BY date,time;";

  $result=mysqli_query($db,$query);



  echo " <div class='boxscores' style='margin-left:10px;'><table cellpadding=2 cellspacing=0 border=0 style='border:0;margin:0;width:100%;'><tr class='title'><td>Upcoming Schedule</td></tr></table>\n";

  while ($row=mysqli_fetch_array($result))

  {

    	  $hid=$row['home_team'];

	  $hname=$teams[$hid]['name'];

    	  $aid=$row['away_team'];

	  $aname=$teams[$aid]['name'];



	  unset($date);

    	  $date=new Date($row['date']." ".$row['time']);

	  echo "<table cellspacing=0 cellpadding=0 style='border:0px;width:430px;margin:10px;'>\n";

    	  echo " <tr>\n";

	  echo "  <td class='hl'>\n";

    	  echo "   ".$date->format("M j, Y")."\n";

	  echo "  </td>\n";

    	  echo " </tr>\n";

	  echo " <tr>\n";

    	  echo "  <td>\n";

	  echo "    <table cellpadding=0 cellspacing=0 style='border:1px black solid;width:430px;margin-top:2px;margin-left:0px;'>\n";

    	  echo "     <tr>\n";

	  echo "      <td style='padding:1px;width:44px;border-right:1px solid #999999;'>\n";

    	  echo "       <img src='$lgpath/images/team_logos/".str_replace(".png","_40.png",$teams[$aid]['logo'])."' width=40 height=40><br>\n";

	  echo "       <img src='$lgpath/images/team_logos/".str_replace(".png","_40.png",$teams[$hid]['logo'])."' width=40 height=40>\n";

    	  echo "      </td>\n";

	  echo "        <td valign='top' style='padding:0px;margin:0px'>\n";

    	  echo "       <table cellspacing=0 cellpadding=1 style='width:100%;margin:0px;border:0px'>\n";

    

	  echo "        <tr><td class='hl' colspan=2>".$date->format("g:i a")."</td></tr>\n";

	  echo "        <tr><td class='gl' width=175><a href='$lgpath/teams/team_$aid.html'>$aname</a></td>\n";

	  echo "            <td class='gl' width=175>";

	  if ($_SESSION['hideScores']==1) {echo "<span title='".$teams[$aid]['w']."-".$teams[$aid]['l']."'>??-??</span>";}

	  else {echo $teams[$aid]['w']."-".$teams[$aid]['l'];}

	  echo ", On Road: ";

	  if ($_SESSION['hideScores']==1) {echo "<span title='".$teams[$aid]['rw']."-".$teams[$aid]['rl']."'>??-??</span>";}

	  else {echo $teams[$aid]['rw']."-".$teams[$aid]['rl'];}

	  echo "</td></tr>\n";

	  echo "        <tr><td class='gl' width=175><a href='$lgpath/teams/team_$hid.html'>$hname</a></td>\n";

    	  echo "            <td class='gl' width=175>";

	  if ($_SESSION['hideScores']==1) {echo "<span title='".$teams[$hid]['w']."-".$teams[$hid]['l']."'>??-??</span>";}

	  else {echo $teams[$hid]['w']."-".$teams[$hid]['l'];}

	  echo ", At Home: ";

	  if ($_SESSION['hideScores']==1) {echo "<span title='".$teams[$hid]['hw']."-".$teams[$hid]['hl']."'>??-??</span>";}

	  else {echo $teams[$hid]['hw']."-".$teams[$hid]['hl'];}

	  echo "</td></tr>\n";

	  echo "        <tr><td class='gl' colspan=2 style='padding:6px 4px 4px 4px;'><a href='./matchups.php?team_id1=$hid&team_id2=$aid'>Season Series</a>:<br />$aname ";

	  if ($_SESSION['hideScores']==1) 

	  {

		  echo "<span title='".max(0,$teams[$aid]['wVs'][$hid])."'>??</span>";

		  echo " vs. ";

		  echo "<span title='".max(0,$teams[$hid]['wVs'][$aid])."'>??</span>";

	  }

	  else

	  {

		  echo max(0,$teams[$aid]['wVs'][$hid]);

		  echo " vs. ";

		  echo max(0,$teams[$hid]['wVs'][$aid]);

	  }

	  echo " $hname</td></tr>\n";

    	  echo "       </table>\n";

	  echo "      </td>\n";

    	  echo "     </tr>\n";

	  echo "    </table>\n";

    	  echo "   </td>\n";

	  echo "  </tr>\n";

    	  echo " </table>\n";

  }



  echo " </div>\n";



  echo "</div>\n";



  mysqli_close($db);



  ################## END OF PAGE #########################

  echo " </div>   <!-- END contentpane DIV -->\n";



  page_foot($_SESSION['lgpath']);





?>

