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

$ID = 0;
$stageID = 0;
if (isset($_POST['ID'])) { $ID = $_POST['ID']; }
if (isset($_POST['stageID'])) { $stageID = $_POST['stageID']; }

if ($ID != 0)
{
    $query = "UPDATE maps SET "
	. "zoom = '" . $_POST['zoom'] . "', "
	. "map_type = '" . $_POST['map_type'] . "', "
	. "current_lat = '" . $_POST['current_lat'] . "', "
	. "current_lon = '" . $_POST['current_lon'] . "', "
	. "center_lat = '" . $_POST['center_lat'] . "', "
	. "center_lon = '" . $_POST['center_lon'] . "' "
	. "WHERE ID = '$ID'";
    $result = mysql_query($query, $db);
}
else
{
    $query = "INSERT INTO maps (stageID, zoom, map_type, current_lat, current_lon, center_lat, center_lon) VALUES (" 
	. "'" . $stageID . "',"
	. "'" . $_POST['zoom'] . "',"
	. "'" . $_POST['map_type'] . "',"
	. "'" . $_POST['current_lat'] . "'," 
	. "'" . $_POST['current_lon'] . "'," 
	. "'" . $_POST['center_lat'] . "'," 
	. "'" . $_POST['center_lon'] . "')";
    $result = mysql_query($query, $db);
}

if (isset($_POST['route']) && $_POST['route'] != "")
{
    $query = "DELETE FROM maps_coords WHERE stageID = $stageID";
    $result = mysql_query($query, $db);
    
    $coords = split(":", $_POST['route']);
    for ($i=0; $i < count($coords) - 1; $i++)
    {
	list($lat, $lon) = split(",", $coords[$i]);
	$query = "INSERT INTO maps_coords (stageID, sequence, latitude, longitude) VALUES (" 
	    . "'" . $stageID . "',"
	    . "'" . $i . "',"
	    . "'" . $lat . "'," 
	    . "'" . $lon . "')";
	$result = mysql_query($query, $db);
    }
}

staticify($stageID);
header("Location: /tick/stages.php");

?>
