// JavaScript Document
function calculatetime(form) {
//  get conversion factors from selected options
var i = form.distunits.selectedIndex;
var distunitsvalue = form.distunits.options[i].value; 
var j = form.speedunits.selectedIndex;
var speedunitsvalue = form.speedunits.options[j].value;
//  calculate time in seconds    
form.secondvalue.value = (form.distvalue.value * distunitsvalue) / (form.speedvalue.value * speedunitsvalue);
//  convert to hours, minutes, seconds    
form.hourvalue.value = parseInt(form.secondvalue.value / 3600);
form.secondvalue.value = form.secondvalue.value - (form.hourvalue.value * 3600);
form.minutevalue.value = parseInt(form.secondvalue.value / 60);
form.secondvalue.value = parseInt(form.secondvalue.value - (form.minutevalue.value * 60));
return true;
}
function calculatedistance(form) {
//  get conversion factors from selected options
var i = form.distunits.selectedIndex;
var distunitsvalue = form.distunits.options[i].value; 
var j = form.speedunits.selectedIndex;
var speedunitsvalue = form.speedunits.options[j].value;
//  convert time to seconds
var temp = ((parseFloat(form.hourvalue.value) * 3600) + (parseFloat(form.minutevalue.value) * 60) + parseFloat(form.secondvalue.value));
//  calculated distance
form.distvalue.value = ((form.speedvalue.value * speedunitsvalue) * temp) / distunitsvalue; 
return true;
}
function calculatespeed(form) {
//  get conversion factors from selected options
var i = form.distunits.selectedIndex;
var distunitsvalue = form.distunits.options[i].value; 
var j = form.speedunits.selectedIndex;
var speedunitsvalue = form.speedunits.options[j].value;
//  convert time to seconds
var temp = ((parseFloat(form.hourvalue.value) * 3600) + (parseFloat(form.minutevalue.value) * 60) + parseFloat(form.secondvalue.value));
//  calculate speed
form.speedvalue.value = ((form.distvalue.value * distunitsvalue)  / (temp * speedunitsvalue)); 
return true;
}
function clearcell(cell) {
cell.value = "";
return true;
}
function d(){
  var a = document.form1.miles.value;
  var b = document.form1.gas.value;
  var c = document.form1.days.value;
  var cost = document.form1.cost.value;
  mpg=a/b;
  gpd=b/c;
  mpg= Math.round(mpg*10)/10;
  gpy=gpd*365;
  co=gpy*19.36;
  cpy=gpy*cost;
  gpd= Math.round(gpd*1000)/1000;
  gpy= Math.round(gpy*10)/10;
  cpy= Math.round(cpy*100)/100;
  co= Math.round(co);
  document.form1.results.value="You are getting "+mpg+" miles per gallon. On the average you use "+gpd+" gallons per day. At that rate of consumption you will burn "+gpy+" gallons per year at a cost of $"+cpy+" per year. In addition, that amount of use will produce "+co+" pounds of CO2 in one year.";
}
function perRound(num, precision) {

	var precision = 3; //default value if not passed from caller, change if desired

	// remark if passed from caller

	precision = parseInt(precision); // make certain the decimal precision is an integer

    var result1 = num * Math.pow(10, precision);

    var result2 = Math.round(result1);

    var result3 = result2 / Math.pow(10, precision);

    return zerosPad(result3, precision);

}



function zerosPad(rndVal, decPlaces) {

    var valStrg = rndVal.toString(); // Convert the number to a string

    var decLoc = valStrg.indexOf("."); // Locate the decimal point

    // check for a decimal 

    if (decLoc == -1) {

        decPartLen = 0; // If no decimal, then all decimal places will be padded with 0s

        // If decPlaces is greater than zero, add a decimal point

        valStrg += decPlaces > 0 ? "." : "";

    }

    else {

        decPartLen = valStrg.length - decLoc - 1; // If there is a decimal already, only the needed decimal places will be padded with 0s

    }

     var totalPad = decPlaces - decPartLen;    // Calculate the number of decimal places that need to be padded with 0s

    if (totalPad > 0) {

        // Pad the string with 0s

        for (var cntrVal = 1; cntrVal <= totalPad; cntrVal++) 

            valStrg += "0";

        }

    return valStrg;

}

// send the value in as "num" in a variable



// clears field of default value

function clear_field(field) {

if (field.value==field.defaultValue) {

field.value=''

}

}



//** function to check if a field is number and within range **//

function checkNumber(input, min, max, msg)

{

    msg = msg + " field has invalid data: " + input.value;

    var str = input.value;

    for (var i = 0; i < str.length; i++) {

        var ch = str.substring(i, i + 1)

        if ((ch < "0" || "9" < ch) && ch != '.') {

            alert(msg);

            return false;

        }

    }

    var num = parseFloat(str)

    if (num < min || max < num) {

        alert(msg + " not in range [" + min + ".." + max + "]");

        return false;

    }

    input.value = str;

    return true;

}





//** function to change null field to zero **//

function null2zero(invalue)

{

    if (invalue.value == null || invalue.value.length == 0 )

        {invalue.value = 0}

}





//** Main function - Course and Distance **//

function navcalc(kmsa)

{

//** null fields to zero **//

null2zero(kmsa.ilatdeg);

null2zero(kmsa.ilatmin);

null2zero(kmsa.lonsdeg);

null2zero(kmsa.lonsmin);

null2zero(kmsa.latodeg);

null2zero(kmsa.latomin);

null2zero(kmsa.flondeg);

null2zero(kmsa.flonmin);





//** Validate Entries **//

if (!checkNumber(kmsa.ilatdeg, 22, 62, "Initial Latitude degree ") ||

        !checkNumber(kmsa.ilatmin, 0, 59.9999999, "Initial Latitude minute ")

        )

 {

        kmsa.dist.value = "Error";

        kmsa.dists.value = "Error";

        kmsa.distk.value = "Error";

        return;

    }





if (!checkNumber(kmsa.lonsdeg, 71, 95, "Initial Longitude degree ") ||

        !checkNumber(kmsa.lonsmin, 0, 59.9999999, "Initial Longitude minute ")

        )

 {

        kmsa.dist.value = "Error";

        kmsa.dists.value = "Error";

        kmsa.distk.value = "Error";

        return;

    }





if (!checkNumber(kmsa.latodeg, 22, 62, "Final Latitude degree ") ||

        !checkNumber(kmsa.latomin, 0, 59.9999999, "Final Latitude minute ")

        )

 {

        kmsa.dist.value = "Error";

        kmsa.dists.value = "Error";

        kmsa.distk.value = "Error";

        return;

    }





if (!checkNumber(kmsa.flondeg, 71, 95, "Final Longitude degree ") ||

        !checkNumber(kmsa.flonmin, 0, 59.9999999, "Final Longitude minute ")

        )

 {

        kmsa.dist.value = "Error";

        kmsa.dists.value = "Error";

        kmsa.distk.value = "Error";

        return;

    }





//** change to minutes or miles **//

var inlat = parseFloat(kmsa.ilatdeg.value) * 60 + parseFloat(kmsa.ilatmin.value);

var inlon = (parseFloat(kmsa.lonsdeg.value) * 60 + parseFloat(kmsa.lonsmin.value)) * 1.3155;

var fnlat = parseFloat(kmsa.latodeg.value) * 60 + parseFloat(kmsa.latomin.value);

var fnlon = (parseFloat(kmsa.flondeg.value) * 60 + parseFloat(kmsa.flonmin.value)) * 1.3155;





//** handle single Point i.e. inlat=fnlat and inlon=fnlon **//

if (inlat == fnlat && inlon==fnlon)

{

kmsa.course.value = "error";

kmsa.dist.value = "Same Point";

kmsa.dists.value = "Same Point";

kmsa.distk.value = "Same Point";

return;

}

//** continue if not single point **//



//** Calculate Distance (range) **//

var dlat = Math.abs(inlat-fnlat);

var dlon = Math.abs(inlon-fnlon);

var drange = Math.sqrt(dlat*dlat+dlon*dlon);



var mystring="";

mystring=""+(drange*100);

var drange2=parseInt(mystring);

mystring=""+(drange2/100);

kmsa.dist.value = perRound(mystring);

kmsa.dists.value = perRound(mystring * 1.15077945);

kmsa.distk.value = perRound(mystring * 1.856095887096774);





//** Calculate angle (bearing) **//

if (inlon == fnlon)

{

if (inlat < fnlat)

{

kmsa.course.value = "000";

return;

}

kmsa.course.value = "180";

return;

}

var bias = 270;

var fact = 0-1;

if (inlat > fnlat)

{

if (inlon < fnlon)

{ 

bias=270;

fact=1;

}

if (inlon > fnlon)

{

bias=90;

fact=0-1;

}

}

if (inlat < fnlat)

{

if (inlon < fnlon)

{ 

bias=270;

fact=0-1;

}

if (inlon >= fnlon)

{

bias=90;

fact=1;

}

}



var dcourse = Math.atan(dlat/dlon)*180/Math.PI;

mystring="";

mystring=(bias+(fact*dcourse)+0.5);

var dcourse2=parseInt(mystring);

mystring=""+dcourse2;

if (mystring.length<3)

{

mystring="0"+mystring;

}

if (mystring.length<3)

{

mystring="0"+mystring;

}

kmsa.course.value = mystring;

kmsa.dir.value = "NA";

if (mystring >= 0 && mystring <= 11.25)
{
var dir = "N";
}
if (mystring > 348.75 && mystring <= 360)
{
var dir = "N";
}
if (mystring > 11.25 && mystring <= 33.75)
{
var dir = "NNE";
}
if (mystring > 33.75 && mystring <= 56.25)
{
var dir = "NE";
}
if (mystring > 56.25 && mystring <= 78.75)
{
var dir = "ENE";
}
if (mystring > 78.75 && mystring <= 101.25)
{
var dir = "E";
}
if (mystring > 101.25 && mystring <= 123.75)
{
var dir = "ESE";
}
if (mystring > 123.75 && mystring <= 146.25)
{
var dir = "SE";
}
if (mystring > 146.25 && mystring <= 168.75)
{
var dir = "SSE";
}
if (mystring > 168.75 && mystring <= 191.25)
{
var dir = "S";
}
if (mystring > 191.25 && mystring <= 213.75)
{
var dir = "SSW";
}
if (mystring > 213.75 && mystring <= 236.25)
{
var dir = "SW";
}
if (mystring > 236.25 && mystring <= 258.75)
{
var dir = "WSW";
}
if (mystring > 258.75 && mystring <= 281.25)
{
var dir = "W";
}
if (mystring > 281.25 && mystring <= 303.75)
{
var dir = "WNW";
}
if (mystring > 303.75 && mystring <= 326.25)
{
var dir = "NW";
}
if (mystring > 326.25 && mystring <= 348.75)
{
var dir = "NNW";
}

kmsa.dir.value = dir;

return

}



//** Functions for nautical & statue mile conversions **//



  function compute(obj)

  {

var mileValue = 1.15077945;

var knotValue = 1;

var kmValue = 1.856095887096774;

     obj.nmiles.value = obj.expr.value/72913.39;

     obj.smiles.value = obj.expr.value/63360;

     obj.ninches.value= 72913.39/obj.expr.value;

     obj.sinches.value = 63360/obj.expr.value;

     obj.finch.value = obj.smiles.value*5280;



  }


function calculaten() {

// Define the variables fuel required for trip
// 

	var gw = 6.25 ; //Gasoline weight per gallon
	var dw = 7.25 ; //Diesel weight per gallon
	var capacityn = document.gas.capacityn.value;
	var gphrn = document.gas.gphrn.value;
	var timen = eval(document.gas.timen.value);
	var stimen = eval(document.gas.stimen.value);
	var gallonsn = (gphrn)*((timen * 1)+(stimen * 1));
	var wgallonsn = (gallonsn)* (gw); //weight gas
	var wcapacityn = (capacityn)* (gw); //weight gas
	var wgallonsnd = (gallonsn)* (dw); //weight diesel
	var wcapacitynd = (capacityn)* (dw); //weight diesel

		document.gas.gallonsn.value = round(gallonsn);
		document.gas.wgallonsn.value = round(wgallonsn);
		document.gas.wcapacityn.value = round(wcapacityn);
		document.gas.wgallonsnd.value = round(wgallonsnd);
		document.gas.wcapacitynd.value = round(wcapacitynd);
}


function round(x) {
	return Math.round(x*100)/100;
}



function perRound(num, precision) {

	var precision = 3; //default value if not passed from caller, change if desired

	// remark if passed from caller

	precision = parseInt(precision); // make certain the decimal precision is an integer

    var result1 = num * Math.pow(10, precision);

    var result2 = Math.round(result1);

    var result3 = result2 / Math.pow(10, precision);

    return zerosPad(result3, precision);

}



function zerosPad(rndVal, decPlaces) {

    var valStrg = rndVal.toString(); // Convert the number to a string

    var decLoc = valStrg.indexOf("."); // Locate the decimal point

    // check for a decimal 

    if (decLoc == -1) {

        decPartLen = 0; // If no decimal, then all decimal places will be padded with 0s

        // If decPlaces is greater than zero, add a decimal point

        valStrg += decPlaces > 0 ? "." : "";

    }

    else {

        decPartLen = valStrg.length - decLoc - 1; // If there is a decimal already, only the needed decimal places will be padded with 0s

    }

     var totalPad = decPlaces - decPartLen;    // Calculate the number of decimal places that need to be padded with 0s

    if (totalPad > 0) {

        // Pad the string with 0s

        for (var cntrVal = 1; cntrVal <= totalPad; cntrVal++) 

            valStrg += "0";

        }

    return valStrg;

}

// send the value in as "num" in a variable



// clears field of default value

  function clear_field(field) {

		if (field.value==field.defaultValue) {

			field.value=''

		}

	}



  function compute(obj)

  {

     obj.nmiles.value = obj.expr.value/72913.39;

     obj.smiles.value = obj.expr.value/63360;

     obj.ninches.value= 72913.39/obj.expr.value;

     obj.sinches.value = 63360/obj.expr.value;

     obj.finch.value = obj.smiles.value*5280;

  }

  function compute2(obj)

  {

     obj.smiles2.value = obj.nmiles2.value*1.15077945;

  }

  function compute3(obj)

  {

     obj.nmiles3.value = obj.smiles3.value*0.86897624;

  }

function initArray() {
this.length = initArray.arguments.length;
for (var i = 0; i < this.length; i++) {
this[i] = initArray.arguments[i];
   }
}
var isn01 = new initArray("4b","5b","8b","8b");
isn01[0] = "~01~10.3937~20.03281~30.01094~40.01~50.0004971~60.00001~70.000006214~8";
isn01[1] = "~02.540~11~20.08333~30.02778~40.0254~50.001263~60.0000254~70.00001578~8";
isn01[2] = "~030.48~112~21~30.3333~40.3048~50.01515~60.0003048~70.0001894~8";
isn01[3] = "~091.44~136~23~31~40.9144~50.04545~60.0009144~70.0005682~8";
isn01[4] = "~0100~139.37~23.281~31.0936~41~50.04971~60.001~70.0006214~8";
isn01[5] = "~02012~1792~266~322~420.12~51~60.0212~70.0125~8";
isn01[6] = "~0100000~139370~23281~31093.6~41000~549.71~61~70.6214~8";
isn01[7] = "~0160934~163360~25280~31760~41609~580~61.609~71~8";
function lenCon() {
for (var i = 0; i < 8; i++) {
if (document.isnform01.leni[i].checked) {
leni = i;
leninm = document.isnform01.leni[i].value;
   }
}
for (var i = 0; i < 8; i++) {
if (document.isnform01.leno[i].checked) {
leno = i;
lenonm = document.isnform01.leno[i].value;
   }
}
useri = document.isnform01.leninp.value;
if (useri == 0) {
useri = 1;
document.isnform01.leninp.value = useri;
}
mulstr = isn01[leni];
picker = "~" + leno;
ps = mulstr.indexOf(picker);
leno++;
picker = "~" + leno;
ps1 = mulstr.indexOf(picker);
mulstr = mulstr.substring((ps + 2),ps1);
ps = (useri * mulstr);
picker = "";
picker += ps
ps1 = picker.indexOf(".");
if (ps1 > -1) {

ps = ps + .000001;
 picker = "";
picker += ps;
ps2 = picker.indexOf("e");
if (ps2 < 0) {
picker = picker.substring(0,(ps1 + 6));
}
if (ps2 == 0 || ps2 > 0) {
ps3 = picker.indexOf("00000");
if (ps3 > 0) {
picker = picker.substring(0,ps3 + 1) + picker.substring(ps2,picker.length);
      }
   }
}
picker = useri + " " + leninm + " = " + picker + " " + lenonm
document.isnform01.lenout.value = picker;
}

factors = new Array(0,0.166667,0.5,1,8,16,32,128,153,0.033814,33.814,
                    0,1,16,0.0352734, 35.2734);
// dry 0,16,32,128,256,1024

function converter(form) {
   var invalue = form.number_in.value;
   var result = invalue;	
   var outtype = form.units_out.options[form.units_out.selectedIndex].value;

   var intype = form.units_in.options[form.units_in.selectedIndex].value;
   if ((intype == outtype) && outtype != "" 
                           && check_input(invalue)) {
      result *= factors[form.units_in.selectedIndex];
      result /= factors[form.units_out.selectedIndex];
      form.number_out.value = result;
   } else {
       if (intype == "liquid" && outtype!="liquid") {
        form.units_out.selectedIndex = 0;
      } else {
         if (intype=="weight" && outtype!="weight") 
	    form.units_out.selectedIndex = 11;
      }
      form.number_out.value = "";
   }
}

function check_input(number) {
   for (var i = 0; i < number.length; i++) {
      var oneChar = number.charAt(i);
      if ((oneChar >= "0" && oneChar <= "9") || (oneChar == ".")) {
         return true;
      } else {
	 return false;
      }
   }
   return false;
}

function defocus() {
   window.document.forms[0].number_in.focus();
}

function Display_Converter() {
   var htmlform = '<CENTER><TABLE CELLSPACING="1" CELLPADDING="1"><TR>'
   htmlform += '<TD><INPUT class="box" NAME="number_in" TYPE="text" SIZE="6" value="1" onChange="converter(this.form)"></TD>'
   htmlform += '<TD><SELECT class="box" NAME="units_in" onChange="converter(this.form)">'
   htmlform += '<OPTION>- - Volume - -'
   htmlform += '<OPTION VALUE="liquid">teaspoon(s)'
   htmlform += '<OPTION VALUE="liquid">tablespoon(s)'
   htmlform += '<OPTION VALUE="liquid" SELECTED>fluid ounce(s)'
   htmlform += '<OPTION VALUE="liquid">cup(s)'
   htmlform += '<OPTION VALUE="liquid">pint(s)'
   htmlform += '<OPTION VALUE="liquid">quart(s)'
   htmlform += '<OPTION VALUE="liquid">gallon(s)'
   htmlform += '<OPTION VALUE="liquid">imperial gallon(s)'
   htmlform += '<OPTION VALUE="liquid">milliliter(s)'
   htmlform += '<OPTION VALUE="liquid">liter(s)'
   htmlform += '<OPTION>- - Weight - -'
   htmlform += '<OPTION VALUE="weight">ounce(s)'
   htmlform += '<OPTION VALUE="weight">pound(s)'
   htmlform += '<OPTION VALUE="weight">gram(s)'
   htmlform += '<OPTION VALUE="weight">kilogram(s)'
   htmlform += '</SELECT></TD>'
   htmlform += '<TD>&nbsp;<B><FONT SIZE="4">=</FONT></B>&nbsp;&nbsp;&nbsp;&nbsp;</TD>'
   htmlform += '<TD><INPUT class="box" NAME="number_out" TYPE="readonly" SIZE="10"'
   htmlform += ' onFocus="setTimeout(\'defocus()\',1)"></TD>'
   htmlform += '<TD><SELECT  class="box" NAME="units_out" onChange="converter(this.form)">'
   htmlform += '<OPTION>- - Volume - -'
   htmlform += '<OPTION VALUE="liquid">teaspoon(s)'
   htmlform += '<OPTION VALUE="liquid">tablespoon(s)'
   htmlform += '<OPTION VALUE="liquid" SELECTED>fluid ounce(s)'
   htmlform += '<OPTION VALUE="liquid">cup(s)'
   htmlform += '<OPTION VALUE="liquid">pint(s)'
   htmlform += '<OPTION VALUE="liquid">quart(s)'
   htmlform += '<OPTION VALUE="liquid">gallon(s)'
   htmlform += '<OPTION VALUE="liquid">imperial gallon(s)'
   htmlform += '<OPTION VALUE="liquid">milliliter(s)'
   htmlform += '<OPTION VALUE="liquid">liter(s)'
   htmlform += '<OPTION>- - Weight - -'
   htmlform += '<OPTION VALUE="weight">ounce(s)'
   htmlform += '<OPTION VALUE="weight">pound(s)'
   htmlform += '<OPTION VALUE="weight">gram(s)'
   htmlform += '<OPTION VALUE="weight">kilogram(s)'
   htmlform += '</SELECT></TD></TR>'
   htmlform += '<TR><TD><FONT COLOR="#FFFFFF" FACE=VERDANA SIZE=1><B>Enter<br>Amount </B></FONT></TD>'
   htmlform += '<TD><FONT COLOR="#FFFFFF" FACE=VERDANA SIZE=1><B>2. Select Units From</B></FONT></TD><TD></TD><TD></TD>'
   htmlform += '<TD><FONT COLOR="#FFFFFF" FACE=VERDANA SIZE=1><B>3. Select Units To<B/><FONT></FONT></TD></TR></TABLE></CENTER>';
   document.write(htmlform);
}
