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

$userID = 0;
if (isset($_COOKIE['userID'])) { $userID = $_COOKIE['userID']; }
else { header("Location: /tick/login.html"); }

$default_stageID = 0;
if (isset($_COOKIE['stageID'])) { $default_stageID = $_COOKIE['stageID']; }

$ID = 0;
$date = ''; $year = 0; $month = 0; $day = 0;
$name = ''; $status = 'new'; $refresh_type = 'meta';
$inc_pics = 'yes'; $inc_video = 'no'; $inc_map = 'yes';
$start_location = ''; $end_location = ''; $distance = '';
$refresh = ''; $extra_html = '';
$tick_sequence = ''; $time_offset = ''; $mobile_notes = ''; $notes = '';

if (isset($_GET['ID']))
{	
    $ID = $_GET["ID"];
    $query = "SELECT * FROM stages WHERE ID = $ID";
    $result = mysql_query($query, $db);
    if ($result)
    {
	while ($r = mysql_fetch_array($result))
	{
	    $status = $r['status'];
	    $inc_pics = $r['inc_pics'];
	    $inc_video = $r['inc_video'];
	    $inc_map = $r['inc_map'];
	    $date = $r['date'];
	    list($year, $month, $day) = split("-", $date);
	    $name = $r['name'];
	    $start_location = $r['start_location'];
	    $end_location = $r['end_location'];
	    $distance = $r['distance'];
	    $refresh = $r['refresh'];
	    $tick_sequence = $r['tick_sequence'];
	    $refresh_type = $r['refresh_type'];
	    $time_offset = $r['time_offset'];
	    $mobile_notes = $r['mobile_notes'];
	    $extra_html = $r['extra_html'];
	    $notes = $r['notes'];
	    $static_url = $r['static_url'];
	}
    }
}

$query = "SELECT *, DATE_FORMAT(events.date_start, '%b %e, %Y') AS datef FROM events WHERE default_flag = 'yes' AND userID = $userID";
$result = mysql_query($query, $db);

if ($result)
{
    while ($r = mysql_fetch_array($result))
    {
	$eventID = $r['ID'];
	$event_name = $r['name'];
	$event_date = $r['datef'];
    }
}
mysql_free_result($result);

?>

<html>
<head>
<title>OneTicker :: Stages</title>
<link rel=stylesheet type="text/css" href="tick.css">
<script src="banner.js" type="text/javascript"></script>
</head>

<body bgcolor=#000000 marginheight=4 marginwidth=15 leftmargin=15 topmargin=4>

<script type="text/javascript">banner(2);</script>

<form name="stages" enctype="multipart/form-data" action="stages_add.php" method="post">
<input type="hidden" name="eventID" value="<? echo $eventID ?>">
<input type="hidden" name="ID" value="<? echo $ID ?>">
<input type="hidden" name="tick_sequence" value="">

<?

if ($default_stageID == 0)
{
  echo "<div class=redtext><img src=\"/tick/images/clear.gif\" border=0 height=2 width=140><b>Please select an active stage.</b><p></div>";
}

?>

<table border=0 cellspacing=1 cellpadding=1 width="640">
<tr>
<td colspan=2><div class=text><span class=smtext>name</span>
<br><input class=blacktext type=text name="name" value="<? echo $name; ?>" size=50 maxlength=175></td>

<td><div class=text><span class=smtext>distance</span>
<br><input class=blacktext type=text name="distance" value="<? echo "$distance"; ?>" size=28 maxlength=100></td>
<td><div class=text><span class=smtext>status</span>
<br><select name=status class=blacktext>
<option value="new"<? if ($status == 'new') { echo " selected"; } ?>>new</option>
<option value="ongoing"<? if ($status == 'ongoing') { echo " selected"; } ?>>ongoing</option>
<option value="complete"<? if ($status == 'complete') { echo " selected"; } ?>>complete</option>
<option value="irrelevant"<? if ($status == 'irrelevant') { echo " selected"; } ?>>irrelevant</option>
</select>
</div></td>
</tr>

<tr>
<td><div class=text><span class=smtext>start location</span>
<br><input class=blacktext type=text name="start_location" value="<? echo $start_location; ?>" size=23 maxlength=150></td>

<td><div class=text><span class=smtext>end location</span>
<br><input class=blacktext type=text name="end_location" value="<? echo $end_location; ?>" size=23 maxlength=150></td>

<td><div class=text><span class=smtext>page refresh</span>
<br><select name=refresh class=blacktext>
<option value="0"<? if ($refresh == 0) { echo " selected"; } ?>>never</option>
<option value="60"<? if ($refresh == 60) { echo " selected"; } ?>>every minute</option>
<option value="120"<? if ($refresh == 120) { echo " selected"; } ?>>every 2 minutes</option>
<option value="180"<? if ($refresh == 180) { echo " selected"; } ?>>every 3 minutes</option>
<option value="240"<? if ($refresh == 240) { echo " selected"; } ?>>every 4 minutes</option>
<option value="300"<? if ($refresh == 300) { echo " selected"; } ?>>every 5 minutes</option>
<option value="480"<? if ($refresh == 480) { echo " selected"; } ?>>every 8 minutes</option>
<option value="600"<? if ($refresh == 600) { echo " selected"; } ?>>every 10 minutes</option>
<option value="1200"<? if ($refresh == 1200) { echo " selected"; } ?>>every 20 minutes</option>
<option value="1800"<? if ($refresh == 1800) { echo " selected"; } ?>>every 30 minutes</option>
<option value="3600"<? if ($refresh == 3600) { echo " selected"; } ?>>every hour</option>
</select>
</div></td>

<td><div class=text><span class=smtext>refresh method</span>
<br><select name="refresh_type" class=blacktext>
<option value="meta"<? if ($refresh_type == 'meta') { echo " selected"; } ?>>not controllable</option>
<option value="javascript"<? if ($refresh_type == 'javascript') { echo " selected"; } ?>>user controlled</option>
</select>
</div></td>

<!--
<td><div class=text><span class=smtext>tick sequence</span>
<br><select name=tick_sequence class=blacktext>
<option value=""<? if ($tick_sequence == '') { echo " selected"; } ?>>most recent last</option>
<option value=" DESC"<? if ($tick_sequence == ' DESC') { echo " selected"; } ?>>most recent first</option>
</select>
</div></td>
-->

</tr>

<tr valign=top>
<td colspan=2><table cellpadding=0 cellspacing=0 border=0 width="100%">
<tr valign=top>
<td><div class=text><span class=smtext>date</span>
<br><select class=blacktext name=month>
<?
while(list($key, $value) = each($month_names))
{
    $selected = "";
    $month_index = $key + 1;
    if ($month_index == $month) { $selected = " selected"; }
    echo "<option value=\"${month_index}\"$selected>${value}</option>";
}
?>
</select>&nbsp;
<select class=blacktext name=day>
<? 
for ($i=1; $i<32;$i++)
{
    $selected = "";
    if ($i == $day) { $selected = " selected"; }
    echo "<option value=\"$i\"$selected>$i</option>";
}
?>
</select>&nbsp;
<select class=blacktext name=year>
<?
for ($i=2009; $i<2015;$i++)
{
    $selected = "";
    if ($i == $year) { $selected = " selected"; }
    echo "<option value=\"$i\"$selected>$i</option>";
}
?>
</select>
</div></td>


<td><div class=text><span class=smtext>PST offset</span>
<br><select class=blacktext name=time_offset>
<? 
for ($i=-23; $i<24;$i++)
{
    $selected = "";
    if ($i == $time_offset) { $selected = " selected"; }
    if ($i > 0) { $i = "+$i"; }
    echo "<option value=\"$i\"$selected>$i hours</option>";
}
?>
</select>
</div></td>
</tr>

<tr>
<td colspan=2><div class=text>
<br>include:&nbsp;&nbsp;
<? $checked = ""; if ($inc_pics == 'yes') { $checked = " checked"; } ?>
<input type=checkbox name=inc_pics value=yes<? echo "$checked"; ?> class=blacktext>pics

<? $checked = ""; if ($inc_video == 'yes') { $checked = " checked"; } ?>
<input type=checkbox name=inc_video value=yes<? echo "$checked"; ?> class=blacktext>video

<? $checked = ""; if ($inc_map == 'yes') { $checked = " checked"; } ?>
<input type=checkbox name=inc_map value=yes<? echo "$checked"; ?> class=blacktext>map
</div></td>
</tr>
</table></td>

<td colspan=2><div class=text><span class=smtext>profile image</span>
<br><input class=blacktext type=file name="profile" size=40>
<ul>
<li>image will be resized automatically</li>
<li>leave blank to keep existing image</li>
</ul>
</div></td>
</tr>

<? /** tr>
<td colspan=2><div class=blacktext><span class=smtext>comments (viewer)</span>
<br><textarea class=blacktext name="notes" cols=46 rows=3 wrap="virtual"><? echo $notes; ?></textarea>
</div></td>
<td colspan=2><div class=blacktext><span class=smtext>comments (mobile viewer)</span>
<br><textarea class=blacktext name="mobile_notes" cols=46 rows=3 wrap="virtual"><? echo $mobile_notes; ?></textarea>
</div></td>
</tr **/ ?>

<tr>
<td colspan=2><div class=text><span class=smtext>static url</span>
<br><input class=blacktext type=text name="static_url" value="<? echo $static_url; ?>" size=23 maxlength=150>
</div></td>
<td colspan=2><div class=blacktext>&nbsp;
</div></td>
</tr>

<tr>
<td colspan=4><div class=blacktext><span class=smtext>extra footer (html)</span>
<br><textarea class=blacktext name="extra_html" cols=100 rows=4 wrap="virtual"><? echo $extra_html; ?></textarea>
</div></td>
</tr>

<tr>
<td colspan=4 align=center>
<input class=boldtext type="submit" name="submit" value="add/update stage"></td>
</tr>
</table>
</form>

<p>
<span class=smtext>Click date to edit stage info.<br />
Click check-mark to activate a given stage. (Red check indicates active stage.)</span>
<table border=0 cellpadding=0 cellspacing=0 bgcolor="#000000" width="700">
<tr><td>
<table border=0 cellpadding=3 cellspacing=1 bgcolor="#000000" width=100%>

<tr valign=top>
<td bgcolor=#cccccc align=center width="15%"><div class=blacktext><b>DATE</b></div></td>
<td bgcolor=#cccccc align=center><div class=blacktext><b>NAME</b></div></td>
<td bgcolor=#cccccc align=center width="35%"><div class=blacktext><b>ROUTE</b></div></td>
<td bgcolor=#cccccc align=center width="9%"><div class=blacktext><b>STATUS</b></div></td>
<td bgcolor=#cccccc align=center width="2%"><div class=blacktext>ACTIVE</div></td>
</tr>

<?php

$query = "SELECT *, DATE_FORMAT(stages.date, '%b %e, %Y') AS datef FROM stages WHERE eventID = $eventID ORDER BY date DESC";
$result = mysql_query($query, $db);

if ($result)
{
    while ($r = mysql_fetch_array($result))
    {
	$stageID = $r['ID'];
	$status = $r['status'];
	$name = $r['name'];
	$date = $r['datef'];
	$start_location = $r['start_location'];
	$end_location = $r['end_location'];
	$default_flag = $r['default_flag'];
	$distance = $r['distance'];
	$time_offset = $r['time_offset'];
	$static_url = $r['static_url'];

	if ($stageID == $ID) { echo "<tr bgcolor=#ffaaaa>"; }
	else { echo "<tr bgcolor=#ffffff>"; }
	echo "<td><div class=blacktext>&nbsp;<a href=\"stages.php?ID=${stageID}\">$date</a></div></td>";
	echo "<td><div class=blacktext>$name";
	echo "<br>[<a target=_blank href=\"viewer.php?ID=${stageID}\">viewer</a>]";
	echo "&nbsp;[<a target=_blank href=\"$static_base${static_url}.html\">static viewer</a>]";
	echo "</div></td>";
	echo "<td><div class=blacktext>${start_location}&nbsp;to&nbsp;${end_location}<br>${distance}<br>&nbsp;- offset ${time_offset} hours from PST</div></td>";
	echo "<td align=center><div class=blacktext>${status}</div></td>";
	//echo "<td><a href=\"javascript:popup('stages_remove.php?ID=${stageID}', 'stage');\"><img src=\"images/x.gif\" border=0></a>&nbsp;";
	echo "<td align=center>&nbsp;";

	if ($stageID == $default_stageID)
	{
	    echo "<img src=\"images/checkmark.gif\" border=0>";
	}
	else
	{
	    echo "<a href=\"stages_set_default.php?ID=${stageID}\"><img src=\"images/checkmark_black.gif\" border=0 alt=\"make default\"></a>";
	}
	echo "</td>";
	echo "</tr>";
    }
}

?>

<tr valign=top>
<td bgcolor=#aaaaaa colspan=5><div class=blacktext>&nbsp;
</div></td>
</tr>

</table></td></tr>
</table>

</body>

</html>
