jQuery(document).ready(function($){

  if (typeof gtag !== 'function') {
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
  }

  console.log('I am I want');

  var iAM = 'not set';
  var iWant = 'not set';
  var selection_value = 'https://uct.ac.za/search';

  $('.iWantSelect').on('change', function() {
    $('[name="selected_choice"]').val(this.value);
  });
  
  $('.i-am-submit-btn').on('click', function(e){
    e.preventDefault();

    var amSelect = $('[name="i_am_select"]');
    var wantSelect = $('.js-webform-states-hidden:visible .iWantSelect');
    var selectedURL = $('[name="selected_choice"]');
    
    console.log('amSelect',amSelect);
    console.log('wantSelect',wantSelect);
    console.log('selectedURL',selectedURL);
    
    selectedURL.val(wantSelect.val());
    selection_value = selectedURL[0].options[selectedURL[0].selectedIndex].text;
    
    console.log('selection_value',selection_value);
    console.log('gtag start');
    if (typeof gtag === 'function' && amSelect.val() && wantSelect ) {

      iAM = amSelect.val();
      iWant = selectedURL.val();
      console.log('gtag true', iAM, iWant, selection_value);

      gtag('event', 'I am I want', {
        'i_am': iAM,
        'i_want': iWant,
        'selection_value': selection_value,
        'event_callback': function() {
          console.log('event_callback fired');
        }
      });

      window.location = selection_value;

    } else {
      window.location = selection_value;
    }
    
  });
  
});