$(document).ready(function() {

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

	$('.buyImage').bind('click', function() {
		var theid = $('#price_id').val();
		var theGrind = $('#grind').val();
		var theQty = $('#qty').val();
		if(theid !='' && theGrind !=''){
  		$.post("/pages/components/add-to-cart.php", "price_id="+ theid +"&grind="+theGrind+"&cartaction=add&qty="+theQty, function(data) {						
		$("#show-cart").html(data);	

		});
		}else{
			alert('Please ensure you select a grind and size.');
		}
  		
	});	
	
	$('.buyImageGallery').bind('click', function() {
		var theid =$(this).attr('id');
  		$.post("/pages/components/add-to-cart.php", "price_id="+ theid +"&cartaction=add&qty=1", function(data) {					
		//$('.headerButtons').html(data);	
		//$("#show-cart").html(data);	

		});
  		
	});	
	
	
	
});

function priceDisplay(){
	var theid = $('#price_id').val();
	$.post("/includes/price_display.php", "price_id="+theid, function(data) {
		if(data !="0"){
			$(".thePrice").html(data);
		}
	});
	
}	

function priceDisplayWholesale(){
	var theid = $('#price_id').val();
	var qty = $('#qty').val();
	$.post("/includes/price_display_wholesale.php", "price_id="+theid+"&qty="+qty, function(data) {
		if(data !="0"){
			$(".thePrice").html(data);
		}
	});
	
}


function change_quantity(qty,id,price,grind) {
	
	$.post("/pages/components/add-to-cart.php", "cartaction=add&price_id="+id+"&qty="+qty+"&grind="+grind, function(data) {
		$("#show-cart").html(data);		
		var newWeight =  parseFloat($("#freight_weight").val());
		$("#freight_weight").val(newWeight);
		thisSubtotal = qty * parseFloat(price);
		$("#"+id).val(qty);
		$("#"+id +"_subtotal").html("$"+ CurrencyFormatted(thisSubtotal));	
		
		$(location).attr('href','/checkout.php');
		
		
	});
	
			
}


function togglePayment(payment) {
	
	if(payment=="Account"){
	$("#accountPayment").show();
	$("#cardPayment").hide();
	$('#ccType').removeClass('required');
	$('#txtCCName').removeClass('required');
	$('#txtCCNumber').removeClass('required');
	$('#ddlExpiryMonth').removeClass('required');
	$('#ddlExpiryYear').removeClass('required');
	
	}
	else{
	$("#accountPayment").hide();
	$("#cardPayment").show();
	$('#ccType').addClass('required');
	$('#txtCCName').addClass('required');
	$('#txtCCNumber').addClass('required');
	$('#ddlExpiryMonth').addClass('required');
	$('#ddlExpiryYear').addClass('required');
	}
				
}
	
	function updateFreight() {
		
		var x = $("#freight-option").val();
		var qty = $("#qty").val();
		var totalWeight = $("#freight-weight").val();
		var subTotal = parseFloat($("#txtAmount").val());
		if(x != "") {
			if((x == "Australia" && $("#freight-postcode").val().length == 4) || x != "Australia") {
				if(x != "Australia") {
					$("#australian-postcode").hide();
					$("#freight-postcode").val("");
					$("#ship_postcode").attr("readonly","").val("");
				} else
					$("#ship_postcode").val($("#freight-postcode").val()).attr("readonly","readonly");
						
				$.post("/includes/check_freight.php", "country="+x+"&totalWeight="+totalWeight+"&qty="+qty+"&postcode="+$("#freight-postcode").val(), function(data) {
					//alert('Freight Cost: '+data);
					//$("#show_freight_cost").html("$"+CurrencyFormatted(data));
					$("#freight-total").html("$"+data);
					$("#freight-total-input").val(data);
					$("#customer-details").show();
					//alert(subTotal);
					
					/*
					var postage = $("#freight_cost").val(data);
					subTotal = subTotal + parseFloat($("#freight_cost").val(data));
					$("#txtAmount").val(subTotal);
		*/
				
					$(location).attr('href','/checkout.php');
				});			
			} else {
				$("#checkout-address").hide();
				$("#australian-postcode").show()
				$("#freight-total").html("---");
				$("#freight-postcode").focus();
				
			}
		}
		else{
		
			$(location).attr('href','/checkout.php');
		}	
		
				
		 	
	}


	function CurrencyFormatted(amount){
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;
		return s;
	}
	
	function removeItem(id) {
	
	if(confirm("Remove Item from cart?")){
		$.post("/pages/components/add-to-cart.php", "action=remove&price_id="+id, function(data) {
				$("#shop-cart").html(data);	
				$(location).attr('href','/checkout.php');	
			});
	}
}





