var selectedVersionPrice = '';
var versionPrice = 0.00;
var addonPrices = 0.00;
var additionalPrices = 0.00;

function updateVersion() {
	if (!selectedVersionPrice) {
		return;
	}
	

   var fVersionPrice = parseFloat($('input#price' + selectedVersionPrice)[0].value);
   if (isNaN(fVersionPrice)) {
	   return;
   }
   var versionQuantity = getCartQuantity();
   versionPrice = fVersionPrice * versionQuantity;   
}

function getCartQuantity() {
	if ($('#AddToCartQuantity').length != 1) {
		return;
	}
	   var versionQuantity = parseFloat($('#AddToCartQuantity')[0].value);
	
	   if (isNaN(versionQuantity)) {	   
		   versionQuantity = 1;
		   return versionQuantity;
	   }
	   else if (versionQuantity < 1) {		   
		   $('#AddToCartQuantity').attr('value', '1');		   
		   versionQuantity = 1;	   
	   }		
	   return versionQuantity;

}

function getVersionPrice(versionId) {
   var fVersionPrice = parseFloat($('input#price' + versionId)[0].value);
   if (isNaN(fVersionPrice)) {
	   return false;
   }
   return fVersionPrice;
	
}

function updateAddons() {
	addonPrices = 0.00;
	var cartQuant = getCartQuantity();
	$('input:checkbox.addon').each(function() {
		var versionPrice = getVersionPrice(this.value);		
		if (!versionPrice) {
			this.checked = false;
			return;
		}
		if (this.checked) {
			addonPrices = cartQuant*versionPrice;
		}
	});
}

function getAdditionalQuantity(id) {
   var quantity = parseFloat($('#additionalQuantity' + id)[0].value);
	
   if (isNaN(quantity)) {	   
	   quantity = 0;
	   return quantity;
   }
   else if (quantity < 0) {		   
	   $('#additionalQuantity' + id).attr('value', '1');		   
	   quantity = 0;	   
   }		
   return quantity;  
}

function updateAdditionals() {	
	additionalPrices = 0;
   $('.additionals-quantity').each(function() {
	   versionId = $(this).attr('version');
	   if (versionId) {
		   var price = getVersionPrice(versionId);
		   if (price) {
			   additionalPrices += getAdditionalQuantity(versionId)*price;
		   }
	   }
   });
}

function updateFullPrice() {
	if ($('#OrderSum .value').length != 1) {
		return;
	}
	updateVersion();
	updateAddons();
	updateAdditionals();
	fullPrice = versionPrice + addonPrices + additionalPrices;
	fullPrice = fullPrice.toFixed(2);
	fullPrice = fullPrice.toString();
	fullPrice = fullPrice.replace('.',',');
	$('#OrderSum .value')[0].innerHTML = fullPrice;
}



$(function() {
	
	$('input.articleversion').click(function() {
	   $('.ArticleVersionPersonalization').hide();
	   $('#ArticleVersionPersonalization' + this.value).show();
	   selectedVersionPrice = this.value;
	   updateFullPrice();
	});
	
	$('input.articleversion').each(function() {
		if (this.checked) {
	       selectedVersionPrice = this.value;
	       updateFullPrice();
		}
	});
	
	$('#AddToCartQuantity').keyup(function() {
		updateFullPrice();
	});
	
	$('.additionals-quantity').keyup(function() {
		updateFullPrice();
	});
	
	$('#clearVersion').click(function() {
		$('#AddToCartQuantity').attr('value', '1');
		updateFullPrice();
	});
	
	$('.clearAdditionals').click(function() {
		$('#additionalQuantity' + this.id).attr('value', '0');
		updateFullPrice();
	});
	
	

	$("input:checkbox.addon").change(function() {
		updateFullPrice();
	});
	
	updateFullPrice();
	
});

