<?php if (!isset($_SESSION['FirstName'])) {
	session_start();
}
    include ('includes/db1config.php');
    include ('../includes/mysql_connect.php');
	include ('includes/admin-config.php');
?>
<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php"><font color="blue">log in here.</font></a>';
	exit;
	}
 if($_SESSION['roladmin'] ==1){$roladmin=1;}
 if($_SESSION['masterAdmin']==1){$masteradmin=1;}
//require_once ('mysql_connect.php');
$action          = $_GET["a"];
include ('includes/admin-config.php');

if(isset($_POST['a'])) {
	$action = trim($_POST['a']);

	// Add hours
	if($action == 'add') {
		if(!isset($_POST['addHoursType']) || !isset($_POST['day']) || !isset($_POST['fromHour']) || !isset($_POST['fromMinute']) || !isset($_POST['fromMeridiem']) || !isset($_POST['toHour']) || !isset($_POST['toMinute']) || !isset($_POST['toMeridiem'])) {
			die('0');
		}

		$addHoursType = trim($_POST['addHoursType']);
		if($addHoursType != 'open' && $addHoursType != 'delivery') {
			// not valid add hours type
			die('0');
		}
		$delivery = ($addHoursType == 'delivery') ? '1' : '0';

		$day = trim($_POST['day']);

		// from time
		$newFromTime = date('H:i:s', strtotime($_POST['fromHour'].':'.$_POST['fromMinute'].' '.$_POST['fromMeridiem']));

		// to time
		$newToTime = date('H:i:s', strtotime($_POST['toHour'].':'.$_POST['toMinute'].' '.$_POST['toMeridiem']));

		$sql = "INSERT INTO hours (start_time, end_time, day, delivery) VALUES ('".$newFromTime."', '".$newToTime."', '".$day."', '".$delivery."')";
		mysql_query($sql) or die(mysql_error());

		echo '1';
	}

	// Update hours
	if($action == 'update') {
		if(!isset($_POST['recordKey']) || !isset($_POST['fromHour']) || !isset($_POST['fromMinute']) || !isset($_POST['fromMeridiem']) || !isset($_POST['toHour']) || !isset($_POST['toMinute']) || !isset($_POST['toMeridiem'])) {
			die('0');
		}
		$recordKey = trim($_POST['recordKey']);

		// from time
		$newFromTime = date('H:i:s', strtotime($_POST['fromHour'].':'.$_POST['fromMinute'].' '.$_POST['fromMeridiem']));

		// to time
		$newToTime = date('H:i:s', strtotime($_POST['toHour'].':'.$_POST['toMinute'].' '.$_POST['toMeridiem']));

		$sql = "UPDATE hours SET start_time = '".$newFromTime."', end_time = '".$newToTime."' WHERE recordKey = '".mysql_real_escape_string($recordKey)."'";
		mysql_query($sql) or die(mysql_error());

		echo '1';
	}

	// Remove Hours
	if($action == 'remove') {
		if(!isset($_POST['recordKey'])) {
			die('0');
		}
		$recordKey = trim($_POST['recordKey']);

		$sql = "DELETE FROM hours WHERE recordKey = '" . mysql_real_escape_string($recordKey) . "'";
		mysql_query($sql) or die(mysql_error());

		echo '1';
	}

}else{
	die('0');
}
?>