function validateEmail( str )
{
	var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return pattern.test( str );
}



function validateUrl( str )
{
	//var pattern = /^(https?:\/\/)?(www\.)?(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i; // i = case insensitive
	var pattern = /^(https?:\/\/)?(www\.)?[\w0-9\-]+\.[\w]{2,4}$/i; // i = case insensitive
	return pattern.test( str );
}



function verifyForm()
{
	var frm    = document.forms[0];
	var labels = frm.getElementsByTagName( 'LABEL' );
	var criteria, label, inputId, input;
	var msg = '';
	
	for ( var i = 0; i < labels.length; i++ ) {
		try {
			inputId = labels[i].attributes['for'].value; // Do not use getAttribute: IE bug
			document.getElementById( inputId ).onchange = function() {
				this.style.backgroundColor = '#fff';
			}
			field    = document.getElementById( inputId );
			criteria = field.className;
			
			if ( typeof criteria == 'string' ) {
				
				label = labels[i].innerHTML.replace( ':', '' );
		
				if ( criteria.indexOf('required') != -1 && field.value.length == 0 ) {
					document.getElementById( inputId ).style.backgroundColor = '#f99';
					msg += "\n- " + label + ' is verplicht';
				}
				
				if ( criteria.indexOf('postcode') != -1 && field.value.length > 0 && field.value.length < 4 ) {
					document.getElementById( inputId ).style.backgroundColor = '#f99';
					msg += "\n- " + label + ' is onjuist';
				}
				
				if ( criteria.indexOf('address') != -1 && field.value.length > 0 ) {
					var name = field.value.match( /\w{3,}/ );
					if ( name == null ) {
						msg += "\n- " + label + ' is verplicht';
						document.getElementById( inputId ).style.backgroundColor = '#f99';
					}
					var number = field.value.match( /\d+/ );
					if ( number == null ) {
						msg += "\n- Huisnummer is verplicht";
						document.getElementById( inputId ).style.backgroundColor = '#f99';
					}
					
				}
				
				if ( criteria.indexOf('phone') != -1 && field.value.length > 0 ) {
					var matches = field.value.match( /\d/g ); // g = global search
					if ( !matches || matches.length < 7 ) {
						document.getElementById( inputId ).style.backgroundColor = '#f99';
						msg += "\n- " + label + ' is onjuist';
					}
				}
				
				if ( criteria.indexOf('url') != -1 && field.value.length > 0 ) {
					var tld = document.getElementById( 'tld' );
					var url = ( tld == null ? field.value : field.value + tld );
					if ( !validateUrl(url) ) {
						document.getElementById( inputId ).style.backgroundColor = '#f99';
						msg += "\n- " + label + ' is onjuist';
					}
				}
				
				if ( criteria.indexOf('mail') != -1 && field.value.length > 0 && !validateEmail(field.value) ) {
					document.getElementById( inputId ).style.backgroundColor = '#f99';
					msg += "\n- " + label + ' is onjuist';
				}
			
			}
		}
		catch( e ) {
			// Ignore
		}
	}
	
	// Algemene voorwaarden
	if ( this.av && this.av.checked == false ) {
		msg += '\n- U moet akkoord gaan met de Algemene Voorwaarden';
	}
	
	if ( msg ) {
		alert( 'Het formulier is niet juist ingevuld:' + msg );
		return false;
	}
	
	return true;
}
