// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- // 
// JavaScript File Name: weather.js 
// Browser support: Internet Explorer, Netscape, Firefox
// Produced By: NWS Miami  
// File Creation Date: 04/23/2008  
// Date Last Modified: 04/23/2008 
// ----------------------------------------------------------- // 
// ------------- Program Description and Details ------------- // 
// ----------------------------------------------------------- // 
//
// External JavaScript code for weather calculations and other 
// weather related elements.
//
// ----------------------------------------------------------- // 

// ##################  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 > 41){
    alert("Temperature is above 45 degrees F, it really doesn't matter.  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, it really does matter.  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 dew point 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 show(ind) {
  var d = document.getElementById("subs")
  var sd = d.getElementsByTagName("div")
  for (var i=0;i<sd.length;i++){
    if (/^sub/.test(sd[i].id))
    sd[i].style.display = "none"
  }
  document.getElementById("sub"+ind).style.display = "block"
}

function RHC(RH2){
    var rhtcc = (RH2.rh_tcc.value-0);
        var rhtdcc = (RH2.rh_tdcc.value-0);
    if (rhtcc < rhtdcc){
      alert("Your dew point 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("Does it really matter at this point?  Click on reset and try again!");
  }
  //
  else if (HI1_C.RHumidity.value < 40) {
     alert("Does it really matter at this point?  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("Does it really matter at this point?  Click on reset and try again!");
    }
        //
        else if (heatdew_c.dewpoint_c.value < 12){
      alert("Does it really matter at this point?  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("It doesn't really matter at this point. Click on reset and try again!");
  }
  //
  else if (HeatIndex.RHumidity.value < 40) {
      alert("It doesn't really matter at this point. 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("It doesn't really matter at this point. Click on reset and try again!");
    }
        //
        else if (heatdew.dewpoint.value < 53) {
      alert("It doesn't really matter at this point. 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 distance(form){
   var rad = 180 / Math.PI;
   form.dist.value = Math.round(3960 * Math.acos(Math.cos((90 - form.citylat.value)/rad) * Math.cos((90 - form.stormlat.value)/rad) + Math.sin((90 - form.citylat.value)/rad) * Math.sin((90 - form.stormlat.value)/rad) * Math.cos((form.citylon.value - form.stormlon.value)/rad)));
}
// ----------------------------------------------------------- //
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //
