<?php 

class LiveSearch {
  private $field;
  private $label;
  private $form;
  private $dbValue;
  private $dbRowId = 0;
  private $sendId;
  private $placeHolder;

  public function __construct ($entry) {
    if (isset($entry)) {
      $this->field   = $entry->field;
      $this->form    = $entry->form;
      $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->field' type='text' class='$this->form'
  name='$this->field' placeholder='$this->placeHolder'
  onkeyup='res$this->field (this.value)'>
<div id='hint$this->field' class='hintlist'></div>
    ";
	return $html;
  }
  
  public function outputScript() {
		
    $script = " 	 


var fkeyId$this->field = 0;

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

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

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

$('#pat$this->field').focusout (function() {
  $('input, select, a, textarea').click(function(event){
    if (event.target.nodeName != 'a')
      document.getElementById('hint$this->field').innerHTML='';
  });
});

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

// set form value and text from formdata
function set$this->field (dbValue, dbText) {
//alert ('set:' + dbValue + ' ' + dbText);
  document.getElementById('pat$this->field').value=dbText;
//  if (dbValue == 0)
//    document.getElementById('pat$this->field').focus();
}

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

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

function set$this->form (value) {
  document.getElementById('valmsg$this->form').innerHTML='';
  document.getElementById('pat$this->field').value = '';
  fkeyId$this->field = value;
}

// generate hint list
function hnt$this->field(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->field(' + args + ');\">' + inner + '</a><br>';
  }	
  return layout;
} 
 
// live search query for current pattern text 
function res$this->field (pattern) {
  if (pattern.length == 0) { 
    document.getElementById('hint$this->field').innerHTML='';
    document.getElementById('hint$this->field').style.border='0px';
  }

  else if (fkeyId$this->field == 0) { 
    document.getElementById('valmsg$this->form').innerHTML='Select an active status';
    document.getElementById('pat$this->field').value = '';
//    document.getElementById('hint$this->field').innerHTML='';
//    document.getElementById('hint$this->field').style.border='0px';
  }

  else {
    var xhr = new XMLHttpRequest();
    var params = 'pattern=' + pattern + '&' + 'fkeyid=' + fkeyId$this->field;

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

    ";
    return $script;
  }
}

?>
