<html>
<head>
<!--
mod_param_Etyp* calls addeditparam*.php which modifies the parameters in param_def_ETyp? table for each estate.
It called with parameters:
  event = js specific
  id = used to get existing value in the textbox when selection is returned by addeditparam*.php
  str = string explaining what the input is to display in addeditparam*.php
-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script>
window.onload = function(){
  var width = document.getElementById("the_table").offsetWidth;
  document.getElementById("assmnt_name").style.width = width * 0.49;
  // document.getElementById("submit_button").style.width = width * 0.25;
  // Now get the current preset using some php. //
  // This allows us to populate initial values. //

  $.ajax({
    url: "./Scripts/check_preset.php",
    type: "POST",
    dataType: "json",
    success: function(result){

      var is_custom = result.is_custom;

      // If a preset is selected, set all boxes.
      if (is_custom==0) {
        
        // Energy.
        set_box("space_heating",result.in01);
        set_box("lighting",result.in02);
        set_box("equipment",result.in03);
        set_box("DHW",result.in04);

        // Emissions.
        set_box("CO2",result.in05);
        set_box("NOX",result.in06);
        set_box("SOX",result.in07);
        set_box("O3",result.in08);

        // Thermal comfort.
        set_box("max_Top_liv",result.in09);
        set_box("min_Top_liv",result.in10);
        set_box("max_Top_kit",result.in11);
        set_box("min_Top_kit",result.in12);
        set_box("max_Top_bed",result.in13);
        set_box("min_Top_bed",result.in14);
        set_box("max_Top_bath",result.in15);
        set_box("min_Top_bath",result.in16);
        set_box("max_Top_WC",result.in17);
        set_box("min_Top_WC",result.in18);
        set_box("max_Top_hall",result.in19);
        set_box("min_Top_hall",result.in20);

        // Indoor air quality.      
        set_box("min_fresh_air",result.in21);
        document.getElementById("air_supply_units").value = result.in22;
        document.getElementById("air_supply_units").setAttribute("disabled",true);
        document.getElementById('air_supply_units').setAttribute('class','greyed-cell grey-text');
        document.getElementById("air_supply_units_fwd").value = result.in22;
        set_box("max_CO2_conc",result.in23);

        // Also deactivate naming box.
        document.getElementById("assmnt_name").value = result.name;
        document.getElementById("assmnt_name").setAttribute("disabled",true);
        document.getElementById("assmnt_name").setAttribute("class","greyed-cell grey-text");
      }

      // Until a preset is selected, disable all boxes.
      else if (is_custom==-1) {
        // Energy.
        set_box("space_heating",'');
        set_box("lighting",'');
        set_box("equipment",'');
        set_box("DHW",'');

        // Emissions.
        set_box("CO2",'');
        set_box("NOX",'');
        set_box("SOX",'');
        set_box("O3",'');

        // Thermal comfort.
        set_box("max_Top_liv",'');
        set_box("min_Top_liv",'');
        set_box("max_Top_kit",'');
        set_box("min_Top_kit",'');
        set_box("max_Top_bed",'');
        set_box("min_Top_bed",'');
        set_box("max_Top_bath",'');
        set_box("min_Top_bath",'');
        set_box("max_Top_WC",'');
        set_box("min_Top_WC",'');
        set_box("max_Top_hall",'');
        set_box("min_Top_hall",'');

        // Indoor air quality.      
        set_box("min_fresh_air",'');
        document.getElementById("air_supply_units").setAttribute("disabled",true);
        document.getElementById('air_supply_units').setAttribute('class','greyed-cell grey-text');
        set_box("max_CO2_conc",'');

        // Also deactivate naming box and submit button.
        document.getElementById("assmnt_name").setAttribute("disabled",true);
        document.getElementById("assmnt_name").setAttribute("class","greyed-cell grey-text");
        document.getElementById('submit_button').disabled = false;
        document.getElementById('submit_button').setAttribute('title','please select test criteria');
      }

      // Disable submit button until user has selected a preset (or confirmed custom).
      if (is_custom!=-1) {
      }
    },
    error: function(jqXHR,errtyp,errstr){
      console.log('check_preset failed');
      console.log(errtyp);
      console.log(errstr);
    }
  });
}
function set_box(id,val){  
  document.getElementById(id).value = val;
  document.getElementById(id).setAttribute("readonly", true);
  document.getElementById(id).setAttribute("class", "greyed-cell grey-text");
  // document.getElementById(id.concat("_a")).setAttribute("onClick","");
}
function fresh_air_changed(){
  var val = document.getElementById('min_fresh_air').value;
  if (val=='approved document F') {
    document.getElementById('air_supply_units').value = 'l/ s';
    document.getElementById('air_supply_units').disabled = true;
    document.getElementById('air_supply_units').setAttribute('class','greyed-cell grey-text');
  }
  else {    
    document.getElementById('air_supply_units').disabled = false;
    document.getElementById('air_supply_units').setAttribute('class','coloured-cell grey-text');
  }
  var val = document.getElementById('air_supply_units').value;
  document.getElementById('air_supply_units_fwd').value = val;
}

// function mod_param_Etyp(e,id,str){
//   var value = document.getElementById(id).value;
//   /*
//   var x = e.clientX;
//   var y = e.clientY;
//   var coords = "X coords: " + x + ", Y coords: " + y;
//   var features = "location=0;titlebar=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=200,height=200,top="+y+",left="+x;
//   */
//   var features = "location=0;titlebar=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=480,height=240";
//   var url = "Pages/addeditparamsimple.php?value=" + value + "&id=" + id + "&str=" + str;
//   window.open(url,'GetParameter',features) ;
// }
// function mod_param_EtypTM52(e,id,str){
//   var value = document.getElementById(id).value;
//   var features = "location=0;titlebar=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=480,height=240";
//   var url = "Pages/addeditparamTM52.php?value=" + value + "&id=" + id + "&str=" + str;
//   window.open(url,'GetParameter',features) ;
// }
// function mod_param_EtypIAQ(e,id,str){
//   var value = document.getElementById(id).value;
//   var units = document.getElementById('air_supply_units').innerHTML;
//   var features = "location=0;titlebar=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=480,height=240";
//   var url = "Pages/addeditparamIAQ.php?value=" + value + "&id=" + id + "&str=" + str + "&units=" + units;
//   window.open(url,'GetParameter',features) ;
// }
// function enableSubmitButton(){
//   var an = document.getElementById('assmnt_name').value;
//   if(!isEmptyOrSpaces(an)){
//     document.getElementById('submit_button').disabled = false;
//   }else{
//     document.getElementById('assmnt_name').value = "";
//   }
// }
// function highlightName(mode){
//   var an = document.getElementById('assmnt_name').value;
//   if(isEmptyOrSpaces(an)){
//     if(mode == "y"){
//       document.getElementById('assmnt_name').style.background = "red";
//     }else{
//       document.getElementById('assmnt_name').style.background = "none";
//     }
//   }
// }
function isEmptyOrSpaces(str){
    return str === null || str.match(/^ *$/) !== null;
}
</script>
</head>

<body>

<datalist id=default>
  <option value=''>enter a number</option>
</datalist>
<datalist id=TM52>
  <option value='CIBSE TM52'>CIBSE TM52 adaptive overheating criteria</option>
  <option value=' '>or, enter a number</option>
</datalist>
<datalist id=ADF>
  <option value='approved document F'>approved document F fresh air requirement by number of bedrooms</option>
  <option value=' '>or, enter a number</option>
</datalist>

<table class='DataInputTable grey-text medium-text' id="the_table">
<?php include "Pages/domestic_table.html" ?>
<tr></tr><tr></tr>
<tr></tr><tr></tr>
<tr>
  <td width='25%'>Assessment name</td>
  <td colspan="2"><input type="text" placeholder="custom" name="assmnt_name" id="assmnt_name" class="coloured-cell grey-text" style='text-align: left;'></td>
  <td><center><input title='ready to submit' type='submit' id="submit_button" style='cursor:pointer;' class='medium-text grey-text' value='request'></center></td>
</tr>
</table>

</body>
</html>
<!---->
