<?php
/*
error_reporting(E_ALL);
ini_set('display_errors', '1');
*/

session_start();

require_once 'class/Calculator.php';

// Erase session values
if(isset($_POST['session']) && $_POST['session'] == "kill") {
	$_POST = array();
	$_SESSION['calculator'] = array();
}

// Create Calculator object
$calc = new Calculator();
?>
<!DOCTYPE html>

<html>
<head>
<title>Fiber power calculator</title>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<script type="text/javascript">
	function submitForm() {
		document.calculator.submit();
	}
</script>
</head>

<body>

	<div id="container" style="margin: 30px auto;">

		<form name="calculator" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

			<div class="box">

<!-- ---------------------------------- Power budget ---------------------------------- -->
			
				<table>
					<tr>
						<th colspan="3">Power budget</th>
					</tr>

<!-- ---------------------------------- Tx ---------------------------------- -->
					
					<tr>
						<td class="col1">
							Tx: Optical output power
						</td>

						<td class="col2 left">
							<span class="values">
								 <input class="text"
									type="text" 
									name="txValue"
									value=
"<?php echo $calc->getVal('txValue');?>"
									onBlur="document.calculator.submit();">
							</span> dBm
						</td>

						<td class="col3 right">
							<select name="tx" onChange="submitForm();">
								<option value="">Select a product</option>
	                       		<?php echo $calc->fiberList('tx'); ?>
	                    	</select>
                    	</td>
					</tr>

<!-- ---------------------------------- Rx ---------------------------------- -->
					
					<tr>
						<td class="col1">
							Rx: Optical sensitivity
						</td>

						<td class="col2 left">
							<span class="values">
								 <input class="text"
									type="text" 
									name="rxValue"
									value=
"<?php 
//echo $calc->getVal('rxValue'); 
if ($calc->getVal('txspeed') == "")
{
   $calc->getVal('txspeed') == "3";
}

if ( ($calc->getVal('rxValue') == "-18") &&
     ($calc->getVal('txspeed') == "1.5") )
{
   echo "-20";
}
elseif ( ( ($calc->getVal('rxValue') == "-18") ||
           ($calc->getVal('rxValue') == "-20") ) &&
         ($calc->getVal('txspeed') == "3") )
{
   echo "-18";
}
else
{
   echo $calc->getVal('rxValue');
}
?>" 
									onBlur="document.calculator.submit();">
							</span> dBm
						</td>

						<td class="col3 right">
						<select name="rx" onChange="submitForm();">
							<option value="">Select a product</option>
                        <?php echo $calc->fiberList('rx'); ?>
                    	</select>
                    </td>
					</tr>

<!-- ---------------------------------- Total power budget ---------------------------------- -->
					
					<tr class="total">
						<td class="col1">
							Total power budget
						</td>

						<td class="col2 left">
							<span class="values">

<?php 
if($calc->getVal('txValue') != 0 && $calc->getVal('rxValue') != "") 
{ 
   echo $calc->totalPowerBudget(); 
} 
elseif ($calc->getVal('txValue') == 0 && $calc->getVal('rxValue') != "")
{
   echo $calc->totalPowerBudget();
}
else
{ 
   echo '0';
}
?>
							</span> dB
						</td>
						<td>
    						<span class="km">
    						    <?php if($calc->totalPowerBudget() != 0) { 
    						    	echo $calc->maximumDistance() . ' km maximum distance'; 
    						    } ?> 
    						</span>
						</td>
					</tr>

<?php

if ( ($calc->getVal('rxValue') == "-18") ||
     ($calc->getVal('rxValue') == "-20") )
{
   echo '
<tr>
  <th colspan="3">Transmission speed</th>
</tr>

<tr>
  <td class="col">Transmission speed</td>
  <td colspan="2">
     <input type="radio" value="1.5" name="txspeed" onChange="document.calculator.submit();" ';

   if ($calc->getVal('txspeed')=='1.5')
   {
      echo 'checked';
   }
   else
   {
      echo '';
   }

   echo '>HD/SD-SDI (1.5 Gbps)
     <input type="radio" value="3" name="txspeed" onChange="document.calculator.submit();" ';

   if ($calc->getVal('txspeed')=='3')
   {
      echo 'checked';
   }
   else
   {
      echo '';
   }

   echo '>3G-SDI (3 Gbps)
  </td>
</tr>';

}
?>

				</table>

			</div>

<!-- ---------------------------------- Singlemode Fiber Loss ---------------------------------- -->
			
			<div class="box">
				
				<table>
					<tr>
						<th colspan="3">
							Singlemode fiber loss
						</th>
					</tr> 
					
			<?php if($calc->getVal('fiberType') != "") { ?>	
			
<!-- ---------------------------------- Fiber loss ---------------------------------- -->
			
					<tr>
						<td class="col1">
							<?php echo $calc->getVal('fiberType'); ?>
						</td>
						
						<td class="col2 left">
							<span class="values">
		                    	<?php echo $calc->getVal('fiberLoss'); ?>
		                    </span> dB
	                    </td>
	                    
						<td class="col3 right">
							<span class="kmtotal">
								<input class="text"
									type="text" name="fiber" value="<?php echo $calc->getVal('fiber'); ?>"
									onBlur="document.calculator.submit();"> km
							</span> 
							<input class="text" type="text" name="fiberValue"
								value="<?php echo $calc->getVal('fiberValue'); ?>"
								onBlur="document.calculator.submit();"> dB/km &nbsp;&nbsp;&nbsp;
						</td>
					</tr>
					
			<?php } ?>
      
<!-- ---------------------------------- Insertion loss ---------------------------------- -->
				
					<tr>
						<td class="col1">
							Insertion loss
						</td>
						
						<td class="col2 left">
							<span class="values">
		                   		<?php echo $calc->getVal('insertionLoss'); ?>
		                    </span> dB
	                    </td>
	                    
						<td class="col3 right">
							<span class="kmtotal">
								<input class="text"
									type="text" name="insertion" value="<?php echo $calc->getVal('insertion'); ?>"
									onBlur="document.calculator.submit();"> patch
							</span> 
							<input class="text" type="text" name="insertionValue"
								value="<?php echo $calc->getVal('insertionValue'); ?>"
								onBlur="document.calculator.submit();"> dB/patch	
						</td>
					</tr>

<!-- ---------------------------------- Splice loss ---------------------------------- -->
				
					<tr>
						<td class="col1">
							Splice loss
						</td>
						
						<td class="col2 left">
							<span class="values">
		                    	<?php echo $calc->getVal('spliceLoss'); ?>
		                    </span> dB
	                    </td>

	                    <td class="col3 right">
							<span class="kmtotal">
								<input class="text"
									type="text" name="splice" value="<?php echo $calc->getVal('splice'); ?>"
									onBlur="document.calculator.submit();"> splice
							</span> 
							<input class="text" type="text" name="spliceValue"
								value="<?php echo $calc->getVal('spliceValue'); ?>"
								onBlur="document.calculator.submit();"> dB/splice
						</td>
					</tr>
				</table>

<table>
  <tr>
   <th colspan="3">Dark fiber loss</th>
  </tr>

  <tr>
    <td class="col1">Insertion loss</td>
    <td class="left">
      <span class="values">
        <input class="text" type="text" name="darkinsertionLoss" value="<?php echo $calc->getVal('darkinsertionLoss'); ?>" onBlur="document.calculator.submit();" />
      </span> dB
    </td>
    <td></td>
  </tr>
</table>

<!-- ---------------------------------- Passive Device Attenuation ---------------------------------- -->

				<table style="width: 100%;">
					<tr>
						<th colspan="3">
							Passive device attenuation
						</th>
					</tr>
            
            <?php if($calc->cwdm()) {  echo $calc->cwdm(); } ?>
            
            		<tr>
						<td colspan="3">
						
<!-- ---------------------------------- Splitters ---------------------------------- -->
						
							<table style="width: 100%;">
			            
			            	<?php echo $calc->getSplitters(); ?>
			
			            	</table>
			
							<div id="newsplitters"></div>
				
						</td>
					</tr>

<!-- ---------------------------------- Safety margin ---------------------------------- -->
					<tr>
						<td class="col1">Safety margin</td>

						<td class="left">
							<span class="values">
							<input class="text" type="text" name="safetyLoss"
									value="<?php echo $calc->getVal('safetyLoss'); ?>"
									onBlur="document.calculator.submit();" />
							</span> dB
						</td>

						<td style="width: 335px;">
							&nbsp;
						</td>
					</tr>

<!-- ---------------------------------- Total power loss ---------------------------------- -->

					<tr class="total">
						<td class="col1">
							Total power loss:
						</td>

						<td class="col2 left">
							<span class="values"><?php echo $calc->totalPowerLoss(); ?></span> dB
						</td>

						<td>
							&nbsp;
						</td>
					</tr>
				</table>

			</div>

		</form>
		
<?php if($calc->getVal('txValue') != "" && $calc->getVal('rxValue') != "") { ?>
      
<!-- ---------------------------------- Total remaining power ---------------------------------- -->
      
		<div class="results">
		
			Remaining power: <span class="<?php echo $calc->color(); ?>"><?php echo $calc->remainingPower(); ?> </span> dB

			<div class="remaining_distance">

				<span class="<?php echo $calc->color(); ?>">
					<?php echo $calc->availableDistance(); ?> km 
                    	<?php if($calc->color() == 'neg') { 
							echo 'OVER!'; 
                    	} else { 
                    		echo 'remaining distance available'; 
                    	} ?>
                </span>

			</div>
			
		</div>
		
      <?php } ?>
      
 <!-- ---------------------------------- Reset button ---------------------------------- -->
 
		<div id="kill">
		
			<form name="killsession" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
				<input type="hidden" name="session" value="kill"> 
				<input type="image" name="reset" src="images/reset.gif"
					style="width: 76px; margin-top: 7px;">
			</form>
			
		</div>
		
		<br />

	</div>
	
</body>
</html>