/*
07-Jan-05
*/

function CalcVelocity ( poForm ) {
	var nImgPix = parseInt(poForm.imgpix.value);	// Width of image in pixels.
	var nImgDeg = parseFloat(poForm.imgdeg.value);	// Field of view in degrees.
	var nObjAlt = parseInt(poForm.objalt.value);	// Altitude in feet.
	var nObjPix = parseInt(poForm.objpix.value);	// Delta position in pixels.
	var nObjTim = parseFloat(poForm.objtim.value);	// Delta time in seconds.

	// Convert meters to feet if necessary.
	if (poForm.objunits[1].checked)
		nObjAlt = nObjAlt * 3.2808399;

	var nTan = Math.round( Math.tan( ( (nImgDeg / 2) * Math.PI ) / 180 ) * 1000000000 ) / 1000000000;
	var nFOV = Math.round( (nTan * nObjAlt) * 1 ) / 1;
	var nFPP = Math.round( (nFOV / nImgPix) * 10000 ) / 10000;
	var nDis = Math.round( (nObjPix * nFPP) * 10 ) / 10;
	var nVel = Math.round( (nDis / nObjTim) * 1000 ) / 1000;
	var nVelMPH = Math.round( ((nVel * 60 * 60) / 5280) * 1 ) / 1;
	var nVelNPH = Math.round( (nVelMPH * 0.868421) * 1 ) / 1;
					     0.86899276
	var nVelKPH = Math.round( (nVelMPH * 1.60936357) * 1 ) / 1;
	var nVelMach = Math.round( (nVel / ComputeSpeedOfSoundAtAltitude(nObjAlt)) * 1000 ) / 1000;

	// If you choose not to show the detailed results,
	// you can comment out the next three lines. 
	if (poForm.resfov) { poForm.resfov.value = nFOV; }
	if (poForm.resfpp) { poForm.resfpp.value = nFPP; }
	if (poForm.resdis) { poForm.resdis.value = nDis; }

	// Always display the final results.
	poForm.resvelfps.value = nVel;
	poForm.resvelmph.value = nVelMPH;
	poForm.resvelnph.value = nVelNPH;
	poForm.resvelkph.value = nVelKPH;
	poForm.resmach.value = nVelMach;
}
function ComputeSpeedOfSoundAtAltitude ( pnAlt ) {
	var h = pnAlt;
	var TEMPSL = 518.67;
	var saTheta = 1.0
	if ( h<232940 ) { saTheta = 1.434843 - h/337634; }
	if ( h<167323 ) { saTheta = 0.939268; }
	if ( h<154199 ) { saTheta = 0.482561 + h/337634; }
	if ( h<104987 ) { saTheta = 0.682457 + h/945374; }
	if ( h<65617 ) { saTheta = 0.751865; }
	if ( h<36089 ) { saTheta = 1.0 - h/145442; }
	var tempVal = TEMPSL * saTheta;
	var soundVal = Math.sqrt( 1.4*1716.56*(tempVal) );
	return soundVal;
}
