(function ($, Drupal) {
  'use strict';

  Drupal.behaviors.esContactForm = {
    attach: function (context, settings) {
      $('.webform-submission-spain-contact-form-form .form-select[name="interest"]').on('change', function () {
        var form = $(this).parents('form:first');
        var interest_value = $("option:selected", $(this)).data('machine-name');
        var ec_id = '';
        var record_type_id = '';
        //var lead_recrod_type = '';
        var show_company = true;

        switch (interest_value) {
          // Residential
          case 'solar_for_home':
            ec_id = '7012T000001xBZK';
            record_type_id = 'Residential Solar Interest';
            //lead_recrod_type = '0128000000037AK';
            show_company = false;
            break;

          // Commercial
          case 'solar_for_business':
            ec_id = '7012T000001xBZK';
            record_type_id = 'Commercial Solar Interest';
            //lead_recrod_type = '01280000000PzPa';
            break;

          // Become an installer
          case 'become_an_installer':
            ec_id = '7012T000001xBZK';
            record_type_id = 'Partner/Distributor Interest';
            //lead_recrod_type = '0128000000037AJ';
            break;

          // Other
          case 'other':
            ec_id = '7012T000001xBZK';
            record_type_id = 'Other';
            //lead_recrod_type = '0128000000037AK';
            show_company = false;
            break;
        }
        $('.form-select[name="ec_id"]', form).val(ec_id).trigger('change');
        $('.form-select[name="lead_record_type_id"]', form).val(record_type_id).trigger('change');
        //$('.form-select[name="leadrecordtype"]', form).val(lead_recrod_type).trigger('change');
      });

      $('.webform-submission-spain-contact-form-form .form-checkbox[name="terms_and_conditions"]').on('change', function () {
        var form = $(this).parents('form:first');
        var input = $('input[name="tic"]', form);
        var hubspot_input = $('input[name="hubspot_terms_and_conditions"]', form);
        if ($(this).is(':checked')) {
          input.val('on');
          hubspot_input.val("true");
        }
        else {
          input.val('');
          hubspot_input.val("false");
        }
      });

      $('.webform-submission-spain-contact-form-form .form-checkbox[name="newsletter_confirm"]').on('change', function () {
        var form = $(this).parents('form:first');
        var input = $('input[name="nltr"]', form);
        var hubspot_input = $('input[name="hubspot_newsletter"]', form);
        if ($(this).is(':checked')) {
          input.val('on');
          hubspot_input.val("true");
        }
        else {
          input.val('');
          hubspot_input.val("false");
        }
      });
      
      $('.webform-submission-spain-contact-form-form .form-checkbox[name="survey_opt_in_checkbox"]').on('change', function () {
        var form = $(this).parents('form:first');
        var input = $('input[name="soptin"]', form);
        var hubspot_input = $('input[name="hubspot_survey"]', form);
        if ($(this).is(':checked')) {
          input.val('on');
          hubspot_input.val("true");
        }
        else {
          input.val('');
          hubspot_input.val("false");
        }
      });
      
      $('.webform-submission-spain-contact-form-form select[name="address[country_code]"]').on('change', function () {
        var form = $(this).parents('form:first');
        var formID = form.attr('id');
        var selectedCountry = $(this, '#' + formID).find('option:selected').val();
        
        $('#edit-country-en-country', '#' + formID).val(selectedCountry).trigger('change');
        var selectedCountryEN = $('select[name="country_en[country]"] option[value="' + selectedCountry + '"]', '#' + formID);
        $('input[name="cntr"]', '#' + formID).val(selectedCountryEN.text());
      });

      // Hide block title when showing webform confirmation message.
      if ($('#block-contact-webform-es:has(div.webform-confirmation)').length > 0) {
        $('#block-contact-webform-es .contact-us-block-title').hide();
      }
      else {
        $('#block-contact-webform-es .contact-us-block-title').show();
      }
    }
  };

})(jQuery, Drupal);