/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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

   
    save: function(){
        alert(this.saveUrl);
        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');

//            if ($('billing:use_for_shipping') && $('billing:use_for_shipping').checked) {
//                $('billing:use_for_shipping').value=1;
//            }

            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)
                }
            );
        }
    },

   

    /**
        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;
        }
alert(response+ "dddddddddd");
        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();
    }
}


