<?php 

class InputText  {
  private $scope;
  private $dbValue;
  private $dbRowId;
  private $iterator;
  private $label;

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->dbValue  = $entry->dbValue;
      $this->dbRowId  = $entry->dbRowId;
      $this->iterator = $entry->iterator;

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

      if (isset($entry->attribute)) 
        $this->attribute = $entry->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->scope' class='$this->iterator' 
  name='$this->scope' value='$this->dbValue'
  placeholder='$this->placeholder' $this->attribute>
     ";
  
 	return $layout;
  }	

  public function outputScript() {

	$script = "

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

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

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

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

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

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

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

function get$this->scope (id) {

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

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

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


    "; 
    return $script;
  }
}

?>