 <?php 

class SelectList  {
  private $scope;
  private $label;
  private $dbValue;
  private $dbRowId;
  private $baseId;
  private $content;
  private $iterator;
  private $msglist;
  private $idProp;
  private $fKeyId;
  private $prompt;

  public function __construct($entry) {

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

      if (isset($entry->content))
        $this->content = $entry->content;
      else
        $this->content[] = 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] . "(dbRowId); ";
        }
      } 
    }
  }
  
  public function outputLayout() {
  
    $layout = "
  <input type='hidden' id='sel$this->scope' class='$this->iterator' name='sel$this->idProp' value='0'>
  <label for='$this->idProp'>$this->label</label><br>
  <select id='$this->idProp' class='$this->iterator' name='$this->idProp' value=0 style='height:27px;' >
    <option value=0 selected>$this->prompt</option>
     ";

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

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

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

 public function outputScript() {

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

	  $script = "
    
$(document).on('change', '#$this->idProp.$this->iterator', function (e) {
//  if ('$this->scope' == 'projectrole')
//    putpwpersonnel ();
  var dbRowId = $(this).val();
  $this->msgList
});  
    
function set$this->scope (dbRowId, name) {
     insert$this->scope ()
}      

function notify$this->scope (msgId) {
//alert (msgId)
  get$this->scope (msgId);
}  

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

  for (var i=0; i<content.length; i++) {
 
    selected = '';
    value = content[i].id;
    text = content[i].descr
   
    if (content[i].id == dbRowId)
      selected = ' selected';    
    
    layout += '<option value=' + value + selected + '>' + text + '</option>';
  }
  return layout;
} 
	  
function get$this->scope (id) {
	
  if (id == 0) { 
    document.getElementById('$this->idProp').innerHTML='';
    return;
  }

  var xhr = new XMLHttpRequest();
  var params = 'arg=' + id + '&' + 'baseid=$this->baseId';
  if ($this->fKeyId > 0)
    params += '&' + 'fkeyid=$this->fKeyId';

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


    "; 
    return $script;
  }

    
    
    
}

?>