$(document).ready(function() {
	
	$('div.Product.P3 div.Description, div.Product.P3 img, div.Product.P2 div.Description, div.Product.P2 img').css('opacity', 0);
	
	$('div.BestelFormulier div.Categorie span.Name').bind('click', function() {
		if ( $(this).parent().hasClass('Active') ) {
			// make inactive
			$(this).parent().removeClass('Active');
		} else {
			// make active
			$(this).parent().addClass('Active');
		}
		$(this).parent().children('div.Product').toggle('slow');
		$(this).parent().children('div.SubCategorie').toggle('slow');
	});
	
	$('div.BestelFormulier div.Categorie div.SubCategorie span.SubName').bind('click', function() {
		if ( $(this).parent().hasClass('Active') ) {
			// make inactive
			$(this).parent().removeClass('Active');
		} else {
			// make active
			$(this).parent().addClass('Active');
		}
		$(this).parent().children('div.Product').toggle('slow');
	});
	
	$('div.Default div.Products div.Categorie span.Name').bind('click', function() {
		if ( $(this).parent().hasClass('Active') ) {
			// make inactive
			$(this).parent().removeClass('Active');
		} else {
			// make active
			$(this).parent().addClass('Active');
		}
		$(this).parent().children('div.Product').toggle('slow');
		$(this).parent().children('div.SubCategorie').toggle('slow');
	});
	
	$('div.Default div.Products div.SubCategorie span.SubName').bind('click', function() {
		if ( $(this).parent().hasClass('Active') ) {
			// make inactive
			$(this).parent().removeClass('Active');
		} else {
			// make active
			$(this).parent().addClass('Active');
		}
		$(this).parent().children('div.Product').toggle('slow');
	});
	
	$('div.BestelFormulier div.Categorie input.Amount').bind('keyup', function() {
		CalculateLine($(this).parent());
		CalculateTotal();
	});
 
});

function CalculateLine(product) {
	var Amount = product.children('input.Amount').attr('value');
	var Price = product.children('input.RealPrice').attr('value');
	var Total = Bedrag( parseInt(Amount) * ( parseInt(Price) / 100 ) );
	var RealTotal = parseInt(Amount) * parseInt(Price);
	
	$.each($('input[name=' + product.children('input.Amount').attr('name') + ']'), function() {
		$(this).parent().children('input.Total').attr('value', Total);
		$(this).parent().children('input.RealTotal').attr('value', RealTotal);
		$(this).attr('value', Amount);
	});


}

function CalculateTotal() {
	var Total = 0;
	var BTW19 = 0;
	var BTW6 = 0;
	
	$('div.BestelFormulier div.Categorie div.Product input.RealTotal').each(function() {
		if($(this).parent().prevAll('span.Name').text() != 'Aanbiedingen') {
			var amount = parseInt($(this).attr('value'));
			//alert(amount);
			if (!isNaN(amount) ) {
				Total += amount;
				
				var btwTarief = parseInt($(this).parent().children('span.BTW').text());
				if  ( btwTarief == 6 ) {
					BTW6 += amount * 0.06;
				}else if ( btwTarief == 19 ) {
					BTW19 += amount * 0.19;
				} else {
					alert('Er is een onbekend btw tarief gevonden.. (' + btwTarief + ')');
				}			
				
			}
		}
	});
	Total /= 100;
	BTW19 /= 100;
	BTW6 /= 100;
	
	$('#Subtotal').attr('value', Bedrag(Total));
	$('#BTW19').attr('value', Bedrag(BTW19));
	$('#BTW6').attr('value', Bedrag(BTW6));
	$('#Total').attr('value', Bedrag ( Total + BTW6 + BTW19 ))
}

function Bedrag(num) {
	var EuroTeken = '€';
	num = num.toString().replace(/\|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + EuroTeken + ' ' + num + ',' + cents);
	}
