<?php

  include("html_functions.php");



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

  $lgid=$_SESSION['lgid'];



  # Check Admin Table

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

  $query="SHOW TABLES LIKE 'ootp_sql_admin';";

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

  if (mysqli_num_rows($result)) 

  {

	  # Get Settings

	  $query="SELECT scope,setting,value FROM ootp_sql_admin WHERE setting LIKE '%nable'";

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

	  if (mysqli_num_rows($result))

	  {

		  while ($row=mysqli_fetch_array($result))

		  {

			  $scope=$row['scope'];

			  $setting=$row['setting'];

			  $value=$row['value'];



			  $settings[$scope][$setting]=$value;

		  }

	  }

  }



  # Get Leagues

  $query="SELECT league_id,name,abbr,parent_league_id,league_level FROM leagues ORDER BY league_id";

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

  $lgCnt=0;

  if (mysqli_num_rows($result)) 

  {

	  while ($row=mysqli_fetch_array($result))

	  {

		  $league_id=$row['league_id'];

		  $leagues[$league_id]['name']=$row['name'];

		  $leagues[$league_id]['abbr']=$row['abbr'];

		  $leagues[$league_id]['parent']=$row['parent_league_id'];

		  $leagues[$league_id]['level']=$row['league_level'];

		  $leagues[$lgCnt]=$league_id;

		  $lgCnt++;

	  }

	  $lgTotCnt=count($leagues)/2;

  }



  mysqli_close($db);



  # Create Pages Array

  $pages=Array(

	       "awEnable"  =>"Awards Voting"

	      ,"asEnable"  =>"All-Star Voting"

	      ,"dftEnable" =>"Draft Module"

	      ,"hofEnable" =>"Hall of Fame Voting"

	      ,"mrEnable"  =>"Milestone Report"

	      ,"poEnable"  =>"Playoff Odds"

	      ,"pstEnable" =>"Postseason Reports"

	      ,"spEnable"  =>"Sim Preview"

	      ,"sotdEnable"=>"Stat of the Day"

	      ,"tbEnable"  =>"Trade Block"

              );

  $maxCols=6;



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

  $title='Enabled Pages Admin';



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

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

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



  if ($_SESSION['user_is_commish']!=1)

  {

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

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

	  echo "You must be a commisioner to access this page";

	

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

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

	  page_foot($_SESSION['lgpath']);

	  exit;

  }

  else

  {

	  nav_toolbar('admin','Admin Pages');

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

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

  }



  if ($lgTotCnt==0)

  {

	  echo "<p>No leagues defined.  Verify leagues table exists.</p>\n";

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

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

	  page_foot($_SESSION['lgpath']);

	  exit;

  }

  else

  {

	  if ($lgTotCnt>$maxCols) {$lgCnt=$maxCols;}

	  else {$lgCnt=$lgTotCnt;}

  }



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

  $module="Pages";

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

  echo "   <FORM ACTION=\"save_settings.php\" METHOD=\"post\">\n";

  echo "    <input type='hidden' name='module' value='".$module."'>\n";

  echo "    <table cellpadding=2 cellspacing=0 style='width:935px;'>";

  echo "     <TR class='title'><TD colspan=".($lgCnt+1).">Enabled Pages</TD></TR>\n";



  for ($i=0;$i<$lgTotCnt;$i++)

  {

	  echo "     <TR class='headline'>\n";

	  echo "      <TD class='hsc2'>&nbsp;</TD>\n";

	  $jmax=min($lgTotCnt,$i+$maxCols);

	  for ($j=$i;$j<$jmax;$j++)

	  {

		  $league_id=$leagues[$j];

		  echo "      <TD class='hsc2'><span title='".$leagues[$league_id]['name']." (ID: $league_id)' onclick=\"toggleLeague($league_id);\" >".$leagues[$league_id]['abbr']."</span>";

		  if (($leagues[$league_id]['level']!=1)&&($leagues[$league_id]['level']!=8))

		  {

			  $pID=$leagues[$league_id]['parent'];

			  if ($pID!=0) {echo " <span title='Parent League: ".$leagues[$pID]['abbr']."' onclick=\"toggleLeague($league_id);\" >(".get_level($leagues[$league_id]['level']).")</span>";}

			  else {echo " <span title='Independant' onclick=\"toggleLeague($league_id);\" >(".get_level($leagues[$league_id]['level']).")</span>";}

		  }

		  echo "</TD>\n";

	  }

	  if (($jmax%$maxCols)!=0)

	  {

		  $extraCols=$maxCols-$jmax%$maxCols;

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

	  }

	  echo "     </TR>\n";

	  echo "     <TR class='headline'>\n";

	  echo "      <TD class='hsc2'>&nbsp;</TD>\n";

	  $jmax=min($lgTotCnt,$i+$maxCols);

	  for ($j=$i;$j<$jmax;$j++)

	  {

		  $league_id=$leagues[$j];

		  echo "      <TD class='hsc2'>(<a onclick=\"toggleLeague($league_id,1);\">On</a>/<a onclick=\"toggleLeague($league_id,-1);\">Off</a>)</TD>\n";

	  }

	  if (($jmax%$maxCols)!=0)

	  {

		  $extraCols=$maxCols-$jmax%$maxCols;

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

	  }

	  echo "     </TR>\n";

	  

	  $rowCnt=0;

	  foreach ($pages as $pgNm => $pgText)

	  {

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

		  echo "     <TR class='$cls'>\n";

		  echo "      <TD style='padding-right:4px;'><span onclick=\"togglePage('$pgNm');\">$pgText</span> (<a onclick=\"togglePage('$pgNm',1);\">On</a>/<a onclick=\"togglePage('$pgNm',-1);\">Off</a>)</TD>\n";

		  for ($j=$i;$j<$jmax;$j++)

		  {

			  $league_id=$leagues[$j];

			  echo "      <TD style='border-left:1px dotted #CCCCCC;'><INPUT type=\"checkbox\" name=\"".$pgNm."^".$league_id."\" value=1";

			  if ($settings[$league_id][$pgNm]==1) {echo " checked";}

			  echo "></TD>\n";

		  }

		  if (($jmax%$maxCols)!=0)

		  {

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

		  }

		  echo "     </TR>\n";

		  $rowCnt++;

	  }



	  $i=$j-1;

  }







  echo "     <TR><TD colspan=$lgCnt>&nbsp;</TD><TD><INPUT type=\"submit\" value=\"Save\"></TD></TR>\n";

  echo "    </table>";

  echo "   </FORM>\n";

  echo " </div>\n";

  

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

  echo "  <p>\n";

  echo "    NOTE:<br/>- Click on a league header or page name to toggle all checkboxes for that league or module.<br/>\n";

  echo "    - Use On/Off to turn all on or all off for a league or page regardless of current state.\n";

  echo "  </p>\n";

  echo " </div>\n";



  echo " <script type='text/javascript'>\n";

  echo "  function toggleLeague(lgid,state) {\n";

  echo "     var elemName;\n";

  echo "     var elemState;\n";

  echo "     state = state || 0;\n";

  foreach ($pages as $pgNm => $pgText)

  {

	  echo "\n";

	  echo "     elemName='$pgNm^' + lgid;\n";

	  echo "     elemState=document.getElementsByName(elemName)[0].checked;\n";

	  echo "     if (((elemState==true)&&(state!=1))||(state==-1)) {document.getElementsByName(elemName)[0].checked=false;}\n";

	  echo "     else {document.getElementsByName(elemName)[0].checked=true;}\n";

  }

  echo "  }\n";

  echo "  function togglePage(pgNm,state) {\n";

  echo "     var elemName;\n";

  echo "     var elemState;\n";

  echo "     state = state || 0;\n";

  for ($i=0;$i<$lgTotCnt;$i++)

  {

	  $league_id=$leagues[$i];

	  echo "\n";

	  echo "     elemName=pgNm + '^$league_id';\n";

	  echo "     elemState=document.getElementsByName(elemName)[0].checked;\n";

	  echo "     if (((elemState==true)&&(state!=1))||(state==-1)) {document.getElementsByName(elemName)[0].checked=false;}\n";

	  echo "     else {document.getElementsByName(elemName)[0].checked=true;}\n";

  }

  echo "  }\n";

  echo " </script>";

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

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

  page_foot($_SESSION['lgpath']);





?>

