(function(){ var loadScript = function(url, callback) { var script = document.createElement("script"); script.type = "text/javascript"; if (script.readyState){ script.onreadystatechange = function(){ if (script.readyState == "loaded" || script.readyState == "complete") { script.onreadystatechange = null; callback(); } }; } else { script.onload = function() { callback(); }; } script.src = url; document.getElementsByTagName("head")[0].appendChild(script); }; var fix = function($) { if(!$('#VatId')) return; //var cart_form; var register_form; var sub_vat_id; var buy_button; var submit_btns; var submit_btn_index; var vars = {}; var manual_mode = 'false'; var execution_delay = 0; if(typeof m4u_deleted === 'undefined') vars['msg_deleted'] = 'The VAT ID has been deleted.'; else vars['msg_deleted'] = m4u_deleted; if(typeof m4u_format_error === 'undefined') vars['msg_format_error'] = 'Please enter a valid EU VAT ID.'; else vars['msg_format_error'] = m4u_format_error; if(typeof m4u_invalid_id === 'undefined') vars['msg_invalid_id'] = 'This VAT ID seems to be invalid, please contact us directly.'; else vars['msg_invalid_id'] = m4u_invalid_id; if(typeof m4u_no_country === 'undefined') vars['msg_no_country'] = 'Please enter a valid country code (e.g., DK, FR, DE,...)'; else vars['msg_no_country'] = m4u_no_country; if(typeof m4u_same_country === 'undefined') vars['msg_same_country'] = 'We are not allowed to issue VAT exempt invoices for businesses in the same country.'; else vars['msg_same_country'] = m4u_same_country; if(typeof m4u_vies_down === 'undefined') vars['msg_vies_down'] = 'The EU validation service is temporarily unavailable. Please contact us.'; else vars['msg_vies_down'] = m4u_vies_down; if(typeof m4u_validated === 'undefined') vars['msg_valid'] = 'Your VAT ID has been successfully validated.'; else vars['msg_valid'] = m4u_validated; if(typeof m4u_no_email === 'undefined' || m4u_no_email == '') vars['msg_no_email'] = 'Please enter a valid Email address.'; else vars['msg_no_email'] = m4u_no_email; vars['msg_deleting'] = 'deleting'; var m4u_url = 'https://www.modules4u.biz/shopify/exemptify/'; var m4u_img = m4u_url + 'images/'; var wait_img = m4u_img+'ajax_loader.gif'; var valid_ok = m4u_img+'checkmark_new.png'; var valid_fail = m4u_img+'red_cross.png'; var close_img = m4u_img+'close.png'; var script_url = m4u_url + 'exemptify_v1.php'; var eu_vat_countries = [ 'AT','BE','BG','HR','CY','CZ','DK','EE','FI','FR','DE','EL','HU', 'IE','IT','LV','LT','LU','MT','NL','PL','PT','RO','SK','SI','ES', 'SE','XI','EU' ]; var vat_rules = [ /^ATU{1}[0-9]{8}$/,/^BE{1}[0-9]{10}$/,/^BG{1}[0-9]{9,10}$/,/^HR{1}[0-9]{11}$/,/^CY{1}[0-9]{8}[A-Z]{1}$/,/^CZ{1}[0-9]{8,10}$/, /^DK{1}[0-9]{8}$/,/^EE{1}[0-9]{9}$/,/^FI{1}[0-9]{8}$/,/^FR{1}[0-9A-Z]{2}[0-9]{9}$/,/^DE{1}[0-9]{9}$/,/^EL{1}[0-9]{9}$/, /^HU{1}[0-9]{8}$/,/^IE{1}[0-9A-Z]{8,9}$/,/^IT{1}[0-9]{11}$/,/^LV{1}[0-9]{11}$/,/^LT{1}([0-9]{9}|[0-9]{12})$/,/^LU{1}[0-9]{8}$/, /^MT{1}[0-9]{8}$/,/^NL{1}[0-9]{9}B{1}[0-9]{2}$/,/^PL{1}[0-9]{10}$/,/^PT{1}[0-9]{9}$/,/^RO{1}[0-9]{2,10}$/,/^SK{1}[0-9]{10}$/, /^SI{1}[0-9]{8}$/,/^ES{1}[0-9A-Z]{1}[0-9]{7}[0-9A-Z]{1}$/,/^SE{1}[0-9]{12}$/,/^XI{1}[a-zA-Z0-9]*$/]; const observer_config = { attributes: true, childList: true, subtree: true }; const callback = function(mutationsList, observer) { for(let mutation of mutationsList) { var target = $(mutation.target); var index = target.attr('click_index'); var exemptify_button = $('.exemptify_button[click_index='+index+']'); if (mutation.type === 'attributes') { var attr = mutation.attributeName; var nval = target.attr(attr); if(!mutation.target.hasAttribute(attr)) exemptify_button.removeAttr(attr); else exemptify_button.attr(attr,nval); if(!exemptify_button.hasClass('exemptify_button')) exemptify_button.addClass('exemptify_button'); } else if(mutation.type === 'childList') { exemptify_button.html(target.html()); } } }; const observer = new MutationObserver(callback); $.expr[':'].regex = function(elem, index, match) { var matchParams = match[3].split(','), validLabels = /^(data|css):/, attr = { method: matchParams[0].match(validLabels) ? matchParams[0].split(':')[0] : 'attr', property: matchParams.shift().replace(validLabels,'') }, regexFlags = 'ig', regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags); return regex.test($(elem)[attr.method](attr.property)); } function setSessionCookie(key,val) { document.cookie = key+"="+val+"; path=/;"; } function deleteSessionCookie(key) { document.cookie = key+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; } function getSessionCookie(key) { var name = key+"="; var ca = document.cookie.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 ""; } function validationState(state,msg) { var img = wait_img; if(state=='fail') img = valid_fail; else if(state=='ok') img = valid_ok; $('#StatusImg').attr('src',img); $('#ErrMsg').html(msg); } function validationResult(args) { deleteVatAtts(validationResultInternal,args); } function validationResultInternal(args) { var data = args[0]; var cb = null; if(args.length > 1) var cb = args[1]; var vat_id = data['vat_id']; var same_cty = data['same_country']; var status = data['status']; var res_msg = ''; var res = false; if(status=='INVALID') { validationState('fail',vars['msg_invalid_id']); res_msg = 'INVALID ID'; } else if(status=='ERROR') { validationState('fail',vars['msg_vies_down']); res_msg = 'GENERAL VIES ERROR'; }else { validationState('ok',vars['msg_valid']); res_msg = 'ID SUCCESSFULLY VERIFIED'; res = true; setSessionCookie('exemptify-vat-id',vat_id); if(same_cty=='false') updateCartAtt('vat-id',vat_id,doSubmit); else updateCartAtt('local-vat-id',vat_id,doSubmit); } var detail = { 'detail': { 'validation_successful': res,'result_message': res_msg, 'same_country' : same_cty } }; var ev = new CustomEvent('ExemptifyValidationComplete',detail ); if(cb!=null) cb(res_msg); document.body.dispatchEvent(ev); return; } function doSubmit() { /* VAT ID mandatory for all customers */ if(false && $('#VatId').val()=='' ) { validationState('fail',vars['msg_format_error']); return; } /* VAT ID mandatory for all EU customers except for customers from merchant country */ if(false && ('US' != $('#ShopCtyId').val()) && false && $('#VatId').val()=='' ) { validationState('fail',vars['msg_format_error']); return; } if(submit_btns[submit_btn_index]!=null) { if(''== submit_btn_index+'' ) { $('form.').submit(); } else setTimeout(function(){ submit_btns[submit_btn_index].click(); }, 500); } } function m4uRequest(path,method,vals) { var cb = null; if(vals.cb) cb = vals.cb; vals.cb=null; vals['shop'] = 'campetella.myshopify.com'; vals['time'] = '1731646823'; vals['hmac'] = '5c8caf43ffa3fbb82331a999303e940511bd009fbebd0106b72e27a1d152e156'; $.ajax({ url : path, type : method, crossDomain: true, data : vals, dataType:'json', success : function(data) { console.log(data); switch (vals.action) { case 'new_get_vat_id' : if(data!=''){ $('#VatId').val(data) } ;break; case 'new_validate_id' : validationResult([data,cb]);break; case 'new_delete_vat_id' : validationState('ok',vars['msg_deleted']);deleteVatAtts(doSubmit);break; case 'get_customer' : getCustomerCB(data);break; case 'get_country' : $('#ShopCtyId').val(data);break; } }, error : function(request,error) { if(vals.action=='new_validate_id' && error=='parsererror') { window.exemptify_validate(); } } }); } function getCustomerCB(data) { $('#CustomerId').val(data['id']); var email = data['email']; window.cart_form.each( function(){ var cart_action = $(this).attr('action'); var con = '?'; if(cart_action.indexOf('?')>-1) con = '&'; $(this).attr('action',cart_action+con+'checkout[email]='+email); //console.log(window.cart_form.length); } ) validate($('#VatId').val(),data['cart_action']); } function htmlentities(str) { str = str.replace('ö', 'ö'); str = str.replace('Ö', 'Ö'); str = str.replace('ä', 'ä'); str = str.replace('Ä', 'Ä'); str = str.replace('ü', 'ü'); str = str.replace('Ü', 'Ü'); return str; } function deleteVatId(cb) { //validationState('wait',vars['msg_deleting']); var cid = $('#CustomerId').val(); deleteSessionCookie('exemptify-vat-id'); $('#VatId').val(''); if(cid!='') m4uRequest(script_url,'POST',{'action' : 'new_delete_vat_id','cid':cid}); else if(typeof cb != 'undefined') deleteVatAtts(cb); } function loadVatId() { var cid = $('#CustomerId').val(); if(cid=='' || cid==null) return; m4uRequest(script_url,'POST',{'action' : 'new_get_vat_id','cid':cid}); } function loadCountry() { m4uRequest(script_url,'POST',{'action' : 'get_country'}); } function updateCartAtt(att,val,cb,args ) { var base = ( typeof window.Shopify.routes != 'undefined' ) ? window.Shopify.routes.root : '/' ; var post_data = att; if(typeof att === 'string') post_data = 'attributes['+att+']='+val; $.ajax({ type: "POST", url: base + 'cart/update.js', data: post_data, success: function(data){ if(typeof cb != 'undefined') cb(args); } , dataType: 'json' }); } function deleteVatAtts(cb, args) { var atts = {}; atts['local-vat-id'] = null; atts['vat-id'] = null; updateCartAtt({ 'attributes' : atts },'', cb, args ); } window.submitCartForm = function(btn) { var act_name= btn.attr('name'); /* VAT ID mandatory for all customers */ if(false && $('#VatId').val()=='' ) { validationState('fail',vars['msg_format_error']); return; } /* VAT ID mandatory for all EU customers except for customers from merchant country */ if(false && ('US' != $('#ShopCtyId').val()) && false && $('#VatId').val()=='' ) { validationState('fail',vars['msg_format_error']); return; } /* VAT ID entered but no valid Email address */ if( ($('#VatId').val()!='' && $('#CustomerId').val()=='') || ( $('#VatId').val()=='' && $('#exemptify_email').val() != '') ) { var email = $('#exemptify_email').val(); email = email.trim(); var emtest = new RegExp(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/,'i'); if(! emtest.test(email) ) { validationState('fail',vars['msg_no_email']); return; } else { setSessionCookie('exemptify-email', email); m4uRequest(script_url,'POST',{'action' : 'get_customer','cart_action':act_name, 'email':email}); return true; } } if(act_name==null) act_name='register'; if(act_name=='register' && $('#VatId').val()=='') { validationResult(['FALSE']); return; } if($('#VatId').val()=='') deleteVatId(doSubmit); else validate( $('#VatId').val(), act_name); }; function validate(val,cart_action,cb) { if((cart_action==null) || (cart_action=='') ) cart_action='NONE'; if(val=='') { if($('#CustomerId').val().length>0) deleteVatId(doSubmit); return true; } validationState('wait'); var msg = ''; val = val.toUpperCase(); val = val.replace(/\s/g,''); $('#VatId').val(val); var cty = val.substring(0,2); var code = val.substring(2); if (eu_vat_countries.indexOf(cty)==-1) { msg = vars['msg_no_country']; validationState('fail',msg); return; } var shopCty = $('#ShopCtyId').val(); if(shopCty=='GR') shopCty='EL'; var same_country = false; if(shopCty==cty ) { same_country = true; if(false) { msg = vars['msg_same_country']; validationState('fail',msg); return; } } var cid = $('#CustomerId').val(); sub_vat_id=val; for(var i=0;i0) { var cv = getSessionCookie('exemptify-vat-id'); if(cv!='') $('#VatId').val(cv); } /* if(register_form.length >0) { if(getLCookie()!='') $('#VatId').val(getLCookie()); } */ $(document).keypress(function(e) { if(false) return; if(e.which == 13) { $('[name=checkout-exemptify]').click(); return true; } }); var ev = new CustomEvent('ExemptifyButtonsReady' ); document.dispatchEvent(ev); } $(document).ready(function() { setTimeout(function() { if(false &! false) { $('#ExemptifyCartValidationSnippet').hide(); return; } if( (( $('#ShopCtyId').val() == 'US') || (! false) ) && false) { $('#ExemptifyCartValidationSnippet').hide(); return; } if( $('#ShopCtyId').val()=='XX' ) return; }, 800); setTimeout(function(){ swapButtons(); },execution_delay); }); }; if ((typeof jQuery === 'undefined') || (parseFloat(jQuery.fn.jquery) < 3)) { loadScript('//modules4u.biz/shopify/js/jquery/jquery-3.2.1.min.js', function() { jQuery32 = jQuery.noConflict(true); fix(jQuery32); } ); } else { fix(jQuery); } })();