<?php 

class LiveUniqInput {
  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 ($(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=dbValue;
//  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, 'formentry', '$this->scope', 'new field value'];
  newValue$this->iterator (msgContent);
}

// generate hint list
function hnt$this->scope(list) {
  var layout='';
//alert (list.length);
  
  if (list.length > 0)
    layout = '<a href=\"#/\" style=color:red>' + 'Similar names:' + '</a><br>'; 

  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>';
	layout += '<a href=\"#/\" >' + 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';
    send$this->scope ('', pattern);
  }

  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=hnt$this->scope(response);
        document.getElementById('hint$this->scope').style.border='1px';

        if (response.length > 0)  
          send$this->scope ('', pattern);
        else
          send$this->scope (pattern, pattern);
      }
    }  
    xhr.send(params);
  }  
}

    ";
    return $script;
  }
}

?>
