// store the text for the VAT not valid var vatText="MwSt stimmt nicht.
Bitte kontaktieren Sie uns."; // define an interval handle for the setInterval so it is globaly and we can acces it from everywhere var checkVATInterval; // reference for the vat field so we can acces it from everywhere var checkoutVATInput; // reference to the country input so we can access it from anywhere var checkoutVATInputCountry; // get the vatResult element(where we will show the message) var vatResultElement = $(".vatResult"); var BillCountry; var ShippingToCountry; // this will ensure that it gets run after all the page has loaded $( document ).ready(function() { // function will recalculate the cart function recalculateCart(){ // get the main cart element ( i updated the cart to have this, it works like a holder) var cartElement = $(".cartWrapper"); // get hiddedn ajax icon var ajaxIcon = $(".cartAjaxLoader"); // save the cart content in a variable (we will use it on fail) var previousCartContent = cartElement.html(); // empty the cart holder HTML (the cart content on the page) cartElement.html(""); // show the loading icon ajaxIcon.show(); // make the ajax call and get the new cart $.ajax({ type: 'POST', url: 'calculatedCart.html', data: "shippingToCountry="+ShippingToCountry, success: function(result) { // hide the ajax icon ajaxIcon.hide(); // load the result of the ajax in the cart wrapper cartElement.html(result); }, error: function(json){ //calculatedCart did no respond // hide the icon ajaxIcon.hide(); // load the previous cart content cartElement.html(previousCartContent); } }); //done } // this will validate the VAT function validateVAT(){ // check the billing country and get its value (usually a DE, GB string) var country = $("[name=BillCountry]").val(); // if the value of the billing country is GB or GB(O) just quit and don't do anything // VAT discount does not apply to GB // if(country == "GB" || country == "GB(O)"){ // exit // return; // } // get the hidden ajax icon var ajaxIcon = $(".miniAjaxLoader"); // empty the html element so we have no message in it (just in case) vatResultElement.html(""); // show the loading icon ajaxIcon.show(); // clear the checkVATInterval interval so this function does not get triggered again (TODO: true implementation should be switched to timeout) clearInterval(checkVATInterval); // make the ajax request for validation of the VAT $.ajax({ type: 'POST', // method url: 'SERVICES/VAT/vat.php', //url where we will post the data data: 'vatNumber='+checkoutVATInputCountry.val()+checkoutVATInput.val(), //data to be posted dataType: 'json', // the result is a json success: function(json) { // all works and the vat.php responded // hide the loading icon ajaxIcon.hide(); // if e have an error from the vat.php then VAT=invalid if(json.error == 1){ // show the invalid text vatResultElement.html(vatText); checkoutVATInput.attr("class",""); checkoutVATInput.addClass("redBold"); recalculateCart(); //exit we are done return; } // if the result is ok -> VAT=valid if(json.result == "OK"){ checkoutVATInput.attr("class",""); checkoutVATInput.addClass("greenBold"); // we need to recalculate the cart recalculateCart(); return; } }, error: function(json){ // the vat.php service did not respond, hide the ajax icon and no nothing ajaxIcon.hide(); checkoutVATInput.attr("class",""); checkoutVATInput.addClass("redBold"); } }); } // bind to the vat element // basically this function will find the element and after this will bind to the keyup event // events go like this // 1) user presses a key -> the interval get cleared and a new one is set in place that will fire in 500ms // 1.1) user does nothing -> validateVAT fires // 1.2) user presses another key -> the interval get cleared and a new one is set in place that will fire in 500ms function bindVATElement(){ // find the element checkoutVATInput = $("#DEVAT_VAT_ID"); checkoutVATInputCountry = $("#DEVAT_VAT_COUNTRY"); // bind to keypress event (when a key is released) checkoutVATInput.bind("keyup", function(){ // clear the interval that was previously set (or not, depending at what point in time this is) clearInterval(checkVATInterval); // set up an interval (repeating event) every 500ms checkVATInterval = setInterval(validateVAT, 500); }); } function sanitizeCountryValue(countryValue){ if(countryValue == "GB(O)"){ return "GB" } else { return countryValue; } } function checkVatAvailability(countryValue){ var hideVatForCountries="LI,IS,NO,CH"; var VAT_row = $("#VAT_row"); if(hideVatForCountries.indexOf(countryValue)!=-1){ VAT_row.hide(); //$("#DEVAT_VAT_COUNTRY").val(""); $("#DEVAT_VAT_ID").val(""); validateVAT(); validateVAT(); } else { VAT_row.show(); vatResultElement.hide(); validateVAT(); } } function bindBillCountry(){ // get BillCountry element BillCountry = $('[name=BillCountry]'); // get DEVAT_VAT_COUNTRY element var DEVAT_VAT_COUNTRY = $("#DEVAT_VAT_COUNTRY"); // bind on chanhge BillCountry.bind("change", function(){ if(!$("#same_as_bill").attr('checked')){ return; } ShippingToCountry = BillCountry.val(); DEVAT_VAT_COUNTRY.val(sanitizeCountryValue(BillCountry.val())); checkVatAvailability(BillCountry.val()); }); // beside the bind we need to do the change on load DEVAT_VAT_COUNTRY.val(sanitizeCountryValue(BillCountry.val())); } function bindShipCountry(){ // get BillCountry element ShipCountry = $('[name=ShipCountry]'); // get DEVAT_VAT_COUNTRY element var DEVAT_VAT_COUNTRY = $("#DEVAT_VAT_COUNTRY"); // bind on chanhge ShipCountry.bind("change", function(){ if($("#same_as_bill").attr('checked')){ return; } ShippingToCountry = ShipCountry.val(); DEVAT_VAT_COUNTRY.val(sanitizeCountryValue(ShipCountry.val())); checkVatAvailability(ShipCountry.val()); }); // beside the bind we need to do the change on load DEVAT_VAT_COUNTRY.val(sanitizeCountryValue(ShipCountry.val())); } $("#same_as_bill").bind("change", function(){ $(".shipingForm *").each( function(index){ if($(this).attr("id") == "same_as_bill"){ return; } $(this).attr("disabled", ($("#same_as_bill").attr('checked') ? "1" : "")); } ); $("[name=ShipCountry]").val($("[name=BillCountry]").val()); if($("#same_as_bill").attr('checked')){ var DEVAT_VAT_COUNTRY = $("#DEVAT_VAT_COUNTRY"); ShippingToCountry = BillCountry.val(); DEVAT_VAT_COUNTRY.val(sanitizeCountryValue(BillCountry.val())); checkVatAvailability(BillCountry.val()); } }); bindBillCountry(); bindShipCountry(); // setup starting point, basically this function will bind to the vat element bindVATElement(); });