(function ($, Drupal, Cookies) {
  "use strict";
  
  getFootprint();
  if(!localStorage.getItem('footprint')) {
    localStorage.setItem('footprint',getFootprint());
  }
  jQuery("input[name='footprint']").val(localStorage.getItem('footprint'));
  jQuery("input[name='usercountry']").val(getCookie('countryCode'));
  jQuery("input[name='cdp_country_code']").val(getCookie('countryCode3'));
  jQuery("input[name='cdp_zone_code']").val(getCookie('timezoneCode'));
  
  window.dataLayer = window.dataLayer || [];
  jQuery(".webform-submission-simple-newsletter-with-consent-form").submit(function(){
    window.dataLayer.push({"category":"lead","label":document.title,"action":"submit","component":'newsletter'});
  });

  var colorMail = jQuery('div.form-type-email input').css('color');
      colorMail = colorMail.toString();
  Drupal.behaviors.newsWithConsent = {
    attach: function (context, settings) {
      if ((jQuery('div.form-type-date input').val().length) > 1) {
        jQuery('div.form-type-date label').hide();
        jQuery('div.form-type-date input').css('color',colorMail);
        jQuery('div.form-type-date input').css('font-weight','bold');
      } else {
        jQuery('div.form-type-date input').css('color','#fff');
        jQuery("div.form-type-date input").focus(function(){
          jQuery('div.form-type-date input').css('color',colorMail);
          jQuery('div.form-type-date input').css('font-weight','bold');
          jQuery('div.form-type-date label').hide();
        });
      }
    }
  }
})(jQuery, Drupal, Cookies);

function getFootprint() {
  var foot = '';
  var searchParams = new URLSearchParams(window.location.search);
  if(searchParams.has('footprint')){
    foot = searchParams.get("footprint");
    localStorage.setItem('footprint',foot);
  }
  return foot;
}

function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i <ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}
