<?php 

class InputText  {
  private $field;
  private $dbValue;
  private $dbRowId;
  private $form;
  private $label;

  public function __construct($field) {

    if (isset($field)) {
      $this->field    = $field->field;
      $this->dbValue  = $field->dbValue;
      $this->dbRowId  = $field->dbRowId;
      $this->form = $field->form;

      if (isset($field->placeholder))
        $this->placeholder = $field->placeholder;
      else
        $this->placeholder = "";
              
      if (isset($field->label))
        $this->label = "<label>$field->label</label><br>";
      else
        $this->label = "";

      if (isset($field->attribute)) 
        $this->attribute = $field->attribute;
      else
        $this->attribute = "";
        
//      if (strpos($this->attribute, "readonly") !== false) 
//        $this->attribute = "$this->attribute style='background-color:lightgrey;'";
//print_r ($this->attribute);      
//print_r (strpos($this->attribute, "readonly"));
    }
    
  }

  public function outputLayout() {


    $layout = "

$this->label
<input type='text' id='txt$this->field' class='$this->form' 
  name='$this->field' value='$this->dbValue'
  placeholder='$this->placeholder' $this->attribute>
     ";
  
 	return $layout;
  }	

  public function outputScript() {

	$script = "

$(document).on('change', '#txt$this->field', function (e) {
  send$this->field ($(this).val(), '');
});

function set$this->field (value, text, source) {
  document.getElementById('txt$this->field').value = value;
}

multiFunction['set$this->field'] = {
  set$this->field:set$this->field
};  
	  
function send$this->field (value, text) {
  var msgContent = [];
  var sender = '$this->field';
  msgContent.$this->field = ['$this->field', value, text, '$this->form', 'new field value'];
  newValue$this->form (msgContent);
}

function com$this->field (message) {
  var key = Object.keys(message);

  if (key == 'selectId')
    get$this->field (message[key][1]);

  else if (key == 'disableId') {
//alert (message[key][1] + ' ' + document.getElementById('txt$this->field').readyOnly);
    if (message[key][1] == 0) {
      document.getElementById('txt$this->field').readOnly = false;
      $('#txt$this->field').attr('placeholder' ,'$this->placeholder');
    } 
    else {
      document.getElementById('txt$this->field').value = '';
      send$this->field ('', '');
      document.getElementById('txt$this->field').readOnly = true;
      $('#txt$this->field').attr('placeholder' ,'');
    }  
//alert (message[key][1] + ' ' + document.getElementById('txt$this->field').readyOnly);
  }
  
}  

multiFunction['com$this->field'] = {
  com$this->field:com$this->field
};  

function get$this->field (id) {

  if (id == 0) {
    document.getElementById('txt$this->field').value = '';
  }

  else {
    var params = 'selectid=' + id;

    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'ajax/get$this->field.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)
        document.getElementById('txt$this->field').value = response;
      }
    }  
    xhr.send(params);
  }
}


    "; 
    return $script;
  }
}

?>