    // Same address-autocomplete behavior as the job form (see
    // includes/manage-jobs.php), named differently here
    // (initCustomerAddressAutocomplete, not initAutocomplete) since both
    // scripts could in principle be enqueued on the same request and two
    // functions with the same name would collide.
    function initCustomerAddressAutocomplete() {
      const addressField = document.getElementById('service_address');
      if (!addressField) return;

      const autocomplete = new google.maps.places.Autocomplete(
        addressField,
        { types: ['geocode'] }
      );

      autocomplete.setFields(['address_component']);

      autocomplete.addListener('place_changed', () => {
        const place = autocomplete.getPlace();
        if (!place.address_components) return;

        let streetNumber = '';
        let streetName = '';
        let city = '';
        let postalTown = '';
        let state = '';
        let zip = '';

        for (const component of place.address_components) {
          const types = component.types;

          if (types.includes('street_number')) {
            streetNumber = component.short_name;
          }
          if (types.includes('route')) {
            streetName = component.long_name;
          }
          if (types.includes('locality')) {
            city = component.long_name;
          }
          // UK addresses often don't get a 'locality' at all, Google uses
          // this separate type (Royal Mail's "post town") instead, so fall
          // back to it below if 'locality' never showed up.
          if (types.includes('postal_town')) {
            postalTown = component.long_name;
          }
          if (types.includes('administrative_area_level_1')) {
            state = component.short_name;
          }
          if (types.includes('postal_code')) {
            zip = component.short_name;
          }
        }

        if (city === '') {
          city = postalTown;
        }

        document.getElementById('service_address').value = `${streetNumber} ${streetName}`.trim();
        document.getElementById('service_city').value = city;
        document.getElementById('service_state').value = state;
        document.getElementById('service_zip').value = zip;
      });
    }
