<?require ("global.php");?>
<?php

$ID = 0;
$datetime = "";
if (isset($_POST['ID'])) { $ID = $_POST['ID']; }
if (isset($_POST['current_time']) && $_POST['current_time'] == 'yes')
{
  $time_offset = 0;
  $query = "SELECT time_offset FROM stages WHERE ID = " . $_POST['stageID'];
  $result = mysql_query($query, $db);
  if ($result)
    {
      if ($r = mysql_fetch_array($result))
	{
	  $time_offset = $r['time_offset'];
	}
    }
  $datetime = "NOW() + INTERVAL $time_offset HOUR";
}
else
{
  $datetime = "'" . $_POST['year'] . "-" . $_POST['month'] . "-"
    . $_POST['day'] . " " . $_POST['hour'] . ":" . $_POST['minute']
    . ":00" . "'";
}

if ($ID != 0)
{
  $query = "UPDATE ticks SET "
    . "stageID = '" . $_POST['stageID'] 
    . "', timestamp = " . $datetime
    . ", headline = '" . myaddslashes($_POST['headline']) 
    . "', body = '" . myaddslashes($_POST['body'])
    . "' WHERE ID = '$ID'";
  $result = mysql_query($query, $db);
}
else
{
  $query = "INSERT INTO ticks (stageID, timestamp, headline, body) VALUES ('" 
    . $_POST['stageID'] . "',"
    . $datetime . ",'" 
    . myaddslashes($_POST['headline']) . "','" 
    . myaddslashes($_POST['body']) . "')";
  $result = mysql_query($query, $db) or mysql_error($result);
}

staticify($_POST['stageID']);

header("Location: /tick/ticks.php");

?>
