<?php
/* Connection to MySQL Database*/
$host="mysql103.mysite4now.com";
$user="external";
$password="ocns";
$database="ocns_calculator";
$table="airports";

//Connection
mysql_select_db('ocns_calculator', mysql_connect($host, $user, $password));

///////////////////////////////////
// save posted vars to session data
$_SESSION['origin'] = $_POST['origin'];
$_SESSION['passengers'] = $_POST['passengers'];
$_SESSION['routing'] = $_POST['routing'];

///////////////////////////////////////////////////////////////
// break apart the origin data and pull iata info from database
$iata = explode(', ', $_POST['origin']);

$iata = mysql_query("SELECT iata FROM airports WHERE city = '{$iata[0]}' AND country = '{$iata[1]}' LIMIT 1");
$iata = mysql_result($iata, 0, 0);

$_POST['origin'] = $iata;

////////////////////////////////////////////////////////////////
// break apart destination data and pull iata info from database
if(!empty($_POST['destination'][0])) {
	for($i = 0; $i < sizeof($_POST['destination']); $i++) {
		$iata = explode(', ', $_POST['destination'][$i]);
		
		$iata = mysql_query("SELECT iata FROM airports WHERE city = '{$iata[0]}' AND country = '{$iata[1]}' LIMIT 1");
		$iata = mysql_result($iata, 0, 0);
		
		$_POST['destination'][$i] = $iata;
	}
} else {
	$error = '<div style="padding:50px; height:326px;"><h2>Please go to the previous page and make sure you have entered at least one destination.</h2><br /><a href="index.php?calc=air">Back</a></div>';
}

//////////////////////////////////////////////////////////
// check the number of passengers and determine multiplier
if ($_POST['passengers'] == '0') {
	$error = "Passengers entry is not a valid number. Please change the input.<br />";
}

///////////////////////////////
// determine routing multiplier
if ($_POST["routing"] == "return") {
	$route = 2; //return
	$_SESSION['route'] = $route;
	$_SESSION['flight'] = 'round';
} else {
	$route = 1;  //single
	$_SESSION['route'] = $route;
	$_SESSION['flight'] = 'one-way';
}

/////////////////////////////////////////////////////
// grab distance information from database for origin
$query = "SELECT * FROM ".$table." WHERE city=\"". $_POST['origin'] ."\"". " OR iata=\"". $_POST['origin'] ."\"";

$result = mysql_query($query);

if ($result == false) {
	echo "<h4>Error: ".mysql_error($cxn)."</h4>";
	return;
} else {
	$row = mysql_fetch_assoc($result);
	
    $orglat = ($row['latdegree'] + ($row['latmin'] * 1/60) + ($row['latsec'] * 1/60 * 1/60));
    if ($row['latdirection'] == "N") $orglat = $orglat * -1;

	$orglong = $row['longdeg'] + ($row['longmin'] * 1/60) + ($row['longsec'] * 1/60 * 1/60);
	if ($row['longdir'] == "E") $orglong = $orglong * -1;

	$origin = ucwords(strtolower($row['city'])).', '.ucwords(strtolower($row['country']));

	$flight = ucwords(strtolower($row['city'])).' to '.ucwords(strtolower($row['city']));		
}

//////////////////////////////////////////////////////////
// grab distance information from database for destination
for ($i = 0; $i < sizeof($_POST['destination']); $i++) {
	$query = "SELECT * FROM ".$table." WHERE city=\"". $_POST['destination'][$i] ."\"". " OR iata=\"". $_POST['destination'][$i] ."\"";

	$result = mysql_query($query);

	if($result == false) {
		echo "<h4>Error: ".mysql_error()."</h4>";
		return;
	} else {
		$row = mysql_fetch_assoc($result);
		
		$deslat_tmp = ($row['latdegree'] + ($row['latmin'] * 1/60) + ($row['latsec'] * 1/60 * 1/60));
		if ($row['latdirection'] == "N") $deslat_tmp = $deslat_tmp * -1;

		$deslong_tmp = $row['longdeg'] + ($row['longmin'] * 1/60) + ($row['longsec'] * 1/60 * 1/60);
		if ($row['longdir'] == "E") $deslong_tmp = $deslong_tmp * -1;

		$deslat[$i] = $deslat_tmp;
		$deslong[$i] = $deslong_tmp;
		
		$destination[$i] = ucwords(strtolower($row['city'])).', '.ucwords(strtolower($row['country']));
		$flight = ucwords(strtolower($row['city'])).' to '.ucwords(strtolower($row['city']));		
	}
}

/////////////////////////////////////////////////////
// random functions that were thrown in with the html
function distance($lat1, $lon1, $lat2, $lon2, $unit)
{
	$theta = $lon1 - $lon2;

	$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
	$dist = acos($dist);
	$dist = rad2deg($dist);
	$miles = $dist * 60 * 1.1515;
	$unit = strtoupper($unit);

	if ($unit == "K")
	{
		return ($miles * 1.609344);
	}
	elseif ($unit == "N")
	{
		return ($miles * 0.8684);
	}
	else
	{
		return $miles;
	}
}

//return emission in kgr
function emission($dist)
{
	$metric = 2;
	//freight allocation
	$freight = 0.10;
	if ($dist <= 500) $haul = 0.15;
	if ($dist <= 1600 && $dist > 500) $haul = 0.12;
	if ($dist > 1600) $haul = 0.11;

	$temission = $dist * $haul;
	//echo $temission."<br>";
	$falloc = $temission * $freight;
	//echo $falloc."<br>";
	$emission = ($temission - $falloc) * $metric;
	return $emission;
}

//return ground emission in kgr
function ground_emission($dist, $type = false)
{
		if ($_SESSION['mode'] == 'car') $haul = 0.2124;
	elseif ($_SESSION['mode'] == 'van') $haul = 0.27376;
	elseif ($_SESSION['mode'] == 'sport') $haul = 0.30208;
	elseif ($_SESSION['mode'] == 'truck') $haul = 0.32804;
	elseif ($_SESSION['mode'] == 'hcar') $haul = 0.130221;
	elseif ($_SESSION['mode'] == 'hsuv') $haul = 0.204295;
	elseif ($_SESSION['mode'] == 'cab') $haul = 0.2457;
	elseif ($_SESSION['mode'] == 'limo') $haul = 0.47194875;
	elseif ($_SESSION['mode'] == 'bus') $haul = 0.0565;

	//now check the distance and if it was any of the 6 cities with ferry emission, add it to them.

		if (strpos($multi = $dist / 255.44, '.') === false) // Campbell River
	{
		if ($_SESSION['mode'] == 'bus') $ferry_emission = 1.8715 * $multi;
		else $ferry_emission = 27.573 * $multi;

		$ferry_distance = 55.56 * $multi;
	}
	elseif (strpos($multi = $dist / 208.44, '.') === false) // Courtenay
	{
		if ($_SESSION['mode'] == 'bus') $ferry_emission = 1.8715 * $multi;
		else $ferry_emission = 27.573 * $multi;

		$ferry_distance = 55.56 * $multi;
	}
	elseif (strpos($multi = $dist / 156.44, '.') === false) // Duncan
	{
		if ($_SESSION['mode'] == 'bus') $ferry_emission = 1.8715 * $multi;
		else $ferry_emission = 27.573 * $multi;

		$ferry_distance = 55.56 * $multi;
	}
	elseif (strpos($multi = $dist / 105.44, '.') === false) // Nanaimo
	{
		if ($_SESSION['mode'] == 'bus') $ferry_emission = 1.8715 * $multi;
		else $ferry_emission = 27.573 * $multi;

		$ferry_distance = 55.56 * $multi;
	}
	elseif (strpos($multi = $dist / 363, '.') === false) // Tofino
	{
		if ($_SESSION['mode'] == 'bus') $ferry_emission = 1.8715 * $multi;
		else $ferry_emission = 27.573 * $multi;

		$ferry_distance = 55.56 * $multi;
	}
	elseif (strpos($multi = $dist / 184.44, '.') === false) // Port Alberni
	{
		if ($_SESSION['mode'] == 'bus') $ferry_emission = 1.8715 * $multi;
		else $ferry_emission = 27.573 * $multi;

		$ferry_distance = 55.56 * $multi;
	}
	elseif (strpos($multi = $dist / 488.44, '.') === false) // Port Hardy
	{
		if ($_SESSION['mode'] == 'bus') $ferry_emission = 1.8715 * $multi;
		else $ferry_emission = 27.573 * $multi;

		$ferry_distance = 55.56 * $multi;
	}
	elseif (strpos($multi = $dist / 187.55, '.') === false) // Victoria
	{
		if ($_SESSION['mode'] == 'bus') $ferry_emission = 1.9266 * $multi;
		else $ferry_emission = 28.385 * $multi;

		$ferry_distance = 44.448 * $multi;
	}

	if (!$type) return $dist * $haul + $ferry_emission;
	elseif ($type == 'ground') return $dist * $haul;
	elseif ($type == 'ferry') return $ferry_emission;
	elseif ($type == 'ferry_distance') return $ferry_distance;
}
?>