<?php
	require_once "header.php";
	const SIDEBAR_ITEMS = WIND_HEADER_ITEMS;
	const SIDEBAR_LINKS = WIND_HEADER_LINKS;
	require_once "sidebar.php";
	
	for ($i = 0; $i <= 6; $i++) {
		$correction[$i] = explode(';', readDB('Wind Correction Table', 'OTWS.' . (($i+1) * 5)));
	}
?>

<section class="content-wrapper" data-heading="Wind" data-subheading="Derived" id="windDerived">
	<div class="title-box">
		<h1>
			Wind Calculation
		</h1>
		<p>
			Adjustments for wind angle and speed calculations, as target and wind speed varies.
		</p>
	</div>
	
	<div class="middle-wrapper box box-top">
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading">
					<h2>Boat Speed</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="boatSpeed" data-field="<?php echo fieldEnum::Calc_BSP_kts;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>CAWA</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="CAWA1" data-field="<?php echo fieldEnum::Calc_CAWA_Degrees;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>OTWA</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="OTWA1" data-field="<?php echo fieldEnum::Calc_ETWA;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
		
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading">
					<h2>Leeway</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="leeway1" data-field="<?php echo fieldEnum::Calc_Sgn_Leeway;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>CAWS</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="CAWS1" data-field="<?php echo fieldEnum::Calc_CAWS_kts;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>OTWS</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="OTWS1" data-field="<?php echo fieldEnum::Calc_ETWS;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
	</div>
	
	<div class="middle-wrapper box box-stack">
		<fieldset>
			<legend><h1>Corrections</h1></legend>
			<table id="wind-table">
				<tr>
					<th>OTWS</th>
					<th></th>
					<th>&le;5kt</th>
					<input hidden type="text" class="database-input" data-key="OTWS.5"
						data-section="Wind Correction Table" id="corrections0"
						value="<?php echo readDB("Wind Correction Table", "OTWS.5")?>">
					<th>10kt</th>
					<input hidden type="text" class="database-input" data-key="OTWS.10"
						data-section="Wind Correction Table" id="corrections1"
						value="<?php echo readDB("Wind Correction Table", "OTWS.10")?>">
					<th>15kt</th>
					<input hidden type="text" class="database-input" data-key="OTWS.15"
						data-section="Wind Correction Table" id="corrections2"
						value="<?php echo readDB("Wind Correction Table", "OTWS.15")?>">
					<th>20kt</th>
					<input hidden type="text" class="database-input" data-key="OTWS.20"
						data-section="Wind Correction Table" id="corrections3"
						value="<?php echo readDB("Wind Correction Table", "OTWS.20")?>">
					<th>25kt</th>
					<input hidden type="text" class="database-input" data-key="OTWS.25"
						data-section="Wind Correction Table" id="corrections4"
						value="<?php echo readDB("Wind Correction Table", "OTWS.25")?>">
					<th>30kt</th>
					<input hidden type="text" class="database-input" data-key="OTWS.30"
						data-section="Wind Correction Table" id="corrections5"
						value="<?php echo readDB("Wind Correction Table", "OTWS.30")?>">
					<th>&ge;35kt</th>
					<input hidden type="text" class="database-input" data-key="OTWS.35"
						data-section="Wind Correction Table" id="corrections6"
						value="<?php echo readDB("Wind Correction Table", "OTWS.35")?>">
				</tr>
				<tr name="angle" id="upwind">
					<td><h2>Upwind</h2></td>
					<td>OTWA</td>
					<td>
						<input type="number" value="<?php echo $correction[0][0]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[1][0]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[2][0]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[3][0]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[4][0]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[5][0]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[6][0]?>">
					</td>
				</tr>
				<tr name="correction">
					<td></td>
					<td>TWA Correction</td>
					<td>
						<input id="test1" type="number" value="<?php echo $correction[0][1]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[1][1]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[2][1]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[3][1]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[4][1]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[5][1]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[6][1]?>" step=0.1>
					</td>
				</tr>
				<tr>
					<td><h2>Reaching</h2></td>
					<td>OTWA</td>
					<td>
						<input disabled type="number" value="90">
					</td>
					<td>
						<input disabled type="number" value="90">
					</td>
					<td>
						<input disabled type="number" value="90">
					</td>
					<td>
						<input disabled type="number" value="90">
					</td>
					<td>
						<input disabled type="number" value="90">
					</td>
					<td>
						<input disabled type="number" value="90">
					</td>
					<td>
						<input disabled type="number" value="90">
					</td>
				</tr>
				<tr name="correction">
					<td></td>
					<td>TWA Correction</td>
					<td>
						<input type="number" value="<?php echo $correction[0][3]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[1][3]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[2][3]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[3][3]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[4][3]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[5][3]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[6][3]?>" step=0.1>
					</td>
				</tr>
				<tr name="angle" id="downwind">
					<td><h2>Downwind</h2></td>
					<td>OTWA</td>
					<td>
						<input type="number" value="<?php echo $correction[0][4]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[1][4]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[2][4]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[3][4]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[4][4]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[5][4]?>">
					</td>
					<td>
						<input type="number" value="<?php echo $correction[6][4]?>">
					</td>
				</tr>
				<tr name="correction">
					<td></td>
					<td>TWA Correction</td>
					<td>
						<input type="number" value="<?php echo $correction[0][5]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[1][5]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[2][5]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[3][5]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[4][5]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[5][5]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[6][5]?>" step=0.1>
					</td>
				</tr>
				<tr name="percent-correction">
					<td></td>
					<td>%TWS Correction</td>
					<td>
						<input type="number" value="<?php echo $correction[0][6]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[1][6]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[2][6]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[3][6]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[4][6]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[5][6]?>" step=0.1>
					</td>
					<td>
						<input type="number" value="<?php echo $correction[6][6]?>" step=0.1>
					</td>
				</tr>
			</table>
		</fieldset>
	</div>
	
	<div class="middle-wrapper box box-bottom">
		<div class="flex-row">
			<div class="flex-heading">
			
			</div>
			<div class="flex-item">
				<a id="ok-button" class="button gray medium submit-button">OK</a>
				<a id="cancel-button" class="button gray medium submit-button">Cancel</a>
				<a id="apply-button" class="button gray medium submit-button">Apply</a>
			</div>
		</div>
	</div>
	
	<div class="top-wrapper bottom-wrapper box">
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading">
					<h2>OTWS</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="OTWS2" data-field="<?php echo fieldEnum::Calc_ETWS;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>TWS Correction</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="TWSCorrection" data-field="<?php echo fieldEnum::Calc_TWS_Correctn;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>TWS</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="TWS1" data-field="<?php echo fieldEnum::Instantaneous_TWWS_Kts;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading">
					<h2>OTWA</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="OTWA2" data-field="<?php echo fieldEnum::Calc_ETWA;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>TWA Correction</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="TWACorrection" data-field="<?php echo fieldEnum::Calc_TWA_Correctn;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>HTWA</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="HTWA" data-field="<?php echo fieldEnum::Calc_Heading_TWA;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading">
					<h2>CAWS</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="CAWS2" data-field="<?php echo fieldEnum::Calc_CAWS_kts;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>AWS Correction</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="AWSCorrection" data-field="<?php echo fieldEnum::Calc_AWS_Correctn;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>AWS</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="AWS" data-field="<?php echo fieldEnum::Calc_AWS_kts;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading">
					<h2>CAWA</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="CAWA2" data-field="<?php echo fieldEnum::Calc_CAWA_Degrees;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>AWA Correction</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="AWACorrection" data-field="<?php echo fieldEnum::Calc_AWA_Correctn;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>AWA</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="AWA" data-field="<?php echo fieldEnum::Calc_AWA_Degrees;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading">
					<h2>Heading</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="headingT" data-field="<?php echo fieldEnum::Calc_Heading_True;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>Leeway</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="leeway2" data-field="<?php echo fieldEnum::Calc_Sgn_Leeway;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>Course</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="courseT" data-field="<?php echo fieldEnum::Calc_Course_Deg_T;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading"></div>
				<div class="flex-field">
					<div class="field-container" id="headingM" data-field="<?php echo fieldEnum::Calc_Heading_Mag;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>VMG</h2>
				</div>
				<div class="field-container" id="VMG" data-field="<?php echo fieldEnum::Calc_VMG_Wind_kts;?>"
					data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
					&nbsp;
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading"></div>
				<div class="flex-field">
					<div class="field-container" id="courseM" data-field="<?php echo fieldEnum::Calc_Course_Deg_M;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading">
					<h2>TWS</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="TWS2" data-field="<?php echo fieldEnum::Instantaneous_TWWS_Kts;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>TWA</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="TWA" data-field="<?php echo fieldEnum::Instantaneous_TWWA_Deg;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading">
					<h2>TWD</h2>
				</div>
				<div class="flex-field">
					<div class="field-container" id="TWDT" data-field="<?php echo fieldEnum::Instantaneous_TWWD_Deg_T;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
				<div class="flex-field">
					<div class="field-container" id="TWDM" data-field="<?php echo fieldEnum::Instantaneous_TWWD_Deg_M;?>"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<script>
$(document).ready(function() {
	
	var pageHandler = new InputPageHandler('windMeasured.php', 'WindData');
		
	pageHandler.validateForm = function() {
		
		var deferred = new $.Deferred();
		
		$('#upwind').find(':input').each(function() {
			if ( !validateRange( $(this), 0, 0, 90, 0 ) ) {
				deferred.resolve(false);
			}
		});
		$('#downwind').find(':input').each(function() {
			if ( !validateRange( $(this), 0, 90, 180, 90 ) ) {
				deferred.resolve(false);
			}
		});
		$('[name="correction"]').find(':input').each(function() {
			if ( !validateRange( $(this), 1, -180, 180, 0 ) ) {
				deferred.resolve(false);
			}
		});
		$('[name="percent-correction"]').find(':input').each(function() {
			if ( !validateNumericInput( $(this), 1 ) ) {
				deferred.resolve(false);
			}
		});
		
		//Clear database-inputs
		$('.database-input').each(function() {
			$(this).val('');
		});
		//And recreate
		$('tr').each(function() {
			var inputString = "";
			$(this).find(':input').each( (function(columnID) {
				//Pull columnID in through function closure
				$input = $('#corrections' + columnID);
				$input.val( $input.val() + $(this).val() + ';');
			}), $(this).index());
		});
		
		deferred.resolve(true);
		
		return deferred.promise();
	}
	
});
</script>

<?php require_once "footer.php"; ?>
