<?php 

class FkeyField  {
 private $scope;
  private $label;
  private $iterator;

  public function __construct($entry) {
    if (isset($entry)) {
      $this->scope  = $entry->scope;
      $this->iterator = $entry->iterator;

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

      if (isset($entry->indexMask))
         $this->indexMask = $entry->indexMask;
      else
        $this->indexMask = 1;

      if (isset($entry->complete))
         $this->complete = $entry->complete;
      else
        $this->complete = 110;

    }
  }
  
  public function outputLayout() {
  
    $layout = "
      <input type='text' id='inp$this->scope'> 
	";

	return $layout;
  }	

  public function outputScript() {

    $script = "

// define data structure once
if (SearchStatus === undefined) {

  // object definition
  function SearchStatus (formid) {
    this.formid = formid; 

    this.setBits = function(id, index) {
      var st = SearchStatus.prototype.complete;

      if (id > 0) 
        SearchStatus.prototype.complete = index | st;
      else
        SearchStatus.prototype.complete = ~index & st;

      SearchStatus.fkey = id;
      
//alert (id + '-' + st + '-' + SearchStatus.prototype.complete + '-'+ index + '-' +  SearchStatus.fkey);
    };
    this.setClear = function(i) {
      SearchStatus.prototype.clear = i;
    };

  }
  SearchStatus.prototype.complete = 000;
  SearchStatus.prototype.clear = 0;
}  
 
// instantiate object for this form 
var stat$this->scope = new SearchStatus('$this->iterator');
stat$this->scope.form = '$this->iterator';
stat$this->scope.fkey = 0;
//alert ('index$this->iterator:' + form$this->scope.index + ' $this->indexMask')
//form$this->scope.index = parseInt($this->hidden ? '000' : '1');

var index$this->scope = '$this->indexMask';
//alert ('set idx:' + index$this->scope);

function set$this->scope (value, text) {
  stat$this->scope.setClear(0);
//alert (value + ' ' + stat$this->scope.fkey);

  if (index$this->scope == 1) {
//alert ('$this->scope ' + value + ' ' + stat$this->scope.fkey);
    $('#$this->scope option[value='+ value +']').prop('selected',true);
  }

 if (value > 0 && stat$this->scope.fkey == 0) {
    var fkyText = document.getElementById('inp$this->scope');
    if (fkyText) 
      document.getElementById('inp$this->scope').value = value;
    stat$this->scope.setBits (value, '$this->indexMask');
    stat$this->scope.fkey = value;
    send$this->scope (value, '', 'sequent_ent');
  }
  
  else if (value == 0 && stat$this->scope.fkey > 0) {
    var source = (stat$this->scope.complete == '$this->complete') ? 'sequent_upd' : 'sequent_frm'; 
    send$this->scope (stat$this->scope.fkey, '', source);
  }    
}

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

function send$this->scope (value, text, source) {
  var msgContent = [];
  msgContent.$this->scope = [value, text, source, index$this->scope, 'new fkey'];
  newValue$this->iterator (msgContent);
}

function com$this->scope (message) {
  var key = Object.keys(message);
logMessage ('<td>com msg</td><td>' + key + '</td><td>$this->scope</td><td>' + message[key][0] + '</td><td>' + message[key][1] + '</td><td>' + message[key][2] + '</td><td>' + message[key][3] + '</td><td>' + message[key][4] + '</td>');  

  if (key == 'fkeyId') {
    var prevComp = stat$this->scope.complete;
    var fkyText = document.getElementById('inp$this->scope');
    if (fkyText) 
      document.getElementById('inp$this->scope').value = message[key][0];

    stat$this->scope.setBits (message[key][0], '$this->indexMask');
    stat$this->scope.fkey = message[key][0];
    var index = '$this->indexMask';
    
    var nindex = ~(index);
    var field = '$this->scope';
    
//alert ('field:' + field.substr(0,3) + ' i:' + index + ' comp:' + stat$this->scope.complete + ' other:' + (~index & stat$this->scope.complete) + ' fkey:' + stat$this->scope.fkey);
    var other = ~index & stat$this->scope.complete;
    if (message[key][0] == 0) {
//alert ('$this->scope:fkey=0' + message[key][0]);    
      if (stat$this->scope.clear == 0) {
        var msgContent = [];
        msgContent.$this->iterator = [0, '', 'sequent_upd', index$this->scope, 'init'];
        newValue$this->iterator (msgContent);
        if (prevComp == 0)
          stat$this->scope.setClear(1);
      }
      if (field.substr(0,3) == 'map') 
        map$this->iterator ();
    }
    
    else if (other == 0) {
      stat$this->scope.setClear(0);
      var summaryId = stat$this->scope.fkey;
//alert ('$this->scope other == 0:' + summaryId);    
      send$this->scope (summaryId, '', 'sequent_frm');
    }
    else if (other > 0 && field.substr(0,3) == 'map') {
//alert ('other > 0');    
      send$this->scope (message[key][0], '', 'sequent_upd');
      map$this->iterator ();
    }
  }    
}

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

    "; 
    return $script;
  }
}

?>