/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
var Checkout = Class.create();
Checkout.prototype = {
initialize: function(accordion, urls){
this.accordion = accordion;
this.progressUrl = urls.progress;
this.reviewUrl = urls.review;
this.saveMethodUrl = urls.saveMethod;
this.failureUrl = urls.failure;
this.billingForm = false;
this.shippingForm= false;
this.syncBillingShipping = false;
this.method = '';
this.payment = '';
this.loadWaiting = false;
this.steps = ['login', 'billing', 'shipping', 'shipping_method', 'payment', 'review'];
//this.onSetMethod = this.nextStep.bindAsEventListener(this);
this.accordion.disallowAccessToNextSections = true;
},
ajaxFailure: function(){
location.href = this.failureUrl;
},
reloadProgressBlock: function(){
var updater = new Ajax.Updater($$('.col-right')[0], this.progressUrl, {method: 'get', onFailure: this.ajaxFailure.bind(this)});
},
reloadReviewBlock: function(){
var updater = new Ajax.Updater('checkout-review-load', this.reviewUrl, {method: 'get', onFailure: this.ajaxFailure.bind(this)});
},
_disableEnableAll: function(element, isDisabled) {
var descendants = element.descendants();
for (var k in descendants) {
descendants[k].disabled = isDisabled;
}
element.disabled = isDisabled;
},
setLoadWaiting: function(step, keepDisabled) {
if (step) {
if (this.loadWaiting) {
this.setLoadWaiting(false);
}
var container = $(step+'-buttons-container');
container.setStyle({opacity:.5});
this._disableEnableAll(container, true);
Element.show(step+'-please-wait');
} else {
if (this.loadWaiting) {
var container = $(this.loadWaiting+'-buttons-container');
var isDisabled = (keepDisabled ? true : false);
if (!isDisabled) {
container.setStyle({opacity:1});
}
this._disableEnableAll(container, isDisabled);
Element.hide(this.loadWaiting+'-please-wait');
}
}
this.loadWaiting = step;
},
gotoSection: function(section)
{
section = $('opc-'+section);
section.addClassName('allow');
this.accordion.openSection(section);
},
setMethod: function(){
if ($('login:guest') && $('login:guest').checked) {
this.method = 'guest';
var request = new Ajax.Request(
this.saveMethodUrl,
{method: 'post', onFailure: this.ajaxFailure.bind(this), parameters: {method:'guest'}}
);
Element.hide('register-customer-password');
this.gotoSection('billing');
}
else if($('login:register') && ($('login:register').checked || $('login:register').type == 'hidden')) {
this.method = 'register';
var request = new Ajax.Request(
this.saveMethodUrl,
{method: 'post', onFailure: this.ajaxFailure.bind(this), parameters: {method:'register'}}
);
Element.show('register-customer-password');
this.gotoSection('billing');
}
else{
alert(Translator.translate('Please choose to register or to checkout as a guest'));
return false;
}
},
setBilling: function() {
if (($('billing:use_for_shipping_yes')) && ($('billing:use_for_shipping_yes').checked)) {
shipping.syncWithBilling();
$('opc-shipping').addClassName('allow');
this.gotoSection('shipping_method');
} else if (($('billing:use_for_shipping_no')) && ($('billing:use_for_shipping_no').checked)) {
$('shipping:same_as_billing').checked = false;
this.gotoSection('shipping');
} else {
$('shipping:same_as_billing').checked = true;
this.gotoSection('shipping');
}
// this refreshes the checkout progress column
this.reloadProgressBlock();
// if ($('billing:use_for_shipping') && $('billing:use_for_shipping').checked){
// shipping.syncWithBilling();
// //this.setShipping();
// //shipping.save();
// $('opc-shipping').addClassName('allow');
// this.gotoSection('shipping_method');
// } else {
// $('shipping:same_as_billing').checked = false;
// this.gotoSection('shipping');
// }
// this.reloadProgressBlock();
// //this.accordion.openNextSection(true);
},
setShipping: function() {
this.reloadProgressBlock();
//this.nextStep();
this.gotoSection('shipping_method');
//this.accordion.openNextSection(true);
},
setShippingMethod: function() {
this.reloadProgressBlock();
//this.nextStep();
this.gotoSection('payment');
//this.accordion.openNextSection(true);
},
setPayment: function() {
this.reloadProgressBlock();
//this.nextStep();
this.gotoSection('review');
//this.accordion.openNextSection(true);
},
setReview: function() {
this.reloadProgressBlock();
//this.nextStep();
//this.accordion.openNextSection(true);
},
back: function(){
if (this.loadWaiting) return;
this.accordion.openPrevSection(true);
},
setStepResponse: function(response){
if (response.update_section) {
$('checkout-'+response.update_section.name+'-load').update(response.update_section.html);
}
if (response.allow_sections) {
response.allow_sections.each(function(e){
$('opc-'+e).addClassName('allow');
});
}
if(response.duplicateBillingInfo)
{
shipping.setSameAsBilling(true);
}
if (response.goto_section) {
this.reloadProgressBlock();
this.gotoSection(response.goto_section);
return true;
}
if (response.redirect) {
location.href = response.redirect;
return true;
}
return false;
}
}
// billing
var Billing = Class.create();
Billing.prototype = {
initialize: function(form, addressUrl, saveUrl){
this.form = form;
this.addressUrl = addressUrl;
this.saveUrl = saveUrl;
this.onAddressLoad = this.fillForm.bindAsEventListener(this);
this.onSave = this.nextStep.bindAsEventListener(this);
this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
},
setAddress: function(addressId){
if (addressId) {
request = new Ajax.Request(
this.addressUrl+addressId,
{method:'get', onSuccess: this.onAddressLoad, onFailure: checkout.ajaxFailure.bind(checkout)}
);
}
else {
this.fillForm(false);
}