var App_Cart_Cart = { _options: [], _elements: [], _priceInfo: null, _loaderSrc: 'images/loading-arrow-16x16.gif', _loaderAlt: 'loading', _loaderClass: 'loader', _qtyElementPrefix: 'quantity-', _updateActionAjax: 'updateItem', _subTotalContainerId: 'subtotal-price-container', _rowCellPriceClass: 'cell_price', _rowCellTotalClass: 'cell_row_total', _headerItemCartCountId: 'header-cart-item-count', _headerItemCartCountTextId: 'header-cart-item-count-text', init: function(options) { $.extend(this._options, options); this._initQuantityElements(); }, _initQuantityElements: function() { var obj = this; $('input[name^=' + this._qtyElementPrefix + ']').autoNumeric({aSep: '', aDec: ''}); if (this._options.qtyList) { for (var elementId in this._options.qtyList) { this._elements[this._qtyElementPrefix + elementId] = this._options.qtyList[elementId]; } } $('input[name^=' + this._qtyElementPrefix + ']').keyup(function(eventObject) { obj._onQuantityKeyup(eventObject); }).blur(function(eventObject) { obj._onQuantityBlur(eventObject); }); }, _onQuantityKeyup: function(eventObject) { var targetName = eventObject.target.name; var itemId = targetName.replace(this._qtyElementPrefix, ''); var existsCacheQty = this._elements.hasOwnProperty(targetName); var newQty = parseInt(eventObject.target.value); if (!isNaN(newQty) && ((!existsCacheQty && newQty) || (existsCacheQty && newQty != this._elements[targetName]))) { if (newQty == 0) { this.restoreQuantity(eventObject) } else { this._elements[targetName] = newQty; this._recalculate(targetName, itemId); } } }, _onQuantityBlur: function(eventObject) { this.restoreQuantity(eventObject); }, restoreQuantity: function(eventObject) { var targetName = eventObject.target.name; var qty = parseInt(this._elements[targetName]); if (!isNaN(qty) && qty > 0) { eventObject.target.value = qty; } }, _recalculate: function(targetName, itemId) { if (!this._elements[targetName]) { return false; } var target = $('input[name=' + targetName + ']'); target.after($('', {'src': this._loaderSrc, 'alt': this._loaderAlt, 'class': this._loaderClass})); var obj = this; $.ajax({ url: qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), data: {action: obj._updateActionAjax, 'qty': obj._elements[targetName], 'itemId': itemId}, dataType: 'json', async: false, type: 'POST' }).fail(function (status) { alert(status.responseText); }).done( function (data) { if (data.success) { target.parents('td').siblings('.' + obj._rowCellPriceClass).html(data.rowPrice); target.parents('td').siblings('.' + obj._rowCellTotalClass).html(data.rowTotal); $('#' + obj._subTotalContainerId).html(data.subTotal); $('#' + obj._headerItemCartCountId).html(data.itemsCount); $('#' + obj._headerItemCartCountTextId).html('Item' + (parseInt(data.itemsCount) == 1 ? 's' : '')) if (typeof App_Cart_Small != 'undefined') { App_Cart_Small.reset(); App_Cart_Small.updateQty(data.quantity); } } } ); target.siblings('.' + this._loaderClass).remove(); return true; } };