<?php 

class clickCheckbox  {
  private $scope;
  private $iterator;
  private $label;
  private $formDefs;
  private $formAlts;
  
  
  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->iterator = $entry->iterator;
      $this->label    = $entry->label;
    }
  
    if (null !== $entry->formAlts)
      $this->formAlts = json_encode($entry->formAlts);
    else
      $this->formAlts = '{}';

    if (null !== $entry->formDefs)
      $this->formDefs = json_encode($entry->formDefs);
    else
      $this->formDefs = '{}';
  }


  public function outputLayout() {

  $layout = "
    <input type='hidden' id=chkval$this->scope class=$this->iterator name='$this->scope' value=0> 
    <label style='width:100%'>$this->label
      <input type='checkbox' id='check$this->scope'>
    </label>
  ";
  
 	return $layout;
  }	

  public function outputScript() {

    $script = "
 
$('#check$this->scope').on('click', function() {

  var alt$this->scope = JSON.parse('$this->formAlts');
  var def$this->scope = JSON.parse('$this->formDefs');
//alert ( alt$this->scope.length);
  if ( $(this).prop('checked') ) {
    $('#chkval$this->scope').val(1);
    if (def$this->scope != '[]')
      detSet$this->scope(alt$this->scope);
  }  
  else {
    $('#chkval$this->scope').val(0);
    if (alt$this->scope != '[]')
      detSet$this->scope(def$this->scope);
  }   
});


$('#chkval$this->scope').on('chg$this->iterator', function() {
  if ( $(this).val() == 1 )
    $('#check$this->scope').prop('checked', true);
  else
    $('#check$this->scope').prop('checked', false);
});

function detSet$this->scope (resp) {
  $('.$this->iterator').each(function(){
//alert ($(this).prop('name') + ' ' + resp[$(this).prop('name')]);    
    if (($(this).prop('type') == 'text') || ($(this).prop('type') == 'hidden'))
      $(this).prop('value', resp[$(this).prop('name')]).trigger('chg$this->iterator');
    else if ($(this).prop('type') == 'time')
      $(this).prop('value', resp[$(this).prop('name')]);
    else if ($(this).prop('type') == 'date')
      $(this).prop('value', resp[$(this).prop('name')]);
    else if($(this).is('textarea')) 
      $(this).val(resp[$(this).prop('name')]);
    else if($(this).is('select')) {
      var selId = $(this).prop('name'); // 
      var optId = resp[$(this).prop('name')];
      $('#'+selId+' option[value='+optId+']').prop('selected',true);
    }
  }); 
}

      
    "; 
    
    return $script;
  }
}

?>