$(document).ready(function() {
		
	$('#objectForm #zipcode').keyup(function (ev) {
		
		var value = $(this).val();
		
		if(value.match(/[\s]/i))
		{
			$(this).val(value.replace(" ",""));
		}
	});
	
	var blockActivationForm = function () {
		
		var form = $('div.object-account-signup');
		
		var html = '<div class="account-block"><h3>Activeren</h3>Er is een activatie e-mail met daarin een code naar het ingevulde e-mailadres verstuurd.<br /><strong>Vul deze code hieronder in:</strong>'
			+ '<form method="post" id="activationForm">'
			+ '<input type="text" class="textfield" name="code" />'
			+ '<br /><input type="image" src="/static/img/account/activate-button.gif" id="checkCode" name="Valideer code" />'
			+ '</form></div>'
		;
	
		form.block({ 
		    message: html, 
            css: { 
				top:  	($(window).height() - 312) /2 + 'px', 
				left: 	($(window).width() - 606) /2 + 'px',
				background: 'url(/static/img/account/account-block-background.png) no-repeat',
				border: '0px',
				padding:'0',
				width: 606,
				height: 312
			} 
        });

		$('#checkCode').click(function (ev) {

			$.post("/account/checkactivationcode",$('form#activationForm').serialize(),function(data) {
				
				if(data.activated) {
					form.unblock(); 
					
					window.location.href = window.location.href;
				}
				else
				{
					alert('Ingevoerde code is niet correct.');
				}
				
			},'json');
			
			ev.preventDefault();
			
		});
	}

	if($('form#accountActivationForm').attr('block')) {
		
		// Formulier disablen
		blockActivationForm();
		
		// Stap 4 knop disablen
		
	}
	
	$('div.bullets dd input[class=bullet-text]').each(function () {
		
		var varLength = parseInt($(this).val().length);
		var emClass = '';
		
		if(varLength > 3)
		{
			emClass = 'ok';
		}
		else
		{
			emClass = 'error';
		}
		
		$(this).after(' <em class="' + emClass + '" rel="' + $(this).attr('name') + '">' + varLength + '</em> van de <strong>50</strong> karakters');
	});
	
	$('div.bullets dd input').keyup(function () {
		
		var varLength = parseInt($(this).val().length);
				
		var em = $('em[rel=' + $(this).attr('name') + ']');
		
		if(varLength > 3)
		{
			em.attr('class','ok');
		}
		else
		{
			em.attr('class','error');
		}
		
		em.html(varLength);
		
		updateDescriptionFlash($(this).attr('nr'),$(this).val());
	});

	$('form#accountActivationForm').submit(function (ev) {
	
		//alert('stop');
		var form = $(this);
		
		//alert($(this).serialize());
		
		$.post(location.href,$(this).serialize(),function(data) {
						
			if(data.send) {
				blockActivationForm();
			}
			else
			{
				alert(data.message);
			}
			
		},'json');
		
		ev.preventDefault();
	});

});
