<?php 

class LiveHintCreate {
  private $scope;
  private $label;
  private $iterator;
  private $placeholder;

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

      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' style='position:relative;'></div>
    ";
	return $html;
  }
  
  public function outputScript() {
		
    $script = " 	 

var unique$this->scope = 0;
var summaryId$this->scope = 0;

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

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';
//  document.getElementById('pat$this->scope').focus();
  
  send$this->scope (frmText, unique$this->scope);
}

function set$this->scope (value, text, source) {
  selid$this->scope = value;
//  document.getElementById('inp$this->scope').value = '';

//  document.getElementById('togunique$this->scope').style.display='block';
//  document.getElementById('togsearch$this->scope').style.display='unique';
  
} 

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

function send$this->scope (value, text) {
//alert ('$this->scope:' + value);        

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


function com$this->scope (message) {
  var key = Object.keys(message); 
//  $('#valmsg$this->scope').html('');

  if (key == 'summaryId') {
    summaryId$this->scope = message[key][0];
    if (message[key][0] == 0) {
//      document.getElementById('togsearch$this->scope').style.display='block';
//      document.getElementById('togunique$this->scope').style.display='none';
    }

    else {
//      document.getElementById('togsearch$this->scope').style.display='none';
//      document.getElementById('togunique$this->scope').style.display='block';
    }  
  }      
}  

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



function gen$this->scope(list) {

  var layout='';

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

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

	layout += '<a href=\"#\" onclick=\"sel$this->scope(' + args + ');\">' + hint + '</a><br>';
  }	
  return layout;
} 
 
 
function res$this->scope (pattern) {

  send$this->scope (pattern, unique$this->scope);

  if (pattern.length == 0) { 
    unique$this->scope = 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 (response == '') {
          send$this->scope (pattern, unique$this->scope);
          unique$this->scope = 1;
          document.getElementById('hint$this->scope').innerHTML='';
          document.getElementById('hint$this->scope').style.border='0px';
        }
        else {
          unique$this->scope = 0;
          document.getElementById('hint$this->scope').innerHTML = 
            gen$this->scope(response);
          document.getElementById('hint$this->scope').style.border='1px';
        }
      }    
    }  
    xhr.send(params);
  }  
}

    ";
    return $script;
  }
}

?>
