<html>
<head>
  <link rel="stylesheet" type="text/css" href="../CSS/styles.css">
  <script>
    window.onload = function(){
      var url = new URL(window.location.href);
      var value = url.searchParams.get("value");
      if(value == ""){
        document.getElementById('value_none').checked = true;
      }else{
        document.getElementById('value_num').checked = true;
        document.getElementById('presc_val_txtbox').value = value;
      }
    }

    function updateParent(i){
      var v = setReturnValue();
      window.opener.document.getElementById(i).value = v;
      window.close();
    }

    function setReturnValue(){
      var v = "";
      if(document.getElementById('value_num').checked){
        v = document.getElementById('presc_val_txtbox').value;
      }else if(document.getElementById('value_none').checked){
        v = "";
        document.getElementById('presc_val_txtbox').value="";
      }
      return v;
    }
    function selectRadioButton(){
      var v = document.getElementById('presc_val_txtbox').value.replace(/\s+/g,"");// remove all whitespace
      if(typeof v == 'string' && v != "")document.getElementById('value_num').checked = true;
    }
  </script>
</head>
<body>
  <?php
    if(isset($_GET['value']))$value = $_GET['value'];
    if(isset($_GET['id']))$id = $_GET['id'];
    if(isset($_GET['str']))$str = $_GET['str'];
  ?>
  <div class='grey-text small-text'>
    <h3><?php echo $str;?></h3>
    <input type="radio" id="value_num" name="rtval" disabled>Prescribed value
    <input type='text'  name='presc_val_txtbox' id='presc_val_txtbox' style='text-align:center' value="<?php echo $value;?>" oninput="selectRadioButton()"><br>
    <input type="radio" id="value_none" name="rtval" onclick="setReturnValue();">None<br><br>
    <input type="submit" class="medium-text grey-text" value="OK" onclick="updateParent('<?php echo $id;?>');">
    <input type="submit" class="medium-text grey-text" value="Cancel" onclick="window.close();">
  </div>
</body>
</html>
