
//
// whh 2020.02.19  switched to calling PHP file to load the JSON data
//
//
//
// 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 loadContactId() {

      console.log("Running - loadContacts.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: "data/contacts.json",
      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.log(data);
  //console.log(data.length);
  */

  $.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);


//load client names into select field
document.getElementById("selected").innerHTML = "<br>"; // 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["name"].localeCompare(b["name"]);
                                });
                            //console.log(list_Objects);
                            var data = list_Objects;



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

            var options = data[x].name + "_" + data[x].firstname  + ": " + data[x].phone ;
//console.log (options);
          var select = document.getElementById("selected");
          //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('selected').selectedIndex = 0;
      document.getElementById('selected').value = 'Default';
      // document.getElementById("phoneNumber").innerHTML = source; // display you phone number at page top
      topFunction(); // go to top of page
    };
