<?php require_once "atp.header.php"; ?>

<section class="content-wrapper" data-heading="wind" id="windDerived">
	<!--Title Container-->
    <div class="page-title">
		<h1>
			Wind Calculations
		</h1>
		<p>
            Adjustments for wind angle and speed calculations as target and wind speed varies.<br>
			If the wind direction is higher on port tack than starboard tack (appears to HEAD tack-to-tack) then ADD
			half the difference to the true wind angle correction. Conversely if the wind direction appears to LIFT then SUBTRACT half the difference.
			This applies equally when tacking upwind, gybing downwind or tacking reach-to-reach.<br>
			(Note: some values are only calculated for the active wind source. If you have multiple sources of wind,
			make sure that the source you are calibrating is currently active.)
        </p>
    </div>

    <!--Live Data Top-->
    <div class="box top-wrapper">
        <!--Wind Angle-->
		<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="boat-speed" data-field="<?php echo fieldEnum::Calc_BSP_kts;?>" data-source="fixed"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Boat speed.">
						&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-source="source"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Calibrated apparent wind angle.">
						&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;?>" title="Uncorrected true wind angle."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="processor">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
		<!--Wind Speed-->
		<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-source="fixed"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Leeway">
						&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;?>" title="Calibrated apparent wind speed."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="source">
						&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;?>" title="Uncorrected true wind speed."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="processor">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
	</div>

    <!--Correction Container-->
	<div id="correction-container">
		<div class="box top-wrapper">
			<fieldset>
				<select id="input-select" hidden></select>
				<legend><h2>Advanced Calibration</h2></legend>
				<!--Correction Table-->
				<table id="wind-table">
					<tr>
						<th>OTWS</th>
                        <th>&nbsp;</th>
						<th>&le;5kt</th>
						<th>10kt</th>
						<th>15kt</th>
						<th>20kt</th>
                        <th>25kt</th>
                        <th>30kt</th>
						<th>&ge;35kt</th>
					</tr>
					<tr>
						<th>Upwind</th>
						<th>OTWA</th>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
					</tr>
					<tr>
						<th></th>
						<th>TWA Correction</th>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
					</tr>
					<tr>
						<th>Reaching</th>
						<th>OTWA</th>
						<td><input type='number' step=0.1 disabled></td>
						<td><input type='number' step=0.1 disabled></td>
						<td><input type='number' step=0.1 disabled></td>
						<td><input type='number' step=0.1 disabled></td>
						<td><input type='number' step=0.1 disabled></td>
						<td><input type='number' step=0.1 disabled></td>
						<td><input type='number' step=0.1 disabled></td>
					</tr>
					<tr>
						<th></th>
						<th>TWA Correction</th>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
					</tr>
					<tr>
						<th></th>
						<th>TWS % Correction</th>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
					</tr>
					<tr>
						<th>Downwind</th>
						<th>OTWA</th>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
					</tr>
					<tr>
						<th></th>
						<th>TWA Correction</th>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
					</tr>
					<tr>
						<th></th>
						<th>TWS % Correction</th>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
						<td><input type='number' step=0.1></td>
					</tr>
				</table>
			</fieldset>
		</div>
	</div>

    <!--Buttons-->
	<div class="button-box" id="button-container">
		<div class="box">
			<a id="ok-button" class="button button-gray button-medium">OK</a>
			<a id="cancel-button" class="button button-gray button-medium">Cancel</a>
			<a id="apply-button" class="button button-gray button-medium">Apply</a>
			<div id="reset-button" class="float-right" hidden>
				<a class="button button-gray button-medium float-right">Reset all to 0%</a>
			</div>
		</div>
	</div>

    <!--Live Data Bottom-->
    <div class="bottom-wrapper box">
        <!--True Wind Speed-->
		<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;?>" title="Uncorrected true wind speed."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="processor">
						&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="tws-correction" data-field="<?php echo fieldEnum::Calc_TWS_Correctn;?>" data-source="processor"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="True wind speed correction.">
						&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::Calc_TWWS_Kts;?>" title="Corrected true wind speed."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="processor">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
        <!--True Wind Angle-->
		<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;?>" title="Uncorrected true wind angle."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="processor">
						&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="twa-correction" data-field="<?php echo fieldEnum::Calc_TWA_Correctn;?>" data-source="processor"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="True wind angle correction.">
						&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-source="processor"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Corrected true wind angle (relative to heading).">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
        <!--Apparent Wind Speed-->
		<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;?>" title="Calibrated apparent wind speed (before correction)."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="source">
						&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="aws-correction" data-field="<?php echo fieldEnum::Calc_AWS_Correctn;?>" data-source="processor"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Apparent wind speed correction.">
						&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;?>" title="Apparent wind speed."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="processor">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
        <!--Apparent Wind Angle-->
		<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-source="source"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Calibrated apparent wind angle (before correction).">
						&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="awa-correction" data-field="<?php echo fieldEnum::Calc_AWA_Correctn;?>" data-source="processor"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Apparent wind angle correction.">
						&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;?>" title="Apparent wind angle."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="processor">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
        <!--Heading/Course True-->
		<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="heading-true" data-field="<?php echo fieldEnum::Calc_Heading_True;?>" data-source="fixed"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Heading (true).">
						&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-source="fixed"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Leeway.">
						&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="course-true" data-field="<?php echo fieldEnum::Calc_Course_Deg_T;?>" data-source="fixed"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Course (true).">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
        <!--Heading/Course Magnetic-->
		<div class="flex-row">
			<div class="flex-third">
				<div class="flex-heading"></div>
				<div class="flex-field">
					<div class="field-container" id="heading-mag" data-field="<?php echo fieldEnum::Calc_Heading_Mag;?>" data-source="fixed"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Heading (magnetic).">
						&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;?>" title="Velocity made good." 
					data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="fixed">
					&nbsp;
				</div>
			</div>
			<div class="flex-third">
				<div class="flex-heading"></div>
				<div class="flex-field">
					<div class="field-container" id="course-mag" data-field="<?php echo fieldEnum::Calc_Course_Deg_M;?>" data-source="fixed"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="Course (magnetic).">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
        <!--True Wind Direction-->
		<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::Calc_TWWS_Kts;?>" title="True wind speed."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="processor">
						&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::Calc_TWWA_Deg;?>" title="True wind angle (relative to course)."
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" data-source="processor">
						&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="twd-true" data-field="<?php echo fieldEnum::Calc_TWWD_Deg_T;?>" data-source="processor"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="True wind direction (true).">
						&nbsp;
					</div>
				</div>
				<div class="flex-field">
					<div class="field-container" id="twd-mag" data-field="<?php echo fieldEnum::Calc_TWWD_Deg_M;?>" data-source="processor"
						data-sender="<?php echo PROCESSOR_ID?>" data-instance="<?php echo PROCESSOR_INSTANCE?>" title="true wind direction (magnetic).">
						&nbsp;
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<script type="text/javascript" src="js/atp.wind-corrections.js?v=<?php echo VERSION_NUMBER?>" defer></script>
<script>
$(document).ready(function()
{
	//Extra check for old database format: Remove when confident no longer needed
	readSection("Wind Correction Table").done(function(result) {
        if (result != '[]') {
            var databaseArray = new Array();
            $.each(JSON.parse(result), function(key, value) {
                databaseArray.push(
                    {section: 'Wind Correction Table:0', key: key, value: value}
                );
            });
            writeToDatabase(databaseArray, "WindData");
            deleteSection("Wind Correction Table").done(function() {
				location.reload(false);
			});
        }
	});

	let model = new WindCorrectionModel();
	let controller = new WindCorrectionController(model);
	let view = new WindCorrectionView(controller)
});
</script>

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