// jCart v1.3 // http://conceptlogic.com/jcart/ $(function() { var JCART = (function() { // This script sends Ajax requests to config-loader.php and relay.php using the path below // We assume these files are in the 'jcart' directory, one level above this script // Edit as needed if using a different directory structure // Modified directory structure to assume /jcart/ folder is at root level. -MLK, 1/16/17 var path = '/jcart', container = $('#jcart'), token = $('[name=jcartToken]').val(), tip = $('#jcart-tooltip'); var config = (function() { var config = null; $.ajax({ url: path + '/config-loader.php', data: { "ajax": "true" }, dataType: 'json', async: false, success: function(response) { config = response; }, error: function() { alert('Ajax error: Edit the path in jcart.js to fix.'); } }); return config; }()); var setup = (function() { if(config.tooltip === true) { tip.text(config.text.itemAdded); // Tooltip is added to the DOM on mouseenter, but displayed only after a successful Ajax request $('.jcart [type=submit]').mouseenter( function(e) { var x = e.pageY + 25, y = e.pageX + -10; $('body').append(tip); tip.css({top: y + 'px', left: x + 'px'}); } ) .mousemove( function(e) { var y = e.pageY + 25, x = e.pageX + -10; tip.css({top: y + 'px', left: x + 'px'}); } ) .mouseleave( function() { tip.hide(); } ); } // Remove the update and empty buttons since they're only used when javascript is disabled $('#jcart-buttons').remove(); // Default settings for Ajax requests $.ajaxSetup({ type: 'POST', url: path + '/relay.php', success: function(response) { // Refresh the cart display after a successful Ajax request container.html(response); $('#jcart-buttons').remove(); }, // See: http://www.maheshchari.com/jquery-ajax-error-handling/ error: function(x, e) { var s = x.status, m = 'Ajax error: ' ; if (s === 0) { m += 'Check your network connection.'; } if (s === 404 || s === 500) { m += s; } if (e === 'parsererror' || e === 'timeout') { m += e; } alert(m); } }); }()); // Check hidden input value // Sent via Ajax request to jcart.php which decides whether to display the cart checkout button or the PayPal checkout button based on its value // We normally check against request uri but Ajax update sets value to relay.php // If this is not the checkout the hidden input doesn't exist and no value is set var isCheckout = $('#jcart-is-checkout').val(); function add(form) { // Input values for use in Ajax post var itemQty = form.find('[name=' + config.item.qty + ']'), itemAdd = form.find('[name=' + config.item.add + ']'); var itemId = form.find('[name=' + config.item.id + ']'); // Add the item and refresh cart display $.ajax({ data: form.serialize() + '&' + config.item.add + '=' + itemAdd.val(), success: function(response) { if(itemId.val() == '10023') { //turn off add cart button for related products $('#add-to-cart10045 input.button').attr("disabled", "disabled"); $('#add-to-cart10045 input:radio[name="my-item-event"]').attr("disabled", "disabled"); //turn off add cart button for related products $('#add-to-cart10046 input.button').attr("disabled", "disabled"); $('#add-to-cart10046 input:radio[name="my-item-event"]').attr("disabled", "disabled"); } if(itemId.val() == '10045' || itemId.val() == '10046') { //only for these specific items! //var isBlockedProducts = $(response).html().search('blockedProductsError'); //search for the blocked products error //alert(isBlockedProducts); //turn off add cart button for related products $('#add-to-cart10023 input.button').attr("disabled", "disabled"); $('#add-to-cart10023 input:radio[name="my-item-event"]').attr("disabled", "disabled"); } // Momentarily display tooltip over the add-to-cart button if (itemQty.val() > 0 && tip.css('display') === 'none') { tip.fadeIn('100').delay('400').fadeOut('100'); } //turn off add cart button $('#add-to-cart'+itemId.val()).css("display", "none"); $('#in-cart'+itemId.val()).css("display", "block"); container.html(response); $('#jcart-buttons').remove(); } }); } function update(input) { // The id of the item to update var updateId = input.parent().find('[name="jcartItemId[]"]').val(); // The new quantity var newQty = input.val(); // As long as the visitor has entered a quantity if (newQty) { // Update the cart one second after keyup var updateTimer = window.setTimeout(function() { // Update the item and refresh cart display $.ajax({ data: { "jcartUpdate": 1, // Only the name in this pair is used in jcart.php, but IE chokes on empty values "itemId": updateId, "itemQty": newQty, "jcartIsCheckout": isCheckout, "jcartToken": token } }); }, 1000); } // If the visitor presses another key before the timer has expired, clear the timer and start over // If the timer expires before the visitor presses another key, update the item input.keydown(function(e){ if (e.which !== 9) { window.clearTimeout(updateTimer); } }); } function remove(link) { // Get the query string of the link that was clicked var queryString = link.attr('href'); queryString = queryString.split('='); // The id of the item to remove var removeId = queryString[1]; // Remove the item and refresh cart display $.ajax({ type: 'GET', data: { "jcartRemove": removeId, "jcartIsCheckout": isCheckout }, success: function(response) { //turn on add cart button, if present $('#add-to-cart'+removeId).css("display", "block"); $('#in-cart'+removeId).css("display", "none"); container.html(response); $('#jcart-buttons').remove(); //check overlapping products AFTER removing item from cart. But first //cycle through registered items to see if we can safely turn these items on. var has10045registered = false; var has10046registered = false; if(removeId == "10023") { //check registered courses for overlapping products. $('input[name="jcartRegisteredItemId[]"]').each( function() { var itemId = $(this).val(); if(itemId == "10045") { has10045registered = true; } if(itemId == "10046") { has10046registered = true; } }); //restore related products 10045 and 10046 if allowed. if(!has10045registered) { $('#add-to-cart10045 input.button').removeAttr("disabled"); $('#add-to-cart10045 input:radio[name="my-item-event"]').removeAttr("disabled"); } if(!has10046registered) { $('#add-to-cart10046 input.button').removeAttr("disabled"); $('#add-to-cart10046 input:radio[name="my-item-event"]').removeAttr("disabled"); } } //cycle through cart to check if we can safely turn product 10023 on, //or if there are still overlapping products in the cart. var hasOverlappingProducts = false; if(removeId == "10045" || removeId == "10046") { //check cart items for overlapping products. $('input[name="jcartItemId[]"]').each( function() { var itemId = $(this).val(); if(itemId == "10045" || itemId == "10046") { hasOverlappingProducts = true; } }); //check registered courses for overlapping products. $('input[name="jcartRegisteredItemId[]"]').each( function() { var itemId = $(this).val(); if(itemId == "10045" || itemId == "10046") { hasOverlappingProducts = true; } }); } if(!hasOverlappingProducts) { //restore access to 10023 product. $('#add-to-cart10023 input.button').removeAttr("disabled"); $('#add-to-cart10023 input:radio[name="my-item-event"]').removeAttr("disabled"); } } }); } // Add an item to the cart $('.jcart').submit(function(e) { add($(this)); e.preventDefault(); }); // Prevent enter key from submitting the cart container.keydown(function(e) { if(e.which === 13 && e.target.tagName != 'TEXTAREA') { e.preventDefault(); } }); // Update an item in the cart container.delegate('[name="jcartItemQty[]"]', 'keyup', function(){ update($(this)); }); // Remove an item from the cart container.delegate('.jcart-remove', 'click', function(e){ remove($(this)); e.preventDefault(); }); }()); // End JCART namespace }); // End the document ready function