// Start functioning when DOM is ready
$( function() {
	
	$('form').submit( function()
	{
		// Set status to true by default
		var status = true;
		
		// Set empty error message
		var error = "De volgende velden zijn niet juist ingevoerd:\n";
		
		// Loop through all fields with class 'checkme'
		$('.checkme').each( function()
		{
			// Get field name
			var name = $(this).attr( 'name' );
			
			// Get the rel
			var rel = $(this).attr( 'rel' );
			
			// Check the rel
			if ( rel == 'email' ) {
				// Set Regular Expression to check the email with
				var regex = new RegExp(/(\w+@[a-zA-Z0-9_]+?\.[a-zA-Z]{2,6})/);
				
				// Check if the field isn't empty
				if ( ! regex.test($(this).val()) ) {
					status = false
					error = error + "\n- " + usForSpace( name );
				}
			} else if ( rel == 'radio' ) {
				// Check if the checkbox is checked
				if ( ! $(this).is(':checked') ) {
					status = false;
					error = error + "\n- " + usForSpace( name );
				}
			} else{
				// Check if the field isn't empty
				if ( $(this).val() == '' ){
					status = false
					error = error + "\n- " + usForSpace( name );
				}
			}
		});
		
		// Check the status after checking
		if ( ! status ) {
			alert( error );
			return false;
		}
	});
	
	
	// function to replace underscores for spaces
	function usForSpace( text )
	{
		return text.replace( /\_/g, ' ' );
	}
	
});

