// Script is by Ron Murphy. Updated 12.26.2000
//
// ##################  Temperature conversions #####
function ftoc(fahrenheit){
  fahrenheit.Celsius.value = Math.round((fahrenheit.tempf.value - 32) * .556) + " C";
}

function ctof(celsius){
  celsius.Fahrenheit.value = 1.80 * celsius.tempc.value + 32.0 + " F";
}
// ##################  Windchill #####
function windchill1(Windchill){
  if (Windchill.intemp.value > 45){
    alert("Temperature is above 45 degrees F, wind chill cannot be calculated.  Click on reset and try again.");
  } else {
    if (Windchill.mph.value <= 4) {
    alert("The wind speed is below 5 mph, temperature and windchill are the same.  Click on reset and try again.");
    } else {
     // old formula Wind Chill ( F) = 35.74 + 0.6215T - 35.75(V<sup>0.16<\/sup>) + 0.4275T(V<sup>0.16<\/sup>)
        var wc = 35.74 + (0.6215*Windchill.intemp.value) - (35.75*Math.pow(Windchill.mph.value,0.16)) + (0.4275*Windchill.intemp.value*Math.pow(Windchill.mph.value,0.16));
//      var wc = 0.0817*(3.71*Math.sqrt(Windchill.mph.value)+5.81 - 0.25 * Windchill.mph.value)*(Windchill.intemp.value-91.4) + 91.4;
      Windchill.windchill.value = Math.round(wc) + " F" + "/" + (Math.round((wc - 32) * .556) + " C");
    }
  }
}
function WC2(Windchill_C){
  if (Windchill_C.intemp_C.value > 5){
    alert("Temperature is above 7 degrees C, wind chill cannot be calculated.  Click on reset and try again.");
  } else {
    if (Windchill_C.mph_C.value <= 4) {
    alert("The wind speed is below 5 mph, temperature and windchill are the same.  Click on reset and try again.");
    } else {
        var intemp_to_F = 1.80 * Windchill_C.intemp_C.value + 32.0;
        var wc_F = 35.74 + (0.6215*intemp_to_F) - (35.75*Math.pow(Windchill_C.mph_C.value,0.16)) + (0.4275*intemp_to_F*Math.pow(Windchill_C.mph_C.value,0.16));
//      var wc_C = 33 + (Windchill_C.intemp_C.value - 33) * (.474 + .454 * Math.sqrt(Windchill_C.mph_C.value * .447) - .0454 * (Windchill_C.mph_C.value * .447));
        Windchill_C.wcvalue_in_c.value=Math.round((wc_F - 32) * .556) + " C" + " / " + Math.round(wc_F) + " F";
//      Windchill_C.wcvalue_in_c.value=Math.round(wc_C) + " C" + "/" + (Math.round(1.80 * wc_C + 32.0)) + " F";
    }
  }
}

// ################### Relative Humidity #####
function RH(humidity){
  var tc = (humidity.airtemp.value - 32) * .556;
  var tdc = (humidity.dewpointtemp.value -32)* .556;
    if (tc < tdc){
      alert("Your dewpoint temperature cannot be higher than the air temperature.  Click on reset and try again.");
    }
    else{
     humidity.relativeh.value = Math.round(100.0*(Math.pow((112-(0.1*tc)+tdc)/(112+(0.9*tc)),8))) + "%";
         humidity.answer3.value = Math.round(tc) + " C";
         humidity.answer4.value = Math.round(tdc) + " C";
    }
}

function RHC(RH2){
    var rhtcc = (RH2.rh_tcc.value-0);
        var rhtdcc = (RH2.rh_tdcc.value-0);
    if (rhtcc < rhtdcc){
      alert("Your dewpoint temperature cannot be higher than the air temperature.  Click on reset and try again.");
    }
    else{
     RH2.TheRHinPercent.value = Math.round(100.0 * (Math.pow((112 - (0.1 * rhtcc) + rhtdcc)/(112 + (0.9 * rhtcc)),8))) + "%";
     RH2.answer1.value = Math.round(1.80 * rhtcc + 32) + " F";
     RH2.answer2.value = Math.round(1.80 * rhtdcc + 32) + " F";
    }
}

//  #################### Heat Index #####

function heat1_c(HI1_C){
  if(parseFloat(HI1_C.tempair.value) < 27){
     alert("The heat index formula only works at temperatures between 80F (27C) and 115F (46C).  Click on reset and try again.");
  }
  //
  else if (HI1_C.RHumidity.value < 40) {
     alert("With RH less than 40%, the heat index is at or below the air temperature.  Click on reset and try again.");
  }
  //  
  else {
     var tempair_in_fahrenheit = 1.80 * HI1_C.tempair.value + 32.0;
     var hi1_c = -42.379 + 2.04901523 * tempair_in_fahrenheit + 10.14333127 * HI1_C.RHumidity.value - 0.22475541 * tempair_in_fahrenheit * HI1_C.RHumidity.value - 6.83783 * (Math.pow(10, -3)) * (Math.pow(tempair_in_fahrenheit, 2)) - 5.481717 * (Math.pow(10, -2)) * (Math.pow(HI1_C.RHumidity.value, 2)) + 1.22874 * (Math.pow(10, -3)) * (Math.pow(tempair_in_fahrenheit, 2)) * HI1_C.RHumidity.value + 8.5282 * (Math.pow(10, -4)) * tempair_in_fahrenheit * (Math.pow(HI1_C.RHumidity.value, 2)) - 1.99 * (Math.pow(10, -6)) * (Math.pow(tempair_in_fahrenheit, 2)) * (Math.pow(HI1_C.RHumidity.value,2));
     HI1_C.heatindex.value = Math.round(hi1_c) + " F"  + " / " + Math.round((hi1_c - 32) * .556) + " C";
     var rh3 = 1 - HI1_C.RHumidity.value/100;
     var tdpc3 = HI1_C.tempair.value - (((14.55 + .114*HI1_C.tempair.value)*rh3) + (Math.pow(((2.5 + .007*HI1_C.tempair.value)*rh3), 3)) + ((15.9 + .117*HI1_C.tempair.value))*(Math.pow(rh3, 14)));
    HI1_C.dewpt2.value = Math.round(1.80 * tdpc3 + 32.0) + " F" + " / " + Math.round(tdpc3) + " C";
   }
}
// Heat index computed using air temperature and dew point temperature.  Degrees C
function HIDEW_c(heatdew_c){
    if(heatdew_c.tempair3.value < 27){
     alert("The heat index formula only works at temperatures between 80<sup>o </sup>F (27C) and 115F (46C).  Click on reset and try again.");
    }
        //
        else if (heatdew_c.dewpoint_c.value < 12){
      alert("The heat index formula only works with dewpoints between 53F (12C) and 85F (27C).  Click on reset and try again.");
        }
        //
    else{
         if (heatdew_c.tempair3.value < heatdew_c.dewpoint_c.value){
         alert("Your dew point temperature cannot be higher than the air temperature.  Click on reset and try again!");
     }
    else{
    tac = parseFloat(heatdew_c.tempair3.value);
    tdac = parseFloat(heatdew_c.dewpoint_c.value);
    var stuff3 = (112 - (0.1 * tac) + tdac) / (112 + (0.9 * tac));
    var RHumidity3 = Math.round(100.0 * (Math.pow(stuff3,8)));
    var airtemp_in_fahrenheit = 1.80 * heatdew_c.tempair3.value + 32.0;

    var hi3 = -42.379 + 2.04901523 * airtemp_in_fahrenheit + 10.14333127 * RHumidity3 - 0.22475541 * airtemp_in_fahrenheit * RHumidity3 - 6.83783 * (Math.pow(10, -3)) * (Math.pow(airtemp_in_fahrenheit, 2)) - 5.481717 * (Math.pow(10, -2)) * (Math.pow(RHumidity3, 2)) + 1.22874 * (Math.pow(10, -3)) * (Math.pow(airtemp_in_fahrenheit, 2)) * RHumidity3 + 8.5282 * (Math.pow(10, -4)) * airtemp_in_fahrenheit * (Math.pow(RHumidity3, 2)) - 1.99 * (Math.pow(10, -6)) * (Math.pow(airtemp_in_fahrenheit, 2)) * (Math.pow(RHumidity3,2));
    heatdew_c.answer.value = Math.round(hi3) + " F" +  " / " + Math.round((hi3 - 32) * .556) + " C";
    heatdew_c.RH3.value = RHumidity3 + "%";
    }
  }
}
// Heat index computed using air temperature F and RH%
function heat1(HeatIndex){
  if(HeatIndex.tempair.value < 80){
     alert("The heat index formula only works at temperatures between 80F (27C) and 115F (46C).  Click on reset and try again.");
  }
  //
  else if (HeatIndex.RHumidity.value < 40) {
     alert("With RH less than 40%, the heat index is at or below the air temperature.  Click on reset and try again.");
  }
 //    
  else {
   var hi = -42.379+2.04901523*HeatIndex.tempair.value+10.14333127*HeatIndex.RHumidity.value-0.22475541*HeatIndex.tempair.value*HeatIndex.RHumidity.value-6.83783*(Math.pow(10, -3))*(Math.pow(HeatIndex.tempair.value, 2))-5.481717*(Math.pow(10, -2))*(Math.pow(HeatIndex.RHumidity.value, 2))+1.22874*(Math.pow(10, -3))*(Math.pow(HeatIndex.tempair.value, 2))*HeatIndex.RHumidity.value+8.5282*(Math.pow(10, -4))*HeatIndex.tempair.value*(Math.pow(HeatIndex.RHumidity.value, 2))-1.99*(Math.pow(10, -6))*(Math.pow(HeatIndex.tempair.value, 2))*(Math.pow(HeatIndex.RHumidity.value,2));
   HeatIndex.heatindex.value = Math.round(hi) + " F" +  " / " + Math.round((hi - 32) * .556) + " C";
   var tempc2 = (HeatIndex.tempair.value - 32) * .556;
   var rh2 = 1 - HeatIndex.RHumidity.value/100;
   var tdpc2 = tempc2 - (((14.55 + .114*tempc2)*rh2) + (Math.pow(((2.5 + .007*tempc2)*rh2), 3)) + ((15.9 + .117*tempc2))*(Math.pow(rh2, 14)));
   HeatIndex.dewpt2.value = Math.round(1.80 * tdpc2 + 32.0) + " F" + " / " + Math.round(tdpc2) + " C";
}
}
// Heat index computed using air temperature and dew point temperature. Degrees F
function HIDEW(heatdew){
    if(heatdew.tempair2.value < 80){
     alert("The heat index formula only works at temperatures between 80F (27C) and 115F (46C).  Click on reset and try again.");
    }
        //
        else if (heatdew.dewpoint.value < 53) {
      alert("The heat index formula only works with dewpoints between 53F (12C) and 85F (27C).  Click on reset and try again.");
        }
        // 
    else{
       var tc2 = (parseFloat(heatdew.tempair2.value) - 32) * .556;
       var tdc2 = (parseFloat(heatdew.dewpoint.value) -32)* .556;
         if (tc2 < tdc2){
         alert("Your dew point temperature cannot be higher than the air temperature.  Click on reset and try again.");
         }
    else{
    var stuff2 = (112-(0.1*tc2)+tdc2)/(112+(0.9*tc2));
    var RHumidity2 = Math.round(100.0*(Math.pow(stuff2,8)));
    var hi2 = -42.379+2.04901523*heatdew.tempair2.value+10.14333127*RHumidity2-0.22475541*heatdew.tempair2.value*RHumidity2-6.83783*(Math.pow(10, -3))*(Math.pow(heatdew.tempair2.value, 2))-5.481717*(Math.pow(10, -2))*(Math.pow(RHumidity2, 2))+1.22874*(Math.pow(10, -3))*(Math.pow(heatdew.tempair2.value, 2))*RHumidity2+8.5282*(Math.pow(10, -4))*heatdew.tempair2.value*(Math.pow(RHumidity2, 2))-1.99*(Math.pow(10, -6))*(Math.pow(heatdew.tempair2.value, 2))*(Math.pow(RHumidity2,2));
    heatdew.answer.value = Math.round(hi2) + " F" +  " / " + Math.round((hi2 - 32) * .556) + " C";
    heatdew.RHumidity2.value = RHumidity2 + "%";
    }
  }
}

// ###################### Form clearing #####

function clearF(form){
  form.Celsius.value="";
  form.tempf.value = "";
}

function clearC(form){
  form.Fahrenheit.value = "";
  form.tempc.value = "";
}

function clearwindchill(form){
  form.windchill.value = "";
  form.mph.value = "";
  form.intemp.value = "";
}

function clearwindchill_C(form){
  form.wcvalue_in_c.value = "";
  form.mph_C.value = "";
  form.intemp_C.value = "";
}

function clearhumidity(form){
  form.relativeh.value = "";
  form.airtemp.value = "";
  form.dewpointtemp.value = "";
  form.answer3.value = "";
  form.answer4.value = "";
}

function clearhumidity2(form){
  form.TheRHinPercent.value = "";
  form.rh_tcc.value = "";
  form.rh_tdcc.value = "";
  form.answer1.value = "";
  form.answer2.value = "";
}

function clearHI(form){
  form.tempair.value = "";
  form.RHumidity.value = "";
  form.heatindex.value = "";
}

function clearHIDEW(form){
  form.tempair2.value = "";
  form.RHumidity2.value = "";
  form.answer.value = "";
}

function clearHI(form){
  form.tempair.value = "";
  form.RHumidity.value = "";
  form.heatindex.value = "";
}

function clearHIDEW(form){
  form.tempair2.value = "";
  form.RHumidity2.value = "";
  form.answer.value = "";
}

function clearHI1_C(form){
  form.tempair.value = "";
  form.RHumidity.value = "";
  form.heatindex.value = "";
  form.dewpt2.value = "";
}

function clearHIDEW_c(form){
  form.tempair3.value = "";
  form.RH3.value = "";
  form.answer.value = "";
  form.dewpoint_c.value="";
}

function decideConvert(speed){
	speed = parseFloat(document.Convert.Windspeed.value);
	if (document.Convert.windunit[0].checked) {
		document.Convert.Mph.value = roundOff(speed);
		document.Convert.Knots.value = 
			roundOff(convertmphtoknots(speed));
		document.Convert.Mps.value = roundOff(convertmphtomps(speed));
		document.Convert.Ftps.value = roundOff(convertmphtoftps(speed));
		document.Convert.Kmph.value = roundOff(convertmphtokmph(speed));
	} else if (document.Convert.windunit[1].checked) {
		document.Convert.Mph.value =
			roundOff(convertknotstomph(speed));
		document.Convert.Knots.value = roundOff(speed);
		document.Convert.Mps.value =
			roundOff(convertknotstomps(speed));
		document.Convert.Ftps.value =
			roundOff(convertknotstoftps(speed));
		document.Convert.Kmph.value =
			roundOff(convertknotstokmph(speed));
	} else if (document.Convert.windunit[2].checked) {
		document.Convert.Mph.value = roundOff(convertmpstomph(speed));
		document.Convert.Knots.value =
			roundOff(convertmpstoknots(speed));
		document.Convert.Mps.value = roundOff(speed);
		document.Convert.Ftps.value = roundOff(convertmpstoftps(speed));
		document.Convert.Kmph.value = roundOff(convertmpstokmph(speed));
	} else if (document.Convert.windunit[3].checked) {
		document.Convert.Mph.value =
			roundOff(convertftpstomph(speed));
		document.Convert.Knots.value =
			roundOff(convertftpstoknots(speed));
		document.Convert.Mps.value =
			roundOff(convertftpstomps(speed));
		document.Convert.Ftps.value =  roundOff(speed);
		document.Convert.Kmph.value =
			roundOff(convertftpstokmph(speed));
	} else if (document.Convert.windunit[4].checked) {
		document.Convert.Mph.value = roundOff(convertkmphtomph(speed));
		document.Convert.Knots.value =
			roundOff(convertkmphtoknots(speed));
		document.Convert.Mps.value = roundOff(convertkmphtomps(speed));
		document.Convert.Ftps.value =
			roundOff(convertkmphtoftps(speed));
		document.Convert.Kmph.value = roundOff(speed);
	}
}

function convertmphtoknots(mph) {
	knots = 0.868391 * mph;
	return knots;
}

function convertmphtomps(mph) {
	mps = 0.44704 * mph;
	return mps;
}

function convertmphtoftps(mph) {
	ftps = 1.46667 * mph;
	return ftps;
}

function convertmphtokmph(mph) {
	kmph = 1.609344 * mph;
	return kmph;
}

function convertknotstomph(knots) {
	mph = 1.15155 * knots;
	return mph;
}

function convertknotstomps(knots) {
	mps = 0.514791 * knots;
	return mps;
}
			
function convertknotstoftps(knots) {
	ftps = 1.68895 * knots;
	return ftps;
}
			
function convertknotstokmph(knots) {
	kmph = 1.85325 * knots;
	return kmph;
}
			
function convertmpstomph(mps) {
	mph = 2.23694*mps;
	return mph;
}
			
function convertmpstoknots(mps) {
	knots = 1.94254 * mps;
	return knots;
}

function convertmpstoftps(mps) {
	ftps = 3.28084 * mps;
	return ftps;
}
			
function convertmpstokmph(mps) {
	kmph = 3.6 * mps;
	return kmph
}
			
function convertftpstomph(ftps) {
	mph = 0.681818*ftps;
	return mph;
}

function convertftpstoknots(ftps) {
	knots =  0.592085 * ftps;
	return knots;
}
			
function convertftpstomps(ftps) {
	mps =  0.3048 * ftps;
	return mps;
}
			
function convertftpstokmph(ftps) {
	kmph = 1.09728 * ftps;
	return kmph;
}
			
function convertkmphtomph(kmph) {
	mph = 0.621371*kmph;
	return mph;
}
			
function convertkmphtoknots(kmph) {
	knots = 0.539593 * kmph;
	return knots;
}
			
function convertkmphtomps(kmph) {
	mps = 0.277778 * kmph;
	return mps;
}
			
function convertkmphtoftps(kmph) {
	ftps = 0.911344 * kmph;
	return ftps;
}				
				
function roundOff(value) {
	value = Math.round(10*value)/10;
	return value;
}

function setToNull() {
	document.Convert.Mph.value="";
	document.Convert.Mps.value="";
	document.Convert.Kmph.value="";
	document.Convert.Knots.value="";
	document.Convert.Ftps.value="";
	document.Convert.Windspeed.value="";
}

