
// 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
// whh 2020.03.19 all console logs commented out
// When a contact is selected load it's data into the formSubmit

function loadPhoneNumberData(event) {

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

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

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

contactNumber = 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;
      phoneNumberUsed = [];

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

        var phoneNumberUsed = $.grep(objCon, function(obj, i){return obj.phone === contactNumber;});

        // console.table(contactUsed);

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

        document.getElementById('recipient').value = phoneNumberUsed[0].phone;
        document.getElementById('firstName').value = phoneNumberUsed[0].firstname;
        document.getElementById('lastName').value = phoneNumberUsed[0].name;
        document.getElementById('type').options[0].text = phoneNumberUsed[0].type;
        document.getElementById("mail").value = phoneNumberUsed[0].mail;
        document.getElementById('notes').value  = phoneNumberUsed[0].notes;
        document.getElementById('selected').value = phoneNumberUsed[0].clientId + ": " + phoneNumberUsed[0].phone;


        // console.log("line 52: " + phoneNumberUsed[0].clientId + ": " + phoneNumberUsed[0].phone);

  } // END success

}) // END ajax call


};
