$(document).ready(function() {

/* This is for the add to cart function on the category page */

	
	$('.addtocart').bind('click', function() {
		var theid =$(this).attr('id');
  		$(this).addClass('added');
  		$.post("/pages/components/include_checkout_buttons.php", "price_id="+ theid +"&cartaction=add&qty=1", function(data) {					
		$('.headerButtons').html(data);
		$("#cartCount").css("backgroundColor","#f88015");
		
		/*
$('#cartCount').fadeOut('slow', function() {
   			$("#cartCount").css("backgroundColor","#f88015");
  			}); 
*/	
		});
		
	});

	
	$('.addtocart_product').bind('click', function() {
		var theid = "price_"+$('#price_id').val();
		var theqty = $('#quantity').val();
  		$(this).addClass('added');
  		$.post("/pages/components/include_checkout_buttons.php", "price_id="+ theid +"&cartaction=add&qty="+ theqty, function(data) {					
		$('.headerButtons').html(data);	
		$("#view_shop_bag").show();	
		
		});
  		
	});
	
	
	
	
});

	
function add_quantity(id,price) {
	var qTest = qtyCheck();
	if(qTest > 9){
		$("#shopping-error").show();
	}
	else{
		newQty = parseFloat($("#"+id).val()) + 1;
		$.post("/pages/components/include_checkout_buttons.php", "cartaction=add&price_id="+id+"&qty="+newQty, function(data) {
			$(".headerButtons").html(data);		
		});
		var newWeight =  parseFloat($("#freight_weight").val()) + 689;
		$("#freight_weight").val(newWeight);
		thisSubtotal = newQty * parseFloat(price);
		$("#"+id).val(newQty);
		$("#"+id +"_subtotal").html("$"+ CurrencyFormatted(thisSubtotal));	
		if($("#shipping").val() !=""){freightCalc();}
	}
	
		
}



function decrease_quantity(id,price) {
	newQty = parseFloat($("#"+id).val()) - 1;
	if(newQty < 0){newQty=0;}
	$.post("/pages/components/include_checkout_buttons.php", "cartaction=add&price_id="+id+"&qty="+newQty, function(data) {
			$(".headerButtons").html(data);		
		});
	if(newQty > 0){	
	var newWeight =  parseFloat($("#freight_weight").val()) - 689;
		$("#freight_weight").val(newWeight);
		
	}		
	thisSubtotal = newQty * parseFloat(price);
	$("#"+id).val(newQty);	
	$("#"+id +"_subtotal").html("$"+ CurrencyFormatted(thisSubtotal));	
	if($("#shipping").val() !=""){freightCalc();}

}

function qtyCheck(){
	myQty = 0;
	$(".quantity").each(function() {
		myQty += parseInt(this.value);
	});
	return myQty;
	
}

function removeItem(id) {
	
	if(confirm("Remove Item from cart?")){
		$.post("/pages/components/include_checkout_buttons.php", "action=remove&price_id="+id, function(data) {
				$(".headerButtons").html(data);	
				$(location).attr('href','/shopping-bag.php');	
			});
	}
}


function freightCalc() {	
	
	var theCountry = $("#shipping").val();
	var totalWeight = $("#freight_weight").val();
	var subTotal = parseFloat($("#txtAmount").val());
	$.post("/includes/check_freight.php", "country="+$("#shipping").val()+"&totalWeight="+totalWeight, function(data) {
		$("#show_freight_cost").html("$"+CurrencyFormatted(data));
		$("#freight_cost").val(data);
		var postage = $("#freight_cost").val(data);
		subTotal = subTotal + parseFloat($("#freight_cost").val(data));
		$("#txtAmount").val(subTotal);
		$("#show_grandtotal").html("$"+CurrencyFormatted(subTotal));
		$(location).attr('href','/shopping-bag.php');
		
		
	});
	
}	


function checkBillCountry() {	
	
	var theCountry = $("#b_country").val();

	if(theCountry !="Australia"){
		$("#gstMsgShow").hide();
		$("#gstShow").hide();	
	}
	else{
		$("#gstMsgShow").show();
		$("#gstShow").show();
	}
	
}


function checkOut() {
	if($("#shipping").val()==""){
		$("#shipping_error").show();
		$("#bag-error").show();
	}
	else{
		
		$(location).attr('href','https://www.fromwoopwoop.com/checkout.php');
	}

}



