var Validator	= Class.create (
	{
		
		do_alert:			true,
		
		error_color:		"#fdaeb4",
		
		normal_color:		"",
		
		//normal_color:		"#ffffff",
		
		required_fields:	[],
		
		wrong_fields:		[],
		
		validate_file:		"_validate.php",
		
		
		
		do_field_error: function ( id, reason ) {
		
			if ( isArray ( id ) ) {
				for ( var i = 0; i < id.length; i++ ) {
					this.wrong_fields [ id [ i ] ]	= reason;
					
					$( id [ i ] ).style.backgroundColor	= this.error_color;
				}
			}
			else {
				this.wrong_fields [ id ]	= reason;
				
				if ( $( id ).style ){
					$( id ).style.backgroundColor	= this.error_color;
				}
			}
			
			if ( this.do_alert ){
				//alert ( reason );
			}
			else{
				return reason;
			}
			return false;
		},
		
		remove_field_error: function ( id ) {
			//this.wrong_fields				= this.wrong_fields.without ( id );
			
			this.wrong_fields [ id ]		= null;
			
			//alert ( print_r ( this.wrong_fields.without ( id ) ) );
			
			$( id ).style.backgroundColor	= this.normal_color;
			
			return true;
		},
		
		field_is_okay: function ( id ) {
			if ( !this.wrong_fields [ id ] || this.wrong_fields [ id ] == "" )
				return true;
			
			return false;
		},
		
		checkField: function ( i, do_alert ) {
			var len	= 1;
			switch ( i.id ) {
				// Following fields are checked on the fly
				case 'email':
				case 'wachtwoord1':
				case 'wachtwoord2':
				case 'telefoonnetnummer':
				case 'telefoonabonummer':
				case 'mobielnetnummer':
				case 'mobielabonummer':
					return ( this.do_alert == true ? true : "" );
					break;
				// True checks starting
				case "postcode1":
					return this.checkNumbers ( i, 4 );
					break;
				case "postcode2":
					return this.checkText ( i, 2 );
					break;
				case "vraagopmerking":
					len	= 15;
				default:
					if ( !this.checkDataLength ( i, len ) ){
						return this.do_field_error ( i.id, ucfirst ( i.id ) + ' dient minimaal ' + len + ' teken te bevatten.' );
						this.wrong_fields [ id [ i ] ]	= reason;
					}
					else{
						//remove error
						this.remove_field_error(i.id);
					}
					break;
			}
		},
		
		checkFieldArray: function ( elements, minimum_selected ) {
			var checkedarray	= Array ( );
			
			for ( var i = 0; i < elements.length; i++ )
				if ( elements [ i ].checked ){
					checkedarray.push ( elements [ i ].value );
				}
			
			if ( checkedarray.size ( ) >= minimum_selected ){
				return checkedarray;
			}
			return false;
		},
		
		checkText: function ( e, len ) {
			if ( len == null ){
				len	= 1;
			}
			this.remove_field_error ( e.id );
			
			if ( !this.checkDataLength ( e.value, len ) )
				return this.do_field_error ( e.id, ucfirst ( e.id ) + ' dient minimaal ' + len + ' teken te bevatten.' );
			
			else if ( !( e.value ).match ( /^[a-zA-Z .,]+$/ ) )
				return this.do_field_error ( e.id, ucfirst ( e.id ) + ' bevat geen correcte tekens ( a-z, A-Z ).' );
			
			return true;
		},
		
		checkNumbers: function ( i, len ) {
	
			if ( len == null ){
				len = 1;
			}
			
			if ( !this.checkDataLength ( i, len ) ){
				return this.do_field_error ( i.id, ucfirst ( i.id ) + ' dient minimaal ' + len + ' teken te bevatten.' );
			}
			return true;
		},
		
		checkDataLength: function ( str, len ) {
			
			if ( len == null ){
				len	= 1;
			}
			if ( str.length < len ){
				return false;
			}
			return true;
		},
		
		validate_email: function ( e, check_unique ) {
			var email				= e.value;
			
			if ( !email.match ( /^[a-z]|[A_Z]|[0-9]/ ) )
				return false;
			
			validator_.remove_field_error ( e.id );
			
			ajax_request (
				this.validate_file,
				
				{
					func:	"validate_email",
					email:	email,
					unique:	bool ( check_unique )
				},
				
				function ( transport, json ) {
					if ( json [ 'valid' ] !== true ){
						throwOverlay('Error', json [ 'reason' ]);
						return validator_.do_field_error ( e.id, json [ 'reason' ] );
					}
						
				}
			);
			
			return true;
		},
		
		validate_passwords: function ( e ) {
			var passw1	= $F( 'wachtwoord1' );
			var passw2	= $F( 'wachtwoord2' );

			if ( !passw1.match ( /^[a-z]|[A_Z]|[0-9]/ ) )
				return false;
			
			if ( e.id == 'wachtwoord2' && this.field_is_okay ( 'wachtwoord1' ) === false )
				return false;
			
			validator_.remove_field_error ( e.id );
			//alert(e.id);
			
			ajax_request (
				this.validate_file,
				
				{
					func:	"validate_passwords",
					passw1:	passw1,
					passw2:	passw2
				},
				
				function ( transport, json ) {
					if ( json [ 'valid' ] !== true ){
						throwOverlay('Error', json [ 'reason' ]);
						return validator_.do_field_error ( e.id, json [ 'reason' ] );
					}
					
					}
			);
			
			return true;
		},
		
		validate_phonenumber: function ( func, net, abo, land, check_unique ) {

			var net		= $( net );
			var abo		= $( abo );
			var land	= $( land );
			
			if ( !( net.value ).match ( /^[0-9]+$/ ) || !( abo.value ).match ( /^[0-9]+$/ ) )
				return false;
			
			validator_.remove_field_error ( net.id );
			validator_.remove_field_error ( abo.id );
			
			ajax_request (
				this.validate_file,
				
				{
					func:		"validate_" + func,
					net:		net.value,
					abo:		abo.value,
					country:	land.value,
					unique:		bool ( check_unique )
				},
				
				function ( transport, json ) {
					if ( json [ 'valid' ] !== true ) {
						var arr = Array (
							net.id,
							abo.id
						);
						
						return validator_.do_field_error ( arr, json [ 'reason' ] );
					}
				}
			);
			
			return true;
		},
		
		validate_rekeningnummer: function ( nr, country ) {
			validator_.remove_field_error ( nr.id );
			
			if ( country == '' )
				return false;
			
			ajax_request (
				this.validate_file,
				
				{
					func:	"validate_rekeningnummer",
					nr:		nr.value,
					land:	country
				},
				
				function ( transport, json ) {
					if ( json [ 'valid' ] !== true )
						return validator_.do_field_error ( nr.id, json [ 'reason' ] );
				}
			);
			
			return true;
		}
		
	}
);

function check_field ( field ){
	var requiredSpecial = new Hash({'telefoonnetnummer':'mobielnetnummer', 'telefoonabonummer':'mobielabonummer'});
	
	if(!$F(field).blank()){
		requiredSpecial.each(function(h){
			if(h.value == $(field).readAttribute('id')){
				$(h.key).setStyle({
					background: ''
				});
			}
			
			if(h.key == $(field).readAttribute('id')){
				$(h.value).setStyle({
					background: ''
				});
			}
		});
		//alert($F(requiredSpecial.get($(field).readAttribute('id'))));
		if($(field).getAttribute('id') == "");
		$(field).setStyle({
			background: ''
		});
	}
	
}
