

var FastCheckout = Class.create();

FastCheckout.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));
        }
               
    },
	
	ajaxFailure: function(){
       alert('Fail to refill. Please check form again');
    },
	
    setLoadWaiting: function(isShow) {
    
		if (isShow) {           
			 Element.show('fastcheckout-please-wait');
            this.loadWaiting  = true;		  			
        } else {
			Element.hide('fastcheckout-please-wait');
			this.loadWaiting  = false;
        }
      
    },
	
	setStepResponse: function(response){
        
        if (response.redirect) {
            location.href = response.redirect;
            return true;
        }
        return false;
    },
	
	save: function(){
        if (fastcheckout.loadWaiting!=false){
			return;	
		} 
        var validator = new Validation(this.form);
        if (validator.validate()) {  
            fastcheckout.setLoadWaiting(true);
			var params = Form.serialize(this.form);
	        params.save = true;
	        var request = new Ajax.Request(
	            this.saveUrl,
	            {
	                method:'post',
	                parameters:params,
	                onComplete: this.onComplete,
	                onSuccess: this.onSave,
	                onFailure: fastcheckout.ajaxFailure.bind(fastcheckout)
	            }
	        );
		}	   
    },

    resetLoadWaiting: function(transport){
        fastcheckout.setLoadWaiting(false);
    },

    nextStep: function(transport){
        fastcheckout.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 FastBilling = Class.create();
FastBilling.prototype = {
    initialize: function(form, reloadShippingUrl, saveUrl,reloadPasswordFieldUrl){
        this.form = form;
        if ($(this.form)) {
            $(this.form).observe('submit', function(event){this.save();Event.stop(event);}.bind(this));
        }
        //this.reloadShippingUrl = reloadShippingUrl;
		//this.reloadPasswordFieldUrl = reloadPasswordFieldUrl;
        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;
		   
		   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='fast_no_display';
		}
	}	
	
}



var FastPayment = Class.create();
FastPayment.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;
    }

   

    
}


   


