var productBuilder = Class.create(
{
	initialize: function(obj)
	{
		var self = this;
		this.obj = obj;
		this.allLi = $$(this.obj.container+' li');
		this.orgPrice = parseFloat($(this.obj.orgPrice).value);
		this.orderSum = 0;
		this.setObservers();
	},
	setObservers: function(){
		
		var self =  this;
		
		this.allLi.each(function(elm,i){
			$($(elm).getElementsByTagName('input')[1]).observe('click',function(){
				self.calculateOrderSum();
			});
		});
	},
	calculateOrderSum: function(){
		var self = this;
		this.orderSum = 0;
		this.allLi.each(function(elm,i){
			if($(elm).getElementsByTagName('input')[1].checked){
				self.orderSum = (parseFloat(self.orderSum) + parseFloat($(elm).getElementsByTagName('input')[0].value));
			}
		});
		
		var price = this.numberFormat((self.orderSum+self.orgPrice),2, ',', '.');
		
		if($(this.obj.getmva).value == 'false'){
			$(this.obj.price).update(price+' eks.mva');
			$(this.obj.price2).update(price+' eks.mva');
		}else{
			$(this.obj.price).update(price+' ink.mva');
			$(this.obj.price2).update(price+' ink.mva');
		}
	},
	numberFormat: function(n,c,d,t){
		c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
  		if((c ? d + Math.abs(n - i).toFixed(c).slice(2) : "") == ',00'){
  			return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + ',-';
 		}else{
			return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
 		}
 	}
});

Event.observe(window, 'load', function()
{
	if($('edit'))
	{
		new productBuilder(
		{
			container: '#edit',
			orgPrice: 'orgPrice',
			price: 'price',
			price2 : 'priceTag2',
			getmva : 'getmva'
		});
	}
	
});
