<!-- Original:  Keith Jenci -->
<!-- Web Site:  http://www.geocities.com/ResearchTriangle/Campus/3835 -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin

var time = 0;
var distance =0;
var pace = 0;

/* Calculate PACE from Distance and Time */
function calc_pace() {
setCommonVars();
if ((distance*time)==0) {
alert("Distance and Time are both required to calculate pace")
} 
else {
pace = time / distance;
if (document.Calc.optpace[1].checked) {
  pace = pace *  1.609344; 
}  

minp = Math.floor(pace);
secp = Math.round(60 * (pace - minp));
if (secp == 60) {
  minp++;
  secp = 0;
}
document.Calc.pacem.value = minp;
document.Calc.paces.value = secp;

}
return;
}

/* Calculate TIME from Pace and Distance */
function calc_time() {
setCommonVars();
if ((pace*distance)==0) {
alert("Pace and Distance are both required to calculate time")
} 
else {
time = (distance * pace)/60 ;
hrs = document.Calc.timeh.value = Math.floor(time);
minsec = 60 * (time - hrs);
min = document.Calc.timem.value = Math.floor(minsec);
document.Calc.times.value = Math.round(60 * (minsec - min));
}
return;
}

/* Calculate DISTANCE from Time and Pace */
function calc_distance() {
setCommonVars();
if ((time*pace)==0) {
alert("Time and Pace are both required to calculate time")
} 
else {
distance = time / pace;
if (document.Calc.optdist[1].checked) {
  distance = distance /  1.609344; 
  }  
document.Calc.defined_distance.value = "";
document.Calc.distance.value = decimalPlaces(distance, 2);
}
return;
}

function decimalPlaces(val, places) {
factor = 1;
for (i = 0; i < places; i++) {
factor *= 10;
}
val *= factor;
val = Math.round(val);
val /= factor;
return val;
}

function setCommonVars() {
hrs=document.Calc.timeh.value;
min=document.Calc.timem.value;
sec=document.Calc.times.value;
time = (hrs *60) + (min * 1) + (sec / 60);

if (document.Calc.defined_distance.value == "") {
  distance = document.Calc.distance.value;
  if (document.Calc.optdist[1].checked) {
    distance = distance * 1.609344; 
  } 
}
else {
  switch(document.Calc.defined_distance.value) {
  case "halfmarathon": distance = 21.097494; break;
  case "marathon": distance = 42.194988; break;
  case "1mi": distance = 1.609344; break; 
  case "10mi": distance = 1.609344*10; break;
  case "50mi": distance = 1.609344*50; break;
  default: distance = document.Calc.defined_distance.value
  }
}

minp = document.Calc.pacem.value;
secp = document.Calc.paces.value;
pace = (minp * 1) + (secp / 60);
if (document.Calc.optpace[1].checked) {
  pace = pace /  1.609344; 
}  

   }
//  End -->


