(function() {
  privateElement = document.getElementById('edit-contact-private-question--wrapper');
  if (privateElement) {
    // default the private question to hidden
    privateElement.classList.add('hide');
  }
  subjectElement = document.getElementById('edit-contact-subject');
  if (subjectElement) {
    subjectElement.addEventListener("change", function(){
      //This input has changed
      var json = this.getAttribute('custom-data');
      // Converting JSON object to JS object
      var obj = JSON.parse(json);
      var displayPrivate = isValueSet(obj, this.value);
      if (displayPrivate == false) {
        // Clear the private question fields
        document.getElementById('edit-contact-date-of-birth').value = '';
        document.getElementById('edit-contact-ssn-itin').value = '';
        document.getElementById('edit-contact-phone-required').value = '';
        // Check the "No" box to hide all the other fields
        document.getElementById('edit-contact-private-question-no').click();
      }
      // Show or hide the element based upon the json list
      showHidden(displayPrivate,"edit-contact-private-question--wrapper");
    });
  }
})();

// Function to check if value is set to "1"
function isValueSet(obj, value) {
    for(var k in obj) {
        if(k == value) {
            if (obj[k] == "1") {
              return true;
            } else {
              return false;
            }
        }
    }
};

// Show or hide an element
function showHidden(unhideElement, elementId)
{
  var element = document.getElementById(elementId);
  if (unhideElement) {
    if (element.classList) {
      element.classList.remove('hide');
    }
  } else {
    if (element.classList) {
      element.classList.add('hide');
    }
  }
}
