/**
* 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.
*
* @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)
*/

function Viewport(){ this.windowX = (document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth; this.windowY = (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight; this.scrollX = (document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft; this.scrollY = (document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop; this.pageX = (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth; this.pageY = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
}

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($$('.image')[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)
   {
       /* Sebastian Enzinger */
       if (section != 'shipping_method' && section != 'payment')
       {
         if (section == 'review')
         {
           if ($('loading-mask') && $('loading-msg'))
           {
             $('loading-mask').hide();
             $('loading-msg').hide();
           }
         }
         section = $('opc-'+section);
         section.addClassName('allow');
         this.accordion.openSection(section);
       } else {
         section = $('opc-'+section);
         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.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);
       }
   },

   newAddress: function(isNew){
       if (isNew) {
           this.resetSelectedAddress();
           Element.show('billing-new-address-form');
       } else {
           Element.hide('billing-new-address-form');
       }
   },

   resetSelectedAddress: function(){
       var selectElement = $('billing-address-select')
       if (selectElement) {
           selectElement.value='';
       }
   },

   fillForm: function(transport){
       var elementValues = {};
       if (transport && transport.responseText){
           try{
               elementValues = eval('(' + transport.responseText + ')');
           }
           catch (e) {
               elementValues = {};
           }
       }
       else{
           this.resetSelectedAddress();
       }
       arrElements = Form.getElements(this.form);
       for (var elemIndex in arrElements) {
           if (arrElements[elemIndex].id) {
               var fieldName = arrElements[elemIndex].id.replace(/^billing:/, '');
               arrElements[elemIndex].value = elementValues[fieldName] ? elementValues[fieldName] : '';
               if (fieldName == 'country_id' && billingForm){
                   billingForm.elementChildLoad(arrElements[elemIndex]);
               }
           }
       }
   },

   setUseForShipping: function(flag) {
       $('shipping:same_as_billing').checked = flag;
   },

   save: function(){        
       if (checkout.loadWaiting!=false) return;

       var validator = new Validation(this.form);
       if (validator.validate()) {
           if (checkout.method=='register' && $('billing:customer_password').value != $('billing:confirm_password').value) {
               alert(Translator.translate('Error: Passwords do not match'));
               return;
           }
					 
					 checkout.setLoadWaiting('billing');					 
					 
           var request = new Ajax.Request(
               '../../../Autocomplete.php?action=check&type=billing',
               {
                   method: 'post',
                   onComplete: (function(transport){
									 	 if (transport && transport.responseText){
										 	 if (transport.responseText !== '') {
											 	 $('advice-required-entry-billing:street').innerHTML = transport.responseText;
											 	 $('advice-required-entry-billing:street').show();
												 checkout.setLoadWaiting(false);
											 	 return;
											 } else {
											 	 $('advice-required-entry-billing:street').hide();												 
											 }
										 }
					
					           var request = new Ajax.Request(
					               this.saveUrl,
					               {
					                   method: 'post',
					                   onComplete: this.onComplete,
					                   onSuccess: this.onSave,
					                   onFailure: checkout.ajaxFailure.bind(checkout),
					                   parameters: Form.serialize(this.form)
					               }
					           );
									 }).bind(this),
                   onFailure: checkout.ajaxFailure.bind(checkout),
                   parameters: Form.serialize(this.form)
               }
           );					 
					
       }
   },

   resetLoadWaiting: function(transport){
       checkout.setLoadWaiting(false);
       if ($('billing:use_for_shipping_yes') && $('billing:use_for_shipping_yes').checked == true)
       {
         if ($('s_method_tablerate_bestway'))
         {
           $('s_method_tablerate_bestway').click();
           if ($('loading-mask') && $('loading-msg'))
           {
		var size = new Viewport;
               $('loading-mask').setStyle({height: size.pageY + "px"});
             $('loading-mask').show();
             $('loading-msg').show();
           }
           shippingMethod.save();
           $('checkout-step-shipping_method').hide();
         }
       } else {
         if ($('s_method_tablerate_bestway'))
         {
           $('s_method_tablerate_bestway').click();
         }
       }
   },

   /**
       This method recieves the AJAX response on success.
       There are 3 options: error, redirect or html with shipping options.
   */
   nextStep: function(transport){
       if (transport && transport.responseText){
           try{
               response = eval('(' + transport.responseText + ')');
           }
           catch (e) {
               response = {};
           }
       }

       if (response.error){
           if ((typeof response.message) == 'string') {
               alert(response.message);
           } else {
               if (window.billingRegionUpdater) {
                   billingRegionUpdater.update();
               }

               alert(response.message.join("\n"));
           }

           return false;
       }

       checkout.setStepResponse(response);

       // DELETE
       //alert('error: ' + response.error + ' / redirect: ' + response.redirect + ' / shipping_methods_html: ' + response.shipping_methods_html);
       // This moves the accordion panels of one page checkout and updates the checkout progress
       //checkout.setBilling();
   }
}

// shipping
var Shipping = Class.create();
Shipping.prototype = {
   initialize: function(form, addressUrl, saveUrl, methodsUrl){
       this.form = form;
       this.addressUrl = addressUrl;
       this.saveUrl = saveUrl;
       this.methodsUrl = methodsUrl;
       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);
       }
   },

   newAddress: function(isNew){
       if (isNew) {
           this.resetSelectedAddress();
           Element.show('shipping-new-address-form');
       } else {
           Element.hide('shipping-new-address-form');
       }
       shipping.setSameAsBilling(false);
   },

   resetSelectedAddress: function(){
       var selectElement = $('shipping-address-select')
       if (selectElement) {
           selectElement.value='';
       }
   },

   fillForm: function(transport){
       var elementValues = {};
       if (transport && transport.responseText){
           try{
               elementValues = eval('(' + transport.responseText + ')');
           }
           catch (e) {
               elementValues = {};
           }
       }
       else{
           this.resetSelectedAddress();
       }
       arrElements = Form.getElements(this.form);
       for (var elemIndex in arrElements) {
           if (arrElements[elemIndex].id) {
               var fieldName = arrElements[elemIndex].id.replace(/^shipping:/, '');
               arrElements[elemIndex].value = elementValues[fieldName] ? elementValues[fieldName] : '';
               if (fieldName == 'country_id' && shippingForm){
                   shippingForm.elementChildLoad(arrElements[elemIndex]);
               }
           }
       }
   },

   setSameAsBilling: function(flag) {
     if($('shipping:same_as_billing')) {
       $('shipping:same_as_billing').checked = flag;
// #5599. Also it hangs up, if the flag is not false
//        $('billing:use_for_shipping_yes').checked = flag;
       if (flag) {
           this.syncWithBilling();
       }
     }
   },

   syncWithBilling: function () {    
       $('billing-address-select') && this.newAddress(!$('billing-address-select').value);
       $('shipping:same_as_billing').checked = true;
       if (!$('billing-address-select') || !$('billing-address-select').value) {
           arrElements = Form.getElements(this.form);
           for (var elemIndex in arrElements) {
               if (arrElements[elemIndex].id) {
                   var sourceField = $(arrElements[elemIndex].id.replace(/^shipping:/, 'billing:'));
                   if (sourceField){
                       arrElements[elemIndex].value = sourceField.value;
                   }
               }
           }
           //$('shipping:country_id').value = $('billing:country_id').value;
           shippingRegionUpdater.update();
           $('shipping:region_id').value = $('billing:region_id').value;
           $('shipping:region').value = $('billing:region').value;
           //shippingForm.elementChildLoad($('shipping:country_id'), this.setRegionValue.bind(this));
       } else {
           $('shipping-address-select').value = $('billing-address-select').value;
       }
   },

   setRegionValue: function(){
       $('shipping:region').value = $('billing:region').value;
   },

   save: function(){
       if (checkout.loadWaiting!=false) return;
       var validator = new Validation(this.form);
       if (validator.validate()) {
					 checkout.setLoadWaiting('shipping');					 
					 
           var request = new Ajax.Request(
               '../../../Autocomplete.php?action=check&type=shipping',
               {
                   method: 'post',
                   onComplete: (function(transport){
									 	 if (transport && transport.responseText){
										 	 if (transport.responseText !== '') {
											 	 $('advice-required-entry-shipping:street').innerHTML = transport.responseText;
											 	 $('advice-required-entry-shipping:street').show();
												 checkout.setLoadWaiting(false);
											 	 return;
											 } else {
											 	 $('advice-required-entry-shipping:street').hide();												 
											 }
										 }
					
					           var request = new Ajax.Request(
					               this.saveUrl,
					               {
					                   method: 'post',
					                   onComplete: this.onComplete,
					                   onSuccess: this.onSave,
					                   onFailure: checkout.ajaxFailure.bind(checkout),
					                   parameters: Form.serialize(this.form)
					               }
					           );
									 }).bind(this),
                   onFailure: checkout.ajaxFailure.bind(checkout),
                   parameters: Form.serialize(this.form)
               }
           );					 	
       }
   },

   resetLoadWaiting: function(transport){
       checkout.setLoadWaiting(false);
       if ($('loading-mask') && $('loading-msg'))
       {
	var size = new Viewport;
	$('loading-mask').setStyle({height: size.pageY + "px"});
         $('loading-mask').show();
         $('loading-msg').show();
       }
       if ($('gift') && $('gift').checked == true)
       {
         if ($('s_method_tablerate2_bestway'))
         {
           $('s_method_tablerate2_bestway').click();
           shippingMethod.save();
           $('checkout-step-shipping_method').hide();
         }
       } else {
         if ($('s_method_tablerate_bestway'))
         {
           $('s_method_tablerate_bestway').click();
           shippingMethod.save();
           $('checkout-step-shipping_method').hide();
         }
       }
   },

   nextStep: function(transport){
       if (transport && transport.responseText){
           try{
               response = eval('(' + transport.responseText + ')');
           }
           catch (e) {
               response = {};
           }
       }
       if (response.error){
           if ((typeof response.message) == 'string') {
               alert(response.message);
           } else {
               if (window.shippingRegionUpdater) {
                   shippingRegionUpdater.update();
               }
               alert(response.message.join("\n"));
           }

           return false;
       }

       checkout.setStepResponse(response);

       /*
       var updater = new Ajax.Updater(
           'checkout-shipping-method-load',
           this.methodsUrl,
           {method:'get', onSuccess: checkout.setShipping.bind(checkout)}
       );
       */
       //checkout.setShipping();
   }
}

// shipping method
var ShippingMethod = Class.create();
ShippingMethod.prototype = {
   initialize: function(form, saveUrl){
       this.form = form;
       this.saveUrl = saveUrl;
       this.validator = new Validation(this.form);
       this.onSave = this.nextStep.bindAsEventListener(this);
       this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
   },

   validate: function() {
       var methods = document.getElementsByName('shipping_method');
       if (methods.length==0) {
           alert(Translator.translate('Your order can not be completed at this time as there is no shipping methods available for it. Please make neccessary changes in your shipping address.'));
           return false;
       }

       if(!this.validator.validate()) {
           return false;
       }

       for (var i=0; i<methods.length; i++) {
           if (methods[i].checked) {
               return true;
           }
       }     

       alert(Translator.translate('Please specify shipping method.'));
       return false;
   },

   save: function(){

       if (checkout.loadWaiting!=false) return;
       if (this.validate()) {
           checkout.setLoadWaiting('shipping-method');
           var request = new Ajax.Request(
               this.saveUrl,
               {
                   method:'post',
                   onComplete: this.onComplete,
                   onSuccess: this.onSave,
                   onFailure: checkout.ajaxFailure.bind(checkout),
                   parameters: Form.serialize(this.form)
               }
           );
       }
   },

   resetLoadWaiting: function(transport){
       checkout.setLoadWaiting(false);
       payment.currentMethod = 'purchaseorder';
       payment.resetLoadWaiting();
       payment.save();
       $('checkout-step-payment').hide();
   },

   nextStep: function(transport){
       if (transport && transport.responseText){
           try{
               response = eval('(' + transport.responseText + ')');
           }
           catch (e) {
               response = {};
           }
       }

       if (response.error) {
           alert(response.message);
           return false;
       }

       if (response.update_section) {
           $('checkout-'+response.update_section.name+'-load').update(response.update_section.html);
           response.update_section.html.evalScripts();
       }

       $$('.cvv-what-is-this').each(function(element){
           Event.observe(element, 'click', toggleToolTip);
       });

       if (response.goto_section) {
           checkout.gotoSection(response.goto_section);
           checkout.reloadProgressBlock();
           return;
       }

       if (response.payment_methods_html) {
           $('checkout-payment-method-load').update(response.payment_methods_html);
       }

       checkout.setShippingMethod();
   }
}


// payment
var Payment = Class.create();
Payment.prototype = {
   initialize: function(form, saveUrl){
       this.form = form;
       this.saveUrl = saveUrl;
       this.onSave = this.nextStep.bindAsEventListener(this);
       this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
   },

   init : function () {
       var elements = Form.getElements(this.form);
       var method = null;
       for (var i=0; i<elements.length; i++) {
           if (elements[i].name=='payment[method]') {
               if (elements[i].checked) {
                   method = elements[i].value;
               }
           } else {
               elements[i].disabled = true;
           }
       }
       if (method) this.switchMethod(method);
   },

   switchMethod: function(method){
       if (this.currentMethod && $('payment_form_'+this.currentMethod)) {
           var form = $('payment_form_'+this.currentMethod);
           form.style.display = 'none';
           var elements = form.getElementsBySelector('input', 'select', 'textarea');
           for (var i=0; i<elements.length; i++) elements[i].disabled = true;
       }
       if ($('payment_form_'+method)){
           var form = $('payment_form_'+method);
           form.style.display = '';
           var elements = form.getElementsBySelector('input', 'select', 'textarea');
           for (var i=0; i<elements.length; i++) elements[i].disabled = false;
           this.currentMethod = method;
       }
   },

   validate: function() {
       var methods = document.getElementsByName('payment[method]');
       if (methods.length==0) {
           alert(Translator.translate('Your order can not be completed at this time as there is no payment methods available for it.'));
           return false;
       }
       for (var i=0; i<methods.length; i++) {
           if (methods[i].checked) {
               return true;
           }
       }
       alert(Translator.translate('Please specify payment method.'));
       return false;
   },

   save: function(){
       if (checkout.loadWaiting!=false) return;
       var validator = new Validation(this.form);
       if (this.validate() && validator.validate()) {
           checkout.setLoadWaiting('payment');
           var request = new Ajax.Request(
               this.saveUrl,
               {
                   method:'post',
                   onComplete: this.onComplete,
                   onSuccess: this.onSave,
                   onFailure: checkout.ajaxFailure.bind(checkout),
                   parameters: Form.serialize(this.form)
               }
           );
       }
   },

   resetLoadWaiting: function(){
       checkout.setLoadWaiting(false);
   },

   nextStep: function(transport){
       if (transport && transport.responseText){
           try{
               response = eval('(' + transport.responseText + ')');
           }
           catch (e) {
               response = {};
           }
       }
       /*
       * if there is an error in payment, need to show error message
       */
       if (response.error) {
           if (response.fields) {
               var fields = response.fields.split(',');
               for (var i=0;i<fields.length;i++) {
                   var field = null;
                   if (field = $(fields[i])) {
                       Validation.ajaxError(field, response.error);
                   }
               }
               return;
           }
           alert(response.error);
           return;
       }

       checkout.setStepResponse(response);

       //checkout.setPayment();
   }
}

var Review = Class.create();
Review.prototype = {
   initialize: function(saveUrl, successUrl, agreementsForm){
       this.saveUrl = saveUrl;
       this.successUrl = successUrl;
       this.agreementsForm = agreementsForm;
       this.onSave = this.nextStep.bindAsEventListener(this);
       this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
   },

   save: function(){
       if (checkout.loadWaiting!=false) return;
       checkout.setLoadWaiting('review');
       var params = Form.serialize(payment.form);
       if (this.agreementsForm) {
           params += '&'+Form.serialize(this.agreementsForm);
       }
       params.save = true;
       var request = new Ajax.Request(
           this.saveUrl,
           {
               method:'post',
               parameters:params,
               onComplete: this.onComplete,
               onSuccess: this.onSave,
               onFailure: checkout.ajaxFailure.bind(checkout)
           }
       );
   },

   resetLoadWaiting: function(transport){
       checkout.setLoadWaiting(false, this.isSuccess);
   },

   nextStep: function(transport){
       if (transport && transport.responseText) {
           try{
               response = eval('(' + transport.responseText + ')');
           }
           catch (e) {
               response = {};
           }
           if (response.redirect) {
               location.href = response.redirect;
               return;
           }
           if (response.success) {
               this.isSuccess = true;
               window.location=this.successUrl;
           }
           else{
               var msg = response.error_messages;
               if (typeof(msg)=='object') {
                   msg = msg.join("\n");
               }
               alert(msg);
           }
       }
   },

   isSuccess: false
}