function calculate (){
	var booktotal = 0;
	var totalprice = 0;
	var cookievalue = ' ';
	var bookid = '';

	$('#cart ul li.active').each(function(index) {
		id = $(this).attr('rel');
		amount = $(this).find('.amount').val();
		if(amount < 0 || amount == ''){
			amount = 0;
		}
		booktotal = eval(booktotal)+eval(amount);
		itemtotalprice = eval(books[id]['price'])*eval(amount);
		itemtotalprice = Math.round(itemtotalprice*Math.pow(10,2))/Math.pow(10,2);
		totalprice = eval(totalprice)+eval(itemtotalprice);
		$(this).find('.total').html('$'+itemtotalprice);
		
		if(amount <= 0 && amount != '') {
			$(this).removeClass('active').fadeOut('fast');
			$(this).find('.amount').val(0);
			
			if($('#cart ul li.active').length == 0) {
				$('#empty').show().addClass('active');
			}
		}
		
		if(amount != 0) {
			cookievalue += id+'-'+amount+'|';
			bookid += id+',';
		}
	});
	
	if(booktotal != 0) {
		$('#stage1').fadeIn('fast');
	}else{
		$('#stage1').fadeOut('fast');
	}
	
	$('#booktotal').html(booktotal);
	var promo = $('.promo').val();
	if(promo) {
		var promo = promo.toLowerCase();
	}
	totalprice = Math.round(totalprice*Math.pow(10,2))/Math.pow(10,2);

	if(promo == 'wholesale' || promo == "shluchim"){
		if(booktotal >= 5 && booktotal <= 9){
			percent = totalprice*.30;
			totalprice = totalprice-percent;
			$('.discount').fadeIn().find('#discount').html('- 30%');
		}else if(booktotal >= 10 && booktotal <= 19){
			percent = totalprice*.35;
			totalprice = totalprice-percent;
			$('.discount').fadeIn().find('#discount').html('- 35%');
		}else if(booktotal >= 20){
			percent = totalprice*.36;
			totalprice = totalprice-percent;
			$('.discount').fadeIn().find('#discount').html('- 36%');
		}else{
			$('.discount').fadeOut().find('#discount').html('0.00');
		}
	}else if(promo == 'chabad' && $('input[name="shipping"]').is(':checked') == false){
		percent = totalprice*.20;
		totalprice = totalprice-percent;
		$('.discount').fadeIn().find('#discount').html('- 20%');
	}else{
		$('.discount').fadeOut().find('#discount').html('0.00');
	}
	
	gifts = $('.gift:checked').size();
	if($('input[name="shipping"]:checked').val() == "same" || $('input[name="shipping"]:checked').val() == undefined) {
		var gift = (gifts*5)*booktotal;
	}else{
		var gift = gifts*5;
	}
	totalprice = totalprice+gift;
	$('#gift').html('$'+gift+'.00');
	
	if(totalprice != 0) {
		if($('input[name="shipping"]').is(':checked')) {
			var sameaddress = $('input[name="formsame"]:checked').size();
			if(sameaddress != 0) {
				sameaddress = eval(sameaddress)*2-2;
			}
			var shipping = ((booktotal-1)*4+4)-sameaddress;
			totalprice = totalprice+shipping;
			$('#giftsubtot').html('5.00');
		}else{
			var giftsubtot = 5*booktotal;
			$('#giftsubtot').html(giftsubtot+'.00');
			var shipping = (booktotal-1)*2+4;
			totalprice = totalprice+shipping;
		}
	}else{
		shipping = 0;
	}
	$('#bookids').val(bookid);
	$('#hidshipping').val(shipping);
	$('#hidgift').val(gift);
	$('#hidbooks').val(booktotal);
	
	$('#shipping').html('$'+shipping+'.00');
	totalprice = Math.round(totalprice*Math.pow(10,2))/Math.pow(10,2);	
	$('#total').html('$'+totalprice);
	$('#hidtotal').val(totalprice);
	setCookie('StoreOrder', cookievalue ,'10');
}
