
function calculateTotalPosters() {
	maxAllowedPosters = 4;
	totalPosters = 0;
	alert_displayed = false;
	// Loop through quantity values and add them togeather
	$(".quantity").each(function(){
		totalPosters = totalPosters + (isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val()));
		if (totalPosters > maxAllowedPosters) {
			if (!alert_displayed) {
				alert("You can only purchase up to " + maxAllowedPosters + " additional prints per order");	
				alert_displayed = true;
			}
			$(this).val("0");
		} else {
			// Set a clean value of the number back to the form
			$(this).val(isNaN(parseInt($(this).val())) ? 0 : parseInt($(this).val()));
		}
		
	});
	return totalPosters;
}

function caluclateCost() {
	totalPosters = calculateTotalPosters();
	cost = 6.95;
	additional_poster_cost = 2.99;
	
	
	
	//if (totalPosters > 1 && totalPosters <= 4) {
		cost = cost + (totalPosters) * additional_poster_cost;
		
		if ($("#freeProductID").val()) {
			cost = cost - 2.99;
		}
	//}
	
	return cost.toFixed(2);
}

$(document).ready(function(){
						   

$('#order_billship .quantity').bind('change keyup', function() {

		if ($(this).hasClass("freeProduct") && $(this).val() <= 0) {
			$(this).val("1");
		}
		$("#GrandTotal").html("$" + caluclateCost());	
});
/*
	$('#order_billship .quantity').change(function(){
		//alert('change');
		if ($(this).hasClass("freeProduct") && $(this).val() <= 0) {
			$(this).val("1");
		}
		$("#GrandTotal").html("$" + caluclateCost());						   
	}); 
	*/
	$(document).pngFix(); 
   $(".FAQList div").slideUp(1);
   $(".FAQList a").click(function(event) {event.preventDefault();$(this).next("div").slideToggle(100);});  
   $(".ExpandAll").click(function(event) {event.preventDefault();$(".FAQList div").slideDown(100);});     
   $(".CollapseAll").click(function(event) {event.preventDefault();$(".FAQList div").slideUp(100);});         
   
   
   /*
	var validator = $("#orderUpload").validate({
		rules: {
			terms: "required"
			
		},
		messages: {
			terms: "You must agree to our terms and conditions"
			
			
		},
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next() );
				
			else if ( element.is(":checkbox") )
				//error.appendTo ( element.parent().next() );
				error.appendTo ( element.parent().add() );
				
			else
				error.appendTo( element.parent().add() );
		},
		errorElement: "div"

	});
	*/

});
