<!--    Angela Slaney 12/08/2003 angela-atSign-activitae.com
//		please do not copy this page without contacting me first. I can then ensure all versions are current and up to date.
//	 	I would prefer for you to point your page links here at activitae.com. Thanks.
//      modified 26 04 2006 added individual magnetic variation facility.


function windVel(theForm, field, fieldId){
windVel.speed=null; // clear any previous vals
windVel.dir=null;
len=eval('theForm.' + field+fieldId).value.length;
tmp = eval('theForm.' + field+fieldId).value.indexOf("/");
if (tmp != 3 || len !=6) {
alert("Enter wind velocity in the form of direction/speed, ie 205/12 or 005/02");
eval('theForm.'+ field+fieldId).value="000/00";
return false;
}
windVel.dir= parseFloat(eval('theForm.' + field + fieldId).value.substring(0,3)); // bug with parseInt????
windVel.speed = parseFloat(eval('theForm.' + field + fieldId).value.substring(4));
if (windVel.dir<0 || windVel.dir>360) {
alert(" Wind direction must be between 1 and 360 degrees"); 
eval('theForm.' + field + fieldId).value="000/00";
return false;
}
if (windVel.speed<0 || windVel.speed>99){
alert(" Wind speed must be between 0 and 99 knots");
eval('theForm.' + field+fieldId).value="000/00"; 
return false;
}
return true;
}
function itemChange(theForm, field, fieldId, lowerLimit, upperLimit){
logic=true;
//set to form start values
windVel.dir=360;
windVel.speed=0;
track.trueTrack=360;
// get TAS and variation
tas = theForm.tas.value; // integerFieldBox
variation= eval('theForm.variation.value'); // integerFieldBox
if (variation=='') alert ('Please set the Magnetic Variation above');
if (tas=='') alert('Please set the True Air Speed above');
//if (variation=='') alert('Please set the Magnetic Variation either globally or for each leg');
// set all windVel fields alike 
for (i=1;i<=8;i++){
rowId=i;
logic=windVel(theForm,'windVel',rowId);
if (logic==false) return;
logic=track(theForm,'trueTrack',rowId);
if (logic==false) return;
magVar=eval('theForm.magVar'+ rowId).value;
distance(theForm,'distance',rowId);
hdg=calcHeading(windVel.dir, windVel.speed, track.trueTrack,tas);
magneticHdg = calcMag(hdg,magVar,theForm, rowId);
magneticHdg=format(magneticHdg);
eval('theForm.'+ 'heading'+ rowId).value=magneticHdg;
groundSpeed=calcGroundSpeed(windVel.dir,windVel.speed,track.trueTrack,tas);
eval('theForm.groundSpeed'+ rowId).value=groundSpeed;
eval('theForm.time'+ rowId).value= time(distance.nm,groundSpeed);
eval('theForm.drift'+rowId).value=drift(hdg,track.trueTrack);
totalDist=distanceTotals(theForm);
theForm.totalDistance.value=totalDist;
totalTm=timeTotals(theForm);
theForm.totalTime.value=totalTm;
if(totalDist!='')
theForm.averageSpeed.value=Math.round(totalDist/(totalTm/60));
}
if (field == 'windVel' && fieldId==1 && logic==true) {
for(w=2;w<=8;w++){
eval('theForm.windVel' + w).value= theForm.windVel1.value; 
}
itemChange(theForm,field,8,0,0); 
}
var fuelc=theForm.fuelcons.value;
fuelConsumption(theForm ,fuelc , totalTm);
}
function timeTotals(theForm){
total=0;

for(l=1;l<=8;l++){
total += parseFloat(eval('theForm.time'+l).value);
}
//total=parseInt(total*100);
total=total*10;
total=Math.round(total);
total=total/10;

//***************************************************************
return(total);
}
function distanceTotals(theForm){
total=0;
for(k=1;k<=8;k++){
total += parseFloat(eval('theForm.distance'+k).value);
}
return(total);
}
function time(distance,groundSpeed){
if(distance==0) return (0); 
if(groundSpeed=='') return (0);
tm=distance/groundSpeed;
// format to minutes
tm= Math.round(tm*600);

tm=tm/10;
return(tm);
}

function calcHeading(wd,ws,tk,tas){
if (tk=="")  return(0);

if (tas=="" || tas==0) return(0);
wd=wd-180;
x=ws*Math.sin(Rad(tk-wd));
gs=Math.sqrt((tas*tas)-(x*x));
hd=Deg(Math.atan(x/gs));
if ((parseFloat(hd)+parseFloat(tk))<0)		
hdg=360+parseFloat(hd)+parseFloat(tk);
else
if ((parseFloat(hd)+parseFloat(tk))>360)		
hdg=parseFloat(hd)+parseFloat(tk)-360;
else
hdg=parseFloat(hd)+parseFloat(tk);
//alert(hdg);
//return(format(Math.round(hdg)));
return(hdg);
}

function format(x){
if (x=="")
return(x);
if (parseFloat(x)<10)
return("00" + parseFloat(x));
if (parseFloat(x)<100)
return("0" + parseFloat(x));
return(parseFloat(x));
}
function calcMag(hdg,variation,theForm,rowId) {
if (hdg=="") return(hdg);
if (variation=="") {variation=parseFloat(eval('theForm.magVar'+rowId).value)};
mag=Math.round(parseFloat(hdg)+parseFloat(variation));
if (mag >360)
return(format(mag-360));
else
if(mag<0)
return(format(mag+360));
else
return(format(mag));
}
function calcGroundSpeed(wd,ws,tk,as){
if (tk=="") return(0);
wd=wd-180;  
gs1=ws*Math.cos(Rad(tk-wd));
x=ws*Math.sin(Rad(tk-wd));
gs2=Math.sqrt((as*as)-(x*x));
return(Math.round(gs1+gs2));

}
function drift(hdg,tk){
if(tk>180 && hdg<179){
dft=Math.abs(Math.round(tk-hdg-360));
dft=dft+' port';
}
else if(hdg>180 && tk<179){
dft=Math.round(tk+(360-hdg));
dft=dft+' stbd';
}
else if(tk<hdg){
dft=Math.abs(Math.round(hdg-tk));
if(dft!=0)
dft=dft+' port';
}
else{
dft=Math.round(tk-hdg);
if(dft!=0)
dft=dft+' stbd';
}
return(dft);
}
function track(theForm, field, fieldId){
len=eval('theForm.' + field+fieldId).value.length;
if (len!=3) {
alert("True Track must be 3 characters long  e.g. 090 degrees");
eval('theForm.' + field+fieldId).value="000";
track.trueTrack=360;
return(false);
}
track.trueTrack= parseFloat(eval('theForm.' + field+fieldId).value.substring(0,3));
if (track.trueTrack > 360 || track.trueTrack < 0){
alert("Track should be between 1 and 360 " );
track.trueTrack=360;
eval('theForm.' + field+fieldId).value="000";
return(false);
}
return(true);
}

function distance(theForm, field, fieldId) {
val= eval('theForm.' + field+fieldId).value;
val= parseInt(val);
if (val > 300 || val < 0) {
alert("Distance must be a mimimum of 1 nM and maximum 300 Nm");
eval('theForm.' + field+fieldId).value="0";
distance.nm=0;
return;
}
distance.nm= val;
return;
}

function Rad(x){
return((Math.PI/180)*x);
}

function Deg(x){
return(x/(Math.PI/180));
}
function newWindow() {
var myWidth = 750;
var myHeight = 480;
var mybars="titlebar=yes,toolbar=yes,location=yes, scrollbars=yes";
try{
newWin = window.open("http://activitae.com/plog/help.htm","helpWindow",mybars);
newWin.resizeTo(myWidth,myHeight);
newWin.moveTo(50,200); 
newWin.focus();  
} catch(errorObject){
   alert("A 'Help Page' has been prevented\r\nfrom loading. It is still available\r\nto you by clicking the help button,\r\nbottom page.");
}
}

function parseMe(frm){
// remove __ from stored cookie and replace with CRLF
frm.textarea.value=frm.textarea.value.split("__").join("\r\n");
return;
}

function fuelConsumption(theForm, fuelc,tm){
fuelc=fuelc/60;
taxi=Math.ceil((fuelc*20));
theForm.taxi.value=taxi;
enroute=Math.ceil((fuelc*tm));
theForm.enroute.value=enroute;
diversion=Math.ceil((fuelc*30));
theForm.diversion.value=diversion;
reserve=Math.ceil((fuelc*45));
theForm.reserve.value=reserve;
theForm.total.value=taxi+enroute+diversion+reserve;
}

function setFrom(theForm, id){
	temp=parseInt(id.substring(2));
	temp=temp+1;
	temp='from'+ temp;
	eval('theForm.' + temp).value=eval('theForm.' + id).value;
}
function magVarChange(theForm,variation){
variation=eval('theForm.variation.value');
for(i=1;i<=8;i++) eval('theForm.magVar'+i).value=variation;
itemChange(theForm,'magVar','1','0','360');
    }
