<?php
  include("html_functions.php");

  ################## PAGE INITIALIZATION #########################
  $lgid=$_SESSION['lgid'];
  $tmID=$_GET['team_id'];
  if ((!isset($_GET['team_id']))&&($_SESSION['user_league_id']==$lgid)) {$tmID=$_SESSION['user_team_id'];}
  $page=$_GET['page'];
  $who=$_GET['who'];
  $view=$_GET['view'];
  $sortMeth=$_GET['sortMeth'];
  if ($sortMeth=="") {$sortMeth="F";}
  if ($sortMeth=="F") {$nextSort="R";} else {$nextSort="F";}
  $showMinors=$_GET['showMinors'];
	
  $pageLimit=200;
  if ($who=="") {$who='batting';}
  if ($view=="") {$view='overall';}
  if ($page=="") {$page=0;}
  if ($_GET['sort1']=="") {$_GET['sort1']='contact';}
  if ($_GET['sort2']=="") {$_GET['sort2']='power';}
  if ($_GET['sort3']=="") {$_GET['sort3']='eye';}


  include($_SESSION['adminpath']."/dbopen.php");
  # Get Team
  if ((isset($_GET['team_id']))&&($_GET['team_id']!=0))
  {
	  $query="SELECT team_id,name,nickname,abbr,level FROM teams WHERE team_id=$tmID OR parent_team_id=$tmID";
	  if ($_GET['showq']==1) {echo "$query<br/>\n";}
	  $result=mysqli_query($db,$query);
	  while ($row=mysqli_fetch_array($result))
	  {
		  $tid=$row['team_id'];
		  $teams[$tid]['abbr']=$row['abbr'];
		  $teams[$tid]['name']=$row['name']." ".$row['nickname'];
		  $teams[$tid]['level_id']=$row['level'];
	  }
  }
  # Get Ratings Config
  $query="SELECT setting,value FROM ootp_sql_admin WHERE scope='ALL' AND setting IN ('Ratings','Talents','Others','scoutsOn','rawRatings')";
  $result=mysqli_query($db,$query);
  if (mysqli_num_rows($result))
  {
	  while ($row=mysqli_fetch_array($result))
	  {
		  $setting=$row['setting'];
		  $settings[$setting]=$row['value'];
	  }
  }
  mysqli_close($db);
  if (!isset($settings['rawRatings'])) {$settings['rawRatings']=0;}


  # Title bar
  if (isset($teams[$tmID]['name'])) {$title=$teams[$tmID]['name']." ";} else {$title=" ";}
  if (($tmID==0)&&(isset($_GET['team_id']))) {$title="Free Agent ";}
  $title.='Ratings';

  page_head($title,$_SESSION['lgpath'],0,"","./filterSorts.js");
  echo " <div id='topbar'>\n";
  main_titlebar($lgid,$_SESSION['lgpath'],$title);
  nav_toolbar('front_office',"Front Office");
  echo " </div>   <!-- END topbar DIV -->\n";
  echo " <div id='contentpane'>\n";

  ################## START OF PAGE CONTENT #########################
  include($_SESSION['adminpath']."/dbopen.php");

  # Display Filters
  $whoArray=Array('batting'=>'Batters','pitching'=>'Pitchers','2'=>'C','3'=>'1B','4'=>'2B','5'=>'3B','6'=>'SS','7'=>'LF','8'=>'CF','9'=>'RF','11'=>'SP','12'=>'RP','13'=>'CL');
  $viewArray=Array('overall'=>'Overall','vsl'=>'Vs Left','vsr'=>'Vs Right','talent'=>'Potential');
  $sortArray=Array('last_name,first_name'=>'Name','age'=>'Age','position'=>'Position','Batters','contact'=>'Contact','gap'=>'Gap','power'=>'Power','eye'=>'Eye','strikeouts'=>'Strikeouts','Pitchers','stuff'=>'Stuff','movement'=>'Movement','control'=>'Control','pitching_ratings_misc_velocity'=>'Velocity','pitching_ratings_misc_stamina'=>'Stamina');

  echo "<div class='textbox' style='width:750px;'>   <!-- open filters table -->\n";
  echo " <form action='./ratings.php' method='GET'>\n";
  echo "  <input type='hidden' value='$nextSort' name='sortMeth' id='sortMeth' />\n";
  echo "  <table border=0 cellpadding=4 cellspacing=0 style='width:100%;margin-left:auto;margin-right:auto;'>   <!-- open filters table -->\n";
  echo "  <tr class='title2'><td colspan=7 style='text-align:left;'>Ratings Filters</td></tr>\n";
  echo "   <tr>\n";
  echo "    <td>Team:</td>\n";
  echo "    <td colspan=3>\n";
  echo "     <select name='team_id' style='float:left;' onchange=\"document.getElementById('sortMeth').value='F';\" >\n";
  echo "      <option value='0'>Free Agents</option>\n";
  $query="SELECT team_id,t.league_id,t.name,nickname,lg.name as lgname FROM teams as t,leagues as lg WHERE t.league_id=lg.league_id AND allstar_team=0 ORDER BY league_id,name,nickname";
  if ($_GET['showq']==1) {echo "$query<br/>\n";}
  $result=mysqli_query($db,$query);
  $lstLg=-1;
  while ($row=mysqli_fetch_array($result))
  {
	  $tid=$row['team_id'];
	  $league_id=$row['league_id'];
	  if ($league_id!=$lstLg) {echo "      <optgroup label='".$row['lgname']."'>\n";}
	  echo "      <option value='$tid'";
	  if ($tid==$tmID) {echo " selected='selected'";}
	  echo ">".$row['name']." ".$row['nickname']."</option>\n";
	  $lstLg=$league_id;
  }
  echo "     </select>\n";
  echo "    </td>\n";
  echo "    <td>Who:</td>\n";
  echo "    <td>\n";
  echo "     <select id='whoFilt' name='who' onchange=\"whoChange(); document.getElementById('sortMeth').value='F';\">\n";
  foreach ($whoArray as $key => $lbl)
  {
	  echo "      <option value='$key'";
	  if ($key==$who) {echo " selected='selected'";}
	  echo ">$lbl</option>\n";
  }
  echo "     </select>\n";
  echo "    </td>\n";
  echo "    <td>\n";
  echo "     <select name='view' onchange=\"document.getElementById('sortMeth').value='F';\" >\n";
  foreach ($viewArray as $key => $lbl)
  {
	  echo "      <option value='$key'";
	  if ($key==$view) {echo " selected='selected'";}
	  echo ">$lbl</option>\n";
  }
  echo "     </select>\n";
  echo "    </td>\n";
  echo "   </tr>\n";
  echo "   <tr>\n";
  echo "    <td>Sort By:</td>\n";
  echo "    <td>\n";
  echo "     <select id='sort1filter' name='sort1' onchange=\"document.getElementById('sortMeth').value='F';\" >\n";
  foreach ($sortArray as $key => $lbl)
  {
	  if (is_numeric($key)) {echo "      <optgroup label='$lbl'>\n";continue;}
	  echo "      <option value='$key'";
	  if ($key==$_GET['sort1']) {echo " selected='selected'";}
	  echo ">$lbl</option>\n";
  }
  echo "     </select>\n";
  echo "    </td>\n";
  echo "    <td>then by:</td>\n";
  echo "    <td>\n";
  echo "     <select id='sort2filter' name='sort2' onchange=\"document.getElementById('sortMeth').value='F';\" >\n";
  foreach ($sortArray as $key => $lbl)
  {
	  if (is_numeric($key)) {echo "      <optgroup label='$lbl'>\n";continue;}
	  echo "      <option value='$key'";
	  if ($key==$_GET['sort2']) {echo " selected='selected'";}
	  echo ">$lbl</option>\n";
  }
  echo "     </select>\n";
  echo "    </td>\n";
  echo "    <td>then by:</td>\n";
  echo "    <td>\n";
  echo "     <select id='sort3filter' name='sort3' onchange=\"document.getElementById('sortMeth').value='F';\" >\n";
  foreach ($sortArray as $key => $lbl)
  {
	  if (is_numeric($key)) {echo "      <optgroup label='$lbl'>\n";continue;}
	  echo "      <option value='$key'";
	  if ($key==$_GET['sort3']) {echo " selected='selected'";}
	  echo ">$lbl</option>\n";
  }
  echo "     </select>\n";
  echo "    </td>\n";
  echo "    <td>&nbsp;</td>\n";
  echo "   </tr>\n";
  echo "   <tr>\n";
  echo "    <td><input type='checkbox' id='showMinors' name='showMinors' value=1";
  if ($showMinors==1) {echo " checked='checked'";}
  echo " onchange=\"document.getElementById('sortMeth').value='F';\" /></td>\n";
  echo "    <td colspan=5><label for='showMinors'>exclude minor league players</label></td>\n";
  echo "    <td><input type='submit' class='submitButton' value='Go' /></td>\n";
  echo "   </tr>\n";
  echo "  </table>   <!-- close filters table -->\n";
  echo " </form>\n";
  echo "</div>   <!-- close filters table -->\n";


  # Display Results
  switch ($who)
  {
  case 'batting':
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
  case 7:
  case 8:
  case 9:
	  $whoTable='batting';
	  $ratArray=Array('batting_ratings_'.$view.'_contact','batting_ratings_'.$view.'_gap','batting_ratings_'.$view.'_power','batting_ratings_'.$view.'_eye','batting_ratings_'.$view.'_strikeouts','running_ratings_speed','running_ratings_stealing','running_ratings_baserunning');
      if ($view=='talent')
      {
          $fldArray=Array('fielding_rating_pos2_pot','fielding_rating_pos3_pot','fielding_rating_pos4_pot','fielding_rating_pos5_pot','fielding_rating_pos6_pot','fielding_rating_pos7_pot','fielding_rating_pos8_pot','fielding_rating_pos9_pot','fielding_ratings_catcher_arm','fielding_ratings_infield_arm','fielding_ratings_outfield_arm');
      }
      else
      {
          $fldArray=Array('fielding_rating_pos2','fielding_rating_pos3','fielding_rating_pos4','fielding_rating_pos5','fielding_rating_pos6','fielding_rating_pos7','fielding_rating_pos8','fielding_rating_pos9','fielding_ratings_catcher_arm','fielding_ratings_infield_arm','fielding_ratings_outfield_arm');
      }
	  $sort1='batting_ratings_'.$view.'_contact';
	  $sort2='batting_ratings_'.$view.'_power';
	  $sort3='batting_ratings_'.$view.'_eye';
	  break;
  case 'pitching':
  case 11:
  case 12:
  case 13:
	  $whoTable='pitching';
	  if ($view=='talent') {$pview='_talent';} else {$pview="";}
	  $ratArray=Array('pitching_ratings_'.$view.'_stuff','pitching_ratings_'.$view.'_movement','pitching_ratings_'.$view.'_control','pitching_ratings_misc_velocity','pitching_ratings_misc_stamina','pitching_ratings_pitches'.$pview.'_fastball','pitching_ratings_pitches'.$pview.'_slider','pitching_ratings_pitches'.$pview.'_curveball','pitching_ratings_pitches'.$pview.'_screwball','pitching_ratings_pitches'.$pview.'_forkball','pitching_ratings_pitches'.$pview.'_changeup','pitching_ratings_pitches'.$pview.'_sinker','pitching_ratings_pitches'.$pview.'_splitter','pitching_ratings_pitches'.$pview.'_knuckleball','pitching_ratings_pitches'.$pview.'_cutter','pitching_ratings_pitches'.$pview.'_circlechange','pitching_ratings_pitches'.$pview.'_knucklecurve','pitching_ratings_misc_ground_fly');
	  $sort1='pitching_ratings_'.$view.'_stuff';
      $sort2='pitching_ratings_'.$view.'_movement';
	  $sort3='pitching_ratings_'.$view.'_control';
	  break;
  }
  if (isset($_GET['sort1']))
  {
	  switch ($_GET['sort1'])
	  {
	  case 'contact':
	  case 'gap':
	  case 'power':
	  case 'eye':
	  case 'strikeouts':
		  $sort1='batting_ratings_'.$view."_".$_GET['sort1'];
		  break;
	  case 'stuff':
	  case 'movement':
      case 'control':
		  $sort1='pitching_ratings_'.$view."_".$_GET['sort1'];
		  break;
	  default:
		  $sort1=$_GET['sort1'];
		  break;
	  }
  }
  if (isset($_GET['sort2']))
  {
	  switch ($_GET['sort2'])
	  {
	  case 'contact':
	  case 'gap':
	  case 'power':
	  case 'eye':
	  case 'strikeouts':
		  $sort2='batting_ratings_'.$view."_".$_GET['sort2'];
		  break;
	  case 'stuff':
	  case 'movement':
      case 'control':
		  $sort2='pitching_ratings_'.$view."_".$_GET['sort2'];
		  break;
	  default:
		  $sort2=$_GET['sort2'];
		  break;
	  }
  }
  if (isset($_GET['sort3']))
  {
	  switch ($_GET['sort3'])
	  {
	  case 'contact':
	  case 'gap':
	  case 'power':
	  case 'eye':
	  case 'strikeouts':
		  $sort3='batting_ratings_'.$view."_".$_GET['sort3'];
		  break;
	  case 'stuff':
	  case 'movement':
      case 'control':
		  $sort3='pitching_ratings_'.$view."_".$_GET['sort3'];
		  break;
	  default:
		  $sort3=$_GET['sort3'];
		  break;
	  }
  }

  # Close page if needed
  if (($tmID!=0)||(isset($_GET['team_id']))) 
  {
	  # Get Total Count
	  if (($who=='batting')||($who=='pitching'))
	  {
		  $cntQuery="SELECT COUNT(DISTINCT player_id) as cnt FROM players WHERE organization_id=$tmID AND retired=0 AND hidden=0";
		  if ($who=='pitching') {$cntQuery.=" AND position=1";} else {$cntQuery.=" AND position!=1";}
	  }
	  elseif ($who>10)
	  {
		  $cntQuery="SELECT COUNT(DISTINCT player_id) as cnt FROM players WHERE organization_id=$tmID AND retired=0 AND hidden=0 AND role=$who";
	  }
	  else
	  {
		  $whoQuery='fielding_rating_pos'.$who;
		  $cntQuery="SELECT COUNT(DISTINCT p.player_id) as cnt FROM players as p,players_fielding";
		  if ($settings['scoutsOn']==1) {$cntQuery.="_scouted";}
		  $cntQuery.=" as pf WHERE p.player_id=pf.player_id AND organization_id=$tmID AND retired=0 AND hidden=0 AND $whoQuery>0";
	  }
	  if ($_GET['showq']==1) {echo "$cntQuery<br/>\n";}
	  $result=mysqli_query($db,$cntQuery);
	  $row=mysqli_fetch_array($result);
	  $totCnt=$row['cnt'];
	  $pgCnt=ceil($totCnt/$pageLimit);

	  # Get roster status for players
	  if ($tmID>0)
	  {
		  $query="SELECT player_id,ps.league_id,is_active,is_on_dl,is_on_dl60,is_on_waivers,designated_for_assignment,is_on_secondary,parent_team_id FROM players_roster_status as ps,teams as t WHERE ps.team_id=t.team_id AND (t.team_id=$tmID OR parent_team_id=$tmID) AND (is_active=0 OR is_on_dl=1 OR is_on_dl60=1 OR is_on_waivers=1 OR designated_for_assignment=1 OR is_on_secondary=1)";
		  if ($_GET['showq']==1) {echo "$query<br/>\n";}
		  $result=mysqli_query($db,$query);
		  while ($row=mysqli_fetch_array($result))
		  {
			  $pid=$row['player_id'];
			  $players[$pid]['status']="";
			  if ($row['is_on_secondary']==1) {$players[$pid]['status'].=" <span title='On Secondary Roster'>~</span>";}
			  if ($row['is_on_waivers']==1) {$players[$pid]['status'].=" <span title='On Waivers'>WV</span>";}
			  if ($row['designated_for_assignment']==1) {$players[$pid]['status'].=" <span title='Designated For Assignment'>DFA</span>";}
			  if ($row['is_on_dl60']==1) {$players[$pid]['status'].=" <span style='color:#990000;' title='60-day DL'>DL</span>";}
			  elseif ($row['is_on_dl']==1) {$players[$pid]['status'].=" <span style='color:#990000;' title='On DL'>DL</span>";}
              elseif (($row['league_id']<0)&&($row['is_active']==0)&&($row['is_on_waivers']==0)&&($row['designated_for_assignment']==0)&&($row['parent_team_id']==0))
              {
                  $players[$pid]['status'].=" <span title='International Complex'>INT</span>";
                  $players[$pid]['int']=1;
              }
              elseif (($row['is_active']==0)&&($row['is_on_waivers']==0)&&($row['designated_for_assignment']==0)&&($row['parent_team_id']==0))
			  {
				  $players[$pid]['status'].=" <span title='On Reserve Roster'>RSV</span>";
				  $players[$pid]['rsv']=1;
			  }
		  }
	  }
	  
	  # Generate Query
	  $query="";
	  if (isset($fldArray))
	  {
		  $query="SELECT x.*";
		  foreach ($fldArray as $key => $ratLbl)
		  {
			  # Get scale
              if ($settings['rawRatings']==1)
              {
                  $ratScale=get_scale($ratLbl);
                  $scale=$settings[$ratScale];
              }
              else {$scale='Raw';}
	
			  # Add to query
			  $query.=",";
			  $query.=ratings_query($ratLbl,$scale);
			  $query.=" as $ratLbl";
		  }
		  $query.=" FROM players_fielding";
		  if ($settings['scoutsOn']==1) {$query.="_scouted";}
		  $query.=" as pf,(";
	  }
	  $query.="SELECT * FROM (";
	  $query.="SELECT p.player_id,first_name,last_name,p.team_id,age,p.position";
	  if (($who=='pitching')||($who>10)) {$query.=",p.role";}
	  foreach ($ratArray as $key => $ratLbl)
	  {
		  # Get scale
          if ($settings['rawRatings']==1)
          {
              $ratScale=get_scale($ratLbl);
              $scale=$settings[$ratScale];
          }
          else {$scale='Raw';}

		  # Add to query
		  $query.=",";
		  if (($settings['scoutsOn']==1)&&(($ratLbl=='running_ratings_speed')||($ratLbl=='running_ratings_stealing')||($ratLbl=='running_ratings_baserunning'))) {$tbAbbr='pr.';}
		  else {$tbAbbr="";}
		  $query.=ratings_query($ratLbl,$scale,$tbAbbr);
		  $query.=" as $ratLbl";
	  }
	  $query.=" FROM players as p,players_$whoTable";
	  if ($settings['scoutsOn']==1) {$query.="_scouted";}
	  $query.=" as pr WHERE p.player_id=pr.player_id AND (";
	  if ($showMinors!=1) {$query.="p.organization_id=$tmID OR ";}
	  $query.="p.team_id=$tmID) AND retired=0 AND hidden=0";
	  if ($who=='pitching') {$query.=" AND p.position=1";}
	  elseif ((is_numeric($who))&&($who>10)) {$query.=" AND role=$who";}
	  else {$query.=" AND p.position!=1";}
	  $query.=") as z";
	  $query.=" ORDER BY ";
	  if ($sort1!='last_name,first_name') 
	  {
		  $query.="CAST($sort1 AS SIGNED)";
		  if ($sortMeth=="F") {$query.=" DESC";}
	  }
	  else
	  {
		  $query.=$sort1;
		  if ($sortMeth!="F") {$query.=" DESC";}
	  }
	  if ($sort2!='last_name,first_name') 
	  {
		  $query.=",CAST($sort2 AS SIGNED)";
		  if ($sortMeth=="F") {$query.=" DESC";}
	  }
	  else
	  {
		  $query.=",".$sort2;
		  if ($sortMeth!="F") {$query.=" DESC";}
	  }
	  if ($sort3!='last_name,first_name') 
	  {
		  $query.=",CAST($sort3 AS SIGNED)";
		  if ($sortMeth=="F") {$query.=" DESC";}
	  }
	  else
	  {
		  $query.=",".$sort3;
		  if ($sortMeth!="F") {$query.=" DESC";}
	  }
	  if (($sort1!="last_name,first_name")&&($sort2!="last_name,first_name")&&($sort3!="last_name,first_name")) {$query.=",last_name,first_name";}
	  if ((!is_numeric($who))||((is_numeric($who))&&($who>10))) {$query.=" LIMIT ".($page*$pageLimit).",$pageLimit";}
	  if (isset($fldArray))
	  {
		  $query.=") as x WHERE pf.player_id=x.player_id";
		  if ((is_numeric($who))&&($who<10)) {$query.=" AND fielding_rating_pos".$who.">0";}
		  $query.=" ORDER BY ";
		  if ($sort1!='last_name,first_name') 
		  {
			  $query.="CAST($sort1 AS SIGNED)";
			  if ($sortMeth=="F") {$query.=" DESC";}
		  }
		  else
		  {
			  $query.=$sort1;
			  if ($sortMeth=="R") {$query.=" DESC";}
		  }
		  if ($sort2!='last_name,first_name') 
		  {
			  $query.=",CAST($sort2 AS SIGNED)";
			  if ($sortMeth=="F") {$query.=" DESC";}
		  }
		  else
		  {
			  $query.=",".$sort2;
			  if ($sortMeth=="R") {$query.=" DESC";}
		  }
		  if ($sort3!='last_name,first_name') 
		  {
			  $query.=",CAST($sort3 AS SIGNED)";
			  if ($sortMeth=="F") {$query.=" DESC";}
		  }
		  else
		  {
			  $query.=",".$sort3;
			  if ($sortMeth=="R") {$query.=" DESC";}
		  }
		  if (($sort1!="last_name,first_name")&&($sort2!="last_name,first_name")&&($sort3!="last_name,first_name")) {$query.=",last_name,first_name";}
	  }
	  if ((is_numeric($who))&&($who<10)) {$query.=" LIMIT ".($page*$pageLimit).",$pageLimit";}
	  if ($_GET['showq']==1) {echo "$query<br/>\n";}
	  $result=mysqli_query($db,$query);

	  # Display Results
	  echo "<div class='textbox' style='width:100%;'>   <!-- open ratings table -->\n";
	  echo " <table border=0 cellpadding=4 cellspacing=0 width='100%' style='margin-left:auto;margin-right:auto;'>   <!-- open ratings table -->\n";
	  echo "  <tr class='title2'><td style='text-align:left;'>";
	  if (is_numeric($who)) {echo get_pos($who);}
	  else {echo ucwords($who);}
	  echo " Ratings (".$viewArray[$view].")";
	  if ($settings['scoutsOn']==1) {echo " - Scouted by StatsLab";}
	  echo "</td>\n";
	  if ($pgCnt>1)
	  {
		  echo "   <td style='text-align:right;vertical-align:top;margin-bottom:0px;padding-bottom:0px;'>\n";
		  echo "    <table border=0 cellpadding=0 cellspacing=0 style='float:right;'>\n";
		  echo "     <tr class='title2'><td>Page</td>\n";
		  echo "     <td>\n";
		  echo "      <form action='./ratings.php' method='GET'>\n";
		  echo "       <input type='hidden' name='who' value='$who' >\n";
		  echo "       <input type='hidden' name='view' value='$view' >\n";
		  echo "       <input type='hidden' name='team_id' value='$tmID' >\n";
		  echo "       <input type='hidden' name='sort1' value='".$_GET['sort1']."' >\n";
		  echo "       <input type='hidden' name='sort2' value='".$_GET['sort2']."' >\n";
		  echo "       <input type='hidden' name='sort3' value='".$_GET['sort3']."' >\n";
		  echo "       <input type='hidden' value='$sortMeth' name='sortMeth' id='sortMeth' />\n";
		  echo "       <select name='page' onchange='this.form.submit()' style='float:right;clear:none;'>\n";
		  for ($i=0;$i<$pgCnt;$i++)
		  {
			  echo "        <option value='$i'";
			  if ($page==$i) {echo " selected='selected'";}
			  echo ">".($i+1)."</option>\n";
		  }
		  echo "       </select>\n";
		  echo "      </form>\n";
		  echo "     </td>\n";
		  echo "     <td style='text-align:left;'>of $pgCnt</td></tr>\n";
		  echo "    </table>\n";
		  echo "   </td>\n";
	  }
	  else {echo "   <td>&nbsp;</td>\n";}
	  echo "  </tr>\n";
	  echo "  <tr><td colspan=2 style='padding:0px;'>\n";
	  echo "   <table border=0 cellpadding=2 cellspacing=0 width='100%' class='sortable'>\n";
	  echo "    <thead><tr class='headline'>";
	  echo "<td class='hsc2_l'>Player</td>";
	  echo "<td class='hsc2_l'>Tm</td>";
	  echo "<td class='hsc2_l'>Lvl</td>";
	  echo "<td class='hsc2'>Age</td>";
	  echo "<td class='hsc2'>Pos</td>";
	  foreach ($ratArray as $key => $ratLbl) 
	  {
		  echo "<td class='hsc2'>".stat_text($ratLbl,2);
		  if ($sort1==$ratLbl) {echo "<sup>1</sup>\n";}
		  if ($sort2==$ratLbl) {echo "<sup>2</sup>\n";}
		  if ($sort3==$ratLbl) {echo "<sup>3</sup>\n";}
		  echo "</td>";
	  }
	  if (isset($fldArray))
	  {
		  foreach ($fldArray as $key => $ratLbl) {echo "<td class='hsc2'>".stat_text($ratLbl,2)."</td>";}
	  }
	  echo "</tr></thead>\n";

	  $rowCnt=0;
	  while ($row=mysqli_fetch_array($result))
	  {
		  $pid=$row['player_id'];
		  $tid=$row['team_id'];
		  $pos=$row['position'];
		  if ($pos==1) {$pos=$row['role'];}

		  $cls='s'.($rowCnt%2+1);

		  echo "    <tr class='$cls'>";
		  echo "<td class='".$cls."_l' sorttable_customkey='".$row['last_name'].",".$row['first_name']."'><a href='./player.php?player_id=$pid'>".$row['first_name']." ".$row['last_name']."</a>";
		  if (isset($players[$pid]['status'])) {echo "<sup>".$players[$pid]['status']."</sup>";}
		  echo"</td>";
		  if ($tid!=0)
		  {
			  echo "<td class='".$cls."_l' sorttable_customkey='".$teams[$tid]['level_id']."'><a href=''>".$teams[$tid]['abbr']."</a></td>";
			  echo "<td class='".$cls."_l' sorttable_customkey='";
			  if (isset($players[$pid]['rsv'])) {echo $teams[$tid]['level_id']+0.1;}
                elseif (isset($players[$pid]['int'])) {echo $teams[$tid]['level_id']+100.2;}
                else {echo $teams[$tid]['level_id']+0.2;}
              if (isset($players[$pid]['int'])) {echo "'>INT</td>";}
                else {echo "'>".get_level($teams[$tid]['level_id'])."</td>";}
		  }
		  else {echo "<td class='".$cls."_l'>FA</td>\n<td class='".$cls."_l'>-</td>\n";}
		  echo "<td>".$row['age']."</td>";
		  echo "<td sorttable_customkey='$pos'>".get_pos($pos,1)."</td>";
		  foreach ($ratArray as $key => $ratLbl)
		  {
			  switch ($ratLbl)
			  {
			  case 'pitching_ratings_misc_velocity':
			  case 'pitching_ratings_misc_ground_fly':
				  echo "<td sorttable_customkey='".$row[$ratLbl]."'>".stat_format($ratLbl,$row[$ratLbl])."</td>";
				  break;
			  default:
                  $ratVal=$row[$ratLbl];
                  echo "<td";
                  if ($ratVal!="-")
                  {
                      $ratScale=get_scale($ratLbl);
                      $scale=$settings[$ratScale];
                      echo " class='dc ".rat_class($scale,$ratVal)."'";
                  }
                  echo ">";
                  echo $ratVal;
                  echo "</td>";
				  break;
			  }
		  }
		  if (isset($fldArray))
		  {
			  foreach ($fldArray as $key => $ratLbl)
              {
                  $ratVal=$row[$ratLbl];
                  echo "<td";
                  if ($ratVal!="-")
                  {
                      $ratScale=get_scale($ratLbl);
                      $scale=$settings[$ratScale];
                      echo " class='dc ".rat_class($scale,$ratVal)."'";
                  }
                  echo ">";
                  echo $ratVal;
                  echo "</td>";
              }
		  }
		  echo "</tr>\n";
		  $rowCnt++;
	  }

	  echo "   </table>\n";
	  echo "  </td></tr>\n";
	  echo " </table>   <!-- close ratings table -->\n";
	  echo "</div>   <!-- close ratings table -->\n";

	  echo "<div class='textbox' style='border:0px;'>\n";
	  echo "<b>Notes:</b><br/>\n";
	  echo " <sup><span title='On Secondary Roster'>~</span></sup> = on secondary roster</br>\n";
	  echo " <sup><span title='On Waivers'>WV</span></sup> = on waivers</br>\n";
	  echo " <sup><span title='Designated For Assignment'>DFA</span></sup> = designated for assignment</br>\n";
	  echo " <sup><span style='color:#990000;' title='On DL'>DL</span></sup> = on DL, hover of DL indicator for popup to indicate 60-day DL</br>\n";
	  echo " <sup><span title='On Reserve Roster'>RSV</span></sup> = on reserve roster</br>\n";
	  echo "</div>\n";
  }

  mysqli_close($db);

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

  echo " <script>\n";
  echo " <!--\n";
  echo "   whoChange();\n";
  echo " -->\n";
  echo " </script>\n";

  page_foot($_SESSION['lgpath']);


?>
