<?php

  session_start();



  if (!isset($_POST['module']))

  {

  	  echo "Invalid request to save settings;";

	  exit;

  }



  $debug=0;
  if (isset($_POST['debug']))
  {
	  $debug=$_POST['debug'];
  	  unset($_POST['debug']);
  }




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



  switch ($_POST['module'])

  {

  case 'admin':

  	  $scope='ALL';

	  $module='admin';



	  # Zero out checkboxes if blank

	  $cbArray=Array(

		   "requireLogin"

		  ,"showAllUsers"

		  ,"renameFiles"

		  ,"scoutsOn"

    		  ,"devEnable"

    		  ,"expEnable"

		  ,"finEnable"

                     ,"ratEnable"
                     ,"rawRatings"

		  ,"trackSplits"

		  ,"addDisable"

		  ,"sendUpdateEmails"

         ,"slackEnable"
         ,"sendUpdateSlack"
         ,"discordEnable"
         ,"sendUpdateDiscord"
         ,"discordTeamEmojis"

	           );



	  foreach ($cbArray as $key => $setting)

	  {

		  if (!isset($_POST[$setting])) {$_POST[$setting]=0;}

	  }



	  # Save settings

  	  foreach ($_POST as $setting => $value)

	  {

		  if ($setting=='module') {continue;}

		  if (($setting=='priLgID')&&(is_numeric($value))) {$_SESSION['lgid']=$value;}



		  $value=mysqli_real_escape_string($db,$value);

		  $query="INSERT INTO ootp_sql_admin (scope, module, setting, value) VALUES (";

  		  $query.="'$scope', '$module', '$setting', \"$value\"";

		  $query.=") ON DUPLICATE KEY UPDATE value=\"$value\";";

  		  if ($debug==1) {echo "$query<br/>\n";}

		  mysqli_query($db,$query);

  		  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	  }

	  break;

  case 'All-Star':

	  # Zero out checkboxes if blank

	  $cbArray=Array("asVotingOn","asModEnable","asResults","qOnTotFIP","splitOF");

	  foreach ($cbArray as $key => $setting)

	  {

		  if (!isset($_POST[$setting])) {$_POST[$setting]=0;}

	  }



	  $module=$_POST['module'];

	  

	  # Save settings

  	  foreach ($_POST as $setting => $value)

	  {

		  if ($setting=='module') {continue;}

		 

		  $scope=$_SESSION['lgid'];



		  $value=mysqli_real_escape_string($db,$value);

		  $query="INSERT INTO ootp_sql_admin (scope, module, setting, value) VALUES (";

  		  $query.="'$scope', '$module', '$setting', \"$value\"";

		  $query.=") ON DUPLICATE KEY UPDATE value=\"$value\";";

  		  if ($debug==1) {echo "$query<br/>\n";}

		  mysqli_query($db,$query);

  		  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	  }

	  break;

  case 'Awards':

	  # Zero out checkboxes if blank

	  $cbArray=Array("awVotingOn","awModEnable","awResults","ggEnable","splitOF","cmEnable","gmEnable","gmSelfVote");

	  foreach ($cbArray as $key => $setting)

	  {

		  if (!isset($_POST[$setting])) {$_POST[$setting]=0;}

	  }

	  

	  $module=$_POST['module'];

	  

	  # Save settings

  	  foreach ($_POST as $setting => $value)

	  {

		  if ($setting=='module') {continue;}

		 

		  $scope=$_SESSION['lgid'];



		  $value=mysqli_real_escape_string($db,$value);

		  $query="INSERT INTO ootp_sql_admin (scope, module, setting, value) VALUES (";

  		  $query.="'$scope', '$module', '$setting', \"$value\"";

		  $query.=") ON DUPLICATE KEY UPDATE value=\"$value\";";

  		  if ($debug==1) {echo "$query<br/>\n";}

		  mysqli_query($db,$query);

  		  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	  }

	  break;

  case 'Draft':

	  # Zero out checkboxes if blank
	  $cbArray=Array("draftOn","pauseAuto","setAuto","autoOpen","createDraft","timerEnable","flexTimer","pauseWkEnd","schedulePicks","skipPicks");

      if (!isset($_POST["createDraft"])) {$_POST["createDraft"]=0;}
	  foreach ($cbArray as $key => $setting)
	  {
		  if ($_POST["createDraft"]!=1)  //Clear draft config if not recreating it
		  {
			  unset($_POST["nRounds"]);
			  unset($_POST["suppRounds"]);
			  unset($_POST["rndLength"]);
		  }

		  if (!isset($_POST[$setting])) {$_POST[$setting]=0;}
	  }

      // Ensure start and stop times are in 24hr format
      if (isset($_POST['timeStart']))
          {
              $tmStrt=$_POST['timeStart'];
              if ((!is_numeric($tmStrt))||($tmStrt<0)) {$tmStrt="0000";}
              if (strlen($tmStrt)<3) {$tmStrt=100*$tmStrt;}
              if (strlen($tmStrt)==3) {$tmStrt="0".$tmStrt;}
              $_POST['timeStart']=$tmStrt;
          }
          else {$_POST['timeStart']="0000";}
      if (isset($_POST['timeStop']))
          {
              $tmStrt=$_POST['timeStop'];
              if ((!is_numeric($tmStrt))||($tmStrt<0)) {$tmStrt="0000";}
              if (strlen($tmStrt)<3) {$tmStrt=100*$tmStrt;}
              if (strlen($tmStrt)==3) {$tmStrt="0".$tmStrt;}
              if ($tmStrt>2400) {$tmStrt=2400;}
              $_POST['timeStop']=$tmStrt;
          }
          else {$_POST['timeStop']="2400";}
      if ($_POST['timeStop']<=$_POST['timeStart'])
          {
              $tmStrt=$_POST['timeStop'];
              $_POST['timeStop']=$_POST['timeStart'];
              $_POST['timeSTart']=$tmStrt;
          }

	  if ($_POST['nRounds']=="") {$_POST['createDraft']=0;}



	  $module=$_POST['module'];

	  

	  # Save settings

  	  foreach ($_POST as $setting => $value)

	  {

		  if (($setting=='module')||($setting=='createDraft')||($setting=='schedulePicks')||($setting=='slackTradeChannel')) {continue;}



		  $e=explode("_",$setting);

		  if ($e[0]=='feeder')

		  {

			  $setting=$e[0];

			  $identifier=$e[1];

		  }

		  else

		  {

			  $identifier=-1;

		  }



		  $scope=$_SESSION['lgid'];



		  $value=mysqli_real_escape_string($db,$value);

		  $query="INSERT INTO ootp_sql_admin (scope, module, setting, identifier, value) VALUES (";

  		  $query.="'$scope', '$module', '$setting', '$identifier', \"$value\"";

		  $query.=") ON DUPLICATE KEY UPDATE value=\"$value\";";

  		  if ($debug==1) {echo "$query<br/>\n";}

		  mysqli_query($db,$query);

  		  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	  }

	  break;

  case 'Draft_Order':

	  $lgid=$_SESSION['lgid'];



	  # Save settings

  	  foreach ($_POST as $setting => $value)

	  {

		  if (($setting=='module')) {continue;}



		  $value=mysqli_real_escape_string($db,$value);



		  $s=explode("_",$setting);

		  $set=$s[0];

		  $rnd=$s[1];

		  $supp=$s[2];

		  $pick=$s[3];



		  $picks[$rnd][$supp][$pick][$set]=$value;

	  }



	  $delPicks=0;

	  foreach ($picks as $rnd => $valr)

	  {

		  foreach ($picks[$rnd] as $supp => $vals)

		  {

			  foreach ($picks[$rnd][$supp] as $pick => $valp)

			  {

				  $tid=$picks[$rnd][$supp][$pick]['team'];

				  $due=$picks[$rnd][$supp][$pick]['due'];



				  if ($tid==-1)

				  {

					  $query="DELETE FROM amateur_draft WHERE league_id=$lgid AND round=$rnd AND supplemental=$supp AND pick_round=$pick";

					  if ($debug==1) {echo "$query<br/>\n";}

					  mysqli_query($db,$query);

					  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	

					  $query="UPDATE amateur_draft SET pick_round=(pick_round-1) WHERE league_id=$lgid AND round=$rnd AND supplemental=$supp AND pick_round>$pick"; 

					  if ($debug==1) {echo "$query<br/>\n";}

					  mysqli_query($db,$query);

					  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}



					  $delPicks++;

				  }

				  else

				  {

					  $pick=$pick-$delPicks;

					  $query="UPDATE amateur_draft SET team_id=$tid,pick_time='$due' WHERE league_id=$lgid AND round=$rnd AND supplemental=$supp AND pick_round=$pick"; 

					  if ($debug==1) {echo "$query<br/>\n";}

					  mysqli_query($db,$query);

					  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

				  }

			  }

		  }

	  }

	  break;

  case 'Draft_Warroom':

	  # Zero out checkboxes if blank

	  $cbArray=Array("auto_list","auto","draftSubscribe");

	  foreach ($cbArray as $key => $setting)

	  {

		  if (!isset($_POST[$setting])) {$_POST[$setting]=0;}

	  }



	  $module='users';

	  $identifier=$_POST['hmid'];



	  # Save settings

  	  foreach ($_POST as $setting => $value)

	  {

		  if (($setting=='module')||($setting=='draftList')||($setting=='dList')||($setting=='hmid')||($setting=='redirect_to')) {continue;}



		  if ($setting=='draftSubscribe') {$scope='ALL';}

		  else {$scope=$_SESSION['lgid'];}

			  

		  $value=mysqli_real_escape_string($db,$value);

		  $query="INSERT INTO ootp_sql_admin (scope, module, setting, identifier, value) VALUES (";

  		  $query.="'$scope', '$module', '$setting', '$identifier', \"$value\"";

		  $query.=") ON DUPLICATE KEY UPDATE value=\"$value\";";

  		  if ($debug==1) {echo "$query<br/>\n";}

		  mysqli_query($db,$query);

  		  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	  }



	  # Process Draft List

	  $scope=$_SESSION['lgid'];

	  $query="DELETE FROM ootp_sql_draft_list WHERE human_manager_id=$identifier AND league_id=$scope";

	  if ($debug==1) {echo "$query<br/>\n";}

	  mysqli_query($db,$query);

	  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}



	  $dList=trim($_POST['dList'],"|");

	  $e=explode("|",$dList);



	  if (count($e)>0)

	  {

		  foreach ($e as $key => $pid)

		  {

			  if ($pid<=0) {continue;}



			  $porder=$key+1;

			  $query="INSERT INTO ootp_sql_draft_list (human_manager_id,league_id,porder,player_id) VALUES ($identifier,$scope,$porder,$pid);";

			  if ($debug==1) {echo "$query<br/>\n";}

			  mysqli_query($db,$query);

			  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

		  }

	  }



	  break;

  case 'HOF':

	  # Zero out checkboxes if blank

	  $cbArray=Array("hofVoteEnable","hofResults",'hofGenerate');

	  foreach ($cbArray as $key => $setting)

	  {

		  if (!isset($_POST[$setting])) {$_POST[$setting]=0;}

	  }

	  

	  $module=$_POST['module'];

	  

	  # Save settings

  	  foreach ($_POST as $setting => $value)

	  {

		  if (($setting=='module')||($setting=='hofGenerate')) {continue;}

		 

		  $scope=$_SESSION['lgid'];



		  $value=mysqli_real_escape_string($db,$value);

		  $query="INSERT INTO ootp_sql_admin (scope, module, setting, value) VALUES (";

  		  $query.="'$scope', '$module', '$setting', \"$value\"";

		  $query.=") ON DUPLICATE KEY UPDATE value=\"$value\";";

  		  if ($debug==1) {echo "$query<br/>\n";}

		  mysqli_query($db,$query);

  		  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	  }



	  if ($_POST['hofGenerate']!=1) {break;}

  case 'HOF Generate':

	  $lgid=$_SESSION['lgid'];

	  $lgyear=$_SESSION['lgyear'];



	  include("gen_functions.php");



	  ## Get settings if needed

	  if ($_POST['hofGenerate']!=1)   // Coming in directly

	  {

	       	  $module='HOF';

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

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

		  if (mysqli_num_rows($result)) 

	       	  {

			  # Get Settings

			  $query="SELECT setting,value FROM ootp_sql_admin WHERE scope IN ('ALL',$lgid) AND module='$module'";

			  if ($debug==1) {echo "$query<br/>\n";}

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

	       		  if (mysqli_num_rows($result))

			  {

	       			  while ($row=mysqli_fetch_array($result))

				  {

	       				  $field=$row['setting'];

					  $val=$row['value'];

	       				  $val=stripslashes($val);



					  $_POST[$field]=$val;

				  }

			  }

		  }

       	  }

	  if ($_POST['minYrsRet']=="")  {$_POST['minYrsRet']=0;}

       	  if ($_POST['minSea']=="")     {$_POST['minSea']=1;}

	  if ($_POST['maxYrsBlt']=="")  {$_POST['maxYrsBlt']=15;}

       	  if ($_POST['numVotes']=="")   {$_POST['numVotes']=10;}

	  if ($_POST['numVotes']<1)     {$_POST['numVotes']=10;}

	  if ($_POST['minPerc']=="")    {$_POST['minPerc']=5;}

	  elseif ($_POST['minPerc']<0)  {$_POST['minPerc']=5;}

       	  elseif ($_POST['minPerc']>100) {$_POST['minPerc']=100;}



	  if ($_POST['bminHs']=="")     {$_POST['bminHs']=2000;}

       	  if ($_POST['bminHRs']=="")    {$_POST['bminHRs']=300;}

	  if ($_POST['bminSBs']=="")    {$_POST['bminSBs']=300;}

       	  if ($_POST['bminAVG']=="")    {$_POST['bminAVG']=0.300;}

	  if ($_POST['bminAVGPA']=="")  {$_POST['bminAVGPA']=1500;}

	  if ($_POST['bminOPS']=="")    {$_POST['bminOPS']=0.750;}

	  if ($_POST['bminOPSPA']=="")  {$_POST['bminOPSPA']=1500;}

	  if ($_POST['bminHOFs']=="")   {$_POST['bminHOFs']=35;}

	  if ($_POST['bminPA']=="")     {$_POST['bminPA']=2000;}

	  $_POST['incBs']=trim($_POST['incBs'],", ");

	  $_POST['excBs']=trim($_POST['excBs'],", ");

	  if ($_POST['incBs']=="") {$_POST['incBs']=-999;}

	  if ($_POST['excBs']=="") {$_POST['excBs']=-999;}

       

	  if ($_POST['pminWs']=="")     {$_POST['pminWs']=150;}

	  if ($_POST['pminKs']=="")     {$_POST['pminKs']=2000;}

	  if ($_POST['pminSVs']=="")    {$_POST['pminSVs']=300;}

	  if ($_POST['pminWPct']=="")   {$_POST['pminWPct']=0.525;}

	  if ($_POST['pminWPctIP']=="") {$_POST['pminWPctIP']=800;}

	  if ($_POST['pminHOFs']=="")   {$_POST['pminHOFs']=35;}

	  if ($_POST['pmaxERA']=="")    {$_POST['pmaxERA']=5.00;}

	  if ($_POST['pminIPs']=="")    {$_POST['pminIPs']=1000;}

	  if ($_POST['pminIPr']=="")    {$_POST['pminIPr']=500;}

	  $_POST['incPs']=trim($_POST['incPs'],", ");

	  $_POST['excPs']=trim($_POST['excPs'],", ");

	  if ($_POST['incPs']=="") {$_POST['incPs']=-999;}

	  if ($_POST['excPs']=="") {$_POST['excPs']=-999;}

       

	  ##### Clear out player listing #####

	  $query="DELETE FROM awards_vote WHERE league_id=$lgid AND human_manager_id=-1 AND year=$lgyear AND award_id=100;";

       	  mysqli_query($db,$query);

       

	  ##### Get players voted off #####

	  $pidExcList="";

       	  if ($_POST['minPerc']>0)

	  {

		  $query="SELECT year,COUNT(DISTINCT(human_manager_id)) as blt FROM awards_vote WHERE league_id=$lgid AND award_id=100 GROUP BY year ORDER BY year;";

		  if ($debug==1) {echo "$query<br/>\n";}

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

       		  if (mysqli_num_rows($result))

		  {

       			  while ($row=mysqli_fetch_array($result))

			  {

       				  $yr=$row['year'];

				  $votesCast=$row['blt'];

       				  $years[$yr]['votes']=$votesCast;

			  }

       		  }

		  if (isset($years))

       		  {

			  foreach ($years as $yr => $val)

       			  {

				  if ($years[$yr]['votes']>0)

       				  {

					  $query="SELECT player_id,SUM(vote) as votes,(100*SUM(vote)/".$years[$yr]['votes'].") as pct FROM awards_vote WHERE league_id=$lgid AND year=$yr AND award_id=100 GROUP BY player_id HAVING (100*SUM(vote)/".$years[$yr]['votes'].")<".$_POST['minPerc']." ORDER BY votes DESC;";

					  if ($debug==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'];

							  $vote=$row['votes'];

       							  $pct=$row['pct'];

							  $pidExcList.=",$pid";

       						  }

					  }

       				  }

			  }

       			  $_POST['excBs']=$_POST['excBs'].$pidExcList;

			  $_POST['excBs']=trim($_POST['excBs'],", ");

       			  $_POST['excPs']=$_POST['excPs'].$pidExcList;

			  $_POST['excPs']=trim($_POST['excPs'],", ");

       		  }

	  }

       

	  ## Get qualifying batters

	  $query="SELECT p.player_id,MIN(year) as minYr,MAX(year) as maxYr,COUNT(DISTINCT year) as yrs FROM players as p,players_career_batting_stats as pcs WHERE p.player_id=pcs.player_id AND pcs.league_id=$lgid AND split_id=1 AND ((retired=1 AND hall_of_fame=0) OR p.player_id IN (".$_POST['incBs'].")) AND p.player_id NOT IN (".$_POST['excBs'].") GROUP BY player_id HAVING";

	  $query.=" (";

	  $query.=" SUM(pa)>=".$_POST['bminPA'];

	  $query.=" AND (";

	  $query.="SUM(h)>=".$_POST['bminHs'];

	  $query.=" OR SUM(hr)>=".$_POST['bminHRs'];

	  $query.=" OR SUM(sb)>=".$_POST['bminSBs'];

	  $query.=" OR (SUM(pa)>=".$_POST['bminAVGPA']." AND ".stat_query('avg').">=".$_POST['bminAVG'].")";

	  $query.=" OR (SUM(pa)>=".$_POST['bminOPSPA']." AND ".stat_query('ops').">=".$_POST['bminOPS'].")";

	  $query.=" )";

	  $query.=" AND MAX(maxYr)<=($lgyear-".$_POST['minYrsRet'].")";

	  $query.=" AND MAX(maxYr)>($lgyear-".$_POST['minYrsRet']."-".$_POST['maxYrsBlt'].")";

	  $query.=" AND COUNT(DISTINCT year)>=".$_POST['minSea'];

	  $query.=") OR p.player_id IN (".$_POST['incBs'].")";

	  if ($debug==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'];



			  $pquery="INSERT IGNORE INTO awards_vote (player_id,league_id,sub_league_id,human_manager_id,year,award_id,vote,position) VALUES ($pid,$lgid,0,-1,$lgyear,100,0,10);";

			  if ($debug==1) {echo "$pquery<br/>\n";}

       			  mysqli_query($db,$pquery);

		  }

	  }

	  

	  ## Get qualifying pitchers

	  $query="SELECT p.player_id,MIN(year) as minYr,MAX(year) as maxYr,COUNT(DISTINCT year) as yrs FROM players as p,players_career_pitching_stats as pcs WHERE p.player_id=pcs.player_id AND pcs.league_id=$lgid AND split_id=1 AND ((retired=1 AND hall_of_fame=0) OR p.player_id IN (".$_POST['incPs'].")) AND p.player_id NOT IN (".$_POST['excPs'].") GROUP BY player_id HAVING";

	  $query.=" (";

	  $query.=" (".stat_query('era').")<=".$_POST['pmaxERA'];

      	  $query.=" AND IF(((SUM(g)-SUM(gs))>SUM(gs))AND(((".stat_query('ip').")/SUM(g))<4),".$_POST['pminIPr'].",".$_POST['pminIPs'].")<=(".stat_query('ip').")";

	  $query.=" AND (";

	  $query.="SUM(w)>=".$_POST['pminWs'];

	  $query.=" OR SUM(k)>=".$_POST['pminKs'];

	  $query.=" OR SUM(s)>=".$_POST['pminSVs'];

	  $query.=" OR ((".stat_query('winpct').")>=".$_POST['pminWPct']." AND ".stat_query('ip').">=".$_POST['pminWPctIP'].")";

	  $query.=" )";

	  $query.=" AND MAX(maxYr)<=($lgyear-".$_POST['minYrsRet'].")";

	  $query.=" AND MAX(maxYr)>($lgyear-".$_POST['minYrsRet']."-".$_POST['maxYrsBlt'].")";

	  $query.=" AND COUNT(DISTINCT year)>=".$_POST['minSea'];

	  $query.=") OR p.player_id IN (".$_POST['incPs'].")";

	  if ($debug==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'];



			  $pquery="INSERT IGNORE INTO awards_vote (player_id,league_id,sub_league_id,human_manager_id,year,award_id,vote,position) VALUES ($pid,$lgid,0,-1,$lgyear,100,0,1);";

			  if ($debug==1) {echo "$pquery<br/>\n";}

			  mysqli_query($db,$pquery);

		  }

	  }



	  break;

  case 'Last Sim':

	  # Zero out checkboxes if blank

	  $cbArray=Array("autoSimLength","poEnable","stEnable","grEnable","mrEnable","spEnable","pstEnable","sotdEnable");

	  foreach ($cbArray as $key => $setting)

	  {

		  if (!isset($_POST[$setting])) {$_POST[$setting]=0;}

	  }

	  

	  $module=$_POST['module'];

	  

	  # Save settings

  	  foreach ($_POST as $setting => $value)

	  {

		  if ($setting=='module') {continue;}



		  switch ($setting)

		  {

		  case 'autoSimLength':

		  case 'simLength':

		  case 'nSeasons':

		  case 'pythMeth':

		  case 'pythExp':

			  $scope='ALL';

			  break;

		  default:

			  $scope=$_SESSION['lgid'];

			  break;

		  }



		  $value=mysqli_real_escape_string($db,$value);

		  $query="INSERT INTO ootp_sql_admin (scope, module, setting, value) VALUES (";

  		  $query.="'$scope', '$module', '$setting', \"$value\"";

		  $query.=") ON DUPLICATE KEY UPDATE value=\"$value\";";

  		  if ($debug==1) {echo "$query<br/>\n";}

		  mysqli_query($db,$query);

  		  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	  }

	  break;

  case 'Pages':

	  $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"

	  );



	  # Get leagues

	  $query="SELECT league_id 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'];



			  # Zero out checkboxes if blank

			  foreach ($pages as $setting => $pgName)

			  {

				  $setting.="^".$league_id;

				  if (!isset($_POST[$setting])) {$_POST[$setting]=0;}

			  }

		  }

	  }

	  

	  # Save settings

  	  foreach ($_POST as $setting => $value)

	  {

		  if ($setting=='module') {continue;}



		  # Explode setting name to get lgID as scope

		  $s=explode("^",$setting);

		  $setting=$s[0];

		  $scope=$s[1];



		  switch ($setting)

		  {

		  case 'asEnable':

			  $module='All-Star';

			  break;

		  case 'awEnable':

			  $module='Awards';

			  break;

		  case 'devEnable':

			  $module='Development';

			  break;

		  case 'dftEnable':

			  $module='Draft';

			  break;

		  case 'hofEnable':

			  $module='Hall of Fame';

			  break;

		  case 'mrEnable':

		  case 'poEnable':

		  case 'pstEnable':

		  case 'spEnable':

		  case 'sotdEnable':

			  $module='Last Sim';

			  break;

		  default:

			  $module='admin';

			  break;

		  }



		  $value=mysqli_real_escape_string($db,$value);

		  $query="INSERT INTO ootp_sql_admin (scope, module, setting, value) VALUES (";

  		  $query.="'$scope', '$module', '$setting', \"$value\"";

		  $query.=") ON DUPLICATE KEY UPDATE value=\"$value\";";

  		  if ($debug==1) {echo "$query<br/>\n";}

		  mysqli_query($db,$query);

  		  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	  }

	  break;

  case 'users':

	  # Zero out checkboxes if blank

	  $cbArray=Array("hideinfo","hideScores","slUpdateSubscribe","draftSubscribe");

	  foreach ($cbArray as $key => $setting)

	  {

		  if (!isset($_POST[$setting]))

		  {

			  $_POST[$setting]=0;

			  if ($setting=='hideScores') {unset($_SESSION['hideScores']);}

		  }

	  }

	  

	  $module=$_POST['module'];

	  $identifier=$_POST['userID'];

	  

	  # Save settings

	  $scope='ALL';

	  foreach ($_POST as $setting => $value)

	  {

		  if (($setting=='module')||($setting=='userID')) {continue;}



		  $value=mysqli_real_escape_string($db,$value);

		  $query="INSERT INTO ootp_sql_admin (scope, module, setting, identifier, value) VALUES (";

  		  $query.="'$scope', '$module', '$setting', '$identifier', \"$value\"";

		  $query.=") ON DUPLICATE KEY UPDATE value=\"$value\";";

  		  if ($debug==1) {echo "$query<br/>\n";}

		  mysqli_query($db,$query);

  		  if (mysqli_errno($db)) {echo mysqli_error($db)."<br/>\n";}

	  }

	  break;

  }



  mysqli_close($db);



  if ($debug==1) 

  {

	  echo "createDraft::".$_POST["createDraft"]."<br/>\n";

	  echo "schedulePicks::".$_POST["schedulePicks"]."<br/>\n";

	  if (!(($_POST['module']=="Draft")&&(($_POST["createDraft"]==1)||($_POST['schedulePicks']==1)))) {exit;}

  }



  if (($_POST['module']=="Draft")&&(($_POST["createDraft"]==1)||($_POST['schedulePicks']==1)))

  {

	  header('Location: ./draftSchedule.php?from=draftAdmin.php&create='.$_POST["createDraft"].'&schedule='.$_POST['schedulePicks'].'&debug='.$debug);

  }

  elseif ($_POST['module']=="Draft_Warroom")

  {

	  header('Location: '.$_POST['redirect_to']);

  }

  else

  {

	  header('Location: '.$_SERVER['HTTP_REFERER']);

  }



?>

