<?php 

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

  public function __construct($field) {
    if (isset($field)) {
      $this->field  = $field->field;
      $this->form = $field->form;

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

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

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

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

	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->field = new SearchStatus('$this->form');
stat$this->field.form = '$this->form';
stat$this->field.fkey = 0;
//alert ('index$this->form:' + form$this->field.index + ' $this->indexMask')
//form$this->field.index = parseInt($this->hidden ? '000' : '1');

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

function set$this->field (value, attr) {

  $('#$this->field option[value='+ value +']').prop('selected',true);

//  stat$this->field.setClear(0);
//alert (value + ' ' + stat$this->field.fkey);

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

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

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

function send$this->field (value, attr, source) {
  var msgContent = [];
  msgContent.$this->field = [value, attr, source, index$this->field, 'new fkey'];
//  newValue$this->form (msgContent);
}

function com$this->field (message) {
  var key = Object.keys(message);
logMessage ('<td>com msg</td><td>' + key + '</td><td>$this->field</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->field.complete;
//    var fkyText = document.getElementById('inp$this->field');
//    if (fkyText) 
//      document.getElementById('inp$this->field').value = message[key][0];

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

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

    "; 
    return $script;
  }
}

?>