/* $Id$ */

var Order = {

	run: function()
	{
		this.delivery_addrress();
		this.quantity_change();
		this.code();
		this.credit_cards();
		this.payment_method();
	},

	delivery_addrress: function()
	{
		var checkbox = $$('.order form input.checkbox[name="delivery_address"]').first();
		var box = $$('.order form fieldset.delivery_address').first();

		if (checkbox && box) {
			if (!checkbox.checked) {
				box.hide();
			}
			checkbox.observe('click', function(event) {
				if (checkbox.checked) {
					box.show();
				} else {
					box.hide();
				}
			});
		}
	},

	quantity_change: function()
	{
		var quantity = $$('.order form input.text[name="quantity"]').first();
		var code = $$('.order form input.text[name="code"]').first();
		var price = $$('.order form li.price').first();

		if (quantity && code && price) {
			quantity.observe('change', function(event) {
				new Ajax.Request(location.href, {
					parameters: {
						plugin: 'order',
						mode: 'ChangePrice',
						quantity: quantity.value,
						code: code.value
					},
					onSuccess: function(transport) {
						price.innerHTML = transport.responseText;
					}
				});
			});
		}
	},

	code: function()
	{
		var quantity = $$('.order form input.text[name="quantity"]').first();
		var code = $$('.order form input.text[name="code"]').first();
		var price = $$('.order form li.price').first();
		var button = code.next('input.code_check');

		if (quantity && code && price && button) {
			button.observe('click', function(event) {
				new Ajax.Request(location.href, {
					parameters: {
						plugin: 'order',
						mode: 'ChangePrice',
						quantity: quantity.value,
						code: code.value
					},
					onSuccess: function(transport) {
						price.innerHTML = transport.responseText;
					}
				});
			});
		}
	},

	credit_cards: function()
	{
		var box_tmp = $$('.credit_card_select').first().hide();
		var box = $$('.order form fieldset.credit_card_select').first();
		if (box) {
			$$('.order form .payment_method input[type="radio"]').each(function(input) {
				input.observe('click', function(event) {
					if (input.value != 2) {
						box.hide();
					} else {
						box.show();
					}
				});
			});
		}
	},

	payment_method: function()
	{
		var box = $$('.order form fieldset.take').first();
		if (box) {
			$$('.order form .payment_method input[type="radio"]').each(function(input) {
				input.observe('click', function(event) {
					if (input.value == 1) {
						box.hide();
					} else {
						box.show();
					}
				});
			});
		}
	}

}
new StartUp(Order);
