
// whh 2019.12.27
// this fills in the SELECT option for contacts in the form field
// whh 2020.02.19 switched to calling PHP to read data files uploading to JS with AJAX
//
// When a contact is selected load it's data into the formSubmit

function loadData(event) {

console.log("Running - loadContactData.js");

  document.getElementById("selectNUM").style.display = "block";
  document.getElementById("selectPhoneNumber").style.display = "none";

// alert(this.options[this.selectedIndex].text);

contact = this.options[this.selectedIndex].text;

var contactBoth = contact.split(":");
var contactId = contactBoth[0];
var contactNumber = contactBoth[1];
var contactNumber = contactNumber.replace(/ /g,''); // remove spaces



// console.log ("Contact Selected: " + contact + " ContactId:" + contactId + " Contact Number:" + contactNumber);

$.ajax({
    url: "ContactsAjax.php",
    async: false,   // asynchronous request? (synchronous requests are discouraged...)
    cache: false,   // with this, you can force the browser to not make cache of the retrieved data
    dataType: "json",  // jQuery will infer this, but you can set explicitly
    success: function(data) {

      objCon = data;
      contactUsed = [];

// console.table(objCon);
//console.log(objCon.length);

        var contactUsed = $.grep(objCon, function(obj, i){return obj.clientId === contactId;});

// console.log(contactUsed[0].type);

        document.getElementById('recipient').value = contactUsed[0].phone;
        document.getElementById('firstName').value = contactUsed[0].firstname;
        document.getElementById('lastName').value = contactUsed[0].name;
        document.getElementById('type').options[0].text = contactUsed[0].type;
        document.getElementById("email").value = contactUsed[0].email;
        document.getElementById('notes').value  = contactUsed[0].notes;

  } // END success

}) // END ajax call


};
