$(document).ready(function () { $('.changePackageLink').click(function(event){ event.preventDefault(); fillJourney($('#changePackageLink-' + $(this).data('packageId'))); $('#toFirstStepSubmit').click(); }); $('#changeServices').click(function(event){ event.preventDefault(); fillJourney(this); $('#changePackageSubmit').click(); }); $.ajaxSetup({ cache : false }); $('form#changeQuntityForm').ajaxForm({ async: true, dataType: 'json', success: function (data) { reloadCartRelatedThings(data.miniCartItemsCount, data.basketTotal, data.grantTotal, data.shipping, data.simpleItems, data.packageItems,data.discountTotal, data); $('#simpleItems').replaceWith(data.simpleItems); if (data.errorMessage != '') { var errorData={errors:[data.errorMessage]}; showMessage('cartErrorMessageTpl', errorData); } else{ $('#messages').hide(); } }, error: function (jqXHR, textStatus, errorThrown) { } }); $('#removeItemForm').ajaxForm({ async: true, dataType: 'json', success: function (data) { // if (data.miniCartItemsCount == '0') { ; // } else { // reloadCartRelatedThings(data.miniCartItemsCount, data.basketTotal, data.grantTotal, data.shipping, data.simpleItems, data.packageItems, data.discountTotal, data); // var typeItem = $('#removeItemForm').find('#typeItem').val(); // var removeItemMessage = $('#removeItemForm').find('#removeItemMessage').val(); // // if (typeItem == 'package') { // $('#packageItems').replaceWith(data.packageItems); // $('#basketTotalMonthlyDiv p strong').html(data.basketTotalMonthlyPrice); // $('#grantTotalMonthlyDiv p').html(data.grantTotalMonthlyPrice); // } // // if (typeItem == 'simple') { // $('#simpleItems').replaceWith(data.simpleItems); // } // var messagesData={messages:[removeItemMessage]}; // showMessage('cartInformationMessageTpl',messagesData); // } }, error: function (jqXHR, textStatus, errorThrown) { } }); if ($('form#couponForm')) { $('form#couponForm').ajaxForm({ async: true, dataType: 'json', success: function (data) { // console.log('couponForm ajax call response data: ' + data); // reloadCartRelatedThings(data.miniCartItemsCount, data.basketTotal, data.grantTotal, data.shipping, data.simpleItems, data.packageItems, data.discountTotal, data); if (data.errorMessage != '') { var errorData={errors:[data.errorMessage]}; //showMessage('cartErrorMessageTpl', errorData); // seen: noCouponFound | CouponExpired | expiredPromotionUserResource | noCouponUses | usedPromotionUserResource // mine: couponsDisabled | couponAlreadyExists // germ: duplicatePromotionUserResource | couponErrorId // ATG: claimingCouponNotAllowed | noCouponPromotions | $('#modal-coupon-error-noCouponFound').hide(); $('#modal-coupon-error-CouponExpired').hide(); $('#modal-coupon-error-expiredPromotionUserResource').hide(); $('#modal-coupon-error-noCouponUses').hide(); $('#modal-coupon-error-usedPromotionUserResource').hide(); $('#modal-coupon-error-general').hide(); if (data.errorCode == 'noCouponFound' || data.errorCode == 'CouponExpired' || data.errorCode == 'expiredPromotionUserResource' || data.errorCode == 'noCouponUses' || data.errorCode == 'usedPromotionUserResource') { $('#modal-coupon-error-'+data.errorCode).show(); } else { $('#modal-coupon-error-general').show(); } $('#modal-coupon-help').show(); } else{ var couponCode = $('#claimCouponCode').val(); for(var i = 0; i < data.promotions.length; i++) { var prom = data.promotions[i]; if (prom.coupon.toUpperCase() == couponCode.toUpperCase()) { $('#modal-coupon-amount').html(prom.adjustedWithCouponAmount); } } $('#modal-coupon-title').hide(); $('#modal-coupon-correct-title').show(); $('#modal-coupon-question').hide(); $('#modal-coupon-form').hide(); $('#modal-coupon-ok-button').show(); $('#modal-coupon-help').hide(); } }, error: function (jqXHR, textStatus, errorThrown) { } }); } // if ($('form#removeForm')) { // $('form#removeForm').ajaxForm({ // async: true, // dataType: 'json', // success: function (data) { // //console.log('removeForm ajax call response data: ' + data); // // reloadCartRelatedThings(data.miniCartItemsCount, data.basketTotal, data.grantTotal, data.shipping, data.simpleItems, data.packageItems, data.discountTotal, data); // // if (data.errorMessage != '') { // var errorData={errors:[data.errorMessage]}; // //showMessage('cartErrorMessageTpl', errorData); // //console.log('removal error: ' + data.errorMessage); // } // else{ // $('#claimCouponCode').removeAttr('readonly'); // $('#claimCouponCode').val(''); // $('#couponValidationLink').show(); // $('#couponRemovalLink').hide(); // $('#couponSuccessMessage').hide(); // $('#couponDetailsMessage').hide(); // } // }, error: function (jqXHR, textStatus, errorThrown) { // } // }); // } $('a.proceedToCheckout').click(function (clickEvent) { cleanErrors(); clickEvent.preventDefault(); // AJAX are asynchronous thus stop event right now $.getJSON('/checkout/validateAJAX', function (data) { if (!data.success) { showMessage('cartErrorMessageTpl', data); } else { var fnCheckoutContinue = $('a.proceedToCheckout'); actuateLink(fnCheckoutContinue); //.click(); } }); }); if($('a.proceedToCheckout').length > 0) { validateCartAndDisplayErrors(); } $('.trimiteComanda').click(function(event){ $('body, html').animate({ scrollTop: $( $(this).attr('href') ).offset().top - 0 }, 700, "swing"); event.preventDefault(); }); }); function showCouponError() { $('#modal-coupon-error').modal(); return false; } function showCouponCampaign() { $('#modal-coupon-success').modal(); return false; } function validateCartAndDisplayErrors() { var valid = false; $.getJSON('/checkout/validateAJAX', function(data) { if(!data.success) { showMessage('cartErrorMessageTpl', data); } else { valid = true; } }); return valid; } function actuateLink(link) { if (link.click) { window.location.href = link.attr('href'); } else if (document.createEvent) { var e = document.createEvent('MouseEvents'); e.initEvent( 'click' // event type ,true // can bubble? ,true // cancelable? ); allowDefaultAction = link.dispatchEvent(e); } } function reloadCartRelatedThings(quantityItems, basketTotalPrice, grantTotalPrice, shipping, simpleItems, packageItems, discountTotalPrice, data) { $('#basket-count').text(quantityItems); $('#basketTotalDiv').find('p strong').html(basketTotalPrice); $('#discountTotalDiv').find('p span.discountTotalSpan').html(discountTotalPrice); $('#discountTotalDiv').find('div.infoDescription').html(data.discountPopup); $('#grantTotalDiv').find('p').html(grantTotalPrice); $('#basketTotalShipping').find('p').html(shipping); $('#simpleItems').replaceWith(simpleItems); $('#packageItems').replaceWith(packageItems); //$('div.rightInfoSum').replaceWith(data.rightInfoSum); updatePromotionBanners(); updateAllCheckoutDetails(); }; function updatePromotionBanners() { $.get("/blocks/onePageCheckout/checkoutPromotionBanners.jsp", function(data) { $("#promotionBannerPlaceholder").replaceWith(data); }); } function validateCoupon() { event.preventDefault(); $('#couponFormCode').val($('#claimCouponCode').val()); $('#couponFormSubmit').click(); return false; } function removeCoupon() { $('#removeFormCode').val($('#claimCouponCode').val()); $('#removeFormSubmit').click(); return false; } function changeQuantity(commerceItemId) { $("#selectChangeQuantityM_" + commerceItemId).val($("#selectChangeQuantity_" + commerceItemId).val()); $("#submitQuantity").click(); }; function changeQuantityM(commerceItemId) { $("#selectChangeQuantity_" + commerceItemId).val($("#selectChangeQuantityM_" + commerceItemId).val()); $("#submitQuantity").click(); }; function removeItemFromCart(itemId, typeItem, removeItemMessage) { $("#typeItem").val(typeItem); $("#removeItemMessage").val(removeItemMessage); $("#removeItem").val(itemId); $("#submitRemoveItem").click(); }; function closeButton(element){ $(element).parent().hide(); }; function showMessage(templateId, data) { cleanErrors(); var previousDiv=$('div.checkout-heading')[0]; var messageContainer='
'; $(messageContainer).insertAfter(previousDiv); var template = $('#'+templateId).html(); var html = Mustache.to_html(template, data); $('#messages').html(html); $("html, body").animate({ scrollTop: 0 }, 400); }; function cleanErrors() { $('#messages').remove(); $('div.alert.alert-error').remove(); }; function fillJourney(element){ var commerceItemId = $(element).siblings('.changePackageCommerceItemId').val(); $('#changePackageCommerceItemId').val(commerceItemId); var packageJourneyType = $(element).siblings('.changePackageJourneyType').val(); $('#changePackageJourneyType').val(packageJourneyType); var packageJourneyStep = $(element).siblings('.changePackageJourneyStep').val(); $('#changePackageJourneyStep').val(packageJourneyStep); }