<?php 

class LiveUnique {
  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>    
<div id='togsearch$this->scope'>    
  <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>
</div>

<div id='togunique$this->scope'>    
  <select id='uniq$this->scope' class='$this->iterator' name='$this->scope' value=0 style='height:27px;' >
    <option value=-1>Select</option>
    <option value=0>Add</option>
  </select>

  <span style='display:inline;'>
    <input id='input$this->scope' type='text' style='width:100%;'>
  </span>  
  <span style='display:inline;'>
    <button id='check$this->scope' class='btn btn-inverse'>Unique</button>
  </span>
</div>  

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

var summaryId$this->scope = 0;

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

$(document).on('change', '#uniq$this->scope', function (e) {
  if ($(this).val() == 0) {
    document.getElementById('uniq$this->scope').style.display='none';
    document.getElementById('input$this->scope').style.display='block';
    document.getElementById('check$this->scope').style.display='block';
    document.getElementById('input$this->scope').focus();
  }
});  

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

$(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 (dbValue, dbText) {
//  document.getElementById('pat$this->scope').value=dbText;
//  if (dbValue == 0)
//    document.getElementById('pat$this->scope').focus();
//}

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

  document.getElementById('check$this->scope').style.display='none';
  document.getElementById('input$this->scope').style.display='none';
  document.getElementById('uniq$this->scope').style.display='block';
  
  var inner = '';
  if (value == '') {
    inner += '<option value=-1>Select</option>';
    inner += '<option value=0>Add</option>';
  }    
  else {    
    inner += '<option value=' + value + ' selected disabled>' + text + '</option>';
    inner += '<option value=0>Update</option>';
  }    
  document.getElementById('uniq$this->scope').innerHTML = inner;
} 


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 += '<a href=\"#\" onclick=\"sel$this->scope(' + args + ');\">' + hint + '</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';
  }

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

?>
