function bereken_snelheid(form) {
  var mn = form.minuten;  
  var sc = form.seconden;  
  var kl = form.kilometers;  
  var hc = form.hectometers;
  var se = form.snelheid;
  var speed;

  speed = ((kl.value*1+hc.value/10)/(mn.value*60+sc.value*1)*3600);
  se.value = Math.round(speed*10)/10;
}

function reset_snelheid(form) {
  form.minuten.value="";
  form.seconden.value="";
  form.kilometers.value="";
  form.hectometers.value="";
  form.snelheid.value="";
}

function bereken_tijd(form) {
  var kil = form.kilometers;
  var hec = form.hectometers;
  var kpu = form.kiloperuur;
  var hpu = form.hectoperuur;
  var tim = form.tijd;
  var tijd;
  var minuten;
  var seconden;

  tijd = (((kil.value*1+hec.value/10)/(kpu.value*1+hpu.value/10))*3600);
  minuten = Math.floor(tijd/60);
  seconden = Math.round(tijd % 60);
  tim.value = minuten+":"+seconden;
}

function reset_tijd(form) {
  form.kiloperuur.value="";
  form.hectoperuur.value="";
  form.kilometers.value="";
  form.hectometers.value="";
  form.tijd.value="";
}

function fcalc(f) {
  var md1, md2;
  var mt1, mt1u, mt1m, mt1s;
  var mt2, mt2u, mt2m, mt2s;

  if (!isNaN(parseInt(f.t1u.value))) {
    mt1u = parseInt(f.t1u.value);
  } else {
    mt1u = 0;
  }

  if (!isNaN(parseInt(f.t1m.value))) {
    mt1m = parseInt(f.t1m.value);
  } else {
    mt1m = 0;
  }

  if (!isNaN(parseInt(f.t1s.value))) {
    mt1s = parseInt(f.t1s.value);
  } else {
    mt1s = 0;
  }

  mt1 = 3600 * mt1u + 60 * mt1m + mt1s;
  md1 = parseInt(f.d1.options[f.d1.selectedIndex].value);
  md2 = parseInt(f.d2.options[f.d2.selectedIndex].value);
  mt2 = mt1 * Math.pow(md2 / md1, 1.06);
  mt2u = Math.floor(mt2 / 3600);
  mt2m = Math.floor((mt2 - (mt2u * 3600)) / 60);
  mt2s = Math.round(mt2 - (mt2u * 3600) - (mt2m * 60));

  if (mt2s > 59) {
    mt2m++;
    mt2s = mt2s - 60;
  }

  if (mt2s < 10) {
    f.t2s.value = '0' + mt2s;
  } else {
    f.t2s.value = mt2s;
  }

  if (mt2s > 59) {
    mt2u++;
    mt2m = mt2m - 60;
  }

  if (mt2m < 10) {
    f.t2m.value = '0' + mt2m;
  } else {
    f.t2m.value = mt2m;
  }

  f.t2u.value = mt2u;

}

var metric; 
var VO2max;

metric = true;

function initGlobals() {
  metric = false; 
  VO2Max = -1; 
}

function  runConversion() {
  
  var time = 0;   // race time in seconds. 
  var rlength = 0;  // race length in meters. 
  var speed;      // speed in meters / min. 
  
  time += document.forms.time.hours.value * 60; 
  time += document.forms.time.minutes.value * 1; 
  time += document.forms.time.seconds.value / 60; 
  
  if(time <= 0 || isNaN(time)) {
    alert("Gelieve een geldige tijd in te vullen."); 
    return; 
  }
  
  rlength += document.forms.leng.number.value * 1; 
  
  if(rlength <= 0 || isNaN(rlength)) { 
    alert("Gelieve een geldige wedstrijdlengte in te vullen (met een punt)."); 
    return;
  }
  
  if(document.forms.leng.units.options[0].selected) rlength *= 1000; 
  else rlength *= 1609; 
  
  speed = rlength / time; 
  
  VO2Max  = velToVO2(speed) / timeToPercentVO2Max(time) ;
  makeCalculations();
}

function makeCalculations () {
    
  if(VO2Max <= 0) return; 
    
  var velEasy   = VO2ToVel(VO2Max * .7); 
  var velTempo  = VO2ToVel(VO2Max * .88);
  var velMaximum  = VO2ToVel(VO2Max);
  var velSpeed  = VO2ToVel(VO2Max * 1.1);
  var velLong   = VO2ToVel(VO2Max * .6);
  var velYasso    = velMaximum * 1.95;  
  
  var toAppend = metric ? " min/km" : " min/mijl" ;  

  var paceEasy    = "" + timeConvert(velEasy)   + toAppend;
  var paceTempo   = "" + timeConvert(velTempo)  + toAppend; 
  var paceMaximum   = "" + timeConvert(velMaximum)  + toAppend;
  var paceSpeed   = "" + timeConvert(velSpeed)  + toAppend;


  var paceLong    =  "" + timeConvert(velEasy) + " - ";
    paceLong    += "" + timeConvert(velLong) + toAppend;

  var oldMetric = metric;
  metric = false;
  var paceYasso   = "" + timeConvert(velYasso)  + " min/800";
  metric = oldMetric; 

  document.forms.output.Easy.value    = paceEasy;
  document.forms.output.Tempo.value   = paceTempo; 
  document.forms.output.Maximum.value   = paceMaximum; 
  document.forms.output.Speed.value   = paceSpeed; 
  document.forms.output.Long.value    = paceLong; 
  document.forms.output.Yasso.value   = paceYasso;      
}
 
function toggleMetric() {
  if(document.forms.controls.paceType.options[0].selected) metric = false;
  else metric = true;
  makeCalculations(); 
}
  

function velToVO2 (vel) {
  return (-4.60 + 0.182258 * vel + 0.000104 * vel * vel);
}
  

function  VO2ToVel (VO2) {
  return (29.54 + 5.000663 * VO2 - 0.007546 * VO2 * VO2);
}

  
function timeToPercentVO2Max (minutes) {
  return (.8 + 0.1894393 * Math.exp(-.012778 * minutes) 
         + 0.2989558 * Math.exp(-.1932695 * minutes)); 
}

  
function timeConvert (speed) {
    var ans; 
    if(!metric) ans = (1 / speed) * 1609;
    else    ans = (1 / speed) * 1000; 
    minutes = Math.floor(ans);
    seconds = Math.floor((ans - minutes) * 60);
    if(seconds > 9) return "" + minutes + ":" + seconds;
    else return "" + minutes + ":0" + seconds;
}

function reken1()
{
d1=document.som1.km1.     value;
u1=document.som1.uren1.   value;
m1=document.som1.minuten1.value;
d2=document.som1.km2.     value;
t1=60*parseFloat(u1)+parseFloat(m1);
temp=parseFloat(d2)/parseFloat(d1);
minuten=Math.pow(temp,1.07)*parseFloat(t1);
u2=Math.floor(minuten/60);
if(u2<10){u0="0"}else{u0=""};
m2=Math.floor(minuten-(u2*60));
if(m2<10){m0="0"}else{m0=""};
document.som1.uren2.value=u0+u2+":"+m0+m2;
}

function reken2()
{
ks2=parseFloat(document.som2.kmsom2 .value);
us2=parseFloat(document.som2.uursom2.value);
ms2=parseFloat(document.som2.minsom2.value);
ss2=parseFloat(document.som2.secsom2.value);
e2=(ks2/(3600*us2+60*ms2+ss2))*3600;
document.som2.v1som2.value=Math.floor(e2*100)/100;

temp2=(3600*us2+60*ms2+ss2)/ks2;
temp2a=Math.floor(temp2/60);
temp2b=Math.floor(temp2-(Math.floor(temp2/60)*60));
document.som2.v2som2.value=temp2a+":"+temp2b;

g2=3600/e2;
hij()
document.som2.k1.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*2;
hij();
document.som2.k2.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*2;
hij();
document.som2.k2.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*3;
hij();
document.som2.k3.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*4;
hij();
document.som2.k4.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*5;
hij();
document.som2.k5.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*6;
hij();
document.som2.k6.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*7;
hij();
document.som2.k7.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*8;
hij();
document.som2.k8.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*9;
hij();
document.som2.k9.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*10;
hij();
document.som2.k10.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*11;
hij();
document.som2.k11.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*12;
hij();
document.som2.k12.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*13;
hij();
document.som2.k13.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*14;
hij();
document.som2.k14.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*15;
hij();
document.som2.k15.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*16;
hij();
document.som2.k16.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*17;
hij();
document.som2.k17.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*18;
hij();
document.som2.k18.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*19;
hij();
document.som2.k19.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*20;
hij();
document.som2.k20.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*21;
hij();
document.som2.k21.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*21.0975;
hij();
document.som2.k211.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*25;
hij();
document.som2.k25.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*30;
hij();
document.som2.k30.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*35;
hij();
document.som2.k35.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*40;
hij();
document.som2.k40.value=h0+h2+":"+i0+i2+":"+j0+j2;
g2=(3600/e2)*42.195;
hij();
document.som2.k42195.value=h0+h2+":"+i0+i2+":"+j0+j2;
}

function hij()
{
h2=Math.floor(g2/3600);
if(h2<10){h0="0"}else{h0=""}
i2=Math.floor((g2-h2*3600)/60);
if(i2<10){i0="0"}else{i0=""};
j2=Math.floor(g2-h2*3600-i2*60);
if(j2<10){j0="0"}else{j0=""};
}

function MaxPulse(){

  var maxpulse = 190.0;

  trainingChoice = document.PredForm.D2.selectedIndex;

  training = document.PredForm.D2.options[trainingChoice].value;

  testedChoice = document.PredForm.D1.selectedIndex;

  tested = document.PredForm.D1.options[testedChoice].value;

  if (parseInt(document.PredForm.T3.value) > 0)

    maxpulse = parseInt(document.PredForm.T3.value) - parseInt(tested);

  else if (parseInt(document.PredForm.T1.value) == 0)

      maxpulse = 190 - 0;

    else if (document.PredForm.R1[0].checked)

         maxpulse = 220 - parseInt(document.PredForm.T1.value) + parseInt(training)

      else    

         maxpulse = 226 - parseInt(document.PredForm.T1.value) + parseInt(training);

  return maxpulse;

}

function RestPulse(){

  trainingChoice = document.PredForm.D2.selectedIndex;

  training = parseInt(document.PredForm.D2.options[trainingChoice].value);

  if (parseInt(document.PredForm.T2.value) > 0)

    return document.PredForm.T2.value

  else if (document.PredForm.R1[0].checked)

      return 65 - training * 2

    else    

      return 75 - training * 2;

}

function Pulse(percent,maxpulse,restpulse) {

  return Math.round(parseInt(restpulse) + (parseInt(maxpulse) - parseInt(restpulse)) * parseInt(percent) / 100)

}

function getresult(){

  restpulse = RestPulse();

  maxpulse = MaxPulse();

  var maxpulse = 190;

  if (MaxPulse() > 0) maxpulse = MaxPulse(); 

  document.PredForm.T4.value = '  Type ....................................... Formule van Karvonen  .................. Rechttoe-rechtaan percentages';

  document.PredForm.T5.value = 'Herstellopen ...................... 60% - 70%  ' + '(' + Pulse(60, maxpulse,restpulse) +

             'bpm - ' + Pulse(70,  maxpulse,restpulse) + 'bpm ' + ') .....' +

             '   65% - 75%  ' + '(' + Math.round(0.65 * maxpulse) + 'bpm - ' +

               Math.round(0.75 * maxpulse) + 'bpm)';

  document.PredForm.T6.value = 'Lange duurlopen .............. 70% - 80%  ' + '(' + Pulse(70,  maxpulse,restpulse) +

             'bpm - ' + Pulse(80,  maxpulse,restpulse) + 'bpm ' + ') .....' + 

             '   75% - 85%  ' + '(' + Math.round(0.75 * maxpulse) + 'bpm - ' +

               Math.round(0.85 * maxpulse) + 'bpm)';

  document.PredForm.T7.value = 'Intensieve duurlopen ...... 80% - 90%  ' +'(' + Pulse(80,  maxpulse,restpulse) +

             'bpm - ' + Pulse(90,  maxpulse,restpulse) + 'bpm '+ ') .....' +

             '   85% - 95%  ' + '(' + Math.round(0.85 * maxpulse) + 'bpm - ' +

               Math.round(0.95 * maxpulse) + 'bpm)';

  document.PredForm.T8.value = 'Intervals .............................. 90% - 100% ' +'(' + Pulse(90,  maxpulse,restpulse) +

             'bpm - ' + maxpulse + 'bpm ' + ') ..... ' +

             ' 95% - 100% ' + '(' + Math.round(0.95 * maxpulse) + 'bpm - ' +

                maxpulse + 'bpm)';

}
