<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];

  $lgpath=$_SESSION['lgpath'];

  $lgdate=$_SESSION['lgdate'];

  $date=new DateTime($_SESSION['lgdate']);

  $lgyear=$date->format("Y");



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

  if (isset($_GET['who'])) {$who=$_GET['who'];} else {$who='both';}

  $oppTmID=$_GET['opp_id'];

  if (($oppTmID=="")||($team_id[0]!='T')) {$oppTmID="All";}



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



  if (!isset($_GET['team_id']))

  {

	  if (($lgid!=$_SESSION['user_league_id'])&&(isset($_SESSION['user_team_id'])))

	  {

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

		  if ($_GET['showq']==1) {echo $query;}

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

		  if (mysqli_num_rows($result)>0)

		  {

			  $row=mysqli_fetch_array($result);

			  $team_id=$row['team_id'];

		  }

		  else {$team_id="ALL";}

	  }

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

	  else {$team_id=-1;}

  }



  ##### Get League Info #####

  if ($team_id[0]=="S") {$sub_league_id=substr($team_id,1);$division_id="";$team_id="";}

  if ($team_id[0]=="T") {$team_id=substr($team_id,1);$sub_league_id="";$division_id="";}

  if ($team_id[0]=="D")

  {

    	  $sLoc=strpos($team_id,"S");

	  $sub_league_id=substr($team_id,$sLoc+1);

    	  $division_id=substr($team_id,1,$sLoc-1);

	  $team_id="";

  }



  mysqli_close($db);



  $scope=$_GET['scope'];

  if ($scope=="") {$scope="LastSim";}

  if ((($lgyear%400)==0) || ((($lgyear%100)!=0) && (($lgyear%4)==0)))

  {

	  $monthDays=Array("1"=>31,"2"=>29,"3"=>31,"4"=>30,"5"=>31,"6"=>30,"7"=>31,"8"=>31,"9"=>30,"10"=>31,"11"=>30,"12"=>31);

  }

  else

  {

	  $monthDays=Array("1"=>31,"2"=>28,"3"=>31,"4"=>30,"5"=>31,"6"=>30,"7"=>31,"8"=>31,"9"=>30,"10"=>31,"11"=>30,"12"=>31);

  }

  $frDay=$_GET['frDay'];

  if ($frDay=="") {$frDay="1/1";$minDay=1;}

  else

  {

	  $e=explode("/",$frDay);

       	  $mo=$e[0];

	  if ($mo[0]==0) {$mo=substr($mo,1);}

       	  if ($mo<1) {$mo=1;}

	  if ($mo>12) {$mo=12;}

       	  $dy=$e[1];

	  if ($dy[0]==0) {$dy=substr($dy,1);}

       	  if ($dy<1) {$dy=1;}

	  if ($dy>$monthDays[$mo]) {$dy=$monthDays[$mo];}

       	  $frDay=$mo."/".$dy;

	  unset($tmpdate);

       	  $tmpdate=new DateTime($frDay."/".$lgyear);

	  $minDay=$tmpdate->format("z");

  }

  $toDay=$_GET['toDay'];

  if ($toDay=="") {$toDay=$date->format("m/d");$maxDay=1;}

  else

  {

	  $e=explode("/",$toDay);

       	  $mo=$e[0];

	  if ($mo[0]==0) {$mo=substr($mo,1);}

       	  if ($mo<1) {$mo=1;}

	  if ($mo>12) {$mo=12;}

       	  $dy=$e[1];

	  if ($dy[0]==0) {$dy=substr($dy,1);}

       	  if ($dy<1) {$dy=1;}

	  if ($dy>$monthDays[$mo]) {$dy=$monthDays[$mo];}

       	  $toDay=$mo."/".$dy;

	  unset($tmpdate);

       	  $tmpdate=new DateTime($toDay."/".$lgyear);

	  $maxDay=$tmpdate->format("z")+1;

  }

  if ($minDay==$maxDay) {$minDay-=2;}

  if ($minDay>$maxDay)

  {

       	  $tmp=$frDay;

	  $frDay=$toDay;

       	  $toDay=$tmp;

	  $tmp=$minDay;

       	  $minDay=$maxDay;

	  $maxDay=$minDay;

  }

  

  # Title Bar

  $title="Last Sim Statistics";



  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 #########################

  ##### Get Sim Length #####	  

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



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



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

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

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

  echo "  <tr class='title'><td style='padding:2px;'>Team Results</td></tr>\n";

  echo "  <tr>\n";

  echo "   <td>\n";

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

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

  echo "     <tr>\n";



  echo "      <td>\n";

  echo "       <select name='team_id' id='team_id' onchange=\"if(this.value.substr(0,1)!='T') {document.getElementById('opp_id').selectedIndex=0;}\">\n";

  echo "        <option value='ALL'>All Teams</option>\n";

  $tmIdx=0;

  ## Show League Filter

  $query="SELECT sub_league_id,name,abbr FROM sub_leagues WHERE league_id=$lgid ORDER BY sub_league_id;";

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

  echo "        <optgroup label='Leagues'>\n";

  while ($row=mysqli_fetch_array($result))

  {

	  $tmIdx++;

    	  $slid=$row['sub_league_id'];

	  $subleagues[$slid]['abbr']=$row['abbr'];

    	  echo "        <option value='S$slid'";

	  if (($sub_league_id==$slid)&&($division_id=="")) {echo " selected";}

    	  echo ">".$row['name']."</option>\n";

  }

  ## Show Division Filter

  $query="SELECT division_id,name,sub_league_id FROM divisions WHERE league_id=$lgid ORDER BY sub_league_id,division_id;";

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

  echo "        <optgroup label='Divisions'>\n";

  while ($row=mysqli_fetch_array($result))

  {

	  $tmIdx++;

	  $divid=$row['division_id'];

    	  $slid=$row['sub_league_id'];

	  echo "        <option value='D".$divid."S$slid'";

    	  if (($division_id==$divid)&&($sub_league_id==$slid)) {echo " selected";}

	  echo ">".$subleagues[$slid]['abbr']." ".$row['name']."</option>\n";

  }

  ## Show Team Filter

  $query="SELECT t.team_id,abbr,name,nickname,t.sub_league_id,tr.division_id FROM teams as t,team_relations as tr WHERE t.team_id=tr.team_id AND t.league_id=$lgid AND allstar_team=0 ORDER BY name,nickname;";

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

  echo "        <optgroup label='Teams'>\n";

  while ($row=mysqli_fetch_array($result))

  {

	  $tmIdx++;

	  if ($lowTeam=="") {$lowTeam=$tmIdx;}

		  $tid=$row['team_id'];

	  if ($team_id==-1) {$team_id=$tid;}

    	  $slid=$row['sub_league_id'];

	  $divid=$row['division_id'];

    	  $teams[$tid]['slid']=$slid;

	  $teams[$tid]['name']=$row['name']." ".$row['nickname'];

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



	  $subleagues[$slid]['teams']=$subleagues[$slid]['teams'].",".$row['team_id'];

    	  $subleagues[$slid]['cnt']=$subleagues[$slid]['cnt']+1;



	  $IDDiv=$slid."^".$divid;

    	  $divisions[$IDDiv]['teams']=$divisions[$IDDiv]['teams'].",".$row['team_id'];

	  $divisions[$IDDiv]['cnt']=$divisions[$IDDiv]['cnt']+1;



	  echo "        <option value='T$tid'";

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

	  echo ">".$row['name']." ".$row['nickname']."</option>\n";

  }

  echo "       </select>\n";

  echo "      </td>\n";



  ## Show Who Filter

  echo "      <td>\n";

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

  $whoArray=array('batters'=>'Batters','pitchers'=>'Pitchers','both'=>'Both');

  foreach ($whoArray as $key => $val)

  {

    	  echo "        <option value='$key'";

	  if ($who==$key) {echo " selected";}

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

  }

  echo "       </select>\n";

  echo "      </td>\n";



  ## Scope Filter

  echo "      <td><b>Scope:</b></td>\n";

  echo "      <td><input type='radio' name='scope' value='LastSim' id='LastSim'";

  if ($scope=='LastSim') {echo " checked='checked'";}

  echo "></td><td valign=top><label for='LastSim'>Last Sim</label></td>\n";

  echo "      <td><input type='radio' name='scope' value='Season' id='Season'";

  if ($scope=='Season') {echo " checked='checked'";}

  echo "></td><td valign=top><label for='Season'>Season</label></td>\n";

  echo "        <td><input type='radio' name='scope' value='dRange' id='dRange'";

  if ($scope=='dRange') {echo " checked='checked'";}

  echo "></td>";

  echo "        <td valign=top><label for='dRange'>Date Range (MM/DD)</label></td>\n";

  echo "        <td><table><tr><td>From</td><td><INPUT type='text' size=5 maxlength=5 name='frDay' value='$frDay' onchange=\"document.getElementById('dRange').checked=true;\"></td><td>To</td><td><INPUT type='text' size=5 maxlength=5 name='toDay' value='$toDay' onchange=\"document.getElementById('dRange').checked=true;\"></td></tr></table></td>";



  $frDay=str_replace("/","-",$frDay);

  $toDay=str_replace("/","-",$toDay);



  echo "     </tr>\n";

  ## Show opponent filter

  echo "     <tr>\n";

  echo "    <td><label for='opp_id'>Show stats versus</label></td>\n";

  echo "     <td colspan=4>\n";

  echo "      <select name='opp_id' id='opp_id' onchange=\"if((this.selectedIndex!=0)&&(document.getElementById('team_id').value.substr(0,1)!='T')) {document.getElementById('team_id').selectedIndex=$lowTeam;}\">\n";

  echo "       <option value='All'>All Teams</option>\n";

  if (isset($teams))

  {

	  foreach ($teams as $tid => $val)

	  {

		  echo "       <option value='$tid'";

		  if ($oppTmID==$tid) {echo " selected";}

		  echo ">".$teams[$tid]['name']."</option>\n";

	  }

  }

  echo "      </select>\n";

  echo "     </td>\n";

  echo "     <td colspan=5>&nbsp;</td>\n";

  echo "      <td>\n";

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

  echo "      </td>\n";

  echo "     </tr>\n";

  ## Close Form

  echo "    </table>\n";

  echo "    </form>\n";

  echo "   <td>\n";

  echo "  </tr>\n";

  echo "  <tr>\n";

  echo "   <td>";



  ##### Get Team List #####

  if ($team_id=="ALL")

  {

	  foreach ($subleagues as $key => $val)

   	  {

		  $teamList=",".$subleagues[$key]['teams'];

   	  }

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

  }

  if ($sub_league_id!="") {$teamList=trim($subleagues[$sub_league_id]['teams'],",");}

  if ($division_id!="") 

  {

	  $IDDiv=$sub_league_id."^".$division_id;

	  $teamList=trim($divisions[$IDDiv]['teams'],",");

  }

  if (($team_id!="")&&($team_id!="ALL")) {$teamList=$team_id;}

       

  ##### Get Batter Stats #####

  if (($who=='batters')||($who=='both'))

  {

    	  $query="SELECT p.player_id,p.first_name,p.last_name,organization_id,COUNT(game_id) as g,sum(ab) as ab,sum(h) as h,sum(d) as d,sum(t) as t,sum(hr) as hr,sum(rbi) as rbi,sum(r) as r,sum(sb) as sb,sum(cs) as cs,sum(bb) as bb,sum(hp) as hbp,sum(sf) as sf,sum(gdp) as gidp,sum(k) as k FROM players_game_batting as pgb,players as p WHERE p.player_id=pgb.player_id AND pgb.team_id IN ($teamList) AND game_id IN (SELECT game_id FROM games WHERE league_id=$lgid";

	  if ($scope=="LastSim") {$query.=" AND played=1 AND DATEDIFF('".$lgdate."',games.date)<".$simLen;}

   	  elseif ($scope=="dRange") {$query.=" AND played=1 AND games.date>='$lgyear-$frDay' AND games.date<='$lgyear-$toDay'";}

	  else {$query.=" AND game_type=0";}

	  if ($oppTmID!='All') {$query.=" AND (home_team=$oppTmID OR away_team=$oppTmID)";}

	  $query.=" AND game_type!=4) GROUP BY player_id ORDER BY last_name,first_name;";

    	  if ($_GET['showq']==1) {echo $query;}

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

    	  echo " <div class='tablebox' style='width:915px;'>\n";

	  echo "  <table cellpadding=0 cellspacing=0 border=0><tr class='title'><td style='padding:2px;'>Batting Stats</td></tr><tr><td>\n";

    	  echo "   <table class='sortable' style='width:915px;'>\n";

	  echo "    <thead><tr class='headline'>\n";

    	  echo "     <td class='hsc2_l'>Player</td>";

	  if (($team_id=="")||($team_id=="ALL")) {echo "<td class='hsc2_l' title='Current organization'>Team</td>";}

    	  echo "<td class='hsc2'>G</td><td class='hsc2'>AB</td><td class='hsc2'>R</td><td class='hsc2'>H</td><td class='hsc2'>2B</td><td class='hsc2'>3B</td><td class='hsc2'>HR</td><td class='hsc2'>RBI</td><td class='hsc2'>BB</td><td class='hsc2'>K</td><td class='hsc2'>SB</td><td class='hsc2'>CS</td><td class='hsc2'>AVG</td><td class='hsc2'>OBP</td><td class='hsc2'>SLG</td><td class='hsc2'>OPS</td><td class='hsc2'>Logs</td>\n";

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

    	  $rownum=0;

	  while ($row=mysqli_fetch_array($result))

   	  {

		  $tid=$row['organization_id'];

	 	  $g=$row['g'];

		  $ab=$row['ab'];

	 	  $h=$row['h'];

		  $d=$row['d'];

	 	  $t=$row['t'];

		  $hr=$row['hr'];

	 	  $rbi=$row['rbi'];

		  $r=$row['r'];

	 	  $sb=$row['sb'];

		  $cs=$row['cs'];

	 	  $bb=$row['bb'];

		  $hbp=$row['hbp'];

	 	  $sf=$row['sf'];

	 $gidp=$row['gidp'];

   	 $k=$row['k'];

	 $Tg+=$g;

   	 $Tab+=$ab;

	 $Th+=$h;

   	 $Td+=$d;

	 $Tt+=$t;

   	 $Thr+=$hr;

	 $Trbi+=$rbi;

   	 $Tr+=$r;

	 $Tsb+=$sb;

   	 $Tcs+=$cs;

	 $Tbb+=$bb;

   	 $Thbp+=$hbp;

	 $Tsf+=$sf;

   	 $Tgidp+=$gidp;

	 $Tk+=$k;



	 $rcls=$rownum%2+1;

   	 echo "    <tr class='s".$rcls."'>";

	 echo "<td class='s".$rcls."_l' sorttable_customkey=\"".$row['last_name'].", ".$row['first_name']."\"><a href='./player.php?player_id=".$row['player_id']."'>".$row['first_name']." ".$row['last_name']."</a></td>";

         if (($team_id=="")||($team_id=="ALL")) {echo "<td class='s".$rcls."_l'><a href='$lgpath/teams/team_$tid.html'>".$teams[$tid]['abbr']."</a></td>";}

   	 echo "<td>".$g."</td>";

	 echo "<td>".$ab."</td>";

   	 echo "<td>".$r."</td>";

	 echo "<td>".$h."</td>";

   	 echo "<td>".$d."</td>";

	 echo "<td>".$t."</td>";

   	 echo "<td>".$hr."</td>";

	 echo "<td>".$rbi."</td>";

   	 echo "<td>".$bb."</td>";

	 echo "<td>".$k."</td>";

   	 echo "<td>".$sb."</td>";

	 echo "<td>".$cs."</td>";

   	 if ($ab==0)

	  {

	    $avg=round(0,3);

	    $slg=round(0,3);

   	  }

 	  else

  	  {

            $avg=$h/$ab;

	    $slg=($h+$d+2*$t+3*$hr)/$ab;

   	  }

 	 if (($ab+$bb+$hbp+$sf)==0) {$obp=0;}

  	  else {$obp=($h+$bb+$hbp)/($ab+$bb+$hbp+$sf);}

         $ops=$obp+$slg;

   	 if ($avg<1) {$avg=strstr(sprintf("%.3f",$avg),".");}

          else {$avg=sprintf("%.3f",$avg);}

         if ($obp<1) {$obp=strstr(sprintf("%.3f",$obp),".");}

          else {$obp=sprintf("%.3f",$obp);}

         if ($slg<1) {$slg=strstr(sprintf("%.3f",$slg),".");}

          else {$slg=sprintf("%.3f",$slg);}

         if ($ops<1) {$ops=strstr(sprintf("%.3f",$ops),".");}

          else {$ops=sprintf("%.3f",$ops);}

         echo "<td>".$avg."</td>";

	 echo "<td>".$obp."</td>";

   	 echo "<td>".$slg."</td>";

	 echo "<td>".$ops."</td>";

   	 echo "<td><a href='./playerStats.php?player_id=".$row['player_id']."&scope=$scope";

         if ($scope=='dRange') {echo "&frDay=".str_replace("-","/",$frDay)."&toDay=".str_replace("-","/",$toDay);}

		   if ($oppTmID!='All') {echo "&opp_id=$oppTmID";}

         echo "'>Log</a></td>";

	 echo "</tr>\n";

   	 $rownum++;

       }

    

      ## Display Team Totals

      echo "    <tfoot><tr class='headline'>";

      echo "<td class='hsc2_l'";

      if (($team_id=="")||($team_id=="ALL")) {echo " colspan=2";}

      echo ">Totals</td>";

      echo "<td class='hsc2'>".$Tg."</td>";

      echo "<td class='hsc2'>".$Tab."</td>";

      echo "<td class='hsc2'>".$Tr."</td>";

      echo "<td class='hsc2'>".$Th."</td>";

      echo "<td class='hsc2'>".$Td."</td>";

      echo "<td class='hsc2'>".$Tt."</td>";

      echo "<td class='hsc2'>".$Thr."</td>";

      echo "<td class='hsc2'>".$Trbi."</td>";

      echo "<td class='hsc2'>".$Tbb."</td>";

      echo "<td class='hsc2'>".$Tk."</td>";

      echo "<td class='hsc2'>".$Tsb."</td>";

      echo "<td class='hsc2'>".$Tcs."</td>";

      if ($Tab==0)

       {

         $avg=round(0,3);

	 $slg=round(0,3);

       }

       else

       {

         $avg=$Th/$Tab;

	 $slg=($Th+$Td+2*$Tt+3*$Thr)/$Tab;

       }

      $pa=$Tab+$Tbb+$Thbp+$Tsf;

      if ($pa==0) {$obp=0;}

       else {$obp=($Th+$Tbb+$Thbp)/$pa;}

      $ops=$obp+$slg;

      if ($avg<1) {$avg=strstr(sprintf("%.3f",$avg),".");}

       else {$avg=sprintf("%.3f",$avg);}

      if ($obp<1) {$obp=strstr(sprintf("%.3f",$obp),".");}

       else {$obp=sprintf("%.3f",$obp);}

      if ($slg<1) {$slg=strstr(sprintf("%.3f",$slg),".");}

       else {$slg=sprintf("%.3f",$slg);}

      if ($ops<1) {$ops=strstr(sprintf("%.3f",$ops),".");}

       else {$ops=sprintf("%.3f",$ops);}

      echo "<td class='hsc2'>".$avg."</td>";

      echo "<td class='hsc2'>".$obp."</td>";

      echo "<td class='hsc2'>".$slg."</td>";

      echo "<td class='hsc2'>".$ops."</td>";

      echo "<td class='hsc2'>&nbsp;</td>";

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



      echo "   </table>\n";

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

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



      $Tg=0;

      $Tr=0;

      $Tk=0;

      $Tab=0;

      $Tsf=0;

    }



   if ($who=='both')

    {

      echo "   </td>\n";

      echo "  </tr>\n";

      echo "  <tr>\n";

      echo "   <td>\n";

    }

  

   ##### Get Pitcher Stats #####

   if (($who=='pitchers')||($who=='both'))

    {

      $query="SELECT p.player_id,p.first_name,p.last_name,organization_id,sum(g) as g,sum(gs) as gs,((sum(IP)*3+sum(ipf))/3) as ip,sum(w) as w,sum(l) as l,sum(s) as sv,sum(r) as r,sum(er) as er,sum(k) as k,sum(ha) as ha,sum(bb) as bba,sum(hra) as hra,sum(cg) as cg,sum(sho) as sho,sum(ab) as ab,sum(sf) as sf FROM players_game_pitching_stats as pgp,players as p WHERE p.player_id=pgp.player_id AND pgp.team_id IN ($teamList) AND game_id IN (SELECT game_id FROM games WHERE league_id=$lgid";

      if ($scope=="LastSim") {$query.=" AND played=1 AND DATEDIFF('".$lgdate."',games.date)<".$simLen;}

       elseif ($scope=="dRange") {$query.=" AND played=1 AND games.date>='$lgyear-$frDay' AND games.date<='$lgyear-$toDay'";}

       else {$query.=" AND game_type=0";}

  	  if ($oppTmID!='All') {$query.=" AND (home_team=$oppTmID OR away_team=$oppTmID)";}

      $query.=" AND game_type!=4) GROUP BY player_id ORDER BY last_name,first_name;";

      if ($_GET['showq']==1) {echo $query;}

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

      echo " <div class='tablebox' style='width:915px;'>\n";

      echo "  <table cellpadding=0 cellspacing=0 border=0><tr class='title'><td style='padding:2px;'>Pitching Stats</td></tr><tr><td>\n";

      echo "   <table class='sortable' style='width:915px;'>\n";

      echo "    <thead><tr class='headline'>\n";

      echo "     <td class='hsc2_l'>Player</td>";

      if (($team_id=="")||($team_id=="ALL")) {echo "<td class='hsc2_l' title='Current organization'>Team</td>";}

      echo "<td class='hsc2'>W</td><td class='hsc2'>L</td><td class='hsc2'>SV</td><td class='hsc2'>CG</td><td class='hsc2'>SHO</td><td class='hsc2'>ERA</td><td class='hsc2'>G</td><td class='hsc2'>GS</td><td class='hsc2'>IP</td><td class='hsc2'>HA</td><td class='hsc2'>R</td><td class='hsc2'>ER</td><td class='hsc2'>HR</td><td class='hsc2'>BB</td><td class='hsc2'>K</td><td class='hsc2'>WHIP</td><td class='hsc2'>OAVG</td><td class='hsc2'>BABIP</td><td class='hsc2'>Logs</td>\n";

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

      $rownum=0;

      while ($row=mysqli_fetch_array($result))

       {

         $rcls=$rownum%2+1;

      

         $tid=$row['organization_id'];

	 $g=$row['g'];

   	 $gs=$row['gs'];

	 $ip=$row['ip'];

   	 $w=$row['w'];

	 $l=$row['l'];

   	 $sv=$row['sv'];

	 $r=$row['r'];

   	 $er=$row['er'];

	 $k=$row['k'];

   	 $ha=$row['ha'];

	 $bba=$row['bba'];

   	 $hra=$row['hra'];

	 $cg=$row['cg'];

   	 $sho=$row['sho'];

	 $ab=$row['ab'];

   	 $sf=$row['sf'];

	 $Tg+=$g;

   	 $Tgs+=$gs;

	 $Tip+=$ip;

   	 $Tw+=$w;

	 $Tl+=$l;

   	 $Tsv+=$sv;

	 $Tr+=$r;

   	 $Ter+=$er;

	 $Tk+=$k;

   	 $Tha+=$ha;

	 $Tbba+=$bba;

   	 $Thra+=$hra;

	 $Tcg+=$cg;

   	 $Tsho+=$sho;

	 $Tab+=$ab;

   	 $Tsf+=$sf;



	 echo "    <tr class='s".$rcls."'>";

   	 echo "<td class='s".$rcls."_l' sorttable_customkey=\"".$row['last_name'].", ".$row['first_name']."\"><a href='./player.php?player_id=".$row['player_id']."'>".$row['first_name']." ".$row['last_name']."</a></td>";

         if (($team_id=="")||($team_id=="ALL")) {echo "<td class='s".$rcls."_l'><a href='$lgpath/teams/team_$tid.html'>".$teams[$tid]['abbr']."</a></td>";}

	 echo "<td>".$w."</td>";

   	 echo "<td>".$l."</td>";

	 echo "<td>".$sv."</td>";

   	 echo "<td>".$cg."</td>";

	 echo "<td>".$sho."</td>";

   	 if ($ip==0) {$era=0;$whip=0;}

	  else 

  	  {

            $era=$er*9/$ip;

	    $whip=($ha+$bba)/$ip;

   	  }

	 $era=sprintf("%.2f",$era);

   	 echo "<td>".$era."</td>";

	 echo "<td>".$g."</td>";

   	 echo "<td>".$gs."</td>";

	 $ip=sprintf("%.1f",$ip);

   	 if ($ip<1) {$ip=strstr($ip,".");}

	 echo "<td>".$ip."</td>";

   	 echo "<td>".$ha."</td>";

	 echo "<td>".$r."</td>";

   	 echo "<td>".$er."</td>";

	 echo "<td>".$hra."</td>";

   	 echo "<td>".$bba."</td>";

	 echo "<td>".$k."</td>";

   	 $whip=sprintf("%.2f",$whip);

	 if ($whip<1) {$whip=strstr($whip,".");}

   	 echo "<td>".$whip."</td>";

	 if ($ab==0) {$oavg=0;}

  	  else {$oavg=$ha/$ab;}

         if ($oavg<1) {$oavg=strstr(sprintf("%.3f",$oavg),".");}

          else {$oavg=sprintf("%.3f",$oavg);}

         echo "<td>".$oavg."</td>";

         $bip=$ab-$k-$hra+$sf;

         if ($bip==0) {$babip=0;}

          else {$babip=($ha-$hra)/$bip;}

         if ($babip<1) {$babip=strstr(sprintf("%.3f",$babip),".");}

          else {$babip=sprintf("%.3f",$babip);}

         echo "<td>".$babip."</td>";

         echo "<td><a href='./playerStats.php?player_id=".$row['player_id']."&scope=$scope";

         if ($scope=='dRange') {echo "&frDay=".str_replace("-","/",$frDay)."&toDay=".str_replace("-","/",$toDay);}

		   if ($oppTmID!='All') {echo "&opp_id=$oppTmID";}

	 echo "'>Log</a></td>";

   	 echo "</tr>\n";

	 $rownum++;

       }

    

      ## Display Team Totals

      echo "    <tfoot><tr class='headline'>";

      echo "<td class='hsc2_l'";

      if (($team_id=="")||($team_id=="ALL")) {echo " colspan=2";}

      echo ">Totals</td>";

      echo "<td class='hsc2'>".$Tw."</td>";

      echo "<td class='hsc2'>".$Tl."</td>";

      echo "<td class='hsc2'>".$Tsv."</td>";

      echo "<td class='hsc2'>".$Tcg."</td>";

      echo "<td class='hsc2'>".$Tsho."</td>";

      if ($Tip==0) {$era=0;$whip=0;}

       else 

       {

         $era=$Ter*9/$Tip;

	 $whip=($Tha+$Tbba)/$Tip;

       }

      $era=sprintf("%.2f",$era);

      echo "<td class='hsc2'>".$era."</td>";

      echo "<td class='hsc2'>".$Tg."</td>";

      echo "<td class='hsc2'>".$Tgs."</td>";

      $ip=sprintf("%.1f",$Tip);

      if ($ip<1) {$ip=strstr($ip,".");}

      echo "<td class='hsc2'>".$ip."</td>";

      echo "<td class='hsc2'>".$Tha."</td>";

      echo "<td class='hsc2'>".$Tr."</td>";

      echo "<td class='hsc2'>".$Ter."</td>";

      echo "<td class='hsc2'>".$Thra."</td>";

      echo "<td class='hsc2'>".$Tbba."</td>";

      echo "<td class='hsc2'>".$Tk."</td>";

      $whip=sprintf("%.2f",$whip);

      if ($whip<1) {$whip=strstr($whip,".");}

      echo "<td class='hsc2'>".$whip."</td>";

      if ($Tab==0) {$oavg=0;}

       else {$oavg=$Tha/$Tab;}

      if ($oavg<1) {$oavg=strstr(sprintf("%.3f",$oavg),".");}

       else {$oavg=sprintf("%.3f",$oavg);}

      echo "<td class='hsc2'>".$oavg."</td>";

      $bip=$Tab-$Tk-$Thra+$Tsf;

      if ($bip==0) {$babip=0;}

       else {$babip=($Tha-$Thra)/$bip;}

      if ($babip<1) {$babip=strstr(sprintf("%.3f",$babip),".");}

       else {$babip=sprintf("%.3f",$babip);}

      echo "<td class='hsc2'>".$babip."</td>";

      echo "<td class='hsc2'>&nbsp;</td>";

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

   

      echo "   </table>\n";

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

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

    }

 

   echo "   </td>\n";

   echo "  </tr>\n";

   echo " </table>\n";

     

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

   

  mysqli_close($db);



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

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

  page_foot($_SESSION['lgpath']);





?>

