function initCalculator(){
	var calculator = $('#order_summary_tbody');
	var init = calculator.attr('init');
	if (init != 'true') {			
		calculator.attr('init', true);
		calculator.html("");	
	}		
	}

	function updateQuantity(obj, item_id, title, price, bottle_count, item_index) {
		initCalculator();
		list_item_id = '#' + item_id.split('_').slice(0, 2).join('_');
		quantity = obj.value.replace(/^[0]+/, '');
		if (quantity > 0){
			total = price * quantity;
			// create shopping cart HTML
			var lineItem = "<tr id='line_" + item_id + "' class='line_item' ><td class='pi_package'>" + title + " <span class='tablenote'>("+ bottle_count + " bottles)</span>" + "</td><td class='pi_price'>$" + price + "</td><td class='pi_quantity'>" + quantity + "</td><td class='pi_total'>$" + total + "</td></tr>";
			// create form HTML
			var	formItem = "<input id='field_for_" + item_id + "' name='field_for_" + item_id + "' class='item_field' type='hidden' value='" + title + " ("+ bottle_count + " bottles);" + price + ";" + quantity + ";" + total + "' >"
			var calculator = $('#order_summary_tbody');
			var items = $('.line_item');
			var orderForm = $('#order_form');
			// see if the line item exists in the cart
			// if it doesn't create it
			var exists = ($('#line_' + item_id).attr('id') || false);
			if (exists == false){
				calculator.append( lineItem );
				list_item = $(list_item_id);
				count = (list_item.attr('count') || 0);
				count++;
				list_item.attr('count', count);
				list_item.addClass('selected_item');	
				
				$(obj).addClass('selected_package');
			}
			// if it does exist replace it
			else {
				$('#line_' + item_id).replaceWith(lineItem);
				list_item = $(list_item_id);
				count = list_item.attr('count');
				count++;
				list_item.attr('count', count);		
			}
			
			// see if the form item exists in the cart
			// if it doesn't create it
			var field_exists = ($('#field_for_' + item_id).attr('id') || false);
			if (field_exists == false){
				orderForm.append( formItem );

			}
			// if it does exist replace it
			else {
				$('#field_for_' + item_id).replaceWith(formItem);	
			}
			
				
		}
		else {
				
				$('#line_' + item_id).remove();
				$('#field_for_' + item_id).remove();
				
				list_item = $(list_item_id);
				count = (list_item.attr('count') || 0);
			
				if( $(obj).hasClass('selected_package')){
				if(count > 0){count -= 1;}
				list_item.attr('count', count);
				}
							
				if(count == 0 ){
					list_item.removeClass('selected_item');
				}	
				
				$(obj).removeClass('selected_package');
		}
		 calculateTotal();			
	}			
	
	function calculateTotal() {
		var items = $('.line_item');
		var total = 0;
		var totalDisplay = $('#order_total');
		var totalField = $('#fields_order_total')
		var orderForm = $('#order_form');
		
		if (items.length > 0) {
			items.each(function( ){
				var line_item = $(this);
				var line_item_total = $(this).find(".pi_total").html();
				line_item_total = parseInt(line_item_total.replace(/\D/g, ''));						
				total += line_item_total;
				line_item_total
			});
		}
		totalDisplay.html('$' + total);	
		totalField.val(total);
	}
	
	function removeFromCalculator(id) {
		var idString = '#' + id;
		$(idString).remove();
		calculateTotal();
	}
	
	
