<?php
////////////////////////////////////////////////////////////////////////
// EB Content Management System
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// Revised: 4/10/2010	ELL
// Added date picker and removed obsolete switch for HebrewDateConversion.
// Revised: 6/23/2010	ELL
// Modified to update each record based on ID rather than trancating table and inserting each record as new.  This one changed because an entire schedule
// was lost once and had to be recovered from backup.  Now if POST is lost, table is likely to remain in tact.
//
	include ("check_session.php");

	$submit = $_POST['submit'];
	
	if ($submit == "Back to Main Menu")
	{
		header("Location: main.php");
		exit;
	}
	
//	include "../connect/db_connection.php";

	$i = 0;
	
//	$DescriptionStr = "Description" . $i;
	$IDStr = "ID" . $i;
//	while ($_POST[$DescriptionStr])
	while ($_POST[$IDStr])
	{
		$DescriptionStr = "Description" . $i;
		$EventDateStr = "EventDate" . $i;
		$StartTimeStr = "StartTime" . $i;
		$EndTimeStr = "EndTime" . $i;
		$URLStr = "URL" . $i;
		$ActiveStr = "Active" . $i;
		
		$ID[$i] = $_POST[$IDStr];
		$Description[$i] = $_POST[$DescriptionStr];
		$EventDate[$i] = $_POST[$EventDateStr];
		$StartTime[$i] = $_POST[$StartTimeStr];
		$EndTime[$i] = $_POST[$EndTimeStr];

		$URL[$i] = $_POST[$URLStr];
		if (isset($_POST[$ActiveStr]))
			$Active[$i] = 1;
		else
			$Active[$i] = 0;
		$i++;
//		$DescriptionStr = "Description" . $i;
		$IDStr = "ID" . $i;
	}
	
	//Start by deleting all old data in table.
//	$query = "TRUNCATE TABLE schedule";
//	$result = mysql_query($query);
	
	//Insert new data into table.
	for ($j = 0; $j < $i; $j++)
	{
		$Description[$j]=addslashes($Description[$j]);
		$EventDate[$j]=addslashes($EventDate[$j]);
		$StartTime[$j]=addslashes($StartTime[$j]);
		$EndTime[$j]=addslashes($EndTime[$j]);
		$URL[$j]=addslashes($URL[$j]);
		
//		$EventDateSort = date("Y-m-d", strtotime($EventDate[$j]));
		$EventDateSort = $EventDate[$j];
		$EventDateArr = explode("-", $EventDate[$j]);
		$EventDate[$j] = $EventDateArr[1] . "/" . $EventDateArr[2] . "/" . $EventDateArr[0];
		
//		$query = "INSERT INTO schedule (Description, EventDate, StartTime, EndTime, URL, EventDateSort, Active) VALUES('".$Description[$j]."','".$EventDate[$j]."','".$StartTime[$j]."','".$EndTime[$j]."','".$URL[$j]."','".$EventDateSort."',".$Active[$j].")";
		$query = "UPDATE schedule SET Description = '" . $Description[$j] . "', EventDate = '" . $EventDate[$j] . "', StartTime = '" . $StartTime[$j] . "', EndTime = '" . $EndTime[$j] . "', URL = '" . $URL[$j] . "', EventDateSort = '" . $EventDateSort . "', Active = " . $Active[$j] . " WHERE ID = " . $ID[$j];
		$result = mysql_query($query);
	}
	
	if ($submit == "Add New Event")
	{
		$query = "INSERT INTO schedule (Description) VALUES('New Event')";
		$result = mysql_query($query);
		header("Location: edit_schedule.php");
		exit;
	}
	
	header("Location: edit_schedule.php");
	
?>
