
var regFee = 0;
var accoFee = 0;
var lunchFee = 0;
var accompanyingRegFee = 0;
var accompanyingAccoFee = 0;
var student = false;
var invited = false;

$(document).ready(function() { 
	
	checkFees();

		// accompanying person
	$("input[name='tx_pilmailform_pi1[text][accompanying_person]']").change(function() {
		updateAccompanyingPerson($(this).val());
	});
	
		// book accomodation
	$("input[name='tx_pilmailform_pi1[checkbox][acco]']").click(function() {
		updateBookAccomodation(this.checked);
		//alert(this.checked);
		if (this.checked) {
			$("input[name='tx_pilmailform_pi1[checkbox][lunch_and_dinner]']").attr("checked", "");
			updateBookLunch(false);
			$("#lunchAndDinner").hide();
			
		} else {
			$("#lunchAndDinner").show();
		}
	});
	
		// book lunch as day guest
	$("input[name='tx_pilmailform_pi1[checkbox][lunch_and_dinner]']").click(function() {
		updateBookLunch(this.checked);
	});
	
		// status					   
	$("select[name='tx_pilmailform_pi1[select][participant]']").change(function() {
		updateStatus($(this).val());																		
	});
});

function updateAccompanyingPerson(val) {
	if(val!='') {
		accompanyingRegFee = 15000;
		accompanyingAccoFee = 20000;
	} else {
		accompanyingRegFee = accompanyingAccoFee = 0;
	}
	setFees();
}

function updateBookAccomodation(val) {
	if (val) {
		if (student) {
			accoFee = 20000;
		} else 
		if (invited) {
			accoFee = 0;
		} else {
			accoFee = 26000;
		}
	} else {
		accoFee = 0;
	}
	setFees();
}

function updateBookLunch(val) {
	if (val) {
		lunchFee = 9000;
	} else {
		lunchFee = 0;
	}
	setFees();
}

function disableBookLunch() {
	//console.log()
}

function updateStatus(val) {
	student = false;
	invited = false;
	switch (val) {
		case 'Invited Speaker':
			regFee = 0;
			invited = true;
		break;
		case 'Student':
			regFee = 30000;
			student = true;
		break;
		case 'Others':
			regFee = 40000;
		break;
		case '':
			regFee = 0;
		break;			
	}
	updateBookAccomodation($("input[name='tx_pilmailform_pi1[checkbox][acco]']").attr('checked'));
}

function format (wert) {
	var Hunderter = "";
	var cent = "";
	Runden = Math.round(wert);
	Hunderter = Math.floor(Runden/100);
	cent = Runden - 100 * Math.floor (Runden / 100) + "";
	while (cent.length < 2) cent = cent + "0";
	wert = Hunderter + "." + cent + " Euro"
	return wert;
}

function checkFees() {
	updateStatus($("select[name='tx_pilmailform_pi1[select][participant]']").val());
	updateBookLunch($("input[name='tx_pilmailform_pi1[checkbox][lunch_and_dinner]']").attr('checked'));
	updateAccompanyingPerson($("input[name='tx_pilmailform_pi1[text][accompanying_person]']").val());
	//alert("book accomodation: "+$("input[name='tx_pilmailform_pi1[checkbox][acco]']").val());
	//alert("accompanying person: "+$("input[name='tx_pilmailform_pi1[text][accompanying_person]']").val());
}

function setFees() {
	$("input[name='tx_pilmailform_pi1[text][registrationfee]']").val(format(regFee+accompanyingRegFee));
	$("input[name='tx_pilmailform_pi1[text][accommodationfee]']").val(format(accoFee+accompanyingAccoFee));
	$("input[name='tx_pilmailform_pi1[text][lunchanddinnerfee]']").val(format(lunchFee));
	$("input[name='tx_pilmailform_pi1[text][totalfee]']").val(format(regFee+accoFee+lunchFee+accompanyingRegFee+accompanyingAccoFee));
}
