////////FOLK ART MUSEUM - shop cart update
$(document).ready(function(){
		//////////////////HIDE message box
		$('.cart_update_message_tag').css('opacity','0.0');
		
		//////////////////type only numbers on fields
		$('.quantity input').keypress(function(event) {
				if (event.which && (event.which < 48 || event.which > 57)) {event.preventDefault();}
		});
		
		
		//////////////////CART UPDATE
		$('.update_cart_button').click(function(){
					//////UPDATE NUMBERS
					var totalQuantity = 0;
					var totalCost = 0;
					$('#cart tbody tr').each(function() {
						  var price = parseFloat($('.price', this).text().replace(/^[^\d.]*/, ''));
						  price = isNaN(price) ? 0 : price;
						  var quantity = parseInt($('.quantity input', this).val(), 10);
						  quantity = isNaN(quantity) ? 0 : quantity;         
						  var cost = quantity * price;
						  $('.cost', this).text('$' + cost.toFixed(2));
						  totalQuantity += quantity;
						  totalCost += cost;
					});
					
					
					//////SUBTOTAL CHANGE
					$('.subtotal .cost').text('$' + totalCost.toFixed(2));
					//var taxRate = parseFloat($('.tax .price').text()) / 100;
					//var tax = Math.ceil(totalCost * taxRate * 100) / 100;
					//$('.tax .cost').text('$' + tax.toFixed(2));
					//totalCost += tax;
					//$('.shipping .quantity').text(String(totalQuantity));
					//var shippingRate = parseFloat($('.shipping .price').text().replace(/^[^\d.]*/, ''));
					//var shipping = totalQuantity * shippingRate;
					//$('.shipping .cost').text('$' + shipping.toFixed(2));
					//totalCost += shipping;
					$('.total .cost').text('$' + totalCost.toFixed(2));
	
					
					////////SEND AJAX
					var tVal = $('#submit_cart').serialize();
					//alert('serial= '+tVal);
					$.ajax({target:'#submit_cart',
							type: 'POST',
							url:'sites/folk/folk_cartshow.php?p=folk&action=update',
							data: tVal,
							success: function(){
								window.open('index.php?p=folk&id=4558','_self');
        						return false;
							}
							//success: function(msg){alert( "Data Saved: " + msg );}
					 });
					
					
					//////UPDATE MESSAGE
					$('.cart_update_message_tag').animate({opacity: '0.8'},300).animate({opacity: '0.9'},1800).animate({opacity: '0.0'},200)
					return false;
		});
		
		
		
		
		
		/////////////CART DELETE
		$('.cart_button_delete').click(function(){
					/////////DELETE ITEM
					$(this).parents('.cartRow').find('.quantity input').val(0).trigger('change').end().hide();
					
					
					/////////SEND AJAX
					var tVal = $('#submit_cart').serialize();
					$.ajax({target:'#submit_cart',
							type: 'POST',
							url:'sites/folk/folk_cartshow.php?p=folk&action=remove&',
							data: tVal,
							success: function(){
								window.open('index.php?p=folk&id=4558','_self');
        						return false;
							}
					});
					
					
					//////UPDATE NUMBERS
					var totalQuantity = 0;
					var totalCost = 0;
					$('#cart tbody tr').each(function(){
							var price = parseFloat($('.price', this).text().replace(/^[^\d.]*/, ''));
							price = isNaN(price) ? 0 : price;
							var quantity = parseInt($('.quantity input', this).val(), 10);
							quantity = isNaN(quantity) ? 0 : quantity;         
							var cost = quantity * price;
							$('.cost', this).text('$' + cost.toFixed(2));
							totalQuantity += quantity;
							totalCost += cost;
					});
					
					
					//////SUBTOTAL CHANGE
					$('.subtotal .cost').text('$' + totalCost.toFixed(2));
					//var taxRate = parseFloat($('.tax .price').text()) / 100;
					//var tax = Math.ceil(totalCost * taxRate * 100) / 100;
					//$('.tax .cost').text('$' + tax.toFixed(2));
					//totalCost += tax;
					//$('.shipping .quantity').text(String(totalQuantity));
					//var shippingRate = parseFloat($('.shipping .price').text().replace(/^[^\d.]*/, ''));
					//var shipping = totalQuantity * shippingRate;
					//$('.shipping .cost').text('$' + shipping.toFixed(2));
					//totalCost += shipping;
					$('.total .cost').text('$' + totalCost.toFixed(2));
					
					
					//////UPDATE MESSAGE
					$('.cart_update_message_tag').animate({opacity: '0.8'},300).animate({opacity: '0.9'},1800).animate({opacity: '0.0'},200);
		});
		
		
		
});///////////////////////////


