 <?php 

class SelectListAdd  {
  private $scope;
  private $scopeId;
  private $scopeIdAdd;
  private $scopeCopy;
  private $iterator;
  private $label;
  private $options;
  private $optDefault; 
  private $msglist;

  public function __construct($entry) {

    if (isset($entry)) {
      $this->scope      = $entry->scope;
      $this->scopeId    = $entry->scopeId;
      $this->scopeIdAdd = $entry->scopeIdAdd;
      $this->scopeCopy  = $entry->scopeCopy;
      $this->iterator   = $entry->iterator;
      $this->label      = $entry->label;
      $this->options    = $entry->options;
      $this->optDefault = $entry->optDefault;
//$cnt = count($this->options);       
//print_r ("cnt:$cnt");      

      $this->msgList = "";
      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);
        for ($i=0; $i<count($messages); $i++) {
          $this->msgList .= "notify" . $messages[$i] . "(curr$this->scope); ";
        }
      }
    }      
  }
  
  public function outputLayout() {
      
    $layout = "
<label for='sel$this->scope'>$this->label</label><br>
<select id='sel$this->scope' class=$iterator name='$this->scope' 
  onchange=putsel$this->scope(this.value) style='position:absolute;width:80px;z-index:1;' >
    ";

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

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

      $layout .= "
  <option value=" . $value . $attribute . " >" . $text . "</option>
      ";
    }
  
    if (count($this->options) == 0)
      $layout .= "
  <option value=-1>None</option>
	    ";
      
    $layout .= "
  <option value=0>Add</option>
</select>
<div id=err$this->scope style='color:red'></div>  
	  ";
	return $layout;
  }	

  public function outputScript() {

	  $script = "

var selid$this->scope = 0;
var curr$this->scope = 0;
$(document).ready(function() {
  curr$this->scope = $this->optDefault;
});
  
function notify$this->scope (msgId) {
  selid$this->scope = msgId;
  putsel$this->scope (curr$this->scope);
} 

function putsel$this->scope (optId) {
//alert ('putsel:' + optId);
  if (optId == 0) {
    $('#sel$this->scope')[0].setAttribute('onchange', 'putadd$this->scope(this.value)');
    getadd$this->scope();
  }
  else {  
    curr$this->scope = optId;
    $this->msgList
  }  
}      

function gen$this->scope(content) {
  var layout='';
  var value;
  var text;
  var attributes = '';
  var firstOf = true;
  
  if (content != null) {
  for (var i=0; i<content.length; i++) {
    selected = '';
    value = content[i].id;
    text = content[i].text
    
    if (content[i].added == '0') {
      $('#sel$this->scope').prop('size',10);
    }        
    else if (content[i].added == '1')  {
      attributes = ' disabled';
      text = text + ' &#10004;';
    }
    else if (content[i].added == '2') {
      attributes = ' selected';
      $('#sel$this->scope').prop('size',1);
    }        
    else if (content[i].added == '3' && firstOf) {
      attributes = ' selected';
      firstOf = false;
    }        
    layout += '<option value=' + value + attributes + '>' + text + '</option>';
  }
  }
//  if (content.length == 0)
    layout += '<option value=-1>None</option>';
  layout += '<option value=0>Add</option>';       

  return layout;
} 

// Previously added selections and addition selection to add
function getadd$this->scope () {

  var params = 'addlistid=$this->scopeIdAdd' + '&' + 'listid=$this->scopeId' +
               '&' + 'selid=' + selid$this->scope;
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/getadd$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)
     
      $('#sel$this->scope').html(gen$this->scope(response));
      $('#sel$this->scope').focus();
    }
  }  
  xhr.send(params);
}  


function getsel$this->scope (id) {

  var params = 'addid=$this->scopeIdAdd' + '&' + 'selid=$this->scopeId'  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/getsel$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)
      $('#sel$this->scope').html(gen$this->scope(response));
      $('#sel$this->scope').focus();
    }
  }  
  xhr.send(params);
}

// Assigned added item to the select list
function putadd$this->scope (id) {

  var params = 'rowid=' + id + '&' + 'selid=$this->scopeId'  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/putadd$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)
      $('#sel$this->scope').html(gen$this->scope(response));
      $('#sel$this->scope').focus();
      $('#sel$this->scope')[0].setAttribute('onchange', 'putsel$this->scope(this.value)');
      curr$this->scope = $('#sel$this->scope option[selected]').val();
      $this->msgList      
    }
  }  
  xhr.send(params);
}	

function dup$this->iterator() {
  if (curr$this->scopeCopy > 0) { 
    var params = 'copyid=' + curr$this->scopeCopy + '&' + 'listid=$this->scopeId';
  
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'ajax/dup$this->iterator.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);
        $('#sel$this->scope').html(gen$this->scope(response));
        $('#sel$this->scope').focus();
        $('#sel$this->scope')[0].setAttribute('onchange', 'putsel$this->scope(this.value)');
        curr$this->scope = $('#sel$this->scope option[selected]').val();
        $this->msgList      
      }
    }   
    xhr.send(params);
  }
  else
    $('#err$this->scope').html('Add travel to vehicle');
    
}

function del$this->iterator() {
  if (curr$this->scope > 0) { 
    
    var params = 'delid=' + curr$this->scope + '&' + 'listid=$this->scopeId' ;
  
    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'ajax/del$this->iterator.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);
        $('#sel$this->scope').html(gen$this->scope(response));
        $('#sel$this->scope').focus();
        $('#sel$this->scope')[0].setAttribute('onchange', 'putsel$this->scope(this.value)');
        curr$this->scope = (response.length > 0) ? response[0].id : 0;
        $('#sel$this->scope').find('option[value='+curr$this->scope+']').prop('selected', true);
        $this->msgList      
      }
    }  
    xhr.send(params);
  }
  
  else
    $('#err$this->scope').html('Nothing to delete');
}

    "; 
    return $script;
  }
}

?>