
//
// newMessages.js
//
// whh 2019.12.21 v1
// whh 2020.02.19 switched to calling PHP to read data files uploading to JS with AJAX
// show existing Phone Numbers from DIDW and new messages for them
// show existing Phone Numbers from DIDW and new messages for them
// whh 2022.04.22  total rewrite to a function to use any phone number in index.php


function newMessages() {

console.log("Runnning  - newMessages.js -"); // message for browser console for debugging

  //clear data
  document.getElementById("phoneNumber").innerHTML = "&nbsp;";
  document.getElementById('newWindows').innerHTML = "&nbsp;";
  document.getElementById('phoneNumberSource').innerHTML = "&nbsp;";
  document.getElementById("firstName").value = "";
  document.getElementById('lastName').value = "";

  // make <div>s by ID in html index.php visible and invisible
  document.getElementById("sms").style.display = "none";
  document.getElementById("phone").style.display = "block";
  document.getElementById("numpad").style.display = "none";
  document.getElementById("selectNUM").style.display = "none";
  document.getElementById("newPhoneList").style.display = "none";
  document.getElementById("phoneNumber").style.display = "none";
  document.getElementById("phoneNumberSource").style.display = "none";
  document.getElementById("type").style.display = "none";
  document.getElementById("backContact").style.display = "none";

  // make this input required for this usage if needed to true
  document.getElementById("recipient").required  = false;


  /* read in the incoming client messages from SMSmessages.json */
  /* read in the incoming client messages from SMSmessages.json */
  /* read in the incoming client messages from SMSmessages.json */

  // call jQuery function to get PHP to read and return data from a file
          $.ajax({
              url: "SMSMessagesAjax.php",
              async: false,   // asynchronous request? (synchronous requests are discouraged...)
              cache: false,   // with this, you can force the browser to cache retrieved data
              dataType: "json",  // jQuery will infer this, but you can set explicitly
              success: function(data) {   // Success function
  //console.table(data);
  //console.log("Data Length from file: " + data.length);
  //console.table(data);
          length = data.length;

            // Sort data by date
              data.sort(function(a, b) {
                  a = new Date(a.dateTime);
                  b = new Date(b.dateTime);
                  return a<b ? -1 : a>b ? 1 : 0;
              });

        var phcount = 1;   // set to first phone number s1 and ph1 in index.php


        // this for loop increments to the next 's' and 'ph' value for the IDs for each phone number
        // It keeps increasing the value for 'sid' till none is found in index.php and exits the functions
          for (let i = 0; i < phcount ; i++) {

    console.log(phcount);

              var sid = "s" + (i + 1); //builds the 's' ID to look for in the index.php

    console.log(sid);



// it is failing here when this sid is not found

              var bolval = !!document.getElementById(sid); // test if current sid exists

              //if (document.getElementById('cal_preview') != null) {
              //    str = document.getElementById("cal_preview").value;
              //}

console.log(bolval);

                // if the next 's' id is not found then boolean value with be 'null' so exit
                if (bolval === false) {

console.log("bolval is false");

                  break; // return or break ??? return exits the function as well
                }

              var val = document.getElementById(sid).value; // get phone numnber of current sid value


                      var obj = $.grep(data, function(obj, i){return obj.recipient === val;}); // get all messages for phonenumber in the array
                      var objnew = $.grep(obj, function(obj, i){return obj.read != "yes";}); // messages in the array where read is yes

    console.table(objnew);

                      var length = objnew.length; // total number of messages in the array where read is yes
    console.log(length);

                      // if the next 's' id is not found then the length will be zero becuase there are no messages
                      //if (length === 0) {
    //console.log("variable is 0");
                        //break; // return or break ??? return exits the function as well
                      //}

                      // increment the valie of ph ID to match the s ID each time thru the looop
                      phid = "ph" + (i + 1);

    console.log(phid);

                      var newMessages = length;
                      document.getElementById(phid).value = newMessages; // load number of phone numbers on the webpage beside phone number

         var phcount = phcount + 1; //increment the test value that i has to be less than to keep the loop going

       } // end New Messages for each phonenumber for loop

    }  // end of success function

  }); // end of getting SMSmessage.json data using ajax

} // end of function NewMEssage
