 <?php 

class SelectOption  {
  private $scope;
  private $label;
  private $selectId;
  private $baseId;
  private $options;
  private $iterator;
  private $msglist;
  private $column;
  private $prompt;

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope  = $entry->scope;
      $this->label  = $entry->label;
      $this->column = $entry->column;
      $this->scopeId = $entry->scopeId;
      $this->baseId = $entry->baseId;
      $this->selectId = $entry->selectId;
      $this->iterator = $entry->iterator;

      if (isset($entry->options))
        $this->options = $entry->options;
      else
        $this->options[] = NULL;

      if (isset($entry->prompt))
        $this->prompt = $entry->prompt;
      else
        $this->prompt = "Select";

      $this->msgList = "";
      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);
        for ($i=0; $i<count($messages); $i++) {
          $this->msgList .= "notify" . $messages[$i] . "(msgId); ";
        }
      } 
    }
  }
  
  public function outputLayout() {
  
    $layout = "
  <input type='hidden' id='selid$this->scope' class='$this->iterator' name='$this->column' value='$this->selectId'>
  <label for='$this->scope' style='width:100%'>$this->label<br>
  <select id='select$this->scope' class='select$this->scope' style='height:27px;' >
    <option value=0 selected>$this->prompt</option>
     ";

    foreach ($this->options as $item) {
      $value = $item->id;
      $text  = $item->descr;
 	  $attribute = "";	

      if ($item->id == $this->selectId)
        $attribute = " selected";

      $layout .= "
    <option value=" . $value . $attribute . " >" . $text . "</option>
      ";
    }
  
    $layout .= "
  </select>
  </label>
	";
	return $layout;
  }	

  public function outputScript() {

    $this->iterator = str_replace(" ", ".", $this->iterator);

    $script = "

var selid$this->scope = $this->selectId; 

$('#selid$this->scope').on('chg$this->iterator', function() {
  var value = $(this).val();
  $('#select$this->scope option[value='+value+']').prop('selected',true);
});

$(document).on('change', '.select$this->scope', function (e) {
  selid$this->scope = $(this).val();
  $('#selid$this->scope').val(selid$this->scope)
  
//  if ('$this->scope' == 'projectrole')
//    putpwpersonnel ();
    
  var msgId = selid$this->scope;
  $this->msgList
});  
    
function notify$this->scope (msgId) {
  get$this->scope (msgId);
}  

function gen$this->scope(options) {
  var dbRowId = $('#selid$this->scope').val();
  var layout='<option value=0 selected>Select</option>';
  var value;
  var text;
  var selected;

  for (var i=0; i<options.length; i++) {
 
    selected = '';
    value = options[i].id;
    text = options[i].descr
   
    if (options[i].id == dbRowId)
      selected = ' selected';    
    
    layout += '<option value=' + value + selected + '>' + text + '</option>';
  }
  return layout;
} 
	  
function get$this->scope (id) {
  var xhr = new XMLHttpRequest();

  var params = 'arg=' + id + '&' + 'scopeid=$this->scopeId';

  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('select$this->scope').innerHTML=gen$this->scope(response);
      document.getElementById('select$this->scope').focus();
    }
  }  
  xhr.send(params);
}

    "; 
    return $script;
  }
}

?>