
Hedera.Checkout = new Class
({
	Extends: Hedera.Form

	,initialize: function (props)
	{
		this.today = new Date ();
		this.today.setHours (0, 0, 0, 0);
		this.parent (props);
	}

	,activate: function ()
	{
		this.autoStepLocked = true;
	}

	,onValuesReady: function ()
	{
		var orderForm = this.$.orderForm;
		var defaultsForm = this.$.defaults;

		if (!(orderForm.ready && defaultsForm.ready))
			return;

		var date;

		if (orderForm.numRows > 0)
		{
			var i = orderForm;
			date = i.get ('date_send');
		}
		else
			var i = defaultsForm;
		
		if (!date || date.getTime () < (new Date ()).getTime ())
		{
			date = new Date ();

			if (i.get('delivery_method') != 'PICKUP')
				date.setDate (date.getDate () + 1);

			date.setHours (0, 0, 0, 0);
		}
		
		this.$.lot.assign ({
			date: date,
			method: i.get ('delivery_method'),
			agency: i.get ('agency_id'),
			address: i.get ('address_id')
		});
		this.autoStepLocked = false;
	}
	
	,disableButtons: function (disable)
	{
		this.$.confirmAgency.disabled = disable;
		this.$.confirmDelivery.disabled = disable;
		this.$.confirmPickup.disabled = disable;
	}

	,onConfirmClick: function ()
	{
		this.disableButtons (true);
	
		var query = 'CALL basketConfigure (#date, #method, #agency, #address)';	
		this.conn.execQuery (query,
			this.onBasketConfigured.bind (this), this.$.lot.params);
	}

	,onBasketConfigured: function (resultSet)
	{
		this.disableButtons (false);

		if (!resultSet.fetchResult ())
			return;
	
		if (this.$.orderForm.numRows > 0)
			Htk.Toast.showMessage (_('OrderUpdated'));
		else
			Htk.Toast.showMessage (_('OrderStarted'));

		this.hash.setAll ({form: 'ecomerce/catalog'});
	}

	,onCancelClick: function ()
	{
		if (this.$.orderForm.numRows > 0)
			window.history.back();
		else
			this.hash.setAll ({'form': 'ecomerce/orders'});
	}
	
	,agencySteps: ['method', 'date', 'address', 'agency', 'confirm-agency']
	,deliverySteps: ['method', 'date', 'address', null, 'confirm-delivery']
	,pickupSteps: ['method', 'date', 'address', 'pickup', 'confirm-pickup']
	
	,stepFunc: function (stepIndex)
	{
		var steps;
		var isDelivery;
	
		switch (this.$.rgMethod.value)
		{
			case 'AGENCY':
				steps = this.agencySteps;
				isDelivery = true;
				break;
			case 'DELIVERY':
				steps = this.deliverySteps;
				isDelivery = true;
				break;
			case 'PICKUP':
			default:
				steps = this.pickupSteps;
				isDelivery = false;
				break;
		}

		var stepId = steps[stepIndex];
		
		if (!stepId)
			return null;

		switch (stepId)
		{
			case 'date':
				Vn.Node.setText (this.$.dateQuestion, isDelivery ?
					_('OrderDateDeliveryQuestion'):
					_('OrderDatePickupQuestion'));
				this.$.calendar.goToSelectedMonth ();
				break;
			case 'agency':
			case 'pickup':
				this.$.agencies.refresh ();
				break;
		}
		
		return this.$[stepId +'Step'];
	}
	
	,onFieldChange: function ()
	{
		if (!this.autoStepLocked)
			setTimeout (this.goNextStep.bind (this), 75);
	}
	
	,goNextStep: function ()
	{
		this.$.assistant.moveNext ();
	}
	
	,addressRenderer: function (builder, form)
	{
		builder.$.address.addEventListener ('click',
			this.onAddressClick.bind (this, form.get ('id')));
	}
	
	,onAddressClick: function (addressId)
	{
		this.$.lot.set ('address', addressId);
		this.goNextStep ();
	}
	
	,onAddressChange: function ()
	{
		if (this.selectedNode)
			Vn.Node.removeClass (this.selectedNode, 'selected');

		var row = this.$.addresses.search ('id', this.$.lot.get ('address'));

		if (row != -1)
		{
			var builder = this.$.repeater.getScope (row);
		
			this.selectedNode = builder.$.address;
			Vn.Node.addClass (this.selectedNode, 'selected');
		}

		this.$.addressForm.row = row;
	}
	
	,onAgenciesReady: function (model)
	{
		if (model.ready && model.numRows == 0)
			Htk.Toast.showError (_('NoAgeciesAvailableForDate'));
	}
	
	,onWarehousesReady: function (model)
	{
		if (model.ready && model.numRows == 0)
			Htk.Toast.showError (_('NoWarehousesAvailableForDate'));
	}
	
	,calendarRestrict: function (date)
	{
		return date.getTime () >= this.today.getTime ();
	}
});

