<?php 

class SelectDate {

  private $scope;
  private $scopeId;
  private $baseId;
  private $label;
  private $date;
  private $iterator;
  private $timeStamp;

  public function __construct($entry) {
    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->scopeId  = $entry->scopeId;
      $this->baseId   = $entry->baseId;
      $this->date     = $entry->date;
      $this->label    = $entry->label;
      $this->iterator = $entry->iterator;
      
      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);
        $this->messageList = "";

        for ($i=0; $i<count($messages); $i++) {
          $this->messageList .= "notify" . $messages[$i] . "(date); ";
        }
      }
    } 
  }
  
  public function outputLayout() {

  $layout = "
<label for='$this->scope'>$this->label<br>
  <input type='date' id=date$this->scope class=$this->iterator name='$this->scope' value=''>
</label>
    "; 

    return $layout;
  }

  public function outputScript() {

    $script = "

var seldate$this->scope = '$this->date';
var scopeid$this->scope = $this->scopeId;

$(document).ready(function() {
  $('#date$this->scope').val(seldate$this->scope);
  var date = seldate$this->scope;
  if ('$this->scope' == 'workday') {
    putchk$this->scope (seldate$this->scope)
    $this->messageList
  }
});

$(document).on('change', '#date$this->scope', function (e) {
  var date = $(this).val();
  seldate$this->scope = date;
  putchk$this->scope (seldate$this->scope)
  $this->messageList
});

function notify$this->scope (msgId) {
  curr$this->scope = msgId;
  $('#msg$this->scope').html('');
  $('#hid$this->scope').val(msgId);
  get$this->scope(msgId);
}

function putchk$this->scope(date) {
  var params = 'date=' + date + '&' + 'baseid=$this->baseId';

  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/putchk$this->iterator.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText);
      scopeid$this->scope = response;
      
    }
  }  
  xhr.send(params);
}

    ";
    return $script;
  }
}

?>

