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

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

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

if ($newStageID != 0)
{
  $query = "DELETE FROM segments WHERE stageID = $stageID";
  $result = mysql_query($query, $db);
  
  $query = "SELECT * from segments WHERE stageID = $newStageID";
  $result = mysql_query($query, $db);
  if ($result)
  {
    while ($r = mysql_fetch_array($result))
    {
      $query2 = "INSERT INTO segments (stageID, name, color, distance_mi, distance_km) VALUES (" 
	. "'" . $stageID . "',"
	. "'" . $r['name'] . "',"
	. "'" . $r['color'] . "',"
	. "'" . $r['distance_mi'] . "'," 
	. "'" . $r['distance_km'] . "')";
      $result2 = mysql_query($query2, $db);

      $ID = mysql_insert_id();
      $segments[$r['ID']] = $ID;
    }
    mysql_free_result($result);
  }

  $query = "DELETE FROM maps_coords WHERE stageID = $stageID";
  $result = mysql_query($query, $db);

  $query = "SELECT * from maps_coords WHERE stageID = $newStageID";
  $result = mysql_query($query, $db);
  if ($result)
  {
    while ($r = mysql_fetch_array($result))
    {
      $query2 = "INSERT INTO maps_coords (stageID, segmentID, sequence, latitude, longitude) VALUES (" 
	. "'" . $stageID . "',"
	. "'" . $segments[$r['segmentID']] . "',"
	. "'" . $r['sequence'] . "',"
	. "'" . $r['latitude'] . "'," 
	. "'" . $r['longitude'] . "')";
      $result2 = mysql_query($query2, $db);
    }
    mysql_free_result($result);
  }
}

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

?>
