// Copyright 1998-2007  All Rights Reserved Richard Shelquist,  January 1998 - Sep 2007

// This calculator is the copyrighted intellectual property of Richard Shelquist, Shelquist Engineering.

// This calculator may be freely used for an individual's personal use via my web site,
// or by means of one copy on an individual's own computer for that person's personal use, 
// but may not be copied or republished in any form on any web site, bulletin board or 
// any other means.

// Global Variables for conversions
var in_per_mb = (1/33.86389);
var m_per_ft = 0.3048;

function resetForm()
{
	var inForm=document.inputs;
	var outForm=document.outputs;

	inForm.temperature.value = "";
	inForm.actpress.value = "";
	inForm.actualvp.value = "";
	
	outForm.dynocf.value = "";

	inForm.temperature.focus();

}


function roundNum(Num, Places)
////  Rounding function by Jason Moon
{
   if (Places > 0) {
      if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
         var Rounder = Math.pow(10, Places);
         return Math.round(Num * Rounder) / Rounder;
      }
      else {return Num;}
   }
   else {return Math.round(Num);}
}


function calcDynoCorrection(temp, abspress, vapress)
// Calculate dyno correction given temp(celsius), absolute pressure(inchesHg)  and vapor pressure(inchesHg)
{
	var p1=29.235/(abspress-vapress);
	var p2=Math.pow( ((temp+273)/298), 0.5);
	var p3=(1.18*(p1*p2) - 0.18);
	return(p3);
}


function validatePrompt (ctrl, prompt)
{
	alert (prompt);
	ctrl.focus();
	ctrl.select();
	return;
}



function validateForm ( temp, pressure, vpress)
{
// Validate temperature
	if (temp.value.length===0)
	{
		validatePrompt (temp, "Temperature entry is required.");
		return false;
	}

// Validate pressure
	if (pressure.value.length===0)
	{
		validatePrompt (pressure, "Absolute Pressure entry is required.");
		return false;
	}

// Validate vapor pressure
	if (vpress.value.length===0)
	{
		validatePrompt (pressure, "Vapor Pressure entry is required.");
		return false;
	}

// If all of the inputs are properly validated then return true
	return true;
}	


function calc()
{
	var frm=document.inputs;
	var frmout=document.outputs;

// Validate all four of the required inputs

	if (!validateForm(frm.temperature, frm.actpress, frm.actualvp)) {return;}

// Process the input values

	var temp=1.0*frm.temperature.value;
	var abspress=1.0*frm.actpress.value;	
	var e=1.0*frm.actualvp.value;
	var tc=(5/9)*(temp-32 );

// Calculate the dyno correction factor

	var dynocf=calcDynoCorrection(tc,abspress, e);
	frmout.dynocf.value=roundNum(dynocf,3);

}

