<?php 

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

  public function __construct ($entry) {
    if (isset($entry)) {
      $this->scope    = $entry->scope;
      $this->label    = $entry->label;
      $this->iterator = $entry->iterator;
      $this->searchId = $entry->searchId;
      $this->buttons  = $entry->buttons;

      if (isset($entry->placeHolder))
        $this->placeHolder = $entry->placeHolder;
      else
        $this->placeHolder = "";
    }
  }
  
  public function outputLayout() {
  
    $html = "

<label>$this->label</label><br>    
<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' class='hintlist'></div>

    ";
	return $html;
  }
  
  public function outputScript() {
		
    $script = " 	 

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

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

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

function set$this->scope (value, text) {
  document.getElementById('pat$this->scope').value=value;
} 

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

function send$this->scope (value, text) {
  var msgContent = [];
  msgContent.$this->scope = [value, text, 'sequent_frm', '---', 'new field value'];
  newValue$this->iterator (msgContent);
}

function com$this->scope (message) {
  var key = Object.keys(message); 
//alert (key + message[key][0]);  

  if (key == 'fkeyId') {
    fkeyId$this->scope = message[key][0];
  } 
  else if (key == 'selectId') {
    selectId$this->scope = message[key][0];
  }
}  

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

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;
} 

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 ('', '');
  }

  else {
  
    var xhr = new XMLHttpRequest();
    var params = 'pattern=' + pattern.trim() + '&' + 'fkeyid=' + fkeyId$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) {
          document.getElementById('hint$this->scope').innerHTML='';
          document.getElementById('hint$this->scope').style.border='0px';
          send$this->scope (pattern, pattern);
        }
        else {
          document.getElementById('hint$this->scope').innerHTML=hnt$this->scope(response);
          document.getElementById('hint$this->scope').style.border='1px solid #ddd';
          send$this->scope ('', '');
        }
      }    
    }  
    xhr.send(params);
  }  
}

    ";
    return $script;
  }
}

?>
