/* speedclc.js -- computes scale speeds -- March 29, 2005
   the form controls are:
   clcd is input control for distance (in inches),
   clct is input control for time (in seconds),
   clcs is select controlscale (text - 1:x)
   clcv is output in scale mph (for report a set of mphs for 1,2, ... 12 secs)
   access is thru the form element id 'clc'
   created by john russell, ve3ll@rac.ca */
/* validated with JSlint 2005 03 29 */
cf=3600 / (5280*12);// inch per sec to mph
sf=null;time=null;dist=null;vel=null;

function roundoff(x,y) {
scale=Math.pow(10,y);return Math.round(x * scale)/scale;}

function about(id) {
alert('SpeedCalc version 4.1 written by John Russell.\n' +
      'Feel free to copy speedclc.js for your own use.');
document.getElementById(id+'d').focus();}

function clrForm(id) {
sf=null;time=null;dist=null;vel=null;
document.getElementById(id+'d').value=" ";
document.getElementById(id+'t').value=" ";
document.getElementById(id+'v').value=" ";
document.getElementById(id+'d').focus();}

function testDist(id) {
if (dist.length < 1) {
   alert("You must set distance!");
   document.getElementById(id+'d').focus();return false;}
if (isNaN(dist) === true) {
   alert("Distance must be a number!");
   document.getElementById(id+'d').focus();return false;}
if (dist < 0.1) {
   alert("Distance must be positive!");
   document.getElementById(id+'d').focus();return false;}
return true;}

function testTime(id) {
if (time.length < 1) {
   alert("You must set time!");
   document.getElementById(id+'t').focus();return false;}
if (isNaN(time) === true) {
   alert("Time must be a number!");
   document.getElementById(id+'t').focus();return false;}
if (time < 0.1) {
   alert("Time must be positive!");
   document.getElementById(id+'t').focus();return false;}
return true;}

function testVel(id) {
if (vel.length < 1) {
   alert("You must set speed!");
   document.getElementById(id+'v').focus();return false;}
if (isNaN(vel) === true) {
   alert("Speed must be a number!");
   document.getElementById(id+'v').focus();return false;}
if (vel < 0.1) {
   alert("Speed must be positive!");
   document.getElementById(id+'v').focus();return false;}
return true;}

function distx(id) { // validate before math
time=document.getElementById(id+'t').value;
vel=document.getElementById(id+'v').value;
el=document.getElementById(id+'s');
sc=el.options[el.selectedIndex].text;
sf=sc.substring(sc.indexOf(":") + 1, sc.length);
if (testTime(id)=== false) {return;}
if (testVel(id)=== false) {return;}
document.getElementById(id+'d').value=roundoff(vel*time/(cf*sf),1);
document.getElementById(id+'d').focus();}

function speed(id) { // validate before math
dist=document.getElementById(id+'d').value;
time=document.getElementById(id+'t').value;
el=document.getElementById(id+'s');
sc=el.options[el.selectedIndex].text;
sf=sc.substring(sc.indexOf(":") + 1, sc.length);
if (testDist(id)=== false) {return;}
if (testTime(id)=== false) {return;}
document.getElementById(id+'v').value=roundoff(dist/time*(cf*sf),1);
document.getElementById(id+'d').focus();}

function timex(id) { // validate before math
dist=document.getElementById(id+'d').value;
vel=document.getElementById(id+'v').value;
el=document.getElementById(id+'s');
sc=el.options[el.selectedIndex].text;
sf=sc.substring(sc.indexOf(":") + 1, sc.length);
if (testDist(id)=== false) {return;}
if (testVel(id)=== false) {return;}
document.getElementById(id+'t').value=roundoff(dist/vel*(cf*sf),1);
document.getElementById(id+'d').focus();}
   
function report(id) {
dist=document.getElementById(id+'d').value;
if (testDist(id)=== false) {return;}
el=document.getElementById(id+'s');
sc=el.options[el.selectedIndex].text;
sf=sc.substring(sc.indexOf(":") + 1, sc.length);
msg='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n';
msg+='<html><head><title>Speed Chart for '+sc+' Scale<\/title>\n';
msg+='<style type="text/css">\nbody {background:linen}\n';
msg+='h1,h3, div {text-align:center}\n';
msg+='table {margin:auto}\n<\/style>\n';
msg+='<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">\n';
msg+='<\/head><body><h1>Speed Chart for '+sc+' Scale<\/h1>\n';
msg+='<h3>Timed distance is '+dist+' inches<\/h3>\n<div><table>\n';
msg+='<tr><th>Time [sec]<\/th><th>Speed [mph]<\/th><\/tr>';
for (rt=1 ;rt <=12;rt++) {
     spd=roundoff(dist/rt*(cf*sf),1);// calc for one unit
     msg+='<tr><th>'+rt+'<\/th><th>'+spd+'</th><\/tr>\n'; 
     }
msg+='<\/table><\/div><\/body><\/html>\n';document.write(msg);}