$(function() {
	$('#userLogOut').click(function() {
		var msgDialog = $('<div></div>')
			.html('Weet u zeker dat u zich wilt afmelden?')
			.dialog({
				resizable: false,
				draggable: false,
				modal: true,
				title: 'Afmelden',
				buttons: {
					Ja: function() {
						$('#userLogOutForm').submit();
					},
					Nee: function() {
						$(this).dialog('close');
					}
				}
			});
	});
});

function lostPassword(handlerUrl, userEmail)
{
	var html = '';
	
	html += '<p class="lostPasswordDialogP" style="text-align:left;">Bent u uw wachtwoord vergeten en wilt u een nieuw wachtwoord aanvragen?</p>';
	html += '<p class="lostPasswordDialogP" style="text-align:left;">Geef dan hier aan voor welk e-mailadres u een nieuw wachtwoord wilt aanvragen.</p>';
	html += '<p class="lostPasswordDialogP" style="text-align:left;"><input type="text" id="lostPasswordDialogEmail" value="' + (userEmail ? userEmail : '') + '" style="width:95%" /></p>';
	html += '<p class="lostPasswordDialogP" style="text-align:left;">Het nieuwe wachtwoord wordt naar uw e-mailadres verzonden</p>';
	
	var msgDialog = $('<div></div>')
		.html(html)
		.dialog({
			resizable: false,
			draggable: false,
			width: 500,
			modal: true,
			title: 'Wachtwoord vergeten',
			buttons: {
				Annuleren: function() {
					$(this).dialog('close');
				},
				'Nieuw wachtwoord aanvragen': function() {
					var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
					var email = $('#lostPasswordDialogEmail').val();
					
					if(email != '' && emailPattern.test(email))
					{
						$('#lostPasswordDialogEmail').attr('title', '');
						$('#lostPasswordDialogEmail').removeClass('error');
						
						var url = handlerUrl + '/lostPassword';
						var data = {email: email};
						
						$.ajax({
							type: 'POST',
							url: url,
							dataType: 'json',
							data: data,
							success: function(data)
							{
								if(data.code == 'success')
								{
									$('.lostPasswordDialogP').remove();
									
									msgDialog.dialog('close');
									
									$('<div></div>')
										.html('<p style="text-align:left;"><span class="ui-icon ui-icon-info" style="float:left; margin:0 7px 20px 0;"></span>Het nieuwe wachtwoord is naar uw e-mailadres verzonden.<br />Controleer uw postvak en gebruik het nieuwe wachtwoord om u aan te melden op deze website.</p>')
										.dialog({
											resizable: false,
											draggable: false,
											width: 500,
											modal: true,
											title: 'Wachtwoord verzonden',
											buttons: {
												'Sluiten': function() {
													$(this).dialog('close');
												}
											}
										});
								}
								else
								{
									$('.lostPasswordDialogP').remove();
									
									msgDialog.dialog('close');
									
									$('<div></div>')
										.html('<p style="text-align:left;"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Dit e-mailadres is onbekend. Controleert u alstublieft het e-mailadres en probeer het nogmaals.</p>')
										.dialog({
											resizable: false,
											draggable: false,
											width: 500,
											modal: true,
											title: 'E-mailadres onbekend',
											buttons: {
												'Annuleren': function() {
													$(this).dialog('close');
												},
												'Nogmaals proberen': function() {
													$(this).dialog('close');
													
													lostPassword(handlerUrl, email);
												}
											}
										});
								}
							}
						});
					}
					else
					{
						$('.lostPasswordDialogP').remove();
						
						msgDialog.dialog('close');
						
						$('<div></div>')
							.html('<p style="text-align:left;"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>Dit e-mailadres is onbekend. Controleert u alstublieft het e-mailadres en probeer het nogmaals.</p>')
							.dialog({
								resizable: false,
								draggable: false,
								width: 500,
								modal: true,
								title: 'E-mailadres onbekend',
								buttons: {
									'Annuleren': function() {
										$(this).dialog('close');
									},
									'Nogmaals proberen': function() {
										$(this).dialog('close');
										
										lostPassword(handlerUrl, email);
									}
								}
							});
					}
				}
			}
		});
}

/* Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}
