// Common Javascript store functions function formatCurrency() { $('.format-currency').blur(function () { var value = parseInt($(this).val().replace(/[^\d.-]/g, '')); $(this).val(value.toLocaleString('en-US', { style: 'currency', currency: 'USD' })); }) } $(document).ready(function () { formatCurrency(); }); $(document).ready(function(){ preloadGlyphicons(); updateCartTotals(getCookie("cart")); $('.add-to-cart').click(function (e) { e.preventDefault(); var productqty = "#qty-" + $(this).data("product"); var product = $(this).data("product") + ":" + $(productqty).val(); var upgrades = []; var error = ""; $('.product-upgrade').each(function (i, u) { isRequired = $(u).find('option[value="' + $(u).val() + '"]').data('is-required') if (isRequired && !error) { error = $(u).attr('name'); } upgrades.push($(u).val()); }); if (error) { $('#message').html('
Please select an option for ' + error + '
'); } else { var jqxhr = $.getJSON("/store/cart/add/" + getCookie("cart") + "?product=" + product + "&upgrades=" + upgrades, function (r) { if (jqxhr.Error) { $('#message').html(jqxhr.Message); } else if (r.Error) { $('#message').html(r.Message); } else { document.location.href = '/cart'; } }) .fail(function () { alert('error'); }); } }); $(".add-to-wishlist").click(function (e) { e.preventDefault(); alert('add product to wishlist ' + $(this).data("product")); }); $(".remove-from-cart").click(function (e) { e.preventDefault(); removeUm(e); }); $(".update-um").click(function (e) { e.preventDefault(); updateUm(e); //var product = $(this).data("product"); //var jqxhr = $.getJSON("/cart/update/" + getCookie("cart") + "?product=" + product + "&qty=" , function (r) { // if (jqxhr.Error) { alert(jqxhr.Message); } // else if (r.Error) { alert(r.Message); } // else { // document.location.href = '/cart'; // } //}) //.fail(function () { // alert('error'); //}); }); $("#search-btn").click(function (e) { e.preventDefault(); if ($("#search-val").val() != "") { document.location.href = '/search-results?s=' + $("#search-val").val(); } }); $('#search-val').keydown(function (event) { var keypressed = event.keyCode || event.which; if (keypressed == 13) { if ($("#search-val").val()) { document.location.href = '/search-results?s=' + encodeURIComponent($("#search-val").val()); } } }); $('#billingForm').submit(function (e) { e.preventDefault(); submitCheckoutBillingForm(e); }); $('#shippingForm').submit(function (e) { e.preventDefault(); submitCheckoutShippingForm(e); }); $('#paymentForm').submit(function (e) { e.preventDefault(); submitCheckoutPaymentForm(e); }); $('#summaryForm').submit(function (e) { e.preventDefault(); submitCheckoutSummaryForm(e); }); $('#couponForm').submit(function (e) { e.preventDefault(); submitCouponForm(e); }); $('.removePromo').click(function (e) { e.preventDefault(); removePromo($(this).data('promo')); }); $('#btnSubmitZipcode').click(function (e) { e.preventDefault(); setZipcode(e) }); $('.shippingRate').change(function (e) { e.preventDefault(); setShipping(e) }); $('#savedAddress').change(function (e) { e.preventDefault(); loadSavedAddress(e); }); $('#paypal-test').click(function (e) { e.stopPropagation(); e.preventDefault(); paypalTest(e) }); $('#contactForm').submit(function (e) { e.preventDefault(); submitFeedback(e); }); $('#print').click(function (e){ e.stopPropagation(); e.preventDefault(); window.print(); }); $('#useBillingAddress').click(function (e) { if ($('#useBillingAddress:checked').length == 1) { $('#inputAttention, #inputAddress1,#inputAddress2,#inputCity,#inputState,#inputPostCode,#inputCountry').each(function () { $(this).val($(this).data('billing')); }) } }); $('#billMeLater').click(enableDisablePaymentField); enableDisablePaymentField(); }); function updateItem(prodNum, qty){ var cartguid = getCookie("cart"); var jqxhr = $.getJSON("/store/cart/add/" + cartguid + "?product=" + prodNum + ":" + qty, function (r) { if (r.Error) { $('#message').html("
" + r.Message + "
"); logError(result.Message); } else { updateCartTotals(cartguid); } }) .fail(function () { $('#message').html("
" + 'error' + "
"); }); } function updateCartTotals(cartguid){ var jqxhr = $.getJSON("/store/cart/status/" + cartguid, function (r) { if (r.Error) { $('#message').html("
" + r.Message + "
"); logError(result.Message); } else { $("#cart-item-count").text(r.TotalQuantity); $(".cart-total-amount").text(r.TotalAmount); } }) } function getCookie(cname) { var name = cname + "="; var decodedCookie = decodeURIComponent(document.cookie); var ca = decodedCookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; c = c.trim(); if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } var guid = createGUID(); setCookie(cname, guid, 14); return guid; } function createGUID() { return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) ); } function setCookie(key, value, expiry) { var expires = new Date(); expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000)); document.cookie = key + '=' + value + ';expires=' + expires.toUTCString() + "; path=/"; } function submitCheckoutBillingForm(e) { var button = $("#billingForm").find("button:focus"); if (button.length == 0) { button = $("#next"); } var buttons = $('#billingForm').find('button'); buttons.prop("disabled", true); button.data("label", button.html()); button.html(" " + button.data("label")); //button.find(".glyphicon").show(); $.ajax({ type: "POST", url: '/store/checkout/billing/save', data: JSON.stringify({ 'firstName': $('#inputFirstName').val(), 'lastName': $('#inputLastName').val(), 'email': $('#inputEmail').val(), 'phone': $('#inputPhone').val(), 'address1': $('#inputAddress1').val(), 'address2': $('#inputAddress2').val(), 'city': $('#inputCity').val(), 'postCode': $('#inputPostCode').val(), 'country': ($('#inputCountry').length > 0 ? $('#inputCountry').val() : ""), 'state': $('#inputState').val(), 'direction': button.attr('id'), }), contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { buttons.prop("disabled", false); button.html(button.data('label')); //button.find(".glyphicon").hide(); $('#message').html("
" + result.Message + "
"); logError(result.Message); } else { document.location.href = '/checkout/shipping'; } } }); } function submitCheckoutShippingForm(e) { var button = $("#shippingForm").find("button:focus"); if (button.length == 0) { button = $("#next"); } var buttons = $('#shippingForm').find('button'); buttons.prop("disabled", true); button.prop("disabled", true); button.data("label", button.html()); button.html(" " + button.data("label")); $.ajax({ type: "POST", url: '/store/checkout/shipping/save', data: JSON.stringify({ 'attention': $('#inputAttention').val(), 'address1': $('#inputAddress1').val(), 'address2': $('#inputAddress2').val(), 'city': $('#inputCity').val(), 'postCode': $('#inputPostCode').val(), 'country': ($('#inputCountry').length > 0 ? $('#inputCountry').val() : ""), 'state': $('#inputState').val(), 'direction': button.attr('id'), }), contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#message').html("
" + result.Message + "
"); buttons.prop("disabled", false); button.html(button.data('label')); logError(result.Message); } else { if (button.attr('id') == 'previous') { document.location.href = '/checkout/billing'; } else { document.location.href = '/checkout/payment'; } } } }); } function submitCheckoutPaymentForm(e) { var button = $("#paymentForm").find("button:focus"); if (button.length == 0) { button = $("#next"); } var buttons = $('#paymentForm').find('button'); buttons.prop("disabled", true); button.data("label", button.html()); button.html(" " + button.data("label")); $.ajax({ type: "POST", url: '/store/checkout/payment/save', data: JSON.stringify({ 'billMeLater': $('#billMeLater:checked').val(), 'nameOnCard': $('#inputNameOnCard').val(), 'cardType': $('#inputCardType').val(), 'cardNumber': $('#inputCardNumber').val(), 'expirationMonth': $('#inputExpirationMonth').val(), 'expirationYear': $('#inputExpirationYear').val(), 'ccv': $('#inputCcv').val(), 'direction': button.attr('id'), }), contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#message').html("
" + result.Message + "
"); buttons.prop("disabled", false); button.html(button.data('label')); logError(result.Message); } else { if (button.attr('id') == 'previous') { document.location.href = '/checkout/shipping'; } else { document.location.href = '/checkout/summary'; } } } }); } function submitCheckoutSummaryForm(e) { var button = $("#summaryForm").find("button:focus"); if (button.length == 0) { button = $("#next"); } var buttons = $('#summaryForm').find('button'); buttons.prop("disabled", true); button.data("label", button.html()); button.html(" " + button.data("label")); if (button.attr('id') == 'previous') { buttons.prop("disabled", false); button.html(button.data('label')); document.location.href = '/checkout/payment'; } else { buttons.prop("disabled", false); button.html(button.data('label')); document.location.href = '/checkout/processing'; } } function submitCouponForm(e) { $.ajax({ type: "POST", url: '/store/cart/add/' + getCookie("cart") + '?promo=' + $('#inputCouponCode').val(), contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#couponMessage').html("
" + result.Message + "
"); logError(result.Message); } else { document.location.reload(); } } }); } function removePromo(promo) { $.ajax({ type: "POST", url: '/store/cart/update/' + getCookie("cart") + '?promo=' + promo, contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#message').html("
" + result.Message + "
"); logError(result.Message); } else { document.location.reload(); } } }); } function removeUm(e) { var guid = $(e.target).data("guid"); if (guid == undefined) { guid = $(e.target).parent().data("guid"); } $.ajax({ type: "POST", url: "/store/cart/update/" + getCookie("cart") + "?product=" + guid + "&action=remove", contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#message').html("
" + result.Message + "
"); logError(result.Message); } else { document.location.reload(); } } }); } function updateUm(e) { var guid = $(e.target).data("guid"); if (guid == undefined) guid = $(e.target).parent().data("guid"); var qty = $('#quantity-' + guid).val(); $.ajax({ type: "POST", url: "/store/cart/update/" + getCookie("cart") + "?product=" + guid + "&action=update-qty&qty=" + qty, contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#message').html("
" + result.Message + "
"); logError(result.Message); } else { document.location.reload(); } } }); } function setZipcode(e) { var zipcode = $('#inputZipCode').val(); $.ajax({ type: "POST", url: "/store/cart/set-zipcode/" + getCookie("cart") + "/" + zipcode, contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#message').html("
" + result.Message); logError(result.Message); } else { document.location.reload(); } } }); } function getGetshipping(e) { $.ajax({ type: "POST", url: "/store/cart/get-shipping/" + getCookie("cart"), contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#message').html("
" + result.Message + "
"); logError(result.Message); } else { document.location.reload(); } } }); } function setShipping(e) { $.ajax({ type: "POST", url: "/store/cart/set-shipping/" + getCookie("cart") + "/" + $(e.target).val(), contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#message').html("
" + result.Message + "
"); logError(result.Message); } else { document.location.reload(); } } }); } function loadSavedAddress(e) { $.ajax({ type: "POST", url: "/store/account/address/" + $(e.target).val(), contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { if (result.Error) { $('#message').html("
" + result.Message + "
"); logError(result.Message); } else { $('#inputAttention').val(result.Attention); $('#inputAddress1').val(result.Address1); $('#inputAddress2').val(result.Address2); $('#inputCity').val(result.City); $('#inputState').val(result.State); $('#inputPostCode').val(result.Zip); $('#inputCountry').val(result.Country); } } }); } function logError(message) { //$.ajax({ // type: "POST", // url: "/store/log-error", // data: JSON.stringify({ // 'message': message, // 'page': window.location.href // }), // contentType: "application/json;charset=utf-8", // dataType: "json", // success: function (result) { // } //}); } function submitFeedback() { var button = $("#contactForm").find("button:focus"); var buttons = $('#contactForm').find('button'); buttons.prop("disabled", true); button.data("label", button.html()); button.html(" " + button.data("label")); $.ajax({ type: "POST", url: '/form/post', data: JSON.stringify({ 'txtFirstName': $('#txtFirstName').val(), 'txtLastName': $('#txtLastName').val(), 'txtEmail': $('#txtEmail').val(), 'txtPhone': $('#txtPhone').val(), 'txtMessage': $('#txtMessage').val(), 'txtSubject': $('#txtSubject').val(), 'txtCompany': $('#txtCompany').val(), 'txtAddress': $('#txtAddress').val(), 'txtCity': $('#txtCity').val(), 'lbxState': $('#lbxState').val(), 'txtZIP': $('#txtZIP').val(), 'rdbInquiryType': $('#rdbInquiryType').val(), 'rdbPreferredMethodOfContact': $('#rdbPreferredMethodOfContact').val(), 'g-recaptcha-response': $('#g-recaptcha-response').val(), }), contentType: "application/json;charset=utf-8", dataType: "json", success: function (result) { buttons.prop("disabled", false); button.html(button.data('label')); if (result.Error) { buttons.prop("disabled", false); button.html(button.data('label')); $('#result').html("
" + result.Message + "
"); logError(result.Message); } else { $('#result').html("
" + result.Message + "
"); $('#txtFirstName').val(""); $('#txtLastName').val(""); $('#txtEmail').val(""); $('#txtMessage').val(""); $('#txtSubject').val(""); $('#txtPhone').val(""); $('#txtCompany').val(""); $('#txtAddress').val(""); $('#txtCity').val(""); $('#lbxState').val(""); $('#txtZIP').val(""); } } }); } function preloadGlyphicons() { var span = $(""); $('body').append(span); setTimeout( function() { span.remove(); }, 50); } function enableDisablePaymentField(){ if ($('#billMeLater:checked').length == 1) { $('#inputNameOnCard, #inputCardType,#inputCardNumber,#inputExpirationMonth,#inputExpirationYear,#inputCcv').each(function () { $(this).prop( "disabled", true ); }) } else { $('#inputNameOnCard, #inputCardType,#inputCardNumber,#inputExpirationMonth,#inputExpirationYear,#inputCcv').each(function () { $(this).prop( "disabled", false ); }) } }