$(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);
									}
								}
							});
					}
				}
			}
		});
}

function showDialog(message, title, type)
{

	if(type == 'error')
	{
		msg = '<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>'+message+'</p>';
	}
	else
	{
		msg = '<p>'+message+'</p>';
	}
	
	var msgConfirm = $('<div></div>')
		.html(msg)
		.dialog({
			resizable: false,
			draggable: false,
			height:200,
			modal: true,
			title: title,
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
	});
}

function isInteger(val)
{
    if(val==null)
    {
        return false;
    }
    if (val.length==0)
    {
        return false;
    }
    for (var i = 0; i < val.length; i++) 
    {
        var ch = val.charAt(i)
        if (i == 0 && ch == "-")
        {
            continue
        }
        if (ch < "0" || ch > "9")
        {
            return false
        }
    }
    return true
}

/* 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];
}

/*
Cookie functionaliteit
*/
function createCookie(name, value, days) {
	var expires = '';
	
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = '; expires=' + date.toGMTString();
	}
	
	document.cookie = name + '=' + value + expires + '; path=/';
};

function readCookie(name) {
	var nameEQ = name + '=';
	var ca = document.cookie.split(';');
	
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		
		while (c.charAt(0) == ' ') {
			c = c.substring(1, c.length);
		}
		
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
	}
	
	return null;
};

function eraseCookie(name) {
	createCookie(name, '', -1);
};
