if(!sn){ var sn={}; } if(typeof(sn) != 'object'){ alert("Variable sn must be a javascript object"); } sn.cart = { indicateChange: function() { var speed = "fast", cartContainerId = '#cartContainer'; var settingsOn = {backgroundColor: '#F9EA68'}; var settingsOff = {backgroundColor: '#FFFFFF'}; window.scrollTo(0,0); $(cartContainerId).animate(settingsOn, {duration:speed, complete:function(){ $(cartContainerId).animate(settingsOff, {duration:speed, complete:function(){ $(cartContainerId).animate(settingsOn, {duration:speed, complete:function(){ $(cartContainerId).animate(settingsOff, {duration:speed}); }}); }}); }}); }, doSyncJobb: function (id, articleId) { var scope = this; var formData = null; $.ajax({ type: "get", url: scope.webshopUrl, cache:false, data:{a:'sync',itemid:id, articleid:articleId}, dataType:'html' }); }, gotoCheckOutUrl: function() { window.location="/webshop_kassa/"; }, itemIdQuantityDelimiter: "#", webshopUrl: "/webshop", allowedNumberFormatRegExp: new RegExp("^-?\\d+$"), validateNumberFormat: function (str){ return this.allowedNumberFormatRegExp.test(str); }, removeCart: function () { var scope = this; var formData = null; $.ajax({ type: "get", url: scope.webshopUrl, cache:false, data:"a=r", dataType:'html', success:function(msg) { var objCartTableBody = $('#cartContainer #cartItems tbody:first'); objCartTableBody.html(""); $('#webShopContainer').hide(); }, error:function (XMLHttpRequest, textStatus, errorThrown) { if(window.console){ console.log('No data received: '+textStatus); } } }); }, updateCart: function () { this.doAjaxRequestAndlistItemsInCart({a:'g'}); }, addToCartWithUserInteraction: function(id, numberOfItems, articleId, htmlElementButton){ if(this.validateNumberFormat(numberOfItems)){ this.addToCart(id, numberOfItems, articleId, htmlElementButton, true); }else{ this.addToCart(id, 1, articleId, htmlElementButton, true); } }, addToCart: function(id, numberOfItems, articleId, htmlElementButton){ if($(htmlElementButton).data('inactive') != 'true') { $(htmlElementButton).data('inactive','true').addClass('inactive'); if( numberOfItems !== undefined ){ strId = id+this.itemIdQuantityDelimiter+numberOfItems; this.doAjaxRequestAndlistItemsInCart({a:'ai',itemid:strId,articleid:articleId}, htmlElementButton, true); } else { this.doAjaxRequestAndlistItemsInCart({a:'ai',itemid:id,articleid:articleId}, htmlElementButton, true); } this.doSyncJobb(id, articleId); } }, removeItem: function(id, numberOfRows){ if(numberOfRows > 1){ this.doAjaxRequestAndlistItemsInCart({a:'ri',itemid:id}); } else { this.removeCart(); } }, doAjaxRequestAndlistItemsInCart: function(objData, htmlElementButton, addToCartEvent){ var scope = this; var formData = null; $.ajax({ type: "get", url: scope.webshopUrl, cache:false, data: objData, dataType:'json', success:function(json) { var totalCost = 0; var shippingCost = 0; var totalCostIncludingShiping = 0; var strCurrency=""; var objCartTableBody = $('#cartContainer #cartItems tbody:first'); var objCartTableFoot = $('#cartContainer #cartItems tfoot:first'); objCartTableBody.html(""); objCartTableFoot.find(".totalCost").text(totalCost); objCartTableFoot.find(".shippngCost").text(shippingCost); objCartTableFoot.find(".totalCostIncludingShipping").text(totalCostIncludingShiping); if(json.numOfItems > 0) { strCurrency = ' '+ json.currency; $('#itemsincart').html(json.numOfItems); $.each(json.items, function(i, objItem) { var strId = objItem['itemId']; var strRowClass = (i%2) ? 'even' : 'odd'; $(''+ '' + ''+ objItem['title'] +'' + '' + '' + ''+ objItem['quantity'] +'st á '+ objItem['price']+ strCurrency +'' + ''+objItem['total']+ strCurrency+'' + '').appendTo(objCartTableBody); }); totalCost = parseInt(json.totalCost); totalCostIncludingShiping = totalCost + shippingCost; objCartTableFoot.find(".totalCost").text(totalCost + "" + strCurrency); objCartTableFoot.find(".shippngCost").text(shippingCost + "" + strCurrency); objCartTableFoot.find(".totalCostIncludingShipping").text(totalCostIncludingShiping + "" + strCurrency); $('#webShopContainer').show(); if(addToCartEvent){ scope.indicateChange(); } } else { $('#webShopContainer').hide(); } if(htmlElementButton !== undefined && $(htmlElementButton).data('inactive') == 'true') { $(htmlElementButton).data('inactive','false').removeClass('inactive'); } }, error:function (XMLHttpRequest, textStatus, errorThrown) { if(window.console){ console.log('No data received: '+textStatus); } } }); } } $(document).ready(function() { sn.cart.updateCart(); });