var cartInfo = { COOKIE_NAME : "apex__force_commerce", empty_str : "", open : false }; var ipLocationInfo = { COOKIE_NAME : "apex__ipLocation", VALUE_STRING : "", ISVALID_VALUE : true }; $(document) .ready( function() { if ($.cookie(ipLocationInfo.COOKIE_NAME) != null) { ipLocationInfo.VALUE_STRING = $ .cookie(ipLocationInfo.COOKIE_NAME); ipLocationInfo.ISVALID_VALUE = ipLocationInfo.VALUE_STRING === "true" ? true : false } if ($("#cart").length) { setIsLocation(); if (verifyCart()) { var a = parseCookieToJson($ .cookie(cartInfo.COOKIE_NAME)); drawCart(a) } else { updateItemCount(); $.cookie(cartInfo.COOKIE_NAME, ":P", { expires : 30, path : "/" }) } initCart(); bindCartEvents(); } $(".mod-scrollable-list").scrollable( { circular : true, mousewheel : true, next : "#mod-scrollitems-next", prev : "#mod-scrollitems-prev" }) }); var bindCartEvents = function() { $(".cart-head-link").on("click", toggleCart) }; var unbindCartEvents = function() { $(".cart-head-link").off("click", toggleCart) }; var toggleCart = function(e) { var b = false; if ($(e.target).closest('a.cart-link').size() !== 0) { return; } ; jQuery.each(parseCookieToJson($.cookie(cartInfo.COOKIE_NAME)), function(g, f) { if (f.autoreord === 'true') { b = true; return; } }); //if (!b) /*if (!cartInfo.open) showCart(); else hideCart();*/ return false; }; var showCart = function() { $(".inner_cart_contents") .slideToggle( "fast", function() { $("#cart").addClass("open"); $("#cart .cart-nav").hide(); $("body") .append( '
'); $("#cart-overlay").on("click", hideCart); if (jResBreakpoint == "handheld" && typeof updateMobileHeaderGradient === "function") updateMobileHeaderGradient(); cartInfo.open = true; }) }; var hideCart = function() { $(".inner_cart_contents").slideToggle( "fast", function() { $("#cart").removeClass("open"); $("#cart .cart-nav").show(); $("#cart-overlay").off("click", hideCart).remove(); if (jResBreakpoint == "handheld" && typeof updateMobileHeaderGradient === "function") updateMobileHeaderGradient(); cartInfo.open = false; }) }; var clearCart = function() { $.cookie(cartInfo.COOKIE_NAME, null, { path : "/" }); var a = parseCookieToJson($.cookie(cartInfo.COOKIE_NAME)); drawCart(a); initCart(); }; var initCart = function() { $("#cart h6").hide(); $(".qty-input").keypress(function(a) { if (a.which != 8 && a.which != 0 && (a.which < 48 || a.which > 57)) return false; }) }; var verifyCart = function() { if ($.cookie(cartInfo.COOKIE_NAME) == null || $.cookie(cartInfo.COOKIE_NAME) == "null") return false; else return true; }; var removeCookie = function() { $.cookie(cartInfo.COOKIE_NAME, "", { path : "/" }); $.cookie(cartInfo.COOKIE_NAME, null, { path : "/" }) }; var deleteProduct = function(b) { var a = parseCookieToJson($.cookie(cartInfo.COOKIE_NAME)); var c = []; jQuery.each(a, function(g, f) { if (f.id != b) c.push(f); }); var e = ""; var d = 0; jQuery.each(c, function(g, f) { e += f.id + "|" + f.name + "|" + f.image + "|" + f.price + "|" + f.qty + "|" + f.max_qty + "|" + f.sku + "|" + f.ins + "|" + f.prsc + "|||"; d += parseFloat(f.price) * parseFloat(f.qty) }); $.cookie(cartInfo.COOKIE_NAME, e, { expires : 30, path : "/" }); $("." + b).fadeOut("fast", function() { $(this).remove(); $(".price_header .value").html((new Number(d)).toFixed(2)); if (d == 0) drawCart(c); }); updateItemCount(); }; var updateEvents = function() { var a, b; $(document) .on( "keydown keyup change", ".qty-input", function(c) { a = $(this).attr("data-id"); if (c.type == "keydown") { if (!validateNumber(c)) return false; b = parseFloat($(this).val()) } else if (c.type == "keyup" || c.type == "change") { if (c.type == "change" && ($(this).val() == "" || isNaN($(this).val()) || parseInt($( this).val()) == "0")) $(this).val("1"); if ($(this).val() != b) $("." + a).find(".update").removeAttr( "disabled").removeClass("disabled") .one( "click", function() { $(this).attr("disabled", "disabled") .addClass( "disabled") }) } }) }; var updateProduct = function(f, g) { var m = parseCookieToJson($.cookie(cartInfo.COOKIE_NAME)); var g = parseInt(g); var j = $("." + f + " .qty-input"); var k = j.val(); var l = g != 0 && g != undefined && g != "" ? true : false; var e = "Quantity for this product is too large"; var b = "Limit " + g + " of this product."; if (k > 1E6) { $("." + f).find(".qty_error p").text(e); $("." + f).find(".qty_error").show(); j.addClass("alert-input"); return false; } else if (l && k > g) { j.addClass("alert-input").val(g); k = g; $("." + f).find(".qty_error p").text(b); $("." + f).find(".qty_error").show() } else { $(".qty_error").hide(); j.removeClass("alert-input"); } if (k == "") k = 1; var a = []; var d = 0; jQuery.each(m, function(o, n) { var p = n; if (n.id == f) { p.qty = k; d += parseFloat(n.price) * parseFloat(n.qty) } a.push(p); }); var c = ""; var i = 0; jQuery.each(a, function(o, n) { c += n.id + "|" + n.name + "|" + n.image + "|" + n.price + "|" + n.qty + "|" + n.max_qty + "|" + n.sku + "|" + n.ins + "|" + n.prsc + "|||"; i += parseFloat(n.price) * parseFloat(n.qty) }); $.cookie(cartInfo.COOKIE_NAME, c, { expires : 30, path : "/" }); updateTotal(i); updateItemCount(); var h = (new Number(d)).toFixed(2); $("." + f + " .price.amm").html(addCommas(h)); $("." + f + " .odd .price").html(addCommas(h)); $("." + f).effect("highlight", "", 1E3, ""); }; var message = function(a) { $(".dialog").children("p").html(a); $(".dialog").dialog("open"); return false; }; var clearMessage = function() { $(".dialog").children("p").html(""); }; var updateCheckoutProduct = function(c, b, h) { var a = parseCookieToJson($.cookie(cartInfo.COOKIE_NAME)); if (b > 1E6) { var msg = "Quantity for this product is too large"; if (typeof $ === "function" && typeof $.toaster === "function") toastMsg(msg); else message(msg); return false; } var d = []; var f = 0; jQuery.each(a, function(k, j) { var m = j; if (j.id == c) { m.qty = b; f += parseFloat(j.price) * parseFloat(j.qty) } d.push(m) }); var g = ""; var e = 0; jQuery.each(d, function(k, j) { g += j.id + "|" + j.name + "|" + j.image + "|" + j.price + "|" + j.qty + "|" + j.max_qty + "|" + j.sku + "|" + j.ins + "|" + j.prsc + "|||"; e += parseFloat(j.price) * parseFloat(j.qty) }); $.cookie(cartInfo.COOKIE_NAME, g, { expires : 30, path : "/" }) }; var addProduct = function(b, a, g, i, m, l, s, ins, prsc, autoreord) { var m = parseInt(m); var l = parseInt(l) || 0; var o = l > 0 ? true : false; var c = false; if (m > 1E6) { alertToast("Quantity for this product is too large"); return false; } if (!verifyCart()) $.cookie(cartInfo.COOKIE_NAME, ":P", { expires : 30, path : "/" }); var n = parseCookieToJson($.cookie(cartInfo.COOKIE_NAME)); var f = false; if (o && m > l) { alertToast("Limit " + l + " of this product."); return false; } jQuery.each(n, function(p, e) { if (e.id == b) { if (m + e.qty > 1E6) { alertToast("Quantity for this product is too large"); f = true; } if (o && m + parseInt(e.qty) > l) { alertToast("Limit " + l + " of this product."); f = true; c = true; } e.qty = parseInt(e.qty) + m; f = true; } }); if (!f) { var h; try { h = jQuery.parseJSON('{"id": "' + b + '", "name":"' + a + '", "image":"' + g + '", "price":"' + i + '", "qty":"' + m + '", "max_qty":"' + l + '","sku":"' + s + '", "ins":"' + ins + '","prsc":"' + prsc + '", "autoreord":"' + autoreord + '"}') } catch (j) { var k = a.replace(/"/g, """); h = jQuery.parseJSON('{"id": "' + b + '", "name":"' + k + '", "image":"' + g + '", "price":"' + i + '", "qty":"' + m + '", "max_qty":"' + l + '","sku":"' + s + '", "ins":"' + ins + '", "prsc":"' + prsc + '", "autoreord":"' + autoreord + '"}') } n.push(h); } if (!c) { var h = ""; jQuery.each(n, function(p, e) { h += e.id + "|" + e.name + "|" + e.image + "|" + e.price + "|" + e.qty + "|" + e.max_qty + "|" + e.sku + "|" + e.ins + "|" + e.prsc + "|" + e.autoreord + "|||" }); $.cookie(cartInfo.COOKIE_NAME, h, { expires : 30, path : "/" }); var drawCartCheck = drawCart(n); initCart(); //var d = "Product added to cart..."; //toastMsg(d); return drawCartCheck; } }; var parseCookieToJson = function(a) { var b = []; if (a != null) { b = a.replace(/^\"/,'').replace(/\"$/,'').split("|||"); b.pop() } else { b = []; } var c = []; jQuery.each(b, function(f, d) { var e = d.split("|"); var _s = e[6] !== undefined ? e[6] : " "; var _i = e[7] !== undefined ? "" + e[7] : "false"; var _p = e[8] !== undefined ? "" + e[8] : "false"; var _a = e[9] !== undefined ? "" + e[9] : "false"; var g = jQuery.parseJSON('{"id": "' + e[0] + '", "name":"' + e[1] + '", "image":"' + e[2] + '", "price":"' + e[3] + '", "qty":"' + e[4] + '", "max_qty":"' + e[5] + '", "sku":"' + _s + '", "ins":"' + _i + '","prsc":"' + _p + '", "autoreord":"' + _a + '"}'); c.push(g); }); return c; }; var htmlDecode = function(a) { return a ? $("
").html(a).text() : ""; }; var drawCart = function(c) { var b = ""; var e = 0; var d = $("#shopping_products").find(".empty"); if (d.length) cartInfo.empty_str = d.text(); var a = Modernizr.touch ? "number" : "text"; jQuery .each( c, function(g, f) { var h = parseFloat(f.price) * parseFloat(f.qty); e += h; b += '
'; b += '
'; b += '
'; b += '
'; b += "
"; b += '
'; b += "" + htmlDecode(f.name) + "
"; if (f.sku != "") b += "Product Code: " + htmlDecode(f.sku) + ""; b += "
"; b += '
'; if (ipLocationInfo.ISVALID_VALUE) { b += '$' + addCommas((new Number(f.price)) .toFixed(2)) + ""; b += ' X '; b += ' = $' + addCommas((new Number(h)).toFixed(2)) + "
" } if (!ipLocationInfo.ISVALID_VALUE) b += ''; b += ''; b += 'Remove"; b += "
"; b += "
"; b += '

Quantity for this product is too large

'; b += "
"; b += "
" }); $(".inner_cart_footer").show(); if (e.length == 0) { b = '

' + cartInfo.empty_str + "

"; $(".inner_cart_footer").hide(); } $(".total .price").html(e); var updateCountCheck = updateItemCount(); updateTotal(e); updateEvents(); $("#shopping_products").html(b) return updateCountCheck; }; var updateItemCount = function() { var c = "empty", b = 0, a; if ($.cookie(cartInfo.COOKIE_NAME) == null || $.cookie(cartInfo.COOKIE_NAME) == "null") { $(".txt_nav_basket").text(c); $(".txt_header_basket").text(c); return false; } a = parseCookieToJson($.cookie(cartInfo.COOKIE_NAME)); jQuery.each(a, function(e, d) { b += parseFloat(d.qty); }); if (typeof b === "number") if (b > 1) c = b + " items"; else if (b == 1) c = b + " item"; else c = "empty"; $(".txt_nav_basket").text(b); $(".txt_header_basket").text(b); return true; }; var updateTotal = function(c) { var a = $(".price_header .value"); var b = (new Number(c)).toFixed(2); a.html(addCommas(b)); }; var setIsLocation = function() { $(".price_header .value").prev().text("Sign In"); if (ipLocationInfo.ISVALID_VALUE) { $(".price_header .value").css("display", ""); $(".price_header .value").prev().text("$"); } }; var getProducts = function() { if (!verifyCart()) return ""; else { var b = parseCookieToJson($.cookie(cartInfo.COOKIE_NAME)); var a = ""; jQuery.each(b, function(d, c) { if (d == 0) a += c.id + ":" + c.qty; else a += ":" + c.id + ":" + c.qty; }); return a; } }; var addCommas = function(b) { b += ""; x = b.split("."); x1 = x[0]; x2 = x.length > 1 ? "." + x[1] : ""; var a = /(\d+)(\d{3})/; while (a.test(x1)) x1 = x1.replace(a, "$1,$2"); return x1 + x2; }; var addFav = function(a) { addToFavorites(a); }; var favToast = function() { toastMsg("Product added to favorites..."); }; var unFavToast = function() { toastMsg("Product excluded from favorites..."); }; var favQtyToast = function() { toastMsg("Quantity of favorites updated..."); }; var QtyErrToast = function() { toastMsg("Quantity must be a whole number..."); }; var alertToast = function(b) { var a = $.toaster( { showTime : 4E3, centerX : true, centerY : true, error : true }); a.toast(b); }; var toastMsg = function(b) { var a = $.toaster( { showTime : 2E3, centerX : true, centerY : true }); a.toast(b); }; var delFav = function(a) { deleteFavorite(a); }; var updateFav = function(a, b) { if (b == 0) removeFav(a); else updateQuantity(a, b); }; var getValId = function(a) { return document.getElementById(a).value; }; var validateNumber = function(e) { var key = !e.keyCode ? e.which : e.keyCode; if (key == null || key == 0 || key == 8 || key == 13 || key == 37 || key == 39 || key == 46 || key == 9 || key >= 96 && key <= 105) return true; if (/\d/.test(String.fromCharCode(key))) return true; else if (key == 16 || key == 17 || key == 18) return false; else { QtyErrToast(); return false; } }; var getNonPrescriptionProducts = function() { if (!verifyCart()) { return ""; } ; var b = parseCookieToJson($.cookie(cartInfo.COOKIE_NAME)); var a = ""; jQuery.each(b, function(d, c) { if (c.prsc !== 'true') { if (d === 0 || a === '') { a += c.id + ":" + c.qty; } else { a += ":" + c.id + ":" + c.qty; } } }); return a; };