var currentQuestion;
var currentQuestionResult;

function checkNumber(inputid) {
	var valInput = document.getElementById(inputid);
	var thisValue = valInput.value
	if (parseInt(thisValue) != thisValue) {
		var error = 'alleen getallen toegestaan';
	} else {
		var aValue = thisValue.split('.');
		if (aValue.length > 1) {
			var error = 'alleen getallen toegestaan';
		} else {
			endQuestion2(true);
		}
	}
}

// Go to a question
function goQuestion(question) {
	var question = document.getElementById('question' + question);
	if(question == null) return false;
	
	if(currentQuestion != null) {
		currentQuestion.style.display = 'none';
	}
	question.style.display = '';
	if(currentQuestionResult != null) {
		currentQuestionResult.style.display = 'none';
	}
	
	currentQuestion = question;
}

// Go to a question result
function goQuestionResult(questionResult) {
	var questionResult = document.getElementById('result' + questionResult);
	if(questionResult == null) return false;
	
	if(currentQuestionResult != null) {
		currentQuestionResult.style.display = 'none';
	}
	questionResult.style.display = '';

	currentQuestionResult = questionResult;
}

// Enable question button
function goQuestionEnable(question, enabled) {
	var qButton = document.getElementById('b' + question);
	if(qButton == null) return false;
	
	qButton.disabled = !enabled;
}


// End of question 1
function endQuestion1() {
	var sQ1_2 = document.getElementById('sQ1_2');
	if(sQ1_2 == null) return false;

	var sQ1_1 = document.getElementById('sQ1_1');
	if(sQ1_1 == null) return false;
	
	// Check answers
	if(sQ1_2.checked) {
		goQuestionResult('Offerte');
	} else if(sQ1_1.checked) {
		goQuestion(2);
	} else {
		goQuestionEnable(1, false);
	}
}

// End of question 2
function endQuestion2(toggle) {
	
	var valInput = document.getElementById('sQ2a');
	var thisValue = valInput.value
	var nonumbererror = '';
	if (parseInt(thisValue) != thisValue && thisValue != '') {
		nonumbererror = 'alleen getallen toegestaan';
	} else {
		var aValue = thisValue.split('.');
		if (aValue.length > 1) {
			nonumbererror = 'alleen getallen toegestaan';
		}
	}
	
	var sQ2a = document.getElementById('sQ2a');
	if(sQ2a == null) return false;

	var sQ2b_2 = document.getElementById('sQ2b_2');
	if(sQ2b_2 == null) return false;

	var sQ2b_1 = document.getElementById('sQ2b_1');
	if(sQ2b_1 == null) return false;
	

	//var sQ2c_2 = document.getElementById('sQ2c_2');
	//if(sQ2c_2 == null) return false;

	//var sQ2c_1 = document.getElementById('sQ2c_1');
	//if(sQ2c_1 == null) return false;
	
	// Validate discount code.
	/*var discount_error = false;
	var sDiscount_code = document.getElementById('sDiscount_code').value;
	var sDiscount_error = document.getElementById('sDiscount_error').innerHTML;
	if((sDiscount_error != 0) && (sDiscount_code != '')) {
		var discount_error = true;
	};*/
	//discount code has been removed, so this one is always false (no error)
	var discount_error = false;
	// Check answers
	if(toggle) {

		// Question about if party lasts or 24 hours is hidden, so don't have to check that for validation
		// (sQ2c_2.checked || sQ2c_1.checked) && <== this was the code entered in the if-statement below.
		if(sQ2a.value != '' && (sQ2b_2.checked || sQ2b_1.checked) &&  !discount_error && nonumbererror == '') {
			var spanerror = document.getElementById('numbererror');
			spanerror.innerHTML = '';
			goQuestionEnable(2, true);
		} else {
			if (nonumbererror != '') {
				var spanerror = document.getElementById('numbererror');
				spanerror.innerHTML = nonumbererror;
			}
			goQuestionEnable(2, false);
		}
		
	} else {
		//sQ2a.value = parseInt(sQ2a.value);
		if(sQ2a.value > 50000) {
			goQuestionResult('Error2a');
			return;
		}
		
		if(sQ2b_1.checked) {
			goQuestionResult('Error2b');
			return;
		}
/*		if(sQ2c_1.checked) {
			goQuestionResult('Error2c');
			return;
		}*/
		
		// Calculate premiums
		updatePremium(1, 0.0135 * sQ2a.value);
		updatePremium(2, 0.0200 * sQ2a.value);
		updatePremium(3, 0.0275 * sQ2a.value);
		
		goQuestion(3);
	}
}

// End of question 3
function endQuestion3(toggle) {
	var sQ2a = document.getElementById('sQ2a');
	if(sQ2a == null) return false;

	var sQ3_3 = document.getElementById('sQ3_3');
	if(sQ3_3 == null) return false;

	var sQ3_2 = document.getElementById('sQ3_2');
	if(sQ3_2 == null) return false;

	var sQ3_1 = document.getElementById('sQ3_1');
	if(sQ3_1 == null) return false;

	// Check answers
	if(toggle) {

		if(sQ3_1.checked || sQ3_2.checked || sQ3_3.checked) {
			goQuestionEnable(3, true);
		} else {
			goQuestionEnable(3, false);
		}
		
	} else {

		if(sQ3_1.checked) {
			updatePremium(1, 0.0135 * sQ2a.value);
		}

		if(sQ3_2.checked) {
			updatePremium(2, 0.0200 * sQ2a.value);
		}

		if(sQ3_2.checked) {
			updatePremium(3, 0.0275 * sQ2a.value);
		}
		
		goQuestionResult('Order');
	}
}

// Calculate premium
function updatePremium(premiumIds, premium) {
	// Set discount is disabled!!
	//var sDiscount_value = document.getElementById('sDiscount_value').value;
	//if(sDiscount_value == null) return false;
	
	// Calculate discount if available.
	//if(sDiscount_value > 0){
	//	premium = premium * (1-(sDiscount_value/100));
	//}
	
	var premium = premium.toFixed(2);
	
	var premiumId = document.getElementById('premium' + premiumIds);
	if(premiumId == null) return false;
	premiumId.innerHTML = number_format(premium, 2, ',', '.');;
	
	var iPremium = document.getElementById('iPremium' + premiumIds);
	if(iPremium == null) return false;
	iPremium.value = premium;
}
