<?php 

class SearchSummary {
  private $iterator;
  private $scope  = "";
  private $label;
  private $placeholder;
  
  public function __construct ($entry) {
    if (isset($entry)) {
      $this->label    = $entry->label;
      $this->scope    = $entry->scope;
      $this->iterator = $entry->iterator;
      $this->clear    = $entry->clear;
      $this->disabled = $entry->disabled;
      $this->summary  = $entry->summary;

      if (isset($entry->summaryId))
        $this->summaryId = $entry->summaryId;
      else
        $this->summaryId = 0; 

      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->placeholder = "";

      if (isset($entry->clear))
        $this->clear = "display:$entry->clear";
      else
        $this->clear = ""; 

      if (isset($entry->disabled))
        $this->disabled = $entry->disabled;
      else
        $this->disabled = ""; 

    }
  }
  
  public function outputLayout() {
  
    $layout = "

$this->label
<div class='con$this->scope'>    
  <table style='border:1px solid #A9A9A9;'>
    <tr>
      <td style='width:90%;height:28px;'>
        <input id='pat$this->scope' type='text' class='$this->iterator'
          name='$this->scope' $this->placeholder $this->disabled
          onkeyup='res$this->scope (this.value)' style='border:none;'>
      </td>
      <td style='width:10%;height:28px;'>
        <button id='clr$this->scope' class='btn btn-inverse btn-xs' 
          style='width:50px;$this->clear'>Clear</button>
      </td>
   </tr>
   </table> 
  <div id='hint$this->scope' class='hintlist''></div>
</div>
    ";
	return $layout;
  }
  
  public function outputScript() {
		
    $script = " 	 

var summaryId$this->scope = 0;
var selectId$this->scope = 0;
var fkeyId$this->scope = 0;

$(document).ready(function() {
  var str = '$this->summaryId';
  summaryId$this->scope = (isNaN (str)) ? str : parseInt (str);
//  sendId$this->scope (summaryId$this->scope,'');
});

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

$(document).on('click', '#clr$this->scope', function (e) {
  document.getElementById('hint$this->scope').innerHTML='';
  document.getElementById('hint$this->scope').style.border='0px';
  send$this->scope ('', '');
  sendId$this->scope (0, '');
});  

function sel$this->scope (frmValue, frmText) {
  document.getElementById('hint$this->scope').innerHTML='';
  sendId$this->scope (frmValue, frmText);  
}

//$('#pat$this->scope').focusout (function() {
//  alert('pat target = ' + event.target.tagName + ', this=' + this.tagName);
//});

//$('.con$this->scope').focusout (function() {
//  alert('con target = ' + event.target.tagName + ', this=' + this.tagName);
//});

//$('#hint$this->scope').focusin (function() {
//alert('hint target = ' + event.target.tagName + ', this=' + this.tagName);
//});

//$('.hintlist').focusin (function() {
//alert('target = ' + event.target.tagName + ', this=' + this.tagName);
//});

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>');  

//  $('#valmsg$this->scope').html('');
//alert ('com$this->scope:' + key + message[key][0] + ' ' + summaryId$this->scope);
  if (key == 'summaryId') {
    summaryId$this->scope = message[key][0];
    getsum$this->scope (summaryId$this->scope);
  }      
  
  else if (key == 'fkeyId') {
    summaryId$this->scope = message[key][0];
  }      

  else if (key == 'summaryInd') {
    if (summaryId$this->scope > 0 && message[key][0] == 0)
      getsum$this->scope (summaryId$this->scope);
  }      

  else 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) {
  document.getElementById('hint$this->scope').innerHTML='';
  document.getElementById('pat$this->scope').value = dbValue;
}

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

function sendId$this->scope (value, text) {
  var msgContent = [];
  msgContent.$this->iterator = [value, text, 'sequent_upd', '---', 'new name id'];
  newValue$this->iterator (msgContent);
}

function send$this->scope (value, text) {
  var msgContent = [];
  msgContent.$this->scope = [value, text, 'sequent_upd', '---', 'new name'];
  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=\"#/\" class=hints$this->scope onclick=\"sel$this->scope(' + args + ');\">' + hint + '</a><br>';
  }	
  return layout;
} 

function res$this->scope (pattern) {

  selectId$this->scope = 0;

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

  else {
    var xhr = new XMLHttpRequest();
    var params = 'pattern=' + pattern + '&' + 'fkey=' + summaryId$this->scope;

    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)
        if (response.length == 0) {
          send$this->scope (pattern, pattern);
//          sendId$this->scope (selectId$this->scope, pattern);
          document.getElementById('hint$this->scope').innerHTML='';
        }
        else {
          send$this->scope (pattern, pattern);
//          sendId$this->scope (selectId$this->scope, '');
          document.getElementById('hint$this->scope').innerHTML=genHints$this->scope(response);
        }
      }
    }  
    xhr.send(params);
  }  
}

    ";
    return $script;
  }
}

?>
