 <?php 

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

  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->dbValue = $entry->dbValue;
      $this->iterator = $entry->iterator;

      if (isset($entry->lookup)) {
        $dal = new DAL();
        $this->lookup = $dal->query($entry->lookup);
      }  
      else
        $this->lookup[] = NULL;
    
      if (isset($entry->content)) {
        $this->content = $entry->content;
      }  
      else
        $this->content[] = NULL;

      $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 = "
  <label for='$this->idProp'>$this->label</label><br>
  <select id='$this->idProp' class='$this->iterator' name='$this->idProp' value=0 style='height:28px;' >
    <option value=0 selected>Select</option>

    ";

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

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

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

    public function outputScript() {

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

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

function gen$this->scope(content) {
  var layout='<option value=0 selected>Select</option>';
  var value;
  var text;
  var selected;
  var dbRowId = 0;
  for (var i=0; i<content.length; i++) {
    selected = '';
    value = content[i].id;
    text = content[i].text
   
    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;
  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;
  }
}

?>