var App_ECommerce_Checkout = { _rateAction: 'getShippingRate', _rateTimeout: null, _shippingElementId: 'serviceShipping-type-element', _paymentElementId: 'servicePayment-type-element', _rateElementId: 'serviceShipping-rate-element', _form: null, _cardTipClass: 'cardCode-tip', _shippingAjaxRequest: undefined, _cartSummaryElementId: 'cartSummary', _cartSummaryContainer: null, _cartSummarySubtotalFields: ['subtotal', 'shippingRate', 'shippingHandling', 'tax', 'promo', 'giftCard'], _cartSummaryTotalElementId: 'subtotal-price-container', init: function(formId) { if (formId) { this._form = $('#' + formId); var obj = this; this._cartSummaryContainer = $('#' + this._cartSummaryElementId); if (!$(this._cartSummaryContainer).size()) { this._cartSummaryContainer = null; } var shippingElements = $('#' + this._shippingElementId); if ($(shippingElements).size()) { this._updateShippingRate(); shippingElements.find('input').change(function() { obj._onShippingClick(this); }); var shippingChecked = shippingElements.find('input:checked'); obj._onShippingClick(shippingChecked); this._initFedexAddressType(); this._updateFedexAddressType(); $('.shipping, #' + this._shippingElementId).find('input').bind('click', function() { obj._updateShippingRate(); }); $('[name*="[state]"], [name*="[zip]"]').change(function(){ obj._updateShippingRate(); }); } var paymentElements = $('#' + this._paymentElementId); if ($(paymentElements).size()) { var cvvTip = $('.' + obj._cardTipClass); if (!qs.constant('IS_TOUCHSCREEN')) { $(cvvTip).tooltip({html: true, trigger: 'hover'}); } else { $(cvvTip).tooltip({html: true, trigger: 'click'}); var hitEvent = ('ontouchstart' in document.documentElement ? 'touchstart' : 'click') + '.cvvTip'; $(document).bind(hitEvent, function (e) { if (!$(e.target).closest('.' + obj._cardTipClass).size()) { var tooltipElement = $('.' + obj._cardTipClass); if ($(tooltipElement).size() && $(tooltipElement).next('.tooltip').size()) { $(cvvTip).tooltip('hide'); } } }); } paymentElements.find('input').change(function() { obj._onPaymentClick(this); }); var paymentChecked = paymentElements.find('input:checked'); obj._onPaymentClick(paymentChecked); } App_User_Form.prototype.onAsBillingClick = obj.onAsBillingClick; $('[name^="billing["]', this._form).each(function () { $(this).blur(function () { obj._populateShippingInfo(obj._form); }); }); } }, _parsePrice: function(str) { str = '' + str; if ('$' == str.charAt(0)) { str = str.substr(1); } str = str.replace(/[, ]+/, ''); str = parseFloat(str); if (isNaN(str)) { str = 0.0; } return str }, _ucfirst: function(str) { var f = str.charAt(0).toUpperCase(); return f + str.substr(1, str.length - 1); }, _cartSummarySetPrice: function(field, value) { value = this._parsePrice(value); field = this._ucfirst(field); var container = $('.cart' + field, this._cartSummaryContainer); $(container).html('$' + _.num.format(value, 2)); if (value > 0) { $(container).closest('div').removeClass('hidden'); } else { $(container).closest('div').addClass('hidden'); } }, _cartSummaryUpdate: function(summary) { if (!this._cartSummaryContainer) { return; } for (var i in this._cartSummarySubtotalFields) { var field = this._cartSummarySubtotalFields[i]; this._cartSummarySetPrice(field, summary[field]); } var total = this._parsePrice(summary.total); $('#' + this._cartSummaryTotalElementId).html('$' + _.num.format(total, 2)); }, _initFedexAddressType: function() { var obj = this; $('#serviceShipping-fedExAddressType-residential').bind('change', function() { obj._updateFedexAddressType() }); $('#serviceShipping-fedExAddressType-commercial').bind('change', function() { obj._updateFedexAddressType() }); }, _updateFedexAddressType: function() { var residential = $('#serviceShipping-fedExAddressType-residential'); var commercial = $('#serviceShipping-fedExAddressType-commercial'); if (residential.size() && commercial.size()) { var ground = $('label[for="serviceShipping-fedExMethod-FEDEXGROUND"]'); var home = $('label[for="serviceShipping-fedExMethod-GROUNDHOMEDELIVERY"]'); if (residential.is(':checked')) { ground.hide(); ground.next('br').hide(); home.next('br').show(); home.show(); if ($('input', ground).is(':checked')) { $('input', home).prop('checked', 'checked'); } } else if (commercial.is(':checked')) { home.next('br').hide(); home.hide(); ground.next('br').show(); ground.show(); if ($('input', home).is(':checked')) { $('input', ground).prop('checked', 'checked'); } } } }, _populateShippingInfo: function(_form) { var asBilling = $('#shipping-asBilling', _form); if ($(asBilling).is(':checked')) { $('[name^="billing["]', this._form).each(function () { var shippingFieldName = $(this).attr('name').replace('billing[', 'shipping['); var shippingField = $('[name="' + shippingFieldName + '"]'); if ($(shippingField).size()) { $(shippingField).val($(this).val()); } }); } }, _onShippingClick: function(element) { $('.shipping').closest('dd' ).hide(); if ($(element).size()) { $('#serviceShipping-' + $(element).val() + 'Methods-element').show(); } }, _onPaymentClick: function(element) { $('.payment').closest('dd').hide(); $('#servicePayment-' + $(element).val() + 'Methods-element').show(); }, _updateShippingRate: function() { var obj = this; if (obj._rateTimeout) { clearTimeout(obj._rateTimeout); } obj._rateTimeout = setTimeout(function() { obj._getShippingRate(); }, 300); }, _getShippingRate: function() { var request = {}; $.each(this._form.serializeArray(), function(i, field) { request[field.name] = field.value; }); request.action = this._rateAction; var obj = this; if (typeof this._shippingAjaxRequest != 'undefined') { this._shippingAjaxRequest.abort(); this._shippingAjaxRequest = undefined; } this._shippingAjaxRequest = qs.ajax( qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), request, { qsDefaultCallbacks: false } ); this._shippingAjaxRequest.done(function (response) { qs.hideLoading(); if (!response || response.isError) { var errorCallback = (response && response.errorCallback) ? response.errorCallback : {fn: qs.errorCallback, args: [response]}; qs.call(errorCallback); return this; } if (response && response.callbacks) { qs.call(response.callbacks); } var html = '$' + parseFloat(response.total).toFixed(2); if (response.message != '' && response.message) { html += '