$(document).ready(function() {
	$("input[id^=product_count_]").each(function() {
		$(this).change(function() {
			var productId = getProductId($(this).attr("id"));
			var count = parseInt($("#product_count_"+productId).val());
			var price = parseFloat($("#product_price_"+productId).val());
			$("#product_position_"+productId).val(kaufm(count*price));
			calculateSum();
		});
	});
	$("input[id^=shipment_]").click(function() {
		calculateSum();
	});
	$("#isInstallation").click(function() {
		calculateSum();
	});
	$("#isRemoval").click(function() {
		calculateSum();
	});
	$("#count_removal").change(function() {
		calculateSum();
	});
	$("#next").click(function() {
		return checkAGB();	
	})
});

function getProductId(inputId) {
	return inputId.split("_")[2];
}

function checkAGB()
{
	if($('#agb:checked').val() !== undefined)
	{
		return true;
	}
	else
	{
		alert("Sie haben den Allgemeinen Geschäftsbedigungen nicht zugestimmt.");
		return false;
	}
}

function calculateShipment() {
	var shipmentPost = 0;
	var shipmentInstallationFirm = 0;
	if ($("#shipment_post:checked").val() == "SHIPMENT_POST") {
		$("input[id^=product_count_]").each(function() {
			if($(this).val() > 0) {
				var productId = getProductId($(this).attr("id"));
				var shipmentPrice = parseFloat($("#shipment_price_"+productId).val());
				var count = parseInt($(this).val());
				shipmentPost += count * shipmentPrice;
			}
		});
	}
	if ($("#shipment_installation_firm:checked").val() == "SHIPMENT_INSTALLATION_FIRM") 
	{
		var productCount = 0;
		$("input[id^=product_count_]").each(function() {
			productCount += parseInt($(this).val());
		});
		shipmentInstallationFirm = Math.ceil(productCount / 10) * parseFloat($("#price_deliver_installation_firm").val());
		$("#isInstallation").attr("disabled", "");
		$("#isRemoval").attr("disabled", "");
		$("#count_removal").attr("disabled", "");
	}
	else
	{
		$("#isInstallation").attr("checked","");
		$("#isInstallation").attr("disabled", "disabled");
		$("#isRemoval").attr("checked","");
		$("#isRemoval").attr("disabled", "disabled");
		$("#count_removal").val("0");
		$("#count_removal").attr("disabled", "disabled");
	}
	$("#position_shipment_post").val(kaufm(shipmentPost));
	$("#position_shipment_installation_firm").val(kaufm(shipmentInstallationFirm));
	return shipmentPost + shipmentInstallationFirm;
}

function calculateInstallation() {
	var installation = 0;
	var removal = 0;
	if($("#isInstallation").is(":checked")) {
		$("input[id^=product_count_]").each(function() {
			if($(this).val() > 0) {
				var productId = getProductId($(this).attr("id"));
				var installationPrice = parseFloat($("#installation_price_"+productId).val());
				var count = parseInt($(this).val());
				installation += count * installationPrice;
			}
		});
	}
	if($("#isRemoval").is(":checked")) {
		removal = parseInt($("#count_removal").val()) * parseFloat($("#price_removal").val());
	}
	$("#position_installation").val(kaufm(installation));
	$("#position_removal").val(kaufm(removal));
	return installation + removal;
}

function calculateHandlingFee(netto_sum)
{
	if(parseFloat($("#payment_limit").val()) > netto_sum)
	{
		var handlingFee = parseFloat($("#handling_fee_constant").val())
		$("#handling_fee").val(kaufm(handlingFee));
		return netto_sum+handlingFee;
	}
	else
	{
		$("#handling_fee").val(kaufm(0));
		return netto_sum;
	}
}

function calculateSum() {
	var netto_sum = 0;
	$("input[id^=product_position_]").each(function() {
		netto_sum += parseFloat($(this).val());
	});
	netto_sum += calculateShipment();
	netto_sum += calculateInstallation();
	netto_sum = calculateHandlingFee(netto_sum);
	$("#netto_sum").val(kaufm(netto_sum));
}

function kaufm(x) {
  	var k = (Math.round(x * 100) / 100).toString();
  	k += (k.indexOf('.') == -1)? '.00' : '00';
  	return k.substring(0, k.indexOf('.') + 3);
}
