ProductOrderClass = new Class(
{	
	'init' : function()
	{
		if ($('tbl_product_order_count'))
		{
			$('tbl_product_order_count').addEvent('keyup', this.validateNumeric.bind(this));
			$('tbl_product_order_count').addEvent('keyup', this.setPrice.bind(this));
			$('tbl_product_order_count').addEvent('blur', this.validateNumeric.bind(this));
			$('tbl_product_order_count').addEvent('blur', this.setPrice.bind(this));
			$$('input[name=tbl_product_order_customer_is_member]').addEvent('change', this.setPrice.bind(this));
			$$('input[name=tbl_product_order_customer_is_member]').addEvent('click', this.setPrice.bind(this));
			$$('input[name=tbl_product_order_customer_is_member]').addEvent('keyup', this.setPrice.bind(this));
			this.setPrice();
		}
	},
	
	'validateNumeric' : function(oEvent)
	{
		
		var oInput = oEvent.target;
		mValue = oInput.value.toInt()
		if (isNaN(mValue) || mValue <= 0)
		{
			mValue = (oEvent.type == 'blur') ? 1 : '';
		}
		oInput.value = mValue;
	},
	
	'setPrice': function()
	{
		iCount = $('tbl_product_order_count').value.toInt();
		if (isNaN(iCount)) iCount = 1;

		sPrice = this.isMember() ? $('tbl_product_members_price').value : $('tbl_product_price').value;
		dPrice = sPrice.replace(',', '.').toFloat();
		dPrice *= iCount;
		aPrice = dPrice.toString().split('.');
		if (aPrice.length == 1) aPrice[1] = '';
		aPrice[1] += '00';
		aPrice[1] = aPrice[1].substr(0,2);
		sPrice = aPrice.join(',');
		$('price').innerHTML = '&euro; ' + sPrice;
	},
	
	'isMember' : function()
	{
		// Doesn't work in IE:
		// return $$('input[name=tbl_product_order_customer_is_member]:checked')[0].value.toInt();
		
		aOptions = $$('input[name=tbl_product_order_customer_is_member]');
		for (var i = 0; i < aOptions.length; i++)
		{
			if (aOptions[i].checked)
			{
				return aOptions[i].value.toInt();
			}
		}
	}
	
});


var ProductOrder;

function initProductOrder()
{
	ProductOrder = new ProductOrderClass();
	ProductOrder.init();
}

window.addEvent('domready', initProductOrder);