	/* Total quantities on price tables */

	var WebFu = window.WebFu || {}

	WebFu.site = function() {
		
		var body = '';

		return {

			initialize: function() {

				WebFu.site.attach_to_tables();
				WebFu.site.reset_page();
				
			},
			
			attach_to_tables: function() {
				
				tables = $A(document.getElementsByClassName('price-list'));
				
				tables.each(function(table) {
					button = $('total-items');
					
					if(button) {
						Event.observe(button, 'click', WebFu.site.total_items);
					}

				});

			},
			
			total_items: function(e) {
				
				Event.stop(e);

				var qty, price, total_price;
				qty = 0;
				total_price = 0;

				qty_fields = $A(document.getElementsByClassName('qty'));
				
				qty_fields.each(function(field) {
					if(parseInt(field.value)) {

						qty += parseInt(field.value);						
						field_parent = field.parentNode;
						price = $A(document.getElementsByClassName('price', field_parent));
	
						total_price += parseFloat(price[0].value) * parseInt(field.value);

					}
				});
				
				$('book-qty').innerHTML = qty;
				$('price-of-books').innerHTML = '$' + format_number(total_price, 2);
				total_price = 0;
				
			},
			
			reset_page: function() {
				document.body.style.display = 'block';
			}

		}

	}();

	Event.observe(window, 'load', WebFu.site.initialize);

	function format_number(pnumber,decimals){
		if (isNaN(pnumber)) { return 0};
		if (pnumber=='') { return 0};
	
		var snum = new String(pnumber);
		var sec = snum.split('.');
		var whole = parseFloat(sec[0]);
		var result = '';
	
		if(sec.length > 1){
			var dec = new String(sec[1]);
			dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
			dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
			var dot = dec.indexOf('.');
			if(dot == -1){
				dec += '.'; 
				dot = dec.indexOf('.');
			}
			while(dec.length <= dot + decimals) { dec += '0'; }
			result = dec;
		} else{
			var dot;
			var dec = new String(whole);
			dec += '.';
			dot = dec.indexOf('.');		
			while(dec.length <= dot + decimals) { dec += '0'; }
			result = dec;
		}	
		return result;
	}

//	document.write('<style type="text/css"> body {display: none;} </style>');