(function ($) { $(document).ready(function() { $('.js-request-form').each(function(){ if ($(this).closest('.js-form-title').length) { $(this).find('[type="submit"]').val($(this).closest('.js-form-title').attr('data-title')); } }); $('.js-request-form .js-calc').keyup(function(){ var $form = $(this).closest('form'); var sum = $form.find('input.js-calc-count').val()*$form.find('input.js-calc-sum').val(); $form.find('.js-total span').html(sum); }); $('.js-request-form .js-prev-step').click(function(e){ e.preventDefault(); var $curStep = $(this).closest('.js-request-form-step'); var curStep = $curStep.attr('data-step')*1; var $nextStep = $(this).closest('form').find('.js-request-form-step[data-step="'+(curStep-1)+'"]'); var error = false; if ($nextStep.length && !error) { $curStep.hide(); $nextStep.show(); } }); $('.js-request-form .js-next-step').click(function(e){ function nextStep() { $curStep.hide(); $nextStep.show(); } e.preventDefault(); var $curStep = $(this).closest('.js-request-form-step'); var curStep = $curStep.attr('data-step')*1; var $nextStep = $(this).closest('form').find('.js-request-form-step[data-step="'+(curStep+1)+'"]'); var $form = $curStep.closest('form'); var sendText = $form.attr('data-send-message'); var submitText = $curStep.find('.js-next-step').val(); var error = false; $curStep.find('input[required]').each(function(){ if (!this.validity.valid) { this.reportValidity(); error = true; return false; } }); if ($nextStep.length && !error) { if ($curStep.attr('data-crm') == 'yes') { request = { 'option' : 'com_ajax', 'module' : 'request', 'data' : $form.serializeArray(), 'format' : 'jsonp' }; $.ajax({ type : 'POST', data : request, dataType : 'json', beforeSend: function(){ $curStep.find('.js-next-step').prop('disabled', 'disabled'); if (sendText) $curStep.find('.js-next-step').val(sendText); }, success: function (response) { $curStep.find('.js-next-step').prop('disabled', false).removeProp('disabled'); if (submitText) $curStep.find('.js-next-step').val(submitText).html(submitText); $form.find('input[name="ref"]').val(response.ref); if (response.ref) { dataLayer.push({'event':'setUserId','UID':response.ref}); /* ga('gtm1.send', 'lead', { 'userId': response.ref }); */ // console.log('ga send lead', response.ref) // ga('set', 'userId', response.ref); } nextStep(); }, complete: function() { gexec(); } }); return; } nextStep(); } }); $('.js-request-form').submit(function(e) { var $form = $(this); var landing = $form.closest('[data-product]').length?$form.closest('[data-product]').attr('data-product'):''; var sendText = $form.attr('data-send-message'); var submitText = $form.find('[type="submit"]').val(); $(this).find('input[name="landing"]').val(landing); $(this).find('[type="submit"]').prop('disabled', 'disabled') if (sendText) $(this).find('[type="submit"]').val(sendText).html(sendText); e.preventDefault(); var value = $(this).serializeArray(), request = { 'option' : 'com_ajax', 'module' : 'request', 'data' : value, 'format' : 'jsonp' }; $.ajax({ type : 'POST', data : request, dataType : 'json', beforeSend: function(){ $('.js-request-form [type="submit"]').addClass('loading'); }, success: function (response) { $('.js-request-form [type="submit"]').removeClass('loading'); if (!response.error && response.redirect) { document.location.href = response.redirect; if (response.ref) { dataLayer.push({'event':'setUserId','UID':response.ref}); /* ga('gtm1.send', 'lead', { 'userId': response.ref }); */ // console.log('ga send lead', response.ref) // ga('set', 'userId', response.ref); } } $form.find('[type="submit"]').prop('disabled', false).removeProp('disabled') if (submitText) $form.find('[type="submit"]').val(submitText).html(submitText); }, complete: function() { gexec(); } }); return false; }); if ($('input[name="phone"]').length) { var listCountries = $.masksSort($.masksLoad("/modules/mod_request/assets/js/data/phone-codes.json"), ['#'], /[0-9]|#/, "mask"); var maskOpts = { inputmask: { definitions: { '#': { validator: "[0-9]", cardinality: 1 } }, showMaskOnHover: false, }, match: /[0-9]/, replace: '#', listKey: "mask", list: listCountries, }; $('input[name="phone"]').inputmasks(maskOpts, { onincomplete: function () { console.log('incomplete'); } }); } }); })(jQuery);