$(function() {
	$("#cart tr .remove input").click(function() {
		var orderCode = $(this).val();
		$.ajax({
			type: "GET",
			url: "bdw-includes/plugins/cart/cart_action.php",
			data: "remove[]=" + orderCode,
			success: function() {
				$("#cart tr .remove input[value=" + orderCode + "]").parent().parent().fadeOut(500, function() {
					$(this).remove();
					if( $('#cartType').val() == 1 ){
						calcPriceCart();
					}else{
						calcPrice( $("#pay_type_table input[name='pay_type']:radio").val() );
					}
					
				});
			},
			error: function() {
				window.location("cart_action.php?remove[]="+orderCode);
			}
		});
	});
	
	$("#cart tr .quantity input").change(function() {
		var orderCode = $(this).attr("name").slice(9, -1);
		var quantity = $(this).val();
		$.ajax({
			type: "GET",
			url: "bdw-includes/plugins/cart/cart_action.php",
			data: "quantity[" + orderCode + "]=" + quantity,
			success: function() {
				var startColor = $("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().hasClass("odd") ? "#eee" : "#fff";
				$("#cart tr .quantity input[name*=" + orderCode + "]").parent().parent().find("td").animate({ backgroundColor: "#ff8" }, 100).animate({ backgroundColor: startColor }, 800);
				
				if( $('#cartType').val() == 1 ){
					calcPriceCart();
				}else{
					calcPrice( $("#pay_type_table input[name='pay_type']:radio").val() );
				}
			},
			error: function() {
				window.location("cart_action.php?quantity[" + orderCode + "]=" + quantity);
			}
		});
	});
	
	$("#pay_type_table input[name='pay_type']:radio").change(function() {
		calcPrice( $(this).val() );
	});
	

	$("#kedvezmeny").change(function() {
		calcPrice( $("#pay_type_table input[name='pay_type']:radio").val() );
	});
	
});
function numberFormat(nStr){
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1))
    x1 = x1.replace(rgx, '$1' + '.' + '$2');
  return x1 + x2;
}

function calcPrice( type ) {
	var totalPrice  = 0;
	var akciosPrice = 0;
	var plus_price  = 0;
		
	
	
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unit_price", this).text();
		var akcios = $(".akcios", this).val();
		if($(".item_size", this).text()=="n/a"){
			var itemSize = 1;
		} else{
			var itemSize = $(".item_size", this).text();
		}
		var extendedPrice = quantity*unitPrice*itemSize;
		
		if(akcios == 0){
			totalPrice += extendedPrice;
		}else{
			akciosPrice += extendedPrice;
		}
		
		
		
		if( type == 1 ){
			if( totalPrice > parseInt( $('#tt_1_up_to').val() ) ){
				plus_price = 0;
			} else{
				plus_price = parseInt( $('#tt_1_price').val() );
			}
		}else if( type == 2 ){
			if( totalPrice > parseInt( $('#tt_2_up_to').val() ) ){
				plus_price = 0;
			} else{
				plus_price = parseInt( $('#tt_2_price').val() );
			}
		}else if( type == 3 ){
			if( totalPrice > parseInt( $('#tt_3_up_to').val() ) ){
				plus_price = 0;
			} else{
				plus_price = parseInt( $('#tt_3_price').val() );
			}
		}
		
		if( totalPrice + plus_price + akciosPrice < 5000 ){
			$("#kedvezmeny").attr("disabled", true);
		} else{
			$("#kedvezmeny").removeAttr("disabled");
		}
		
		totalPrice += plus_price;
		
		
		
		$(".extended_price", this).html(extendedPrice + " Ft");
		//$(".total_price").html( $('#tot_price').val() + " Ft");
		$("#plus_price").val(plus_price);
		
		
		
		
	});
	if( $('#kedvezmeny').is(':checked') ){
		totalPrice = totalPrice * 0.9;
		totalPrice=parseFloat(totalPrice);
		totalPrice=Math.round(totalPrice);
	}
	
	$('#tot_price').val(totalPrice+akciosPrice);
	$('#veg_osszeg').html( numberFormat(totalPrice+akciosPrice) + ' Ft');
	
	if ( totalPrice == 0 ) {
		
		$("#cart").parent().replaceWith("<p class='center'>few</p>");
		$(".total_price").html( '0' );
		$('#rendeles_elkuldes').attr('disabled', true)
		$('#tot_price').val(0);
	}
}

function calcPriceCart() {
	var totalPrice  = 0;
	var akciosPrice = 0;
	var plus_price  = 0;
		
	
	
	$("#cart tr .quantity").parent().each(function() {
		var quantity = $(".quantity input", this).val();
		var unitPrice = $(".unit_price", this).text();
		var akcios = $(".akcios", this).val();
		if($(".item_size", this).text()=="n/a"){
			var itemSize = 1;
		} else{
			var itemSize = $(".item_size", this).text();
		}
		var extendedPrice = quantity*unitPrice*itemSize;
		
		if(akcios == 0){
			totalPrice += extendedPrice;
		}else{
			akciosPrice += extendedPrice;
		}	
		
		
		$(".extended_price", this).html(extendedPrice + " Ft");
		$(".total_price").html( numberFormat(totalPrice) + " Ft");
		$("#plus_price").val(plus_price);
		
	});
	

	
	if ( totalPrice == 0 ) {
		
		$("#cart").parent().replaceWith("<p class='center'>few</p>");
		$(".total_price").html( '0' );
		$('#rendeles_elkuldes').attr('disabled', true)
		$('#tot_price').val(0);
	}
}

