Guest User

Magento default address overwrite fix

a guest
Dec 30th, 2014
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Magento
  3.  *
  4.  * NOTICE OF LICENSE
  5.  *
  6.  * This source file is subject to the Academic Free License (AFL 3.0)
  7.  * that is bundled with this package in the file LICENSE_AFL.txt.
  8.  * It is also available through the world-wide-web at this URL:
  9.  * http://opensource.org/licenses/afl-3.0.php
  10.  * If you did not receive a copy of the license and are unable to
  11.  * obtain it through the world-wide-web, please send an email
  12.  * to [email protected] so we can send you a copy immediately.
  13.  *
  14.  * DISCLAIMER
  15.  *
  16.  * Do not edit or add to this file if you wish to upgrade Magento to newer
  17.  * versions in the future. If you wish to customize Magento for your
  18.  * needs please refer to http://www.magentocommerce.com for more information.
  19.  *
  20.  * @category    design
  21.  * @package     base_default
  22.  * @copyright   Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
  23.  * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
  24.  */
  25. var Checkout = Class.create();
  26. Checkout.prototype = {
  27.     initialize: function(accordion, urls) {
  28.         this.accordion = accordion;
  29.         this.progressUrl = urls.progress;
  30.         this.reviewUrl = urls.review;
  31.         this.saveMethodUrl = urls.saveMethod;
  32.         this.failureUrl = urls.failure;
  33.         this.billingForm = false;
  34.         this.shippingForm = false;
  35.         this.syncBillingShipping = false;
  36.         this.method = '';
  37.         this.payment = '';
  38.         this.loadWaiting = false;
  39.         this.steps = ['login', 'billing', 'shipping', 'shipping_method', 'payment', 'review'];
  40.         //We use billing as beginning step since progress bar tracks from billing
  41.         this.currentStep = 'billing';
  42.  
  43.         this.accordion.sections.each(function(section) {
  44.             Event.observe($(section).down('.step-title'), 'click', this._onSectionClick.bindAsEventListener(this));
  45.         }.bind(this));
  46.  
  47.         this.accordion.disallowAccessToNextSections = true;
  48.     },
  49.     /**
  50.      * Section header click handler
  51.      *
  52.      * @param event
  53.      */
  54.     _onSectionClick: function(event) {
  55.         var section = $(Event.element(event).up().up());
  56.         if (section.hasClassName('allow')) {
  57.             Event.stop(event);
  58.             this.gotoSection(section.readAttribute('id').replace('opc-', ''), false);
  59.             return false;
  60.         }
  61.     },
  62.     ajaxFailure: function() {
  63.         location.href = this.failureUrl;
  64.     },
  65.     reloadProgressBlock: function(toStep) {
  66.         this.reloadStep(toStep);
  67.         if (this.syncBillingShipping) {
  68.             this.syncBillingShipping = false;
  69.             this.reloadStep('shipping');
  70.         }
  71.     },
  72.     reloadStep: function(prevStep) {
  73.         var updater = new Ajax.Updater(prevStep + '-progress-opcheckout', this.progressUrl, {
  74.             method: 'get',
  75.             onFailure: this.ajaxFailure.bind(this),
  76.             onComplete: function() {
  77.                 this.checkout.resetPreviousSteps();
  78.             },
  79.             parameters: prevStep ? {prevStep: prevStep} : null
  80.         });
  81.     },
  82.     reloadReviewBlock: function() {
  83.         var updater = new Ajax.Updater('checkout-review-load', this.reviewUrl, {method: 'get', onFailure: this.ajaxFailure.bind(this)});
  84.     },
  85.     _disableEnableAll: function(element, isDisabled) {
  86.         var descendants = element.descendants();
  87.         for (var k in descendants) {
  88.             descendants[k].disabled = isDisabled;
  89.         }
  90.         element.disabled = isDisabled;
  91.     },
  92.     setLoadWaiting: function(step, keepDisabled) {
  93.         if (step) {
  94.             if (this.loadWaiting) {
  95.                 this.setLoadWaiting(false);
  96.             }
  97.             var container = $(step + '-buttons-container');
  98.             container.addClassName('disabled');
  99.             container.setStyle({opacity: .5});
  100.             this._disableEnableAll(container, true);
  101.             Element.show(step + '-please-wait');
  102.         } else {
  103.             if (this.loadWaiting) {
  104.                 var container = $(this.loadWaiting + '-buttons-container');
  105.                 var isDisabled = (keepDisabled ? true : false);
  106.                 if (!isDisabled) {
  107.                     container.removeClassName('disabled');
  108.                     container.setStyle({opacity: 1});
  109.                 }
  110.                 this._disableEnableAll(container, isDisabled);
  111.                 Element.hide(this.loadWaiting + '-please-wait');
  112.             }
  113.         }
  114.         this.loadWaiting = step;
  115.     },
  116.     gotoSection: function(section, reloadProgressBlock) {
  117.  
  118.         if (reloadProgressBlock) {
  119.             this.reloadProgressBlock(this.currentStep);
  120.         }
  121.         this.currentStep = section;
  122.         var sectionElement = $('opc-' + section);
  123.         sectionElement.addClassName('allow');
  124.         this.accordion.openSection('opc-' + section);
  125.         if (!reloadProgressBlock) {
  126.             this.resetPreviousSteps();
  127.         }
  128.     },
  129.     resetPreviousSteps: function() {
  130.         var stepIndex = this.steps.indexOf(this.currentStep);
  131.  
  132.         //Clear other steps if already populated through javascript
  133.         for (var i = stepIndex; i < this.steps.length; i++) {
  134.             var nextStep = this.steps[i];
  135.             var progressDiv = nextStep + '-progress-opcheckout';
  136.             if ($(progressDiv)) {
  137.                 //Remove the link
  138.                 $(progressDiv).select('.changelink').each(function(item) {
  139.                     item.remove();
  140.                 });
  141.                 $(progressDiv).select('dt').each(function(item) {
  142.                     item.removeClassName('complete');
  143.                 });
  144.                 //Remove the content
  145.                 $(progressDiv).select('dd.complete').each(function(item) {
  146.                     item.remove();
  147.                 });
  148.             }
  149.         }
  150.     },
  151.     changeSection: function(section) {
  152.         var changeStep = section.replace('opc-', '');
  153.         this.gotoSection(changeStep, false);
  154.     },
  155.     setMethod: function() {
  156.         if ($('login:guest') && $('login:guest').checked) {
  157.             this.method = 'guest';
  158.             var request = new Ajax.Request(
  159.                     this.saveMethodUrl,
  160.                     {method: 'post', onFailure: this.ajaxFailure.bind(this), parameters: {method: 'guest'}}
  161.             );
  162.             Element.hide('register-customer-password');
  163.             this.gotoSection('billing', true);
  164.         }
  165.         else if ($('login:register') && ($('login:register').checked || $('login:register').type == 'hidden')) {
  166.             this.method = 'register';
  167.             var request = new Ajax.Request(
  168.                     this.saveMethodUrl,
  169.                     {method: 'post', onFailure: this.ajaxFailure.bind(this), parameters: {method: 'register'}}
  170.             );
  171.             Element.show('register-customer-password');
  172.             this.gotoSection('billing', true);
  173.         }
  174.         else {
  175.             alert(Translator.translate('Please choose to register or to checkout as a guest').stripTags());
  176.             return false;
  177.         }
  178.         document.body.fire('login:setMethod', {method: this.method});
  179.     },
  180.     setBilling: function() {
  181.         if (($('billing:use_for_shipping_yes')) && ($('billing:use_for_shipping_yes').checked)) {
  182.             shipping.syncWithBilling();
  183.             $('opc-shipping').addClassName('allow');
  184.             this.gotoSection('shipping_method', true);
  185.         } else if (($('billing:use_for_shipping_no')) && ($('billing:use_for_shipping_no').checked)) {
  186.             $('shipping:same_as_billing').checked = false;
  187.             this.gotoSection('shipping', true);
  188.         } else {
  189.             $('shipping:same_as_billing').checked = true;
  190.             this.gotoSection('shipping', true);
  191.         }
  192.  
  193.         // this refreshes the checkout progress column
  194.  
  195. //        if ($('billing:use_for_shipping') && $('billing:use_for_shipping').checked){
  196. //            shipping.syncWithBilling();
  197. //            //this.setShipping();
  198. //            //shipping.save();
  199. //            $('opc-shipping').addClassName('allow');
  200. //            this.gotoSection('shipping_method');
  201. //        } else {
  202. //            $('shipping:same_as_billing').checked = false;
  203. //            this.gotoSection('shipping');
  204. //        }
  205. //        this.reloadProgressBlock();
  206. //        //this.accordion.openNextSection(true);
  207.     },
  208.     setShipping: function() {
  209.         //this.nextStep();
  210.         this.gotoSection('shipping_method', true);
  211.         //this.accordion.openNextSection(true);
  212.     },
  213.     setShippingMethod: function() {
  214.         //this.nextStep();
  215.         this.gotoSection('payment', true);
  216.         //this.accordion.openNextSection(true);
  217.     },
  218.     setPayment: function() {
  219.         //this.nextStep();
  220.         this.gotoSection('review', true);
  221.         //this.accordion.openNextSection(true);
  222.     },
  223.     setReview: function() {
  224.         this.reloadProgressBlock();
  225.         //this.nextStep();
  226.         //this.accordion.openNextSection(true);
  227.     },
  228.     back: function() {
  229.         if (this.loadWaiting)
  230.             return;
  231.         //Navigate back to the previous available step
  232.         var stepIndex = this.steps.indexOf(this.currentStep);
  233.         var section = this.steps[--stepIndex];
  234.         var sectionElement = $('opc-' + section);
  235.  
  236.         //Traverse back to find the available section. Ex Virtual product does not have shipping section
  237.         while (sectionElement === null && stepIndex > 0) {
  238.             --stepIndex;
  239.             section = this.steps[stepIndex];
  240.             sectionElement = $('opc-' + section);
  241.         }
  242.         this.changeSection('opc-' + section);
  243.     },
  244.     setStepResponse: function(response) {
  245.         if (response.update_section) {
  246.             $('checkout-' + response.update_section.name + '-load').update(response.update_section.html);
  247.         }
  248.         if (response.allow_sections) {
  249.             response.allow_sections.each(function(e) {
  250.                 $('opc-' + e).addClassName('allow');
  251.             });
  252.         }
  253.  
  254.         if (response.duplicateBillingInfo)
  255.         {
  256.             this.syncBillingShipping = true;
  257.             shipping.setSameAsBilling(true);
  258.         }
  259.  
  260.         if (response.goto_section) {
  261.             this.gotoSection(response.goto_section, true);
  262.             return true;
  263.         }
  264.         if (response.redirect) {
  265.             location.href = response.redirect;
  266.             return true;
  267.         }
  268.         return false;
  269.     }
  270. }
  271.  
  272. // billing
  273. var Billing = Class.create();
  274. Billing.prototype = {
  275.     initialize: function(form, addressUrl, saveUrl) {
  276.         this.form = form;
  277.         if ($(this.form)) {
  278.             $(this.form).observe('submit', function(event) {
  279.                 this.save();
  280.                 Event.stop(event);
  281.             }.bind(this));
  282.         }
  283.         this.addressUrl = addressUrl;
  284.         this.saveUrl = saveUrl;
  285.         this.onAddressLoad = this.fillForm.bindAsEventListener(this);
  286.         this.onSave = this.nextStep.bindAsEventListener(this);
  287.         this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
  288.     },
  289.     setAddress: function(addressId) {
  290.         if (addressId) {
  291.             request = new Ajax.Request(
  292.                     this.addressUrl + addressId,
  293.                     {method: 'get', onSuccess: this.onAddressLoad, onFailure: checkout.ajaxFailure.bind(checkout)}
  294.             );
  295.         }
  296.         else {
  297.             this.fillForm(false);
  298.         }
  299.     },
  300.     newAddress: function(isNew) {
  301.         if (isNew) {
  302.             this.resetSelectedAddress();
  303.             Element.show('billing-new-address-form');
  304.         } else {
  305.             Element.hide('billing-new-address-form');
  306.         }
  307.     },
  308.     resetSelectedAddress: function() {
  309.         var selectElement = $('billing-address-select')
  310.         if (selectElement) {
  311.             selectElement.value = '';
  312.         }
  313.     },
  314.     fillForm: function(transport) {
  315.         var elementValues = {};
  316.         if (transport && transport.responseText) {
  317.             try {
  318.                 elementValues = eval('(' + transport.responseText + ')');
  319.             }
  320.             catch (e) {
  321.                 elementValues = {};
  322.             }
  323.         }
  324.         else {
  325.             this.resetSelectedAddress();
  326.         }
  327.         arrElements = Form.getElements(this.form);
  328.         for (var elemIndex in arrElements) {
  329.             if (arrElements[elemIndex].id) {
  330.                 var fieldName = arrElements[elemIndex].id.replace(/^billing:/, '');
  331.                 arrElements[elemIndex].value = elementValues[fieldName] ? elementValues[fieldName] : '';
  332.                 if (fieldName == 'country_id' && billingForm) {
  333.                     billingForm.elementChildLoad(arrElements[elemIndex]);
  334.                 }
  335.             }
  336.         }
  337.     },
  338.     setUseForShipping: function(flag) {
  339.         $('shipping:same_as_billing').checked = flag;
  340.     },
  341.     save: function() {
  342.         if (checkout.loadWaiting != false)
  343.             return;
  344.  
  345.         var validator = new Validation(this.form);
  346.         if (validator.validate()) {
  347.             checkout.setLoadWaiting('billing');
  348.  
  349. //            if ($('billing:use_for_shipping') && $('billing:use_for_shipping').checked) {
  350. //                $('billing:use_for_shipping').value=1;
  351. //            }
  352.  
  353.             var request = new Ajax.Request(
  354.                     this.saveUrl,
  355.                     {
  356.                         method: 'post',
  357.                         onComplete: this.onComplete,
  358.                         onSuccess: this.onSave,
  359.                         onFailure: checkout.ajaxFailure.bind(checkout),
  360.                         parameters: Form.serialize(this.form)
  361.                     }
  362.             );
  363.         }
  364.     },
  365.     resetLoadWaiting: function(transport) {
  366.         checkout.setLoadWaiting(false);
  367.         document.body.fire('billing-request:completed', {transport: transport});
  368.     },
  369.     /**
  370.      This method recieves the AJAX response on success.
  371.      There are 3 options: error, redirect or html with shipping options.
  372.      */
  373.     nextStep: function(transport) {
  374.         if (transport && transport.responseText) {
  375.             try {
  376.                 response = eval('(' + transport.responseText + ')');
  377.             }
  378.             catch (e) {
  379.                 response = {};
  380.             }
  381.         }
  382.  
  383.         if (response.error) {
  384.             if ((typeof response.message) == 'string') {
  385.                 alert(response.message);
  386.             } else {
  387.                 if (window.billingRegionUpdater) {
  388.                     billingRegionUpdater.update();
  389.                 }
  390.  
  391.                 alert(response.message.join("\n"));
  392.             }
  393.  
  394.             return false;
  395.         }
  396.  
  397.         checkout.setStepResponse(response);
  398.         payment.initWhatIsCvvListeners();
  399.         // DELETE
  400.         //alert('error: ' + response.error + ' / redirect: ' + response.redirect + ' / shipping_methods_html: ' + response.shipping_methods_html);
  401.         // This moves the accordion panels of one page checkout and updates the checkout progress
  402.         //checkout.setBilling();
  403.     }
  404. }
  405.  
  406. // shipping
  407. var Shipping = Class.create();
  408. Shipping.prototype = {
  409.     initialize: function(form, addressUrl, saveUrl, methodsUrl) {
  410.         this.form = form;
  411.         if ($(this.form)) {
  412.             $(this.form).observe('submit', function(event) {
  413.                 this.save();
  414.                 Event.stop(event);
  415.             }.bind(this));
  416.         }
  417.         this.addressUrl = addressUrl;
  418.         this.saveUrl = saveUrl;
  419.         this.methodsUrl = methodsUrl;
  420.         this.onAddressLoad = this.fillForm.bindAsEventListener(this);
  421.         this.onSave = this.nextStep.bindAsEventListener(this);
  422.         this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
  423.     },
  424.     setAddress: function(addressId) {
  425.         if (addressId) {
  426.             request = new Ajax.Request(
  427.                     this.addressUrl + addressId,
  428.                     {method: 'get', onSuccess: this.onAddressLoad, onFailure: checkout.ajaxFailure.bind(checkout)}
  429.             );
  430.         }
  431.         else {
  432.             this.fillForm(false);
  433.         }
  434.     },
  435.     newAddress: function(isNew) {
  436.         if (isNew) {
  437.             this.resetSelectedAddress();
  438.             Element.show('shipping-new-address-form');
  439.         } else {
  440.             Element.hide('shipping-new-address-form');
  441.         }
  442.         shipping.setSameAsBilling(false);
  443.     },
  444.     resetSelectedAddress: function() {
  445.         var selectElement = $('shipping-address-select')
  446.         if (selectElement) {
  447.             selectElement.value = '';
  448.         }
  449.     },
  450.     fillForm: function(transport) {
  451.         var elementValues = {};
  452.         if (transport && transport.responseText) {
  453.             try {
  454.                 elementValues = eval('(' + transport.responseText + ')');
  455.             }
  456.             catch (e) {
  457.                 elementValues = {};
  458.             }
  459.         }
  460.         else {
  461.             this.resetSelectedAddress();
  462.         }
  463.         arrElements = Form.getElements(this.form);
  464.         for (var elemIndex in arrElements) {
  465.             if (arrElements[elemIndex].id) {
  466.                 var fieldName = arrElements[elemIndex].id.replace(/^shipping:/, '');
  467.                 arrElements[elemIndex].value = elementValues[fieldName] ? elementValues[fieldName] : '';
  468.                 if (fieldName == 'country_id' && shippingForm) {
  469.                     shippingForm.elementChildLoad(arrElements[elemIndex]);
  470.                 }
  471.             }
  472.         }
  473.     },
  474.     setSameAsBilling: function(flag) {
  475.         $('shipping:same_as_billing').checked = flag;
  476. // #5599. Also it hangs up, if the flag is not false
  477. //        $('billing:use_for_shipping_yes').checked = flag;
  478.         if (flag) {
  479.             this.syncWithBilling();
  480.         }
  481.     },
  482.     syncWithBilling: function() {
  483.         $('billing-address-select') && this.newAddress(!$('billing-address-select').value);
  484.         $('shipping:same_as_billing').checked = true;
  485.         if (!$('billing-address-select') || !$('billing-address-select').value) {
  486.             arrElements = Form.getElements(this.form);
  487.             for (var elemIndex in arrElements) {
  488.                 if (arrElements[elemIndex].id) {
  489.                     var sourceField = $(arrElements[elemIndex].id.replace(/^shipping:/, 'billing:'));
  490.                     if (sourceField) {
  491.                         arrElements[elemIndex].value = sourceField.value;
  492.                     }
  493.                 }
  494.             }
  495.             //$('shipping:country_id').value = $('billing:country_id').value;
  496.             shippingRegionUpdater.update();
  497.             $('shipping:region_id').value = $('billing:region_id').value;
  498.             $('shipping:region').value = $('billing:region').value;
  499.             //shippingForm.elementChildLoad($('shipping:country_id'), this.setRegionValue.bind(this));
  500.         } else {
  501.             $('shipping-address-select').value = $('billing-address-select').value;
  502.         }
  503.     },
  504.     setRegionValue: function() {
  505.         $('shipping:region').value = $('billing:region').value;
  506.     },
  507.     save: function() {
  508.         if (checkout.loadWaiting != false)
  509.             return;
  510.         var validator = new Validation(this.form);
  511.         if (validator.validate()) {
  512.             checkout.setLoadWaiting('shipping');
  513.             var data = Form.serialize(this.form);
  514.             console.log('shipping save data is:')
  515.             console.log(data);
  516.             var request = new Ajax.Request(
  517.                     this.saveUrl,
  518.                     {
  519.                         method: 'post',
  520.                         onComplete: this.onComplete,
  521.                         onSuccess: this.onSave,
  522.                         onFailure: checkout.ajaxFailure.bind(checkout),
  523.                         parameters: data
  524.                     }
  525.             );
  526.         }
  527.     },
  528.     resetLoadWaiting: function(transport) {
  529.         checkout.setLoadWaiting(false);
  530.     },
  531.     nextStep: function(transport) {
  532.         if (transport && transport.responseText) {
  533.             try {
  534.                 response = eval('(' + transport.responseText + ')');
  535.             }
  536.             catch (e) {
  537.                 response = {};
  538.             }
  539.         }
  540.         if (response.error) {
  541.             if ((typeof response.message) == 'string') {
  542.                 alert(response.message);
  543.             } else {
  544.                 if (window.shippingRegionUpdater) {
  545.                     shippingRegionUpdater.update();
  546.                 }
  547.                 alert(response.message.join("\n"));
  548.             }
  549.  
  550.             return false;
  551.         }
  552.  
  553.         checkout.setStepResponse(response);
  554.  
  555.         /*
  556.          var updater = new Ajax.Updater(
  557.          'checkout-shipping-method-load',
  558.          this.methodsUrl,
  559.          {method:'get', onSuccess: checkout.setShipping.bind(checkout)}
  560.          );
  561.          */
  562.         //checkout.setShipping();
  563.     }
  564. }
  565.  
  566. // shipping method
  567. var ShippingMethod = Class.create();
  568. ShippingMethod.prototype = {
  569.     initialize: function(form, saveUrl) {
  570.         this.form = form;
  571.         if ($(this.form)) {
  572.             $(this.form).observe('submit', function(event) {
  573.                 this.save();
  574.                 Event.stop(event);
  575.             }.bind(this));
  576.         }
  577.         this.saveUrl = saveUrl;
  578.         this.validator = new Validation(this.form);
  579.         this.onSave = this.nextStep.bindAsEventListener(this);
  580.         this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
  581.     },
  582.     validate: function() {
  583.         var methods = document.getElementsByName('shipping_method');
  584.         if (methods.length == 0) {
  585.             alert(Translator.translate('Your order cannot be completed at this time as there is no shipping methods available for it. Please make necessary changes in your shipping address.').stripTags());
  586.             return false;
  587.         }
  588.  
  589.         if (!this.validator.validate()) {
  590.             return false;
  591.         }
  592.  
  593.         for (var i = 0; i < methods.length; i++) {
  594.             if (methods[i].checked) {
  595.                 return true;
  596.             }
  597.         }
  598.         alert(Translator.translate('Please specify shipping method.').stripTags());
  599.         return false;
  600.     },
  601.     save: function() {
  602.  
  603.         if (checkout.loadWaiting != false)
  604.             return;
  605.         if (this.validate()) {
  606.             checkout.setLoadWaiting('shipping-method');
  607.             var request = new Ajax.Request(
  608.                     this.saveUrl,
  609.                     {
  610.                         method: 'post',
  611.                         onComplete: this.onComplete,
  612.                         onSuccess: this.onSave,
  613.                         onFailure: checkout.ajaxFailure.bind(checkout),
  614.                         parameters: Form.serialize(this.form)
  615.                     }
  616.             );
  617.         }
  618.     },
  619.     resetLoadWaiting: function(transport) {
  620.         checkout.setLoadWaiting(false);
  621.     },
  622.     nextStep: function(transport) {
  623.         if (transport && transport.responseText) {
  624.             try {
  625.                 response = eval('(' + transport.responseText + ')');
  626.             }
  627.             catch (e) {
  628.                 response = {};
  629.             }
  630.         }
  631.  
  632.         if (response.error) {
  633.             alert(response.message);
  634.             return false;
  635.         }
  636.  
  637.         if (response.update_section) {
  638.             $('checkout-' + response.update_section.name + '-load').update(response.update_section.html);
  639.         }
  640.  
  641.         payment.initWhatIsCvvListeners();
  642.  
  643.         if (response.goto_section) {
  644.             checkout.gotoSection(response.goto_section, true);
  645.             checkout.reloadProgressBlock();
  646.             return;
  647.         }
  648.  
  649.         if (response.payment_methods_html) {
  650.             $('checkout-payment-method-load').update(response.payment_methods_html);
  651.         }
  652.  
  653.         checkout.setShippingMethod();
  654.     }
  655. }
  656.  
  657.  
  658. // payment
  659. var Payment = Class.create();
  660. Payment.prototype = {
  661.     beforeInitFunc: $H({}),
  662.     afterInitFunc: $H({}),
  663.     beforeValidateFunc: $H({}),
  664.     afterValidateFunc: $H({}),
  665.     initialize: function(form, saveUrl) {
  666.         this.form = form;
  667.         this.saveUrl = saveUrl;
  668.         this.onSave = this.nextStep.bindAsEventListener(this);
  669.         this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
  670.     },
  671.     addBeforeInitFunction: function(code, func) {
  672.         this.beforeInitFunc.set(code, func);
  673.     },
  674.     beforeInit: function() {
  675.         (this.beforeInitFunc).each(function(init) {
  676.             (init.value)();
  677.             ;
  678.         });
  679.     },
  680.     init: function() {
  681.         this.beforeInit();
  682.         var elements = Form.getElements(this.form);
  683.         if ($(this.form)) {
  684.             $(this.form).observe('submit', function(event) {
  685.                 this.save();
  686.                 Event.stop(event);
  687.             }.bind(this));
  688.         }
  689.         var method = null;
  690.         for (var i = 0; i < elements.length; i++) {
  691.             if (elements[i].name == 'payment[method]') {
  692.                 if (elements[i].checked) {
  693.                     method = elements[i].value;
  694.                 }
  695.             } else {
  696.                 elements[i].disabled = true;
  697.             }
  698.             elements[i].setAttribute('autocomplete', 'off');
  699.         }
  700.         if (method)
  701.             this.switchMethod(method);
  702.         this.afterInit();
  703.     },
  704.     addAfterInitFunction: function(code, func) {
  705.         this.afterInitFunc.set(code, func);
  706.     },
  707.     afterInit: function() {
  708.         (this.afterInitFunc).each(function(init) {
  709.             (init.value)();
  710.         });
  711.     },
  712.     switchMethod: function(method) {
  713.         if (this.currentMethod && $('payment_form_' + this.currentMethod)) {
  714.             this.changeVisible(this.currentMethod, true);
  715.             $('payment_form_' + this.currentMethod).fire('payment-method:switched-off', {method_code: this.currentMethod});
  716.         }
  717.         if ($('payment_form_' + method)) {
  718.             this.changeVisible(method, false);
  719.             $('payment_form_' + method).fire('payment-method:switched', {method_code: method});
  720.         } else {
  721.             //Event fix for payment methods without form like "Check / Money order"
  722.             document.body.fire('payment-method:switched', {method_code: method});
  723.         }
  724.         if (method) {
  725.             this.lastUsedMethod = method;
  726.         }
  727.         this.currentMethod = method;
  728.     },
  729.     changeVisible: function(method, mode) {
  730.         var block = 'payment_form_' + method;
  731.         [block + '_before', block, block + '_after'].each(function(el) {
  732.             element = $(el);
  733.             if (element) {
  734.                 element.style.display = (mode) ? 'none' : '';
  735.                 element.select('input', 'select', 'textarea', 'button').each(function(field) {
  736.                     field.disabled = mode;
  737.                 });
  738.             }
  739.         });
  740.     },
  741.     addBeforeValidateFunction: function(code, func) {
  742.         this.beforeValidateFunc.set(code, func);
  743.     },
  744.     beforeValidate: function() {
  745.         var validateResult = true;
  746.         var hasValidation = false;
  747.         (this.beforeValidateFunc).each(function(validate) {
  748.             hasValidation = true;
  749.             if ((validate.value)() == false) {
  750.                 validateResult = false;
  751.             }
  752.         }.bind(this));
  753.         if (!hasValidation) {
  754.             validateResult = false;
  755.         }
  756.         return validateResult;
  757.     },
  758.     validate: function() {
  759.         var result = this.beforeValidate();
  760.         if (result) {
  761.             return true;
  762.         }
  763.         var methods = document.getElementsByName('payment[method]');
  764.         if (methods.length == 0) {
  765.             alert(Translator.translate('Your order cannot be completed at this time as there is no payment methods available for it.').stripTags());
  766.             return false;
  767.         }
  768.         for (var i = 0; i < methods.length; i++) {
  769.             if (methods[i].checked) {
  770.                 return true;
  771.             }
  772.         }
  773.         result = this.afterValidate();
  774.         if (result) {
  775.             return true;
  776.         }
  777.         alert(Translator.translate('Please specify payment method.').stripTags());
  778.         return false;
  779.     },
  780.     addAfterValidateFunction: function(code, func) {
  781.         this.afterValidateFunc.set(code, func);
  782.     },
  783.     afterValidate: function() {
  784.         var validateResult = true;
  785.         var hasValidation = false;
  786.         (this.afterValidateFunc).each(function(validate) {
  787.             hasValidation = true;
  788.             if ((validate.value)() == false) {
  789.                 validateResult = false;
  790.             }
  791.         }.bind(this));
  792.         if (!hasValidation) {
  793.             validateResult = false;
  794.         }
  795.         return validateResult;
  796.     },
  797.     save: function() {
  798.         if (checkout.loadWaiting != false)
  799.             return;
  800.         var validator = new Validation(this.form);
  801.         if (this.validate() && validator.validate()) {
  802.             checkout.setLoadWaiting('payment');
  803.             var request = new Ajax.Request(
  804.                     this.saveUrl,
  805.                     {
  806.                         method: 'post',
  807.                         onComplete: this.onComplete,
  808.                         onSuccess: this.onSave,
  809.                         onFailure: checkout.ajaxFailure.bind(checkout),
  810.                         parameters: Form.serialize(this.form)
  811.                     }
  812.             );
  813.         }
  814.     },
  815.     resetLoadWaiting: function() {
  816.         checkout.setLoadWaiting(false);
  817.     },
  818.     nextStep: function(transport) {
  819.         if (transport && transport.responseText) {
  820.             try {
  821.                 response = eval('(' + transport.responseText + ')');
  822.             }
  823.             catch (e) {
  824.                 response = {};
  825.             }
  826.         }
  827.         /*
  828.          * if there is an error in payment, need to show error message
  829.          */
  830.         if (response.error) {
  831.             if (response.fields) {
  832.                 var fields = response.fields.split(',');
  833.                 for (var i = 0; i < fields.length; i++) {
  834.                     var field = null;
  835.                     if (field = $(fields[i])) {
  836.                         Validation.ajaxError(field, response.error);
  837.                     }
  838.                 }
  839.                 return;
  840.             }
  841.             alert(response.error);
  842.             return;
  843.         }
  844.  
  845.         checkout.setStepResponse(response);
  846.  
  847.         //checkout.setPayment();
  848.     },
  849.     initWhatIsCvvListeners: function() {
  850.         $$('.cvv-what-is-this').each(function(element) {
  851.             Event.observe(element, 'click', toggleToolTip);
  852.         });
  853.     }
  854. }
  855.  
  856. var Review = Class.create();
  857. Review.prototype = {
  858.     initialize: function(saveUrl, successUrl, agreementsForm) {
  859.         this.saveUrl = saveUrl;
  860.         this.successUrl = successUrl;
  861.         this.agreementsForm = agreementsForm;
  862.         this.onSave = this.nextStep.bindAsEventListener(this);
  863.         this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
  864.     },
  865.     save: function() {
  866.         if (checkout.loadWaiting != false)
  867.             return;
  868.         checkout.setLoadWaiting('review');
  869.         var params = Form.serialize(payment.form);
  870.         if (this.agreementsForm) {
  871.             params += '&' + Form.serialize(this.agreementsForm);
  872.         }
  873.         params.save = true;
  874.         var request = new Ajax.Request(
  875.                 this.saveUrl,
  876.                 {
  877.                     method: 'post',
  878.                     parameters: params,
  879.                     onComplete: this.onComplete,
  880.                     onSuccess: this.onSave,
  881.                     onFailure: checkout.ajaxFailure.bind(checkout)
  882.                 }
  883.         );
  884.     },
  885.     resetLoadWaiting: function(transport) {
  886.         checkout.setLoadWaiting(false, this.isSuccess);
  887.     },
  888.     nextStep: function(transport) {
  889.         if (transport && transport.responseText) {
  890.             try {
  891.                 response = eval('(' + transport.responseText + ')');
  892.             }
  893.             catch (e) {
  894.                 response = {};
  895.             }
  896.             if (response.redirect) {
  897.                 this.isSuccess = true;
  898.                 location.href = response.redirect;
  899.                 return;
  900.             }
  901.             if (response.success) {
  902.                 this.isSuccess = true;
  903.                 window.location = this.successUrl;
  904.             }
  905.             else {
  906.                 var msg = response.error_messages;
  907.                 if (typeof (msg) == 'object') {
  908.                     msg = msg.join("\n");
  909.                 }
  910.                 if (msg) {
  911.                     alert(msg);
  912.                 }
  913.             }
  914.  
  915.             if (response.update_section) {
  916.                 $('checkout-' + response.update_section.name + '-load').update(response.update_section.html);
  917.             }
  918.  
  919.             if (response.goto_section) {
  920.                 checkout.gotoSection(response.goto_section, true);
  921.             }
  922.         }
  923.     },
  924.     isSuccess: false
  925. }
Advertisement
Add Comment
Please, Sign In to add comment