/**
* JAVASCRIPT FUNCTIONS
**/
$(window).ready(function() {
	// E-mail tekst weghalen bij focus, terugzetten bij blur
	$('#email').focus(function() {
		if($('#email').val()=="Email:") $('#email').val('');
	});
	
	$('#email').blur(function() {
		if($('#email').val()=="") $('#email').val('Email:');
	});
	
	// Password tekst weghalen bij focus, terugzetten bij blur
	$('#wachtwoord').focus(function() {
		if($('#wachtwoord').val()=="Wachtwoord:") $('#wachtwoord').val('');
	});
	
	$('#wachtwoord').blur(function() {
		if($('#wachtwoord').val()=="") $('#wachtwoord').val('Wachtwoord:');
	});

	// zoeken tekst weghalen bij focus, terugzetten bij blur
	$('#zoek').focus(function() {
		if($('#zoek').val()=="Zoek:") $('#zoek').val('');
	});

	$('#zoek').blur(function() {
		if($('#zoek').val()=="") $('#zoek').val('Zoek:');
	});

	// pagina als pdf maken
	$('#pdf').click(function() {
		var html = $('#subcontent').html();
		var dialog = jqDialog.notify('Pdf wordt aangemaakt. Even geduld aub...');

		$.ajax({
			type: 'POST',
			url: '/index/pdf',
			data: {html : html},
			success: function(response,success) {
				dialog.close();
				if(success) {
					window.open("/tmp/" + response);
				}
			}
		});
	});
	
	// pagina als mail versturen
	$('#mail').click(function() {
		var html = $('#subcontent').html();
		jqDialog.prompt(
			"Vul het email adres in, naarwaar u deze pagina wilt zenden",
			'',
			function(email) {
				$.ajax({
					type: 'POST',
					url: '/index/mail',
					data: {html : html, email : email},
					success: function() {
						jqDialog.notify("De pagina werd correct verzonden!",2);
					}
				})
			},
			function() {}
		);
	});
	
	// pagina afprinten
	$('#print').click(function() {
		var html = $('#subcontent').html();
		$.ajax({
			type: 'POST',
			url: '/index/print',
			data: {html : html},
			success: function(response,success) {
				if(success) {
					if (navigator.appName.indexOf('Microsoft') != -1) {
						self.printWin = window.open('','','width=221,height=115,scrollbars=yes,toolbar=yes,location=yes,status=yes,menubar=yes,directories=yes,resizable=yes');
						self.printWin.document.open();
						self.printWin.document.write(response);
						self.printWin.location.reload();
						self.printWin.document.close();
						setTimeout('self.printWin.print()',500);
						setTimeout('self.printWin.window.close()',501);
					} else {
						var printWin = window.open('','','width=221,height=115,scrollbars=yes,toolbar=yes,location=yes,status=yes,menubar=yes,directories=yes,resizable=yes');
						printWin.document.open();
						printWin.document.write(response);
						printWin.document.close();
						printWin.print();
						printWin.window.close();						
					}
				}
			}
		});
	});

	// inschrijven op studiedag
	inschrijven = function(id) {
		fields = ['naam','voornaam','email','riziv'];
		jqDialog.prompt2(
			fields,
			function(values) {
				var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
				var rizivPattern = /[0-9]\/[0-9]{5}\/[0-9]{2}\/[0-9]{3}/;
				if (values[0] == "" || values[1] == "" || values[2] == "" || values[3] == "" || !emailPattern.test(values[2])) {
					alert('Gelieve alle velden correct in te vullen.');
					return false;
				} else if (!rizivPattern.test(values[3])) {
					alert('Gelieve het riziv nummer in volgende vorm in te vullen: x/xxxxx/xx/xxx');
					return false;
				}
				$.ajax({
					type: 'POST',
					url: '/studiedag/submit',
					data: {studiedag_id : id, naam : values[0], voornaam : values[1], email : values[2], riziv : values[3]},
					success: function() {
						jqDialog.alert("Uw inschrijving werd correct ontvangen!");
					}
				})
			},
			function() {}
		);
	}
	
	// accrediterings attest aanvragen
	accrediteringen1 = function() {
		jqDialog.alert('U dient ingelogd te zijn als lid om een attest aan te vragen');
	}
	accrediteringen2 = function(id) {
		var id = id;
		jqDialog.confirm(
			'Wenst u een accrediterings-attest te bekomen?',
			function() {
				$.ajax({
					type: 'POST',
					url: '/accreditering/submit',
					data: {accreditering_id : id},
					success: function() {
						jqDialog.notify("Uw aanvraag werk correct ontvangen.<br />We sturen u het attest zo snel mogelijk op.",6);
					}
				})
			}
		
		)
	}
});


