<?php session_start();  ?>
<br/> 
<a href="http://www.whistler.com/">
<img src="http://www.whistler.com/images/common/whistler.gif" border="0" /></a>
<br/>
<?php 


	  if(isset($_POST['calculate']))
	  {
              $calculate = false;  
	      $passengers = $_SESSION['pass'] ;
	  	  $rout = $_SESSION['rout'] ;
		  $count =  $_SESSION['count'];
		 // $origin = $_POST["radioorigin"];
		   for($i = 0; $i <= $count ; $i++ )
		  {
		  	   $destination[$i] = $_POST["radiodestin" . $i];
		  }
		 
	  }
	  else
	  {
	       $calculate = true;  
	      $count = $_POST["destination"] ;
  	      $_SESSION['count'] = $count;
		  $passengers = (int)$_POST["passengers"] ;
		  $flymode = $_POST["FlyMode"];
		  $mode = $_POST["mode"];
		  $_SESSION['mode'] = $mode;
		  $_SESSION['flymode'] = $flymode;
		  $_SESSION['pass'] = $passengers;
		  if( $_POST["Routing"] == "return" )
	  	  {
	        $rout = 2; //return
		    $_SESSION['rout'] = $rout;
			$_SESSION['flight'] = 'round';
	 	  }
		  else 
	 	  {
	        $rout = 1;  //single
	        $_SESSION['rout'] = $rout;
			$_SESSION['flight'] = 'One-way';
		  }
	      
		  for($i = 0; $i <= $count ; $i++ )
		  {
		  	   $destination[$i] = $_POST["destination". $i];
		  }
	  }
	  
	
 
	  if ($passengers == '0')
	  {
	      echo "Passengers entry is not a valid number. Please change the input.<br>";
		  return;
	  }
	  
	  /* Connection to MySQL Database*/
	  $host="mysql103.mysite4now.com";
      $user="external";
      $password="ocns";
	  $database="ocns_calculator";
	  $table="ground";
	  //Connection
	  $cxn = mysqli_connect($host,$user,$password,$database);
	  $multiple = false; 
      if($calculate)
	  {
         				    if(empty($destination[0])) 
		                    {
								echo "<h3> Please enter origin city. </h3>";
								return; //set a flag here to write the error. 
							}
							echo 'Ground transport from <strong>' . $destination[0] . '</strong>'. ' to Whistler ';
							echo '<br/>';
							 
							$query="SELECT * FROM ground WHERE city=\"". $destination[0] ."\"";
	       					$result = mysqli_query($cxn,$query);
							$row = mysqli_fetch_assoc($result);
							echo 'Place of origin:<strong>' . ucwords(strtolower( $row['City'])) .  '</strong> with distance <strong>' 
								  . ucwords(strtolower( $row['Distance'])).  '</strong><br/>';
						
						$ground_dist=$row['Distance'];
						$dist = $ground_dist;
						
						echo 'Mode of ground transport to Whistler: '.'<strong>'. $_SESSION['mode'] . '</strong>';
						echo '<br/>';
						
						$total =  ground_emission($ground_dist) ;
						$cton += round($total/1000,3);
						//add the tonn CO2e/let per car of ferry emission.
						$totaldist += floor($dist);
						$emission  += round($total);
						echo 'Distance: <b>'.floor($dist) . ' </b>kilometers per leg,&nbsp;&nbsp;&nbsp;Emissions per passenger: <b>' . round($total) . ' </b>kilograms of CO2e per leg.<br/><br/>'; 
						         
				    
	   }
		
		
		echo '<hr>';
			echo '<h3> Emission, Distance, and Cost Summary:</h3>';
		echo 'Total trip distance:<b> '  . $totaldist * $rout  . '</b> km/passenger. <br/> ';
		echo 'Total trip emissions:<b> ' .number_format($rout * $cton, 2, '.', '') . '</b> tonne/passenger. <br/> ';
		//$number = $rout * $passengers * $cton * 20 ;
		$number = total_offset_ground($rout,$passengers,$cton);
		echo 'Total offset cost for<b> '  .$passengers  . '</b> passenger(s) climate friendly  
		      is only: <b>$' .number_format($number, 2, '.', '').' </b>(CDN)<br/>';
		
 ?>


<br/>
<a href="index.php" title="New" accesskey="n" target="_self">New calculation</a>
<br/>
<br/>

  <a href="http://www.offsetters.ca">
<img src="http://www.trekescapes.com/offsetting/images/ofsetters-logo.gif" width="250" height="98" border="0" /></a>

<?php
//$rout * $passengers * $cton * 20 (dollar per tonn)
function total_offset_ground($rout,$passengers,$cton) 
{ 
	if($_SESSION['mode'] == "bus")
	{	
	   $offset = $rout * $passengers * $cton * 20 ;
	}
	else
	{
	   $offset = $rout * 1 * $cton * 20 ;
	}
	return $offset;
}

//return ground emission in kgr
function ground_emission($dist) { 
	//use $_SESSION['mode'] for each emiision rateand haul.
		
  if($_SESSION['mode'] == "car")
  {
	$haul = 0.2124;
	echo '<br/>DEBUG: You are using <b> Car/Station Wagon </b> and Emissions for Gasoline Modes
         is <b>' . $haul . '</b>.<br/>';
  }
  if($_SESSION['mode'] == "van")
  {
      $haul = 0.27376; 
	  echo '<br/>DEBUG: You are using <b> Van </b> and Emissions for Gasoline Modes
         is <b>' . $haul . '</b>.<br/>';
  }
  if($_SESSION['mode'] == "sport")
  {
      $haul = 0.30208; 
	   echo '<br/>DEBUG: You are using <b> SUV </b> and Emissions for Gasoline Modes
         is <b>' . $haul . '</b>.<br/>';
  }
  if($_SESSION['mode'] == "truck")
  {
      $haul = 0.32804; 
	    echo '<br/>DEBUG: You are using <b> Truck </b> and Emissions for Gasoline Modes
         is <b>' . $haul . '</b>.<br/>';
  }
  if($_SESSION['mode'] == "hcar")
  {
      $haul = 0.130221; 
	  echo '<br/>DEBUG: You are using <b> Hybrid Car </b> and Emissions for Diesel Modes
 is <b>' . $haul . '</b>.<br/>';
  }
  if($_SESSION['mode'] == "hsuv")
  {
      $haul = 0.204295; 
	  echo '<br/>DEBUG: You are using <b> Hybrid SUV </b> and Emissions for Diesel Modes
 is <b>' . $haul . '</b>.<br/>';
  }
  if($_SESSION['mode'] == "cab")
  {
      $haul = 0.2457; 
	  echo '<br/>DEBUG: You are using <b> Cab </b> and Emissions for Diesel Modes
 is <b>' . $haul . '</b>.<br/>';
  }    
  if($_SESSION['mode'] == "limo")
  {
      $haul = 0.47194875; 
	 echo '<br/>DEBUG: You are using <b> LIMO </b> and Emissions for Diesel Modes
 is <b>' . $haul . '</b>.<br/>';
  }
  if($_SESSION['mode'] == "bus")
  {
      $haul = 0.0565; 
	  echo '<br/>DEBUG: You are using <b> BUS </b> and Emissions for Diesel Modes
 is <b>' . $haul . '</b>.<br/>';
 }
  
   //now check the distance and if it was any of the 6 cities with ferry emission, add it to them.
   $ferry_emission = 0;
   if($dist == 255.44)
   {
		 // Campbell River
	   if($_SESSION['mode'] == "bus")
       {
		    $ferry_emission = 1.8715; 
	   }
	   else
	   {
			$ferry_emission = 27.573; 
	   }
   }
   if($dist == 208.44)
   {
		 //Courtenay
	   $ferry_emission = 27.573; 
   }
   if($dist == 156.44)
   {
		 // Duncan
	   if($_SESSION['mode'] == "bus")
       {
		    $ferry_emission = 1.8715; 
	   }
	   else
	   {
			$ferry_emission = 27.573; 
	   }
   }
   if($dist == 105.44)
   {
		 // Nanaimo
	   if($_SESSION['mode'] == "bus")
       {
		    $ferry_emission = 1.8715; 
	   }
	   else
	   {
			$ferry_emission = 27.573; 
	   }
   }
     if($dist == 184.44)
   {
		 // Port Alberni
	   if($_SESSION['mode'] == "bus")
       {
		    $ferry_emission = 1.8715; 
	   }
	   else
	   {
			$ferry_emission = 27.573; 
	   }
   }
   if($dist == 488.44)
   {
		 // Port Hardy
	   if($_SESSION['mode'] == "bus")
       {
		    $ferry_emission = 1.8715; 
	   }
	   else
	   {
			$ferry_emission = 27.573; 
	   }
   }
   if($dist == 187.55)
   {
		 // Victoria
		 if($_SESSION['mode'] == "bus")
       {
		    $ferry_emission = 1.9266; 
	   }
	   else
	   {
			$ferry_emission = 28.385; 
	   }
	   
   }
    $emission = $dist * $haul; 
     echo '<br/>DEBUG: Emission for ground mode is <b>'. $emission.'</b>kg and Emissions for ferry is <b>' . $ferry_emission . '</b>Kg.<br/>';
   $emission += $ferry_emission; 
   return $emission;
}




function createInput($name, $value, $type, $checked)
{
	return "<input type=\"{$type}\" name=\"{$name}\" value=\"{$value}\" " . ( $checked ? " checked = \"checked\" " : "" ) . "/>" ;
}
?>
 






























