
function insertQualifiers(){
    //alert("qualifying....");

    surveyId = document.getElementById("surveyId").value;
    if (/^SV_[a-zA-Z0-9]+$/.test(surveyId)) {
        console.log("Valid: starts with SV_ and followed by letters/numbers only");
        console.log(surveyId);
    } else {
        console.log("Invalid: must start with SV_ and be followed by only letters or numbers");
        alert("Invalid survey ID");
        return;
    }


  // Create the form data object
  const formData = new FormData();
  formData.append('surveyId', surveyId);

  // Send the form data using fetch
  fetch('get_qualifiers.php', {
    method: 'POST',
    body: formData
  })
  .then(response => response.text())
  .then(data => {
    // Output result
    console.log(data);
    const parsed = JSON.parse(data);
    
    document.querySelector('label[for="location"]').textContent = parsed["1"][0];

    //  .shift(); //
    parsed["1"][0] = "All";
    select = document.getElementById('location');
    optionCount = select.options.length;
    if (optionCount > 1) {return;}
    select.innerHTML = '';  // removes all option elements inside

    parsed["1"].forEach((loc, index) => {
        const option = document.createElement("option");
        option.value = index;
        option.textContent = loc;
        select.appendChild(option);
    });

    document.querySelector('label[for="level"]').textContent = parsed["2"][0];

    //  .shift(); //
    parsed["2"][0] = "All";
    select = document.getElementById('level');
    optionCount = select.options.length;
    if (optionCount > 1) {return;}
    select.innerHTML = '';  // removes all option elements inside

    parsed["2"].forEach((loc, index) => {
        const option = document.createElement("option");
        option.value = index;
        option.textContent = loc;
        select.appendChild(option);
    });

    document.querySelector('label[for="gov"]').textContent = parsed["3"][0];
    parsed["3"][0] = "All";
    select = document.getElementById('gov');
    optionCount = select.options.length;
    if (optionCount > 1) {return;}
    select.innerHTML = '';  // removes all option elements inside

    parsed["3"].forEach((loc, index) => {
        const option = document.createElement("option");
        option.value = index;
        option.textContent = loc;
        select.appendChild(option);
    });

    //Disable the surveyId input
    document.getElementById("surveyId").disabled = true;

    /*document.getElementById("record_no").textContent = total;
    if (total < 9){
      return;
    }*/
    // return data; // Should be an array like [{ value: -3, count: 2 }, ..., { value: 3, count: 5 }]

  })
  .catch(error => {
    console.error('Error:', error);
  });
}





