//
// whh 2020.02.19 switched to calling PHP to read data files uploading to JS with AJAX
//
// read clientsId.JSON file and extract Client Info
// read clientsId.JSON file and extract Client Info
// read clientsId.JSON file and extract Client Info

function loadPhoneNumbers() {

    console.log("Running - loadPhoneNumbers.js");
/*
  document.getElementById("sms").style.display = "block";
  document.getElementById("phone").style.display = "none";
  document.getElementById("selectNUM").style.display = "block";
  document.getElementById("firstName").value = "";
  document.getElementById('lastName').value = "";
  document.getElementById('typeDelete').style.display = "block";
  document.getElementById('mail').style.display = "block";
  document.getElementById('notes').style.display = "block";
*/
  $.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) {
  //console.table(data);
  //console.log(data.length);

// console.log("Load Phone Numbers for Select")

//load client names into select field
document.getElementById("selectedPhone").innerHTML = ""; // clear list before populating it

    var text = "";
      var x;
        for (x in data) {
            text += data[x];
              //console.log(data[x].clientId);
              //console.log(data[x].firstname);
              //console.log(data[x].name);
              //console.log(data[x].plate);
              //console.log(data[x].phone);

              // Sort the list of name alphabetically
                            var list_Objects = data;
                            //console.log(list_Objects);
                                list_Objects.sort(function(a,b){
                                    return a["phone"].localeCompare(b["phone"]);
                                });
                            //console.log(list_Objects);
                            var data = list_Objects;



//var options = data[x].firstname + " " + data[x].name + " " + data[x].phone;

            var options = data[x].phone ;
//console.log (options);
          var select = document.getElementById("selectedPhone");
          //document.getElementById("selected").textContent = "Select a Contact";

//console.log (select);
              var opt = options;
              var el = document.createElement("option");
              el.textContent = opt;
              el.value = opt;
              select.appendChild(el);
          }
        }
      }); //END AJAX call
      document.getElementById('selectedPhone').selectedIndex = 0;
      document.getElementById('selectedPhone').value = 'Default';
      // document.getElementById("phoneNumber").innerHTML = source; // display you phone number at page top

      topFunction(); // go to top of page

    };
