<?PHP

if (!$_POST){ header('Location: https://slowtwitch.com/calendar/'); }

include("config.php");

  $name = trim($_POST[name]);
  $nametag = strtolower(str_replace(" ","", $name));
  if(strlen($nametag) > 12){ //Shorten it
    $nametag = substr($nametag, 0, 12);
  }
  $state = $_POST[state];
  $statetag = strtolower(str_replace(" ","", $state));
  $type = $_POST[type];
  $indivfee = trim($_POST[indivfee]);
  $teamfee = trim($_POST[teamfee]);
  $swim = $_POST[swim];
  $oneunit = $_POST[oneunit];
  $twounit = $_POST[twounit];
  $threeunit = $_POST[threeunit];
  $onetype = $_POST[onetype];
  $twotype = $_POST[twotype];
  $threetype = $_POST[threetype];
  $bike = $_POST[bike];
 if ($_POST[run] != "") { $run = $_POST[run]; } else { $run = 0; }
  $city = trim($_POST[city]);
  $address = trim($_POST[address]);
  $phone = trim($_POST[phone]);
  $email = trim($_POST[email]);
  $courseinfo = nl2br(substr(trim($_POST[courseinfo]), 0, 4096));
  $moreinfo = nl2br(substr(trim($_POST[moreinfo]), 0, 4096));
  $directions = nl2br(substr(trim($_POST[directions]), 0, 4096));
  $register = trim($_POST[register]);
  $website = trim($_POST[website]);
  $results = trim($_POST[results]);
  $sixresults = trim($_POST[sixresults]);
  $hour = $_POST[hour];
  //if($_POST[daypart] == 1){ $hour = $hour + 12; } 
  $minute = $_POST[minute];
  $year = $_POST[year];
  $month = $_POST[month];
  $day = $_POST[day];
  $date = "$year-$month-$day $hour:$minute:00";
  $uid = $_POST[uid];
  $submitted_by = $_POST[submitted_by];
  
   $one_points = 0;
	  $two_points = 0;
	  $three_points = 0;
	
 /** $swim_points = array(1 => 17.6, 2 => 0.011, 3 => 0.01, 4 => 11);
	  $bike_points = array(1 => 1, 2 => 0.00062, 3 => 0.00057, 4=> .62);
	  $run_points = array(1 => 4, 2 => 0.0025, 3 => 0.0023, 4 => 2.5);
	  $paddle_points = array(1 => 2.6667, 2 => 0.00167, 3 => 0.00153, 4 => 1.6667);
	  $ski_points = array(1 => 2.75, 2 => 0.00172, 3 => 0.00158, 4 => 1.72);
	  $mtb_points = array(1 => 2, 2 => 0.00124, 3 => 0.00114, 4=> 1.24);
	  points = array(1 => $swim_points, 2=> $bike_points, 3 => $run_points, 4 => $paddle_points, 5 => $ski_points); **/

	
	if ($onetype == 1) {
	  if ($oneunit == 1) {
		$one_points = $swim*17.6;
	  } elseif ($oneunit == 2) {
		$one_points = $swim*0.011;
	  } elseif ($oneunit == 3) {
		$one_points = $swim*0.01;
	  } elseif ($oneunit == 4) {
		$one_points = $swim*11;
	  }
	} elseif ($onetype == 2) {
	  if ($oneunit == 1) {
		$one_points = $swim*1;
	  } elseif ($oneunit == 2) {
		$one_points = $swim*0.00062;
	  } elseif ($oneunit == 3) {
		$one_points = $swim*0.00057;
	  } elseif ($oneunit == 4) {
		$one_points = $swim*0.62;
	  }
	} elseif ($onetype == 3) {
		if ($oneunit == 1) {
		$one_points = $swim*4;
	  } elseif ($oneunit == 2) {
		$one_points = $swim*0.0025;
	  } elseif ($oneunit == 3) {
		$one_points = $swim*0.0023;
	  } elseif ($oneunit == 4) {
		$one_points = $swim*2.5;
	  }
	} elseif ($onetype == 4) {
		if ($oneunit == 1) {
		$one_points = $swim*2.67;
	  } elseif ($oneunit == 2) {
		$one_points = $swim*0.00167;
	  } elseif ($oneunit == 3) {
		$one_points = $swim*0.00153;
	  } elseif ($oneunit == 4) {
		$one_points = $swim*1.67;
	  }
	} elseif ($onetype == 5) {
		if ($oneunit == 1) {
		$one_points = $swim*2.75;
	  } elseif ($oneunit == 2) {
		$one_points = $swim*0.00172;
	  } elseif ($oneunit == 3) {
		$one_points = $swim*0.00158;
	  } elseif ($oneunit == 4) {
		$one_points = $swim*1.72;
	  }
	}
	
	
	if ($twotype == 1) {
	  if ($twounit == 1) {
		$two_points = $bike*17.6;
	  } elseif ($twounit == 2) {
		$two_points = $bike*0.011;
	  } elseif ($twounit == 3) {
		$two_points = $bike*0.01;
	  } elseif ($twounit == 4) {
		$two_points = $bike*11;
	  }
	} elseif ($twotype == 2) {
	  if ($twounit == 1) {
		$two_points = $bike*1;
	  } elseif ($twounit == 2) {
		$two_points = $bike*0.00062;
	  } elseif ($twounit == 3) {
		$two_points = $bike*0.00057;
	  } elseif ($twounit == 4) {
		$two_points = $bike*0.62;
	  }
	} elseif ($twotype == 3) {
		if ($twounit == 1) {
		$two_points = $bike*4;
	  } elseif ($twounit == 2) {
		$two_points = $bike*0.0025;
	  } elseif ($twounit == 3) {
		$two_points = $bike*0.0023;
	  } elseif ($twounit == 4) {
		$two_points = $bike*2.5;
	  }
	} elseif ($twotype == 4) {
		if ($twounit == 1) {
		$two_points = $swim*2.67;
	  } elseif ($twounit == 2) {
		$two_points = $swim*0.00167;
	  } elseif ($twounit == 3) {
		$two_points = $swim*0.00153;
	  } elseif ($twounit == 4) {
		$two_points = $swim*1.67;
	  }
	} elseif ($twotype == 5) {
		if ($twounit == 1) {
		$two_points = $swim*2.75;
	  } elseif ($twounit == 2) {
		$two_points = $swim*0.00172;
	  } elseif ($twounit == 3) {
		$two_points = $swim*0.00158;
	  } elseif ($twounit == 4) {
		$two_points = $swim*1.72;
	  }
	}
	
	if ($threetype == 1) {
	  if ($threeunit == 1) {
		$three_points = $run*17.6;
	  } elseif ($threeunit == 2) {
		$three_points = $run*0.011;
	  } elseif ($threeunit == 3) {
		$three_points = $run*0.01;
	  } elseif ($threeunit == 4) {
		$three_points = $run*11;
	  }
	} elseif ($threetype == 2) {
	  if ($threeunit == 1) {
		$three_points = $run*1;
	  } elseif ($threeunit == 2) {
		$three_points = $run*0.00062;
	  } elseif ($threeunit == 3) {
		$three_points = $run*0.00057;
	  } elseif ($threeunit == 4) {
		$three_points = $run*0.62;
	  }
	} elseif ($threetype == 3) {
		if ($threeunit == 1) {
		$three_points = $run*4;
	  } elseif ($threeunit == 2) {
		$three_points = $run*0.0025;
	  } elseif ($threeunit == 3) {
		$three_points = $run*0.0023;
	  } elseif ($threeunit == 4) {
		$three_points = $run*2.5;
	  }
	} elseif ($threetype == 4) {
		if ($threeunit == 1) {
		$three_points = $swim*2.67;
	  } elseif ($threeunit == 2) {
		$three_points = $swim*0.00167;
	  } elseif ($threeunit == 3) {
		$three_points = $swim*0.00153;
	  } elseif ($threeunit == 4) {
		$three_points = $swim*1.67;
	  }
	} elseif ($threetype == 5) {
		if ($threeunit == 1) {
		$three_points = $swim*2.75;
	  } elseif ($threeunit == 2) {
		$three_points = $swim*0.00172;
	  } elseif ($threeunit == 3) {
		$three_points = $swim*0.00158;
	  } elseif ($threeunit == 4) {
		$three_points = $swim*1.72;
	  }
	}
	
	$points = $one_points + $two_points + three_points;
	
	if ($points < 40) { $pointclass = 1; 
	} elseif ($points >= 40 AND $points < 100) { $pointclass = 2; 
	} elseif ($points >= 100 AND $points < 180) { $pointclass = 3; 
	} else { $pointclass = 4; } 
  
  if(isset($_POST[add_race])){
 $sql = "INSERT INTO gforum_Triathlons(onetype, twotype, threetype, oneunit, twounit, threeunit, racetags, pic1, pic2, pic3, valid, name, nametag, date, type, indivfee, teamfee, swim, bike, run, state, statetag, city, address, phone, email, courseinfo, moreinfo, directions, register, website, results, sixresults, submitted_by, one_points, two_points, three_points, points, pointclass) VALUES('$onetype', '$twotype', '$threetype', '$oneunit', '$twounit', '$threeunit', '0', '$picname1', '$picname2', '$picname3', 0, '$name', '$nametag', '$date', $type, '$indivfee', '$teamfee', $swim, $bike, $run, '$state', '$statetag', '$city', '$address', '$phone', '$email', '$courseinfo', '$moreinfo', '$directions', '$register', '$website', '$results', '$sixresults', '$submitted_by', $one_points, $two_points, $three_points, $points, $pointclass)"; 
	mysql_query($sql);
	//echo($sql);
	//$results = mysql_query("SELECT uid FROM gforum_Triathlons WHERE date = '$date' AND name = '$name' LIMIT 1");
	//$row = mysql_fetch_array($results);
	//header('Location: https://slowtwitch.com/calendar/individual.php?uid='.$row[uid]);
	header('Location: https://slowtwitch.com/calendar/add.php?confirmed=yes');
  } elseif(isset($_POST[save_changes])) {
    $sql = "UPDATE gforum_Triathlons
	        SET onetype = '$onetype', 
			    twotype = '$twotype',
 				threetype = '$threetype',
 				oneunit = '$oneunit',
 				twounit = '$twounit',
 				threeunit = '$threeunit',
 				name = '$name',
 				nametag = '$nametag',
 				date = '$date',
 				type = '$type',
 				indivfee = '$indivfee',
 				teamfee = '$teamfee',
 				swim = $swim,
 				bike = $bike,
 				run = $run,
 				state= '$state',
 				statetag = '$statetag',
 				city = '$city',
 				address = '$address',
 				phone = '$phone',
 				email = '$email',
 				courseinfo = '$courseinfo',
 				moreinfo = '$moreinfo',
 				directions = '$directions',
 				register = '$register',
 				website = '$website',
 				one_points = $one_points,
 				two_points = $two_points,
 				three_points = $three_points,
 				points = $points,
 				pointclass = $pointclass
				WHERE uid = '$uid'";
    mysql_query($sql);
	header('Location: https://slowtwitch.com/calendar/individual.php?uid='.$uid);
  } else {
    echo "no action taken";
  }
?>
