
var bigtotal = 0.00;
var totalr = new Array();
var qtyr = new Array();

function update(  ) {
	bigtotal = 0.00;
	totalr[1] = 0.00;
	totalr[2] = 0.00;
	totalr[3] = 0.00;
	qtyr[1] = 0;
	qtyr[2] = 0;
	qtyr[3] = 0;
	
	
	$('#calcul tr').each(function(){
		var eltr = this;
		
		// skip title rows
		if( eltr.className != 'performance' ) {
			return null;
		}
		
		// for each row
		// calculate 3 price
		var i;
		var rowtotal = 0.00;
		for( i=1; i<=3; i++ ) {			
			var iqty = $(this).find('.qty'+i).get(0);
			var elprice = $(this).find('.price'+i).get(0);
			var eltotal = $(this).find('.total'+i).get(0);

			// skip if no qty field
			if( ! iqty ) break;

			// update row total
			if( iqty.value && iqty.value > 0 ) {

				var amount = parseFloat( $(elprice).html() );
				var qty = parseInt( $(iqty).val() );
				var tot = amount * qty;
				$(eltotal).html( show_price(tot) );
				
				totalr[i] += tot;
				qtyr[i] += qty;
				rowtotal += tot;
				bigtotal += tot;

			} else {

				// empty total
				$(eltotal).html( '' );

			}

		}
		
		// update row totals
		$(this).find('.rowtotal').html( show_price(rowtotal) );
		
	});
	
	// update selection totals
	$('#qty_col1').html( qtyr[1] );
	$('#qty_col2').html( qtyr[2] );
	$('#qty_col3').html( qtyr[3] );
	$('#total_selection').html( show_price(bigtotal) );

	// update abonnement+
	var plus_fee =  parseInt( $('#abon_plus_qty').val() ) * 5;
	$('#abon_plus').val( show_price( plus_fee , true ) );
	if( plus_fee ) bigtotal += plus_fee;
	
	// update total payement (before family section)
	$('#total_selection_fee').val( show_price( bigtotal , true ) );
	
	// calcul family fee
	var qtyf = 0;
	var totalf = 0.00;
	$('#calculf tr').each(function(){
		var eltr = this;
		
		// for each row
		// calculate qty x price
		var iqty = $(this).find('.qty').get(0);
		var amount = $(this).attr('price');
		var eltotal = $(this).find('.total').get(0);

		// skip if no qty field
		if( ! iqty ) return null;

		// update row total
		if( iqty.value && iqty.value > 0 ) {

			var qty = parseInt( $(iqty).val() );
			var tot = amount * qty;
			$(eltotal).html( show_price(tot) );
			
			qtyf += qty;
			totalf += tot;

		} else {

			// empty total
			$(eltotal).html( '' );

		}
	});
		
	// update family selection total
	$('#total_fselection_fee').val( show_price(totalf,true) );
	if( totalf ) bigtotal += totalf;
				
	// update postal fee
	var postal_fee = $('#envoi_poste').attr('checked') ? 4 : 0;
	if( postal_fee ) bigtotal += postal_fee;
	
	// show bigtotal
	$('#bigtotal').html( show_price(bigtotal) );
}

function show_price( price , amount_only ) {
	str = '';
	if( price > 0 ) {
		str = price.toFixed(2);
		if( ! amount_only ) {
			str += '&nbsp;$';
		}
		return str;
	} else {
		return '';
	}
}

function add_one( el ) {
	var el = el;
	var iqty = $(el).parent().parent().find('.qty').get(0);
	var icheck = $(el).parent().parent().find('.check').get(0);
	if( ! iqty.value ) {
		iqty.value = 1;
		$(icheck).attr('checked',1);
	} else {
		iqty.value = parseInt(iqty.value)+1;
	}
	update();
}

