$(function() {
	// Add a mark to all required fields
	$(':input.required').each(function() {
		$(this).parents('TD:first')
			.prev('TD')
				.append('<span style="color: red; margin-left: 5px; cursor: help" title="Verplicht">*</span>');
	});

  $('FORM').submit(function() {
		$form = $(this);
	
		// Remove all error messages
		$form.find('DIV.error').remove();
	
		// Check required fields
		valid = true;
		$form.find(':input.required').each(function() {
			if ($(this).val().length == 0) {
				// Add error message
				markFieldError($(this), 'Verplicht in te vullen!');
				valid = false;
			}
		});
		
		// Check email addresses
		$form.find(':input.email').each(function() {
			if (!$(this).val().match(/^\s*[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\s*$/i)) {
				markFieldError($(this), 'Vul een geldig e-mailadres in.');
				valid = false;
			}
		});
		
		if (!valid) alert('U heeft een of meer velden niet juist ingevuld. \nControleer uw invoer en probeer opnieuw.');

		return valid;
	});
});

function markFieldError($field, error) {
	if ($field.next('.error').length > 0) 
		// Only show one error at a time.
		return;
	$field.after('<div class="error" style="color: red; font-weight: bold; font-size: 70%;">' + error + '</div>');
}