/*
 * Copyright Wolfgang Kuehn
 * Modified with permission by Dave Howorth
 */

// Fields
var Tin;	// the room temperature.
var Tout;	// the external temperature.
var DPT;	// the dew point temperature.
var deltaDPT;	// the accuracy of the dew point temperature.

/*
 * Called once when the page is loaded.
 * Initializes all input fields and computes the output fields.
 */
function init() {
  var form = document.FORM;
  
  this.Tin	= new Temperature(20, CELSIUS, form.Tin);
  this.Tout	= new Temperature(0, CELSIUS, form.Tout);
  this.DPT	= new Temperature(0, CELSIUS, form.DPT);
  this.deltaDPT	= new TemperatureChange(20, CELSIUS, form.deltaDPT);
  
  document.FORM.TScale[1].checked=true;
  document.FORM.RH.value = 50;
  document.FORM.DELTARH.value = 3;
  document.FORM.Uw.value = 5;

  compute();
}

/*
 * From the input temperatue and relative humidity, computes dew 
 * point temperature and Saturation Vapor Pressure.
 */
function compute () {
  
  // Check room temperature in range.
  var t = Tin.getKelvin();
  if (t < minT || t > maxT) {
    alert("Room temperature out of range [" + minT + ".." + maxT +"] Deg.Kelvin.");
    return;
  }

  // Check outside temperature in range.
  var tout = Tout.getKelvin();
  if (tout < minT || tout > maxT) {
    alert("Outside temperature out of range [" + minT + ".." + maxT +"] Deg.Kelvin.");
    return;
  }

  // Check relative humidity in range.
  var RH = stringToFloat(document.FORM.RH.value);
  if (isNaN(RH) || RH<=0 || RH>100) {
    alert("Relative humidity out of range (0..100].");
    return;
  }

  // Check relative humidity accuracy.
  var deltaRH = stringToFloat(document.FORM.DELTARH.value);
  if (deltaRH<0 || deltaRH>100) {
    alert("Accuracy of relative humidity out of range [0..100].");
    return;
  }

  // Check U-value of window in range
  var Uw = stringToFloat(document.FORM.Uw.value);
  if (isNaN(Uw) || Uw <= 0 || Uw > 10) {
    alert("U-value of window out of range (0..10] W/m2.K");
    return;
  }

  // Compute dew point temperature.
  var Tdp;
  try {
    Tdp = dewPoint(RH,t);
  } catch (e) {
    alert("Dewpoint below valid range.");
    return;
  }

  // Compute dew point temperature accuracy.
  var deltaTdp = Math.abs(Tdp-dewPoint(RH+deltaRH,t));
  
  // Display temperature.
  DPT.setKelvin(Tdp);
  // Display accuracy.
  deltaDPT.setKelvin(deltaTdp);
  // Display saturation vapor pressure
//  document.FORM.PVS.value = truncate(PVS(t)/1000, 5, RELATIVE);

  if (Tdp > tout) { // condensation is possible
    // Compute heat flux and display it
    var q      = (Tdp - tout) * Uw;
    var q_btu  = 0.3172105 * q;
    document.FORM.q.value     =  truncate(q, 3, RELATIVE);
    document.FORM.q_btu.value =  truncate(q_btu, 3, RELATIVE);

    // Compute critical shutter U-value and display it
    var Us    = q / (t - Tdp);
    var R_btu = 5.678 / Us;
    document.FORM.Us.value    = truncate(Us, 3, RELATIVE);
    document.FORM.R_btu.value = truncate(R_btu, 3, RELATIVE);
  }
  else { // condensation is not possible
    document.FORM.q.value     = 'none';
    document.FORM.q_btu.value = 'none';
    document.FORM.Us.value    = 'none';
    document.FORM.R_btu.value = 'none';
  }
}

