<?php 

class LiveHintCreate {
  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;

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

<label>$this->label</label><br> 
<table>
  <tr>
    <td style='width:80%;'>
       <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;color:red'></div>
    </td>
    <td style='width:20%;'>
      <div id='togbut$this->scope'>
        <button id='new$this->scope' class='btn btn-inverse'>Create</button>
      <div>    
    </td>
  </tr>
</table>  
    ";
	return $html;
  }
  
  public function outputScript() {
		
    $script = " 	 

var summaryId$this->scope = 0;

$(document).ready(function() {
  document.getElementById('togbut$this->scope').style.display='none';
});

$(document).on('click', '#new$this->scope', function (e) {
  var text = $('#input$this->scope').val();
  send$this->scope (0, text);
  
//alert ('val:' + selid$this->scope);
//  if (text != '') {
//    add$this->scope (text);
//  }
//  else {
//    document.getElementById('togbut$this->scope').style.display='none';
//  }
});  

$(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 (frmValue, frmText);
}

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) {
  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 += hint + '<br>';
  }	
  return layout;
} 
 
 
function res$this->scope (pattern) {
  if (pattern.length == 0) { 
    document.getElementById('togbut$this->scope').style.display='none';
    document.getElementById('hint$this->scope').innerHTML='';
    document.getElementById('hint$this->scope').style.border='0px';
  }

  else {
  
    var xhr = new XMLHttpRequest();
    var params = 'pattern=' + pattern.trim();

    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 == '') {
          document.getElementById('togbut$this->scope').style.display='block';
          document.getElementById('hint$this->scope').innerHTML='';
          document.getElementById('hint$this->scope').style.border='0px';

        }
        else {
          document.getElementById('togbut$this->scope').style.display='none';
          document.getElementById('hint$this->scope').innerHTML=gen$this->scope(response);
          document.getElementById('hint$this->scope').style.border='1px solid #ddd';
        }
      }    
    }  
    xhr.send(params);
  }  
}

// Previously added selections and addition selection to add
function chk$this->scope (name) {
  var params = 'chkname=' + name;  
  
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/chk$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText);
      if (response != '') {
        $('#valmsg$this->iterator').html('');
        add$this->scope (response);
      }
      else {
        $('#valmsg$this->iterator').html('Feature Name is not unique');
      } 
    }
  }  
  xhr.send(params);
}  

// Assigned added item to the select list
function add$this->scope (name) {
  var params = 'addname=' + name + '&' + 'selid=' + selid$this->scope + 
               '&' + 'summaryid=' + summaryId$this->scope;
               
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'ajax/add$this->scope.php', true);
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onreadystatechange = function() {//Call a function when the state changes.
    if (xhr.readyState == 4 && xhr.status == 200) {
      var response = JSON.parse(xhr.responseText);
      selid$this->scope = response; 
      set$this->scope (name, name, '');
      send$this->scope (selid$this->scope, name, '');

//      if ('$this->summaryTable' != '') {
//        var msgContent = [];
//        msgContent.updateId = [response,'', 'updateId', '$this->scope', 'updated row id'];
//        com$this->summaryTable (msgContent);
//      }
    }
  }  
  xhr.send(params);
}	


    ";
    return $script;
  }
}

?>
