EbayTemplateGeneralShippingHandler = Class.create(); EbayTemplateGeneralShippingHandler.prototype = Object.extend(new CommonHandler(), { //---------------------------------- initialize: function(accountShippingDiscountProfiles) { this.counter = {local: 0, international: 0, total: 0}; this.getItFastNotSupportSites = [2, 13, 15, 16, 18, 19, 20]; this.cashOnDeliverySites = [10]; this.taxSites = [1, 2, 9, 19]; this.vatSites = [5, 6, 11, 14, 8, 13, 7, 17, 16, 10, 12, 3]; this.shippingRateTableSites = { 'local' : [1, 3, 8], 'international' : [3, 8] }; this.englishUnitSites = [1, 9]; this.marketplaceId = 0; var tempAccountShippingDiscountProfiles = {}; accountShippingDiscountProfiles.each(function (account) { tempAccountShippingDiscountProfiles[account.id] = account.profiles; }); this.accountShippingDiscountProfiles = tempAccountShippingDiscountProfiles; this.defaultOptions = { 'measurement_system': $('measurement_system').innerHTML, 'local_shipping_mode': $('local_shipping_mode').innerHTML, 'international_shipping_mode': $('international_shipping_mode').innerHTML, 'weight_mode': $('weight_mode').innerHTML }; Validation.add('M2ePro-location-or-postal-required', M2ePro.text.no_location_or_postal_error, function(value) { return $('address').value != '' || $('postal_code').value != ''; }); Validation.add('M2ePro-validate-international-ship-to-location', M2ePro.text.no_international_ship_to_location_error, function(value, el) { var isChecked = false; $$('input[name="'+el.name+'"]').each(function(o) { if (o.checked) { isChecked = true; } }); return isChecked; }); }, //---------------------------------- prepareShippingObservers: function(isFlat) { isFlat = isFlat || false; $('measurement_system') .observe('change', EbayTemplateGeneralShippingHandlerObj.measurement_system_change) .simulate('change'); !isFlat && $('package_size_mode') .observe('change', EbayTemplateGeneralShippingHandlerObj.package_size_mode_change) .simulate('change'); !isFlat && $('package_size_ebay') .observe('change', EbayTemplateGeneralShippingHandlerObj.package_size_ebay_change) .simulate('change'); !isFlat && $('dimension_mode') .observe('change', EbayTemplateGeneralShippingHandlerObj.dimension_mode_change) .simulate('change'); $('weight_mode') .observe('change', EbayTemplateGeneralShippingHandlerObj.weight_mode_change) .simulate('change'); }, setSettingsForMarketplace: function(marketplaceId) { var self = EbayTemplateGeneralShippingHandlerObj; self.marketplaceId = parseInt(marketplaceId); var showCalculated = (marketplaceId == 1 || marketplaceId == 2 || marketplaceId == 4 || marketplaceId == 9 || marketplaceId == 19); var showFreight = (marketplaceId == 1 || marketplaceId == 3 || marketplaceId == 4); self.setLocalShippingTypes(showCalculated, showFreight); self.setInternationalShippingTypes(showCalculated); var showGetItFast = ($('local_shipping_mode').value == 0 || $('local_shipping_mode').value == 1); self.setGetItFastVisibility(showGetItFast); self.setVATVisibility(); self.setSalesTaxVisibility(); if ($('taxation_tax_table_tr').visible() || $('taxation_vat_tr').visible()) { $('magento_block_ebay_template_general_shipping_tax').show(); } else { $('magento_block_ebay_template_general_shipping_tax').hide(); } if (self.englishUnitSites.indexOf(self.marketplaceId) != -1) { $('measurement_system').remove(1); } else { $('measurement_system').update(self.defaultOptions['measurement_system']); } M2ePro.customData.marketplaceWasChanged = true; }, setSalesTaxVisibility: function() { var self = EbayTemplateGeneralShippingHandlerObj; if (self.taxSites.indexOf(self.marketplaceId) == -1) { $('taxation_tax_table_tr').hide(); } else { $('taxation_tax_table_tr').show(); } }, setVATVisibility: function() { var self = EbayTemplateGeneralShippingHandlerObj; if (self.vatSites.indexOf(self.marketplaceId) == -1) { $('taxation_vat_tr').hide(); } else { $('taxation_vat_tr').show(); } }, setShippingRateTableVisibility: function(locationType) { var self = EbayTemplateGeneralShippingHandlerObj, supportedMarketplaces = self.shippingRateTableSites[locationType] || [], shippingMode = $(locationType + '_shipping_mode').value; if (supportedMarketplaces.indexOf(self.marketplaceId) == -1 || shippingMode != 0) { $('use_ebay_'+locationType+'_shipping_rate_table_tr').hide(); if (self.marketplaceId != 0) { $('use_ebay_'+locationType+'_shipping_rate_table').value = 0; } } else { $('use_ebay_'+locationType+'_shipping_rate_table_tr').show(); } }, use_ebay_local_shipping_rate_table_change: function() { var self = EbayTemplateGeneralShippingHandlerObj; self.checkFlatOptionsVisibility(); }, //---------------------------------- setGetItFastVisibility: function(showGetItFast) { var self = EbayTemplateGeneralShippingHandlerObj; if (self.getItFastNotSupportSites.indexOf(self.marketplaceId) == -1 && showGetItFast) { $('local_shipping_git_tr').show(); } else { $('local_shipping_git_tr').hide(); } }, setInternationalShippingTypes: function(showCalculated) { var self = EbayTemplateGeneralShippingHandlerObj; if (!showCalculated) { $('international_shipping_mode').remove(2); } else { $('international_shipping_mode').update(self.defaultOptions['international_shipping_mode']); } if (M2ePro.customData.marketplaceWasChanged) { $('international_shipping_mode').value = 4; } $('international_shipping_mode').simulate('change'); }, setLocalShippingTypes: function(showCalculated, showFreight) { var self = EbayTemplateGeneralShippingHandlerObj; $('local_shipping_mode').update(self.defaultOptions['local_shipping_mode']); if (!showCalculated) { $('local_shipping_mode_calculated').remove(); } if (!showFreight) { $('local_shipping_mode_freight').remove(); } if (M2ePro.customData.marketplaceWasChanged) { $('local_shipping_mode').value = 0; } $('local_shipping_mode').simulate('change'); }, setCashOnDeliveryVisibility: function() { var self = EbayTemplateGeneralShippingHandlerObj; var showCashOnDelivery = self.cashOnDeliverySites.indexOf(self.marketplaceId) != -1 && ($('local_shipping_mode').value == 0 || $('local_shipping_mode').value == 1); if (self.cashOnDeliverySites.indexOf(self.marketplaceId) != -1 && showCashOnDelivery) { $('local_shipping_cash_on_delivery_cost_mode_tr').show(); $('local_shipping_cash_on_delivery_cost_mode').simulate('change'); } else { $('local_shipping_cash_on_delivery_cost_mode_tr').hide(); $('local_shipping_cash_on_delivery_cost_ca_tr').hide(); $('local_shipping_cash_on_delivery_cost_cv_tr').hide(); } }, //---------------------------------- postal_code_change: function() { if ($('postal_code').value != '' && $('originating_postal_code').value == '' && ($('local_shipping_mode').value == 1 || $('international_shipping_mode').value == 1)) { $('originating_postal_code').value = $('postal_code').value; } }, local_shipping_mode_change: function() { var self = EbayTemplateGeneralShippingHandlerObj; $('magento_block_ebay_template_general_shipping_international').hide(); $('block_notice_ebay_template_general_shipping_local').hide(); $('block_notice_ebay_template_general_shipping_freight').hide(); $('local_shipping_methods_tr').hide(); // clear selected shipping methods $$('#shipping_local_tbody .icon-btn').each(function(el) { self.removeRow( el,'local'); }); $$('.local-shipping-tr').invoke((this.value == 0 || this.value == 1) ? 'show' : 'hide'); var showGetItFast = (this.value == 0 || this.value == 1); self.setGetItFastVisibility(showGetItFast); self.checkFlatOptionsVisibility(); self.checkCalculatedOptionsVisibility(); self.setCashOnDeliveryVisibility(); self.renderShippingDiscountProfiles('local'); self.setShippingRateTableVisibility('local'); if (this.value == 0 || this.value == 1) { $('magento_block_ebay_template_general_shipping_international').show(); $('local_shipping_methods_tr').show(); if (this.value == 1) { $('local_handling_cost_tr').show(); $('local_handling_cost_mode').simulate('change'); $('postal_code').simulate('change'); } else { $('local_handling_cost_tr').hide(); $('local_handling_cost_mode').value = 0; $('local_handling_cost_mode').simulate('change'); } } else if (this.value == 2) { $('block_notice_ebay_template_general_shipping_freight').show(); $('international_shipping_mode').value = 4; $('international_shipping_mode').simulate('change'); } else if (this.value == 3) { $('block_notice_ebay_template_general_shipping_local').show(); $('international_shipping_mode').value = 4; $('international_shipping_mode').simulate('change'); } }, international_shipping_mode_change: function() { var self = EbayTemplateGeneralShippingHandlerObj; // clear selected shipping methods $$('#shipping_international_tbody .icon-btn').each(function(el) { self.removeRow( el,'international'); }); $$('.international-shipping-tr').invoke((this.value == 0 || this.value == 1) ? 'show' : 'hide'); self.checkFlatOptionsVisibility(); self.checkCalculatedOptionsVisibility(); self.renderShippingDiscountProfiles('international'); self.setShippingRateTableVisibility('international'); if (this.value == 1) { $('international_handling_cost_tr').show(); $('international_handling_cost_mode').simulate('change'); $('postal_code').simulate('change'); } else { $('international_handling_cost_tr').hide(); $('international_handling_cost_mode').value = 0; $('international_handling_cost_mode').simulate('change'); } }, updateShippingDiscountProfiles: function() { var self = EbayTemplateGeneralShippingHandlerObj; var accountId = $('account_id').value; var marketplaceId = $('marketplace_id').value; if (accountId == '' || marketplaceId == '') { return; } new Ajax.Request(M2ePro.url.updateShippingDiscountProfilesByAccount, { method: 'get', parameters: { account_id: accountId, marketplace_id: marketplaceId }, onSuccess: function(transport) { var account = transport.responseText.evalJSON(true); self.accountShippingDiscountProfiles[account.id] = account.profiles; self.renderShippingDiscountProfiles('local'); self.renderShippingDiscountProfiles('international'); } }); }, renderShippingDiscountProfiles: function(locationType) { var self = EbayTemplateGeneralShippingHandlerObj, html = self.getShippingDiscountProfilesHtml(locationType), value = M2ePro.formData[locationType + '_shipping_combined_discount_profile_id']; html = '' + html; $$('#'+locationType+'_shipping_combined_discount_profile_id')[0].update(html); $$('#'+locationType+'_shipping_combined_discount_profile_id')[0].value = value; }, getShippingDiscountProfilesHtml: function(locationType) { var self = EbayTemplateGeneralShippingHandlerObj, shippingModeSelect = $$('#' + locationType + '_shipping_mode')[0], accountId = $('account_id').value, marketplaceId = $('marketplace_id').value, desiredProfileType = '', html = ''; if (accountId == '' || marketplaceId == '') { return ''; } if (shippingModeSelect.value != 0 && shippingModeSelect.value != 1) { return ''; } if (typeof self.accountShippingDiscountProfiles[accountId] == 'undefined') { return ''; } desiredProfileType = (shippingModeSelect.value == 0) ? 'flat_shipping' : 'calculated_shipping'; self.accountShippingDiscountProfiles[accountId].each(function (profile) { if (profile.type != desiredProfileType || profile.marketplace_id != marketplaceId) { return; } html += ''; }); return html; }, international_trade_change: function(select) { var self = EbayTemplateGeneralShippingHandlerObj; if (select.value == 0) { $('international_shipping_none').show(); } else { $('international_shipping_none').hide(); if ($('international_shipping_mode').value == self.SHIPPING_TYPE_NO_INTERNATIONAL) { $('international_shipping_mode').selectedIndex = 1; $('international_shipping_mode').simulate('change'); } } }, //---------------------------------- local_shipping_cash_on_delivery_cost_mode_change: function() { var self = EbayTemplateGeneralShippingHandlerObj; $('local_shipping_cash_on_delivery_cost_cv_tr', 'local_shipping_cash_on_delivery_cost_ca_tr').invoke('hide'); if (this.value == self.CASH_ON_DELIVERY_COST_MODE_CUSTOM_VALUE) { $('local_shipping_cash_on_delivery_cost_cv_tr').show(); } else if (this.value == self.CASH_ON_DELIVERY_COST_MODE_CUSTOM_ATTRIBUTE) { $('local_shipping_cash_on_delivery_cost_ca_tr').show(); } }, addRow: function(type) // local|international { var self = EbayTemplateGeneralShippingHandlerObj; var id = 'shipping_' + type + '_tbody'; var i = self.counter.total; var tpl = $$('#block_listing_template_shipping_table_row_template_table tbody')[0].innerHTML; tpl = tpl.replace(/%i%/g, i); tpl = tpl.replace(/%type%/g, type); $(id).insert(tpl); var row = $('shipping_variant_' + type + '_' + i + '_tr'); AttributeSetHandlerObj.renderAttributesWithEmptyOption('shipping_cost_attribute[' + i + ']', row.select('.shipping-cost-ca')[0]); AttributeSetHandlerObj.renderAttributesWithEmptyOption('shipping_cost_additional_attribute[' + i + ']', row.select('.shipping-cost-additional-ca')[0]); this.renderServices(row.select('.shipping-service')[0], type); self.shipping_mode_one_row_change(row, $(type + '_shipping_mode').value == 1); if (type == 'international') { tpl = $$('#block_listing_template_shipping_table_locations_row_template_table tbody')[0].innerHTML; tpl = tpl.replace(/%i%/g, i); $(id).insert(tpl); self.renderShipToLocationCheckboxes(i); } self.counter[type]++; self.counter.total++; if (type == 'local' && self.counter[type] >= 4) { $(id).up('table').select('tfoot')[0].hide(); } if (type == 'international' && self.counter[type] >= 5) { $(id).up('table').select('tfoot')[0].hide(); } return row; }, removeRow: function(btn, type) { var self = EbayTemplateGeneralShippingHandlerObj; var table = $(btn).up('table'); if (type == 'international') { $(btn).up('tr').next().remove(); } $(btn).up('tr').remove(); self.counter[type]--; if (type == 'local' && self.counter[type] < 4) { table.select('tfoot')[0].show(); } if (type == 'international' && self.counter[type] < 5) { table.select('tfoot')[0].show(); } self.checkCalculatedOptionsVisibility(); }, //---------------------------------- renderServices: function(el, type) { var txt = ''; var isCalculatedType = $F(type + '_shipping_mode') == 1; var SelectedPackage = $('package_size_ebay').value; var categoryMethods = ''; // not selected international shipping service if (type == 'international') { txt += ''; } $H(this.data.shipping).each(function(category) { categoryMethods = ''; category.value.methods.each(function(service) { var isServiceOfSelectedDestination = (type == 'local' && service.is_international == 0) || (type == 'international' && service.is_international == 1); var isServiceOfSelectedType = ( isCalculatedType && service.is_calculated == 1) || (! isCalculatedType && service.is_flat == 1); if (isServiceOfSelectedDestination && isServiceOfSelectedType) { if (isCalculatedType) { if (service.data.ShippingPackage.indexOf(SelectedPackage) != -1) { categoryMethods += ''; } } else { categoryMethods += ''; } } }); if (categoryMethods != '') { noCategoryTitle = category[0] == ''; if (noCategoryTitle) { txt += categoryMethods; } else { txt += ''; } } }); $(el).update(txt); }, renderShipToLocationCheckboxes: function(i) { var self = EbayTemplateGeneralShippingHandlerObj; var txt = ''; self.data.shipping_locations.each(function(location) { if (location.ebay_id == 'Worldwide') { txt += '
'; } else { txt += '' } }); $$('#shipping_variant_locations_' + i + '_tr td')[0].innerHTML = '