<?php 

class SearchSummary {
  private $iterator;
  private $scope  = "";
  private $label;
  private $placeholder;
  
  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->placeholder))
        $this->placeholder = "placeholder='$entry->placeholder'";
      else
        $this->prompt = "";

      if (isset($entry->selectId))
        $this->selectId = $entry->selectId;
      else
        $this->selectId = 0;
    }
  }
  
  public function outputLayout() {
  
    $layout = "

$this->label
<div id='togsearch$this->scope'>    
  <input id='pat$this->scope' type='text' class='$this->iterator'
    name='$this->scope' $this->prompt
    onkeyup='res$this->scope (this.value)'>
  <div id='hint$this->scope' style='position:relative;'></div>
</div>
<div id=togselect$this->scope> 
  <select id='select$this->scope' class='$this->iterator' style='height:27px;' >
  </select>
</div>
    ";
	return $layout;
  }
  
  public function outputScript() {
		
    $script = " 	 

var selectId$this->scope = $this->selectId; 

$(document).ready(function() {
  document.getElementById('togselect$this->scope').style.display='none';
});

$(document).on('click', '#pat$this->scope', function (e) {
  document.getElementById('pat$this->scope').value = '';
  selectId$this->scope = 0;
//  send$this->scope (0,'');
});  

$(document).on('change', '#select$this->scope', function (e) {
  send$this->scope ($(this).val(), $(this).text());
});  

function sel$this->scope (frmValue, frmText) {
  document.getElementById('togsearch$this->scope').style.display='none';
  document.getElementById('togselect$this->scope').style.display='block';

  document.getElementById('hint$this->scope').innerHTML='';
  document.getElementById('hint$this->scope').style.border='0px';

  document.getElementById('pat$this->scope').value=frmText;

//  send$this->scope (frmValue, frmText);

  if (frmValue > 0)
    get$this->scope (frmValue);
}

function com$this->scope (message) {
  var key = Object.keys(message); 

logMessage ('<td>' + message[key][3] + '</td><td>$this->scope</td><td>' + key +  '</td><td>' + message[key][0] + '</td><td>' + message[key][2] + '</td><td>' + message[key][4] + '</td>');  

//  $('#valmsg$this->scope').html('');

  if (key == 'optionsId') {
    if (message[key][0] == 0) {
      document.getElementById('togsearch$this->scope').style.display='block';
      document.getElementById('togselect$this->scope').style.display='none';
    }

    else {
      document.getElementById('togsearch$this->scope').style.display='none';
      document.getElementById('togselect$this->scope').style.display='block';
      get$this->scope (message[key][0]);
    }  
  }      
}  

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

function set$this->scope (dbValue, dbText) {
//alert (dbValue + ' ' + dbText);
//  selectId$this->scope = dbValue;

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

  document.getElementById('pat$this->scope').value=dbText;
//  if (dbValue == 0)
//    document.getElementById('pat$this->scope').focus();
}

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

function send$this->scope (value, text) {
//alert ('$this->scope:' + value + ':' + text);
  if (value != selectId$this->scope) {
    selectId$this->scope = value;
  
    var msgContent = [];
    msgContent.$this->scope = [value, text, 'formentry', 'xxxxxxx', 'new field value'];
    newValue$this->iterator (msgContent);
  }    
}

function genHints$this->scope(list) {
  var layout='';

  for (var i=0; i<list.length; i++) {

    var hint = list[i].hint;
    var args = list[i].id + ', \'' + hint + '\'';

	layout += '<a href=\"#\" onclick=\"sel$this->scope(' + args + ');\">' + hint + '</a><br>';
  }	
  return layout;
} 

function res$this->scope (pattern) {

  if (pattern.length == 0) { 
    document.getElementById('hint$this->scope').innerHTML='';
    document.getElementById('hint$this->scope').style.border='0px';
  }

  else {
    var xhr = new XMLHttpRequest();
    var params = 'pattern=' + pattern;

    xhr.open('POST', 'ajax/res$this->scope.php', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function() {//Call function when the state changes.
      if(xhr.readyState == 4 && xhr.status == 200) {
        var response = JSON.parse(xhr.responseText)
     
        document.getElementById('hint$this->scope').innerHTML=genHints$this->scope(response);
        document.getElementById('hint$this->scope').style.border='1px';
      }
    }  
    xhr.send(params);
  }  
}

function genOptions$this->scope(options) {
  var layout='';

  var optLength = options.length;
  for (var i=0; i<optLength; i++) {
 
    var selected = '';
    var optValue = options[i].id;
    var optText = options[i].text

    if (optValue == selectId$this->scope || optLength == 1) {
      selected = ' selected';
      send$this->scope (optValue, optText);
    }
    else
      selectId$this->scope = 0;

    layout += '<option value=' + optValue + selected + '>' + optText + '</option>';
  }
  return layout;
} 
	  
function get$this->scope (id) {
  var xhr = new XMLHttpRequest();

  var params = 'sumid=' + id;

  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('select$this->scope').innerHTML=genOptions$this->scope(response);
    }
  }  
  xhr.send(params);
}


    ";
    return $script;
  }
}

?>
