<?php 

class LiveSearch {
  private $iterator;
  private $scope  = "";
  private $dbValue = "";
  private $dbRowId = 0;
  private $message = "";
  private $label;
  
  public function __construct ($entry) {
    if (isset($entry)) {
      $this->label    = $entry->label;
      $this->scope    = $entry->scope;
      $this->scope    = $entry->scope;
      $this->iterator = $entry->iterator;
      $this->dbValue  = $entry->dbValue;
      $this->dbRowId  = $entry->dbRowId;
      $this->message  = $entry->message;
 
      if ($this->dbValue === "")
        $this->attribute = "placeholder='Type some characters'";
      else
        $this->attribute = "value='$this->dbValue'";

      if (null !== $entry->message) {
        $messages = explode(",", $entry->message);

        $this->messageList = "";
        for ($i=0; $i<count($messages); $i++) {
          $this->messageList .= "notify" . $messages[$i] . "(dbRowId); ";
        }
      }  
    }
  }
  
  public function outputLayout() {
  
    $html = "
<label for='row$this->scope' style='width:100%'>$this->label<br>
	<input type='hidden' id='row$this->scope' name='row$this->scope' class='$this->iterator' value=$this->dbRowId>
  <input id='live$this->scope' type='text' class='live$this->scope $this->iterator'
    name='$this->scope'	$this->attribute autofocus onkeyup='search$this->scope (this.value)'>
</label>    
<div id='hint$this->scope' style='position:relative;'></div>
    ";
	return $html;
  }
  
  public function outputScript() {
		
    $script = " 	 

var dbRowId = document.getElementById('row$this->scope').value;

if (dbRowId < 1)
  var dbRowId = document.getElementById('live$this->scope').value;

if (dbRowId > 0) {
  $this->messageList
}

$('#row$this->scope').on('chg$this->iterator', function() {

  var dbRowId = $(this).val();
  if (dbRowId > 0)
    $this->messageList
});
  
$(document).on('click', '#live$this->scope', function (e) {
  $('#live$this->scope').val('');
  $('#row$this->scope').val(0);
 });  
  
function set$this->scope (dbRowId, name) {

  document.getElementById('hint$this->scope').innerHTML='';
  document.getElementById('hint$this->scope').style.border='0px';
  document.getElementById('live$this->scope').value=name;
  document.getElementById('row$this->scope').value=dbRowId;
  if (dbRowId < 1)
    dbRowId = name;  

  $this->messageList
}

function gen$this->scope(list) {

  var layout='';
  var inner;

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

	if (list[i].refno == '')
	  inner = list[i].name;
    else
	  inner = list[i].refno + ': ' + list[i].name;
    args = list[i].id + ', \'' + inner + '\'';

	layout += '<a href=\"#\" onclick=\"set$this->scope(' + args + ');\">' + inner + '</a><br>';
  }	
  return layout;
 
} 
 
function search$this->scope (str) {
  if (str.length==0) { 
    document.getElementById('hint$this->scope').innerHTML='';
    document.getElementById('hint$this->scope').style.border='0px';
    return;
  }

  var xhr = new XMLHttpRequest();
  var params = 'arg=' + document.getElementById('live$this->scope').value;

  xhr.open('POST', 'ajax/search$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('hint$this->scope').innerHTML=gen$this->scope(response);
      document.getElementById('hint$this->scope').style.border='1px';
    }
  }  
  xhr.send(params);
}

    ";
    return $script;
  }
}

?>
