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, init: function(formId) { if (formId) { this._form = $('#' + formId); var obj = this; 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()) { $('.' + obj._cardTipClass).tooltip({html: true}); 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); }); }); } // $("#checkout-total").affix({ // offset: { // top: 100, // bottom: ($("footer.block").outerHeight(true) + $("#checkout-footer").outerHeight(true) + 24) // } // }); }, _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(); $('#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 += '
' + response.message + '
'; } $('#' + obj._rateElementId).html(html); return this; }); this._shippingAjaxRequest.fail(function (xhr, textStatus, errorThrown) { qs.hideLoading(); if ('abort' == textStatus) { return this; } qs.debug.debug('qs.ajax: Ajax request failed: ', textStatus, xhr); var message = (qs.constant('DEBUG')) ? xhr.responseText : textStatus + ' ' + errorThrown; return this; }); }, onAsBillingClick: function (asBilling, type) { for (var i = 0; i < this.options.addressFields.length; i++) { var name = this.options.addressFields[i]; var selector = '#' + type + '-' + name; if ($(asBilling).is(':checked')) { $(selector).attr('disabled', 'disabled'); this.setElementValue(type, name, this.getElementValue(this.options.inheritedAddress, name)); } else { if ($(selector).attr('disabled') == 'disabled') { $(selector).removeAttr('disabled'); this.setElementValue(type, name, ''); } } } return true; } }; var App_ECommerce_Checkout_OtherServices = { _containerId: null, _tooltipTimeout: 3000, init: function(id) { this._containerId = id; $('#' + this._containerId).find('input[type=checkbox]').click(function() { var valueFieldId = $(this).attr('id') + 'Value-element' ; var valueField = $('#' + valueFieldId); if ($(this).is(':checked')) { valueField.show(); } else { valueField.hide(); } }); }, _removeErrors: function() { $('#' + this._containerId).find('ul.errors').remove(); }, _addErrors: function(message, afterContainer) { afterContainer.after(''); }, applyAction: function(requestAction, element) { this._removeErrors(); var obj = this; var request = { code: $(element).parent().children('input').val() }; request.action = requestAction; qs.ajax(qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), request).done(function(data){ if (!data.success) { obj._addErrors(data.message, $(element)); } else { var inputId = $(element).parent().children('input').attr('id'); var checkboxId = inputId.replace('Value', ''); $('#' + checkboxId + '-label').remove(); $('#' + checkboxId + '-element').remove(); $('#' + inputId + '-label').show(); $('#' + inputId + '-element').html(data.elementValue).show(); } }).fail(function(status) {alert(status.responseText);}); }, applyPromoCode: function(element) { this.applyAction('applyPromoCode', element); }, applyGiftCard: function(element) { this.applyAction('applyGiftCard', element); } };