$(document).ready(function(){

try{
	
	var totalAmount = 0;
	$("#voltarief").blur(function(){
		totalAmount = calculateAmount();
		showTotal(totalAmount);
	});
	$("#reductietarief").blur(function(){
		totalAmount = calculateAmount();
		showTotal(totalAmount);
	});
	
	var sum = Math.sqrt(4044121);
	var dt = new Date();
	
	$("#submit").click(function(){
		$("#sum").val(sum);
		$("#tdate").val(formatDateTime(dt));
	});
	
	$("#form_reserv").validate({
		errorLabelContainer:$("div.error ul"),
		errorElement:"li",
		rules:{
			mv:"required",
			voorl:"required",
			achternaam: {
				required:true,
				minlength:3
			},
			email: {
				required:true,
				email:true
			},
			telefoon:"required",
			voltarief:{
				digits:true,
				required:{
					depends:function(element) {
	           return $("#reductietarief").val() == '';
	        }
				}
			},
			reductietarief:{
				digits:true,
				required:{
					depends:function(element) {
	           return $("#voltarief").val() == '';
	        }
				}
			}
		},
		messages: {
			mv: "De heer of mevrouw?",
			voorl: "Vul uw voorletters in",
			achternaam: {
				required:"Vul uw naam in",
				minlength:"Vul uw naam in"
			},
			email: {
				required:"Vul uw e-mailadres in",
				email:"Vul een geldig e-mailadres in"
			},
			telefoon:"Vul uw telefoonnummer in",
			voltarief: {
				required:"Vul het aantal kaarten in",
				digits:"Alleen cijfers gebruiken"
			},
			reductietarief: {
				required:"",
				digits: "Alleen cijfers gebruiken"
			}
		}
	});
} catch(err){ }

jQuery.validator.addMethod("phoneNL", function(value) {
	return /^-?(?:[0]{1}[1-9]{1,3}[\s\-]{0,3}[1-9]{1}[0-9]{5,8})?$/.test(value);
}, "Vul uw telefoonnummer in");

});

function valuta(input){
	var output = "&euro; ";
	if(Math.floor(input) == input){
		// geen cijfers achter de komma
		output += input + ',-';
	} else {
		var spl = input.split('.');
		if(spl[1]){
			var strdecim = String(spl[1]);
			if(strdecim.length == 1){
				output += spl[0]+','+spl[1]+'0';
			} else if(strdecim.length == 2){
				output += spl[0]+','+spl[1];
			}
		}
	}
	return output;
}

function calculateAmount(){
	var volTarief = Number($("#concertVol").val());
	var reductieTarief = Number($("#concertReductie").val()) || 0;
	var nVol = Number($("#voltarief").val());
	var nRed = Number($("#reductietarief").val()) || 0;
	return volTarief * nVol + reductieTarief * nRed;
}

function showTotal(total){
	$("span.amount").html('('+valuta(total)+')');
}

function submitForm(formname){
	$("#"+formname).submit();
}

function prefix(n, maxValue){
	var output = String(n < maxValue ? "0" : "");
	return output;
}

function formatDateTime(input){
	var month = prefix(input.getMonth(),9)+(input.getMonth()+1);
	var day = prefix(input.getDate(), 10)+input.getDate();
	var hours = prefix(input.getHours(), 10)+input.getHours();
	var minutes = prefix(input.getMinutes(),10)+input.getMinutes();
	var seconds = prefix(input.getSeconds(),10)+input.getSeconds();
	var output = input.getFullYear()+'-'+month+'-'+day+' '+hours+':'+minutes+':'+seconds;	
	return output;
}

