var EasyCheckout = Class.create();
EasyCheckout.prototype = {
    initialize: function(form, saveUrl, successUrl, failureUrl){
    this.form = form;
		this.failureUrl = failureUrl;
		this.saveUrl = saveUrl;
		this.successUrl = successUrl;
		this.loadWaiting = false;
		this.onSave = this.nextStep.bindAsEventListener(this);
        this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
		
		
        if ($(this.form)) {
            $(this.form).observe('submit', function(event){this.save();Event.stop(event);}.bind(this));
        }
        
       
        //this.onShippingLoad = this.loadShippingSuccess.bindAsEventListener(this);
        //this.onSave = this.nextStep.bindAsEventListener(this);
        //this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
    },
	
	ajaxFailure: function(){
        location.href = this.failureUrl;
    },
	
	

    setLoadWaiting: function(isShow) {
    
		if (isShow) {           
			 Element.show('easycheckout-please-wait');
            this.loadWaiting  = true;		  			
        } else {
			Element.hide('easycheckout-please-wait');
			this.loadWaiting  = false;
        }
      
    },
	
	setStepResponse: function(response){
        
        if (response.redirect) {
            location.href = response.redirect;
            return true;
        }
        return false;
    },
	
	save: function(){
        if (easycheckout.loadWaiting!=false) return;
		
        var validator = new Validation(this.form);
        if (validator.validate()) {  
			
            easycheckout.setLoadWaiting(true);
			
			var params = Form.serialize(payment.form);
	        
	        params.save = true;
	        var request = new Ajax.Request(
	            this.saveUrl,
	            {
	                method:'post',
	                parameters:params,
	                onComplete: this.onComplete,
	                onSuccess: this.onSave,
	                onFailure: easycheckout.ajaxFailure.bind(easycheckout)
	            }
	        );
		}	   
    },

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

    nextStep: function(transport){
        easycheckout.setLoadWaiting(false);
		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
}


var EasyBilling = Class.create();
EasyBilling.prototype = {
    initialize: function(form, reloadShippingUrl, saveUrl,reloadOrderTotalUrl){
        this.form = form;
        if ($(this.form)) {
            $(this.form).observe('submit', function(event){this.save();Event.stop(event);}.bind(this));
        }
        this.reloadShippingUrl = reloadShippingUrl;
		this.reloadOrderTotalUrl = reloadOrderTotalUrl;
        this.saveUrl = saveUrl;
       
    },

	 loadShippingMethod: function(){
			var country_id;
			var postcode;
			var city;
			var region_id;
			var region;
			var url;
			
			city = $('billing:city').value;
			region_id = $('billing:region_id').value;
			country_id = $('billing:country_id').value;
			region = $('billing:region').value;
			postcode = $('billing:postcode').value;
		   
		   if($('billing:region_id').options.length > 0)
			{
				$('billing:region_id').selectedindex = 2;
			}
		   
		   url = this.reloadShippingUrl+"city/" + city + "/region_id/" + region_id + "/country_id/"+ country_id+  "/postcode/"+ postcode;
		   
		   
			new Ajax.Updater("method_container", url, {method: 'get', onFailure: ""});
		    
	}
	,

	reloadPasswordField: function()
	{		
		if($('checkout_method').checked)
		{
			$('customer_register').className = '';
			
		}
		else
		{
			$('customer_register').className = 'no-display';
		}
		
		
	}	
	,
	reloadOrderTotal: function()
	{
		
		var shipping_method;
		var url;
		var elements;
       elements = Form.getElements(this.form);
        for (var i=0; i<elements.length; i++) {
            if (elements[i].name=='shipping_method') {
                if (elements[i].checked) {
					shipping_method = elements[i].value;
                }
            } 
        }
		
		
		url = this.reloadOrderTotalUrl + "shipping_method/" +shipping_method  + '/';
		
		new Ajax.Updater("shopping-cart-totals", url, {method: 'get', onFailure: ""});
	}
}



var EasyPayment = Class.create();
EasyPayment.prototype = {
    initialize: function(form){
        this.form = form;       
    },

    init : function () {
        if ($(this.form)) {
           
        }
        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;
                }
            } 
        }
        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.select('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.select('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;
    }
}