<?php 

class LiveSearch {
  private $scope;
  private $label;
  private $iterator;
  private $dbValue;
  private $dbRowId = 0;
  private $sendId;
  private $placeholder;

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

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

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

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

$this->label    
<input id='pat$this->scope' type='text' class='$this->iterator'
  name='$this->scope' placeholder='$this->placeHolder'
  onkeyup='res$this->scope (this.value)'>
<div id='hint$this->scope' style='position:relative;'></div>
    ";
	return $html;
  }
  
  public function outputScript() {
		
    $script = " 	 


var setState$this->scope = 'formentry';

$(document).ready(function() {
  document.getElementById('pat$this->scope').value = '';
//  document.getElementById('pat$this->scope').focus();
});

// blank input when clicked
$(document).on('click', '#pat$this->scope', function (e) {
  document.getElementById('pat$this->scope').value = '';
//  document.getElementById('pat$this->scope').focus();
});

// send (only) input text to formdata
$(document).on('change', '#pat$this->scope', function (e) {
//alert ($(this).val()); 
  send$this->scope (0, $(this).val());
});

// on selecting hint, send id and text to formdata
function sel$this->scope (frmValue, frmText) {
  document.getElementById('pat$this->scope').value=frmText;
  document.getElementById('hint$this->scope').innerHTML='';
  document.getElementById('hint$this->scope').style.border='0px';
  send$this->scope (frmValue, frmText);
}

// set form value and text from formdata
function set$this->scope (dbValue, dbText) {
//alert ('set:' + dbValue + ' ' + dbText);
  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
}; 

// send form value and text to formdata
function send$this->scope (value, text) {
//alert ('send');
  var msgContent = [];
  msgContent.$this->scope = [value, text, 'sequent_ent', '$this->scope', 'new field value'];
  newValue$this->iterator (msgContent);
}

// generate hint list
function hnt$this->scope(list) {
  var layout='';

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

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

	layout += '<a href=\"#/\" onclick=\"sel$this->scope(' + args + ');\">' + inner + '</a><br>';
  }	
  return layout;
} 
 
// live search query for current pattern text 
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)

//        if (pattern.length == 1) { 
//          send$this->scope (0, pattern);
//        }   
     
        document.getElementById('hint$this->scope').innerHTML=hnt$this->scope(response);
        document.getElementById('hint$this->scope').style.border='1px';
      }
    }  
    xhr.send(params);
  }  
}

    ";
    return $script;
  }
}

?>
