// Example 1: Make 'dni' field required only for Italy - add this Custom JS:

tc_confirmOrderValidations['dni_required_for_italy'] = function () {
      var italyCountryId = 2;
      var errorMsg = 'Required field';
      $('#thecheckout-address-invoice input[name=dni]').removeClass('-error');
      removeError('#thecheckout-address-invoice .error-msg');
      if (
        italyCountryId == $('#thecheckout-address-invoice select[name=id_country]').val() && 
        $('#thecheckout-address-invoice input[name=dni]').is(':visible') &&
        '' == jQuery.trim($('#thecheckout-address-invoice input[name=dni]').val())
      ) {
        $('#thecheckout-address-invoice input[name=dni]').addClass('-error');
        $('#thecheckout-address-invoice input[name=dni]').after('<div class="field error-msg">' + errorMsg + '</div>');
        scrollToError();
        return false;
      } else {
        return true;
      }
};

// Example 2: Make 'dni' field required only for Italy and only when 'I am a business customer' is *unticked*,
// including *-mark next to field - add this Custom JS:

tc_confirmOrderValidations['dni_required_for_italy_non_business'] = function () {
      var italyCountryId = 2;
      var errorMsg = 'Required field';
      $('#thecheckout-address-invoice input[name=dni]').removeClass('-error');
      removeError('#thecheckout-address-invoice .error-msg');
      if (
        italyCountryId == $('#thecheckout-address-invoice select[name=id_country]').val() &&
        $('#thecheckout-address-invoice input[name=dni]').is(':visible') &&
        '' == jQuery.trim($('#thecheckout-address-invoice input[name=dni]').val()) &&
        !$('#i_am_business').is(':checked')
      ) {
        $('#thecheckout-address-invoice input[name=dni]').addClass('-error');
        $('#thecheckout-address-invoice input[name=dni]').after('<div class="field error-msg">' + errorMsg + '</div>');
        scrollToError();
        return false;
      } else {
        return true;
      }
};

$('body').on('change', '[data-link-action=x-i-am-business]', function () {
        let dniLabelEl = $('#thecheckout-address-invoice input[name=dni]').closest('label');
        if ($(this).prop('checked')) {
            dniLabelEl.removeClass('required');
        } else {
            dniLabelEl.addClass('required');
        }

        return false;
});

$(document).ready(function() {
        // Initial setup - set field required (visually) when above conditions are met
        $('[data-link-action=x-i-am-business]').trigger('change');
});