$(function() {

    var func = function() {
	
		var quantity = $('#quantity').val();
		
		if(quantity && quantity !== '') {
			$('#quantityDisplay').text(quantity);
			
			$('.edition').each(function() {
			
				var edition = $(this).val();
				
				$('.' + edition).html('<i class="' + $('body').data('iconset') + ' fa-spinner fa-spin"></i>');
		        
				var priceCode = $('#productHolder').val() + '/' 
					+ edition + '/'
					+ $('#termHolder').val();
				
				setTimeout(function() {	
				$.post('/app/api/price', $.param({ productCode: priceCode, 
		           				   quantity: quantity,
		           				   currency:  $('body').data('currency') }),
		           function(data)
		           {
		           	   $('.' + edition).empty();
		           	   if(data.success) {
		           	   		$('.' + edition).text(data.resource.price + ' ' + data.resource.currencyCode);
		           	   		$('.' + edition).closest('form').find('input[name="signature"]').val(data.resource.signature);
		           	   		$('.' + edition).closest('form').find('input[name="price"]').val(data.resource.price);
		           	   		$('.' + edition).closest('form').find('input[name="quantity"]').val(quantity);
		           	   } else {
		           	        $('.' + edition).text('POA');
		           	   }
		           });
		        }, 500);
			});
		}
	};
	
	$('#quantity').keyup(func);
	$('#quantity').change(func);
	func();	
});