function LTrim( value ) { var re = /\s*((\S+\s*)*)/; return value.replace(re, "$1"); } function RTrim( value ) { var re = /((\s*\S+)*)\s*/; return value.replace(re, "$1"); } function trim( value ) { return LTrim(RTrim(value)); } $(document).ready( function() { $('#cartProducts .productCart').each(function(){ var productId = $(this).attr('product_id'); $(this).find('a').bind( 'click', function(){ var json = $.ajax({ type: "POST", url: "http://www.studiokeepsakes.com/index.html", data: "http_module=cart&http_action=removeProduct&product_id=" + productId, async: false }).responseText; json = trim(json); json = eval('(' + json + ')'); if (json['status'] == 'success') { $(this.parentNode).DropOutDown( 400, function() { $(this).remove(); calculateCartTotal(); } ); } return false; } ); $('#cartProducts .productCart').each( function () { $(this).find('input.quantityInput') .bind( 'change', function(){ var quantity = $(this).val(); var json = $.ajax({ type: "POST", url: "http://www.studiokeepsakes.com/index.html", data: "http_module=cart&http_action=updateQuantity&quantity=" + quantity + "&product_id=" + productId, async: false }).responseText; json = trim(json); json = eval('(' + json + ')'); if (json['status'] == 'success') { $(this).val(json['quantity']); calculateCartTotal(); } else { alert("There was a problem updating the quantity"); $(this).val(json['quantity']); calculateCartTotal(); } return false; } ) } ); }); calculateCartTotal(); $('#products a.addToCartLink') .bind( 'click', function() { $(this.parentNode) .TransferTo( { to:addProductToCart(this.parentNode), className:'transferProduct', duration: 400 } ); return false; } ); $('div.product').Draggable({revert: true, fx: 300, ghosting: true, opacity: 0.4}); $('#cart').Droppable( { accept : 'product', activeclass: 'activeCart', hoverclass: 'hoverCart', tolerance: 'intersect', onActivate: function(dragged) { if (!this.shakedFirstTime) { $(this).Shake(3); this.shakedFirstTime = true; } }, onDrop: addProductToCart } ); } ); var addProductToCart = function(dragged) { var cartItem; var productName = $('h2', dragged).html(); var productPrice = parseFloat($('span.productPrice', dragged).html()); productPrice = formatNr(productPrice); //alert("price: " + productPrice); var productHtmlId = $(dragged).attr('id'); var productId = $(dragged).attr('product_id'); var isInCart = $('#' + productHtmlId + '_cart'); //alert("isInCart: " + isInCart.size() + " " + productHtmlId); if (isInCart.size() == 1) { var quantity = $(this).val(); var json = $.ajax({ type: "POST", url: "http://www.studiokeepsakes.com/index.html", data: "http_module=cart&http_action=addOneToQuantity&quantity=" + quantity + "&product_id=" + productId, async: false }).responseText; json = trim(json); json = eval('(' + json + ')'); if (json['status'] == 'success') { //quantity = parseInt(isInCart.find('input.quantityInput').val()) + 1; quantity = json['quantity']; isInCart.find('input.quantityInput').val(quantity+'').end().Pulsate(300, 2); $(this).val(quantity); calculateCartTotal(); } else { alert("There was a problem updating the quantity"); $(this).val(json['quantity']); calculateCartTotal(); } } else { //cartItem = $('div.productCart:last').get("http://www.studiokeepsakes.com"); var json = $.ajax({ type: "POST", url: "http://www.studiokeepsakes.com/index.html", data: "http_module=cart&http_action=addToCart&product_id=" + productId, async: false }).responseText; json = trim(json); json = eval('(' + json + ')'); //alert(json); if (json['status'] == 'success') { $('#cartProducts') .append('
') .find('div.productCart:last') .fadeIn(400) .find('a') .bind( 'click', function(){ var json = $.ajax({ type: "POST", url: "http://www.studiokeepsakes.com/index.html", data: "http_module=cart&http_action=removeProduct&product_id=" + productId, async: false }).responseText; json = trim(json); json = eval('(' + json + ')'); if (json['status'] == 'success') { $(this.parentNode).DropOutDown( 400, function() { $(this).remove(); calculateCartTotal(); } ); } return false; } ); $('#cartProducts').find('div.productCart:last').find('input.quantityInput').bind('change', function(){ var quantity = $(this).val(); var json = $.ajax({ type: "POST", url: "http://www.studiokeepsakes.com/index.html", data: "http_module=cart&http_action=updateQuantity&quantity=" + quantity + "&product_id=" + productId, async: false }).responseText; json = trim(json); json = eval('(' + json + ')'); if (json['status'] == 'success') { $(this).val(json['quantity']); calculateCartTotal(); } else { alert("There was a problem updating the quantity"); $(this).val(json['quantity']); calculateCartTotal(); } return false; } ); } } calculateCartTotal(); return cartItem; }; var calculateCartTotal = function() { var total = 0; $('#cartProducts .productCart').each( function() { var price = parseFloat($('span.productPrice', this).html()); var quantity = parseInt($('input.quantityInput', this).val()); total += price * quantity; } ); $('#cartTotal').html(formatNr(total)); $('#cart p').Highlight(500, '#ec6495', function(){$(this).css('backgroundColor', 'transparent');}); }; var formatNr = function(nr) { thousands = parseInt(nr/1000); hundreds = parseInt(nr - thousands*1000); decimals = parseInt((nr - parseInt(nr)) * 100); return (thousands > 0 ? thousands + ' ' : '') + (nr > 1000 & hundreds < 100 ? '0' : '') + (nr > 1000 & hundreds < 10 ? '0' : '') + hundreds + '.' + (decimals > 0 ? decimals : '00'); }