// Used in multiple functions to check page url var loc = location.href; var minibasket = 0; var altunits = 0; var incvat = 0; var currentSearchRequest = null; var currentQuickRequest = null; var currentMobileRequest = null; /** * Main on document load function **/ $(function () { /** * Alternate unit calculation on select change */ if($('select.altunitselect').length > 0 && altunits === 1){ // Alt unit select change $('select.altunitselect').change(function () { var price = $('.pdetails .price:eq(0)'); var opt = $(this).find('option:selected'); var prc = opt.data('price'); var prcinc = opt.data('priceinc'); var prcex = opt.data('priceex'); var prctext = ""; // Price per label update var priceper = "Price per " + $(opt).text(); $(".price-per").html(priceper); if(incvat === 0){ if(prcex !== "0"){ prctext += prcex + EXCVAT_TEXT; } } else { if(prcex !== "0"){ prctext += prcex + INCVAT_TEXT; } } price.html("" + prctext + ""); // Quantity break quantities and prices for(var i = 1; i <= 6; i++){ // Skip bad qty breaks (can't buy in certain qty breaks (e.g 1-0) if(opt.data('break-' + i + '-skip') === ""){ if(incvat == 0) { $('.qtybreak' + i + '-qtys').html(opt.data('break-' + i + '-qtys')); $('.qtybreak' + i + '-price').html(opt.data('break-' + i + '-priceex')); if(i === 1){ price.html("" + opt.data('break-' + i + '-priceex') + ""); } } else { $('.qtybreak' + i + '-qtys').html(opt.data('break-' + i + '-qtys')); $('.qtybreak' + i + '-price').html(opt.data('break-' + i + '-priceinc')); if(i === 1) { price.html("" + opt.data('break-' + i + '-priceinc') + ""); } } } else { // not available, empty it $('.qtybreak' + i + '-qtys').html(""); $('.qtybreak' + i + '-price').html(""); } } //changes a hidden form element inside the actual form. $(this).parent().parent().find('input#unit').val(opt.val()); }); $("#basketsubmit").bind("click", function(e){ // Stop customer changing alt unit $('select.altunitselect').find('option').not(":selected").each(function (i) { $(this).attr("disabled", true); }); }); // preselect first one on load $( "select.altunitselect" ).trigger( "change" ); } // Alternate products on basket page (uses related products) $(".alternative-product").on("click", function(e){ e.preventDefault(); // Change chevron to up else down if($(this).children("i").hasClass("fa-chevron-down")){ $(this).children("i").removeClass("fa-chevron-down"); $(this).children("i").addClass("fa-chevron-up"); } else { $(this).children("i").removeClass("fa-chevron-up"); $(this).children("i").addClass("fa-chevron-down"); } // slide related box up else down var suggestedBox = $(this).parent().find(".suggestedbox"); if(suggestedBox.is(":visible")){ suggestedBox.slideUp(); } else { suggestedBox.slideDown(); } }); /* Begin mega menu hover delay logic */ var menu = $('.level1'); var menu_items = menu.children('li'); var overlay = $('#menu-overlay'); var setDiv; menu.bind('mouseenter', function (e) { e.preventDefault(); $('.navtop').addClass('hovered'); overlay.stop(true, true).delay( (2 * 100) ).fadeTo(0, 0.6); var scope_this = $(this); scope_this.children("ul").eq(0).attr("style", "display:none"); $(function () { setDiv = setTimeout(function () { scope_this.children("ul").eq(0).attr("style", "display:block"); }, (2 * 100) ) }); }); menu.bind('mouseleave', function () { menu_items.children('div').hide(); $('.dropdown-menu').removeAttr("style"); clearTimeout(setDiv); }); $('.navtop').bind('mouseleave', function () { $('.navtop').removeClass('hovered'); overlay.stop(true, true).delay(100).fadeTo(100, 0); overlay.css("display", "none"); }); /* End mega menu hover delay logic */ // Account switch table $('#account-switch-table').dataTable({ /** Good defaults to have for eShop */ /*responsive: true,*/ stateSave: true, "sPaginationType": "full_numbers", paging: true, ordering: true, info: true, }); // Points log table - sorting disabled as it doesn't work well with the dates $('#reward-points-table').dataTable({ stateSave: true, "pagingType": "full_numbers", paging: true, "aaSorting": [], "bSort" : false, info: true, }); // When changing from business to personal change vat calc by triggering basket update $('input[name^="personal_business"]').change(function() { $('#editbasketform input[value="Update Basket"]').click(); }); // Global popover $('[data-toggle="popover"]').popover(); // Product video js $('.btnVideo').on('click', function () { $('.main-image').hide(); $('#video').show(); }); $('.zoom-thumbs a').click(function () { $('.main-image').show(); $('#video').hide(); }); window.onload = function () { $('#video').show(); }; // Product/group lightbox on clicks // Already included in productgroup.php //$(".lightbox").colorbox({photo: true, width: "80%", height: "80%"}); // Main drilldown menu $('.drilldown').drilldown(); // Quick order search suggest if($('#quick-order-form').length > 0 ) { // if on fast order form lets hijack the links $("#QuickSearchResults").on('click', 'a',function(e) { e.preventDefault(); copyStockcode('mouseclick', this); $('#QuickSearchResults').hide(); return false; }); //focus on field on page load $( "#stockcode").focus(); $( "#stockcode" ).keyup(function(e){ // lookup var term = $(this).val(); if (term.length >= 3 && (e.keyCode!==38 && e.keyCode!==40 && e.keyCode!==13)){ doQuickSearch($(this).val(), "quickorder"); } else if(term.length === 0){ $( "#QuickSearchResults").hide(); } }); // Fast/Quick order add to basket function fastOrderAjaxBasket = new AjaxBasket(); $('#quick-order-form').bind('submit', function (e) { var sBasketAddString = $(this).serialize(); var term = $('#stockcode').val(); var action=checkCancelCopySubmit(term); if (action==='copy'){ copyStockcode('enter'); e.preventDefault(); $('#QuickSearchResults').hide(); } else if (action==='cancel'){ e.preventDefault(); } else if (action==='submit'){ e.preventDefault(); fastOrderAjaxBasket.add(sBasketAddString); $('#stockcode').val('').focus(); $('#qty').val('1'); } return false; }); var liSelected; var li; $(window).keydown(function (e) { if ($('#stockcode').is(":focus")) { if (e.which === 40 || e.which === 38) { e.preventDefault(); li = $('#QuickSearchResults ul#prodList').children('li'); } else { liSelected = ''; } if (e.which === 40) { if (liSelected) { liSelected.removeClass('selected'); next = liSelected.next(); if (next.length > 0) { liSelected = next.addClass('selected'); } else { liSelected = li.eq(0).addClass('selected'); } } else { liSelected = li.eq(0).addClass('selected'); } } else if (e.which === 38) { if (liSelected) { liSelected.removeClass('selected'); next = liSelected.prev(); if (next.length > 0) { liSelected = next.addClass('selected'); } else { liSelected = li.last().addClass('selected'); } } else { liSelected = li.last().addClass('selected'); } } } }); $(window).resize(function() { $("#QuickSearchResults").css("width", $("#stockcode").css("width")); }); } function checkCancelCopySubmit(term){ if(term.length >= 3 && $(".selected").length){ return 'copy'; } else if(term.length >= 3 && !$(".selected").length){ return 'submit'; } else if(term.length < 3){ return 'cancel'; } } function copyStockcode(method, cntrl){ console.log(cntrl); if(method==='mouseclick'){ var stcd = $(cntrl).attr('data-stcode'); // stockcode } else if (method==='enter'){ var stcd = $('#QuickSearchResults').children().children(".selected").children().attr('data-stcode'); // stockcode } console.log("Quick order selecting stock code: " + stcd); $("#stockcode").val(stcd); $("#QuickSearchResults").html(''); $('#stockcode').focus(); } function hide_message(){ // Remove notification message $('#tooltipcontainer').delay(4500).fadeOut(1000); } /* ====================================================================================================== ============================== Ajax Add Multiple to Quote ============================================ ====================================================================================================== */ $("#quotesubmitinput, .quotesubmitinput-prodlist").on("click", function(e){ e.preventDefault(); var sItems; var sForm; var searchFor='quoterequest'; var currentUrl=$(location).attr('href'); if(currentUrl.indexOf(searchFor)!==-1){ // If we're in the console sForm = $("form[data-attr='ajaxquoteform']").serialize(); } else if($(".quotesubmitinput-prodlist").length>0){ // If we're on the product list page sForm = $(this).parents('form:first').serialize(); } else{ // If we're on the product page sForm = $("form.addtobasket").serialize(); } $.ajax({ type: 'POST', //format: 'json', url: '/ajaxQuote.php', data: sForm, beforeSend:function(){ // this is where we append a loading image $('#tooltipcontainer').html('
').fadeIn(500); }, success:function(data){ // successful request; do something with the data $('#tooltipcontainer').empty(); var obj = jQuery.parseJSON( data ); if(obj.totalitems===1){ sItems='item'; } else if(obj.totalitems > 1){ sItems='items'; } if (obj.totalitems > 0) { new Noty({ id: 'tooltip-notifications', type: 'success', text: 'Successfully added ' + obj.totalitems + ' ' + sItems + ' - Go To Quote Form', timeout: 3000, progressBar: true }).show(); //$('#tooltipcontainer').html('
Successfully added ' + obj.totalitems + ' ' + sItems + ' - Go To Quote Form
').fadeIn(500); } else { new Noty({ id: 'tooltip-notifications', type: 'error', text: 'You have not added any items to the quote form', timeout: 3000, progressBar: true }).show(); //$('#tooltipcontainer').html('
You have not added any items to the quote form
').fadeIn(500); } if (currentUrl.indexOf(searchFor) !== -1) { setTimeout(location.reload(), 600); } hide_message(); } }); }); /* End Ajax Add Multiple to Quote */ /* ---Begin Add to favourites------------------------------------------------------------------------------------ */ var stockCode; // stock code of favourite currently being processed var selectedButton; // the element that instigated the update var onSaveCallback; // callback to call after saving /** * Adds/deletes a favourite based on current existence * * @param stockCode - stock code of the favourite * @param categoryId - category the favourite should belong to * @param callback - optional function to call when operation is successful */ function saveFavourite(stockCode, categoryId, callback ) { $.ajax({ url: "/ajaxFavourites.php", type: 'POST', data: {action: 'addFavourite', stockCode: stockCode, category: categoryId}, dataType: 'json', success: function (data) { $("#favourites-modal").modal('hide'); if (callback !== null && typeof callback === "function") { callback(data); } } }); } function removeFavourite(stockCode, callback ) { $.ajax({ url: "/ajaxFavourites.php", type: 'POST', data: {action: 'removeFavourite', stockCode: stockCode}, dataType: 'json', success: function (data) { if (callback !== null && typeof callback === "function") { callback(data); } } }); } /** * Updates form for product page when a favourite is saved, used as a callback * * @param data */ function onProductPageFavouriteSave(data) { var html; var toolTip; if (data.status === "favAdded") { html = 'Remove from Favourites '; toolTip = 'Successfully added to favourites - Go To Favourites'; } else { html = 'Add to Favourites '; toolTip = 'Successfully removed from favourites'; } selectedButton.html(html); new Noty({ id: 'tooltip-notifications', type: 'success', text: toolTip, timeout: 3000, progressBar: true }).show(); //$('#tooltipcontainer').html(toolTip).fadeIn(500); hide_message(); } /** * Updates form for a product list page when a favourite is saved, used as a callback * * @param data */ function onListPageFavouriteSave(data) { if (data.status === "favAdded") { new Noty({ id: 'tooltip-notifications', type: 'success', text: 'Successfully added to favourites - Go To Favourites', timeout: 3000, progressBar: true }).show(); //$('#tooltipcontainer').html('
Successfully added to favourites - Go To Favourites
').fadeIn(500); $(selectedButton).html(''); } else { new Noty({ id: 'tooltip-notifications', type: 'success', text: 'Successfully removed from favourites', timeout: 3000, progressBar: true }).show(); //$('#tooltipcontainer').html('
Successfully removed from favourites
').fadeIn(500); $(selectedButton).html(''); } hide_message(); } /** * Checks if a favourite exists and displays a modal if it doesn't, delete the favourite if it does * */ function checkIfFavouriteExists() { $.ajax({ url: "/ajaxFavourites.php", type: 'POST', data: {action: 'checkStatus', stockCode: stockCode}, dataType: 'json', success: function (data) { if (data.status === 'isNotFavourite') { $("#favourites-modal").modal({ show: true }); } else { removeFavourite(stockCode, onSaveCallback); } } }); } /*--- Category selection modal -----------------------------------------------------------------------------------*/ /** * Updates the category selection drop-down based on contents of category name input */ function updateCategorySelect() { var categoryName = $("#new-category-name"); var select = $("#favourite-categories"); if (categoryName.val() === "") { select.removeAttr('disabled'); } else { select.attr('disabled', ''); } } $(function () { // on modal open $('#favourites-modal').on('show.bs.modal', function () { // clear existing categories var select = $("#favourite-categories"); select.empty(); select.append(""); // fetch categories from server and update drop-down $.ajax({ url: '/ajaxFavourites.php', type: 'POST', data: {action: 'getCategories'}, dataType: 'json', success: function (data) { $.each(data.categories, function (i, v) { var option = $("