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

  ##### Clear Votes #####	
  if ($_GET['clear_votes']==1)
  {
	  include($_SESSION['adminpath']."/dbopen.php");

	  $lgid=$_GET['lgid'];
       	  $year=$_GET['year'];
	  $hmid=$_GET['human_manager_id'];
 	  $pos=$_GET['pos'];
	  if ($year=="") 
      	  {
		  $query="DELETE FROM awards_vote WHERE league_id=$lgid AND award_id=9;";
  	  }
	  elseif ($hmid=="")
      	  {
		  $query="DELETE FROM awards_vote WHERE league_id=$lgid AND year=$year AND award_id=9;";
      	  }
	  else
      	  {
		  $query="DELETE FROM awards_vote WHERE league_id=$lgid AND year=$year AND human_manager_id=$hmid AND award_id=9";
  		  if ($pos!="") {$query.=" AND position=$pos;";}
      	  }
	  mysqli_query($db,$query);
	  mysqli_close($db);
	  header("Location: ./asAdmin.php");
	  exit;
  }

  ## Initialize
  $lgid=$_SESSION['lgid'];
  $date=new Date($_SESSION['lgdate']);
  $year=$_SESSION['lgyear'];

  include($_SESSION['adminpath']."/dbopen.php");
  
  ##### Get Settings #####
  $module='All-Star';
  $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 ($_GET['showq']==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);
		 	  switch ($field)
			  {
			  case 'splitOF'    : $splitOF=$val;     break;
			  }
		  }
      	  }
  }
  if ($splitOF=="") {$splitOF=0;}

  ##### Get User Input #####
  $ballot=$_POST['dList'];
  $hmid=$_POST['hmid'];
  $slid=$_POST['slid'];
  $pos=$_POST['position'];
	
  ##### Create Update Statements #####
  $updText="DELETE FROM awards_vote WHERE league_id=$lgid AND sub_league_id=$slid AND year=$year AND human_manager_id=$hmid AND award_id=9 AND position=$pos;\n";
  $pids=explode("|",$ballot);
  $tot=count($pids);
  for ($i=0;$i<$tot;$i++)
  {
	  $pid=$pids[$i];

	  $insQuery="INSERT INTO awards_vote (player_id,league_id,sub_league_id,year,human_manager_id,award_id,position,vote) VALUES ($pid,$lgid,$slid,$year,$hmid,9,$pos,1);";
	  $updText.=$insQuery."\n";
  }

  ##### Write Votes to File #####
  if ($updText!="")
  {
	  $updText=trim($updText,",");
       	  $updText=trim($updText);
	  $e=explode("\n",$updText);
       	  foreach ($e as $key => $query)
	  {
	    	  mysqli_query($db,$query);
		  $err=mysqli_error($db);
	    	  if ($err!="") {echo $err."<br />\n";}
	  }
  }
 
  mysqli_close($db);
  header("Location: ./allstar.php");
?>
