/* 	
==================================== 
	base functions formvalidation
	
	init:
	array to be checked
		var checkFields = new Array;
	
	class used for error messages
		var error_class = 'form_error';
	WARNING: only 1 input MUST be in a 
		container element (td, dd,..) for
		the error message to work
==================================== 
*/

var checkFields = new Array;
var error_class = 'form_error';

/* 	
==========================================
	new validator(
		id=id van field
		required=true/false,
		type='[validatie functie]/false',
		error='melding'
		(, optionele parameters (nodig voor sommige functies ))
		)
========================================== 
*/


function validator (id,req,type,error,params)
{
	this.id = id;
	this.req = req;
	this.type = type;
	this.error = error;
	this.params = params;
	this.inputValue = '';
}

function validate( x )
{
	//check single field
	if( x )
	{
		alert(x);
		var i;
		for(i = 0; i < checkFields.length; i++)
		{
			if( !checkFields[ x ].id ) 
			{
				alert( x.id + ' Field not in checkFields Array: ' + checkFields[x.id] );
				return false;
			}
			else
			{
				return validateField( x );
			}
		}
	}
	else  // check all fields in checkfields
	{
		var no_error = true;
		var i;
		
		for(i = 0; i < checkFields.length; i++)
		{
			f = document.getElementById( checkFields[i].id );
			
			if( f != null ) {
				checkFields[i].inputValue = f.value;
			
				if( ! validateField( checkFields[i] ) )
					no_error = false;
			}
		}
		rePopulate();
		
		return no_error ;
	}
}
/*
	Voor firefox
	om na tonen errormessages toch ingetypte waarden te tonen
*/
function rePopulate()
{
	var i;
	for(i = 0; i < checkFields.length; i++)
	{	
		f = document.getElementById( checkFields[i].id );
		
		if( f != null ) {
			f.value = checkFields[i].inputValue;
		}
	}
}

/**
 * input a validator object
 * returns true if valid
 */
function validateField( x )
{
	// required?
	field = document.getElementById( x.id );
	
	empty = validate_empty( field );
	
	if( x.req && !empty )
	{
		var error = 'empty error';
	}
	
	// Aparte validatie functie? (als niet leeg)
	
	if( x.type )
	{
		
		c = eval( 'validate_' + x.type +'( field );' ); 
		if( c == false )
			var error = 'validation error';
	}

	/*
		 display or remove error
	*/
	htmlpiece = field.parentNode;
	//eerdere melding weghalen
	for( var i = 0; i < htmlpiece.childNodes.length; i++)
	{
		if( htmlpiece.childNodes[i].className == error_class )
			htmlpiece.removeChild( htmlpiece.childNodes[i] );
	}

	if( error )
	{
		field.parentNode.innerHTML += '<div class="' + error_class + '">' + x.error +'</div>';
		return false;
	}else
		return true;
}

/* 	
==================================== 
	functions for types of validation		
==================================== 
*/

function validate_empty(field)
{
	with (field)
	{
		s = value;
		value = s.replace(/(^\s+)|(\s+$)/g, "")
		if (value==null||value=="")
		{
			return false;
		}
		else 
		{
			return true;
		}
	}
}

function validate_email(field)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		var contents = field.value;
		if (apos<1||dotpos-apos<2)
		{
			field.value = contents;
			return false;
		}
		else 
			return true ;
	}
}
/*
	functie om twee velden op dezelfde invoer te checken
*/
function validate_controlField(field)
{
	if(	checkFields[field.id].params != '' )
	{
		var firstField = document.getElementById( checkFields[field.id].params ) ;

		if(  firstField.value == field.value )
		{
			return true;
		}
		else
			return false;
		
	}
	else
		alert('geen params ingevuld!');
}

/*
	1234AB or 1234 AB
*/
function validate_postcode(field)
{
	with (field)
	{
		if( value.match(/[1-9][0-9]{3} ?[a-zA-Z]{2}/) == field.value )
			return true;
		else
			return false;
	}
}


/*
	datum formaat checken
	
	src: een of andere vage javascipt lib
*/
function validate_date(field)
{
	var strValue = field.value ;

	var objRegExp = /^\d{1,2}(\-)\d{1,2}\1\d{4}$/

	//check to see if in correct format
	if(!objRegExp.test( strValue ))
	
		return false; //doesn't match pattern, bad date
	
	else{
    
		var strSeparator = strValue.substring(2,3);
		
		var arrayDate = strValue.split(strSeparator); //split date into month, day, year
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
		                    '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
		var intDay = parseInt(arrayDate[1]);
		
		//check if month value and day value agree
		if(arrayLookup[arrayDate[0]] != null) {
		
			if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
		
				return true; //found in lookup table, good date
		}
		
		//check for February (bugfix 20050322)
		var intMonth = parseInt(arrayDate[0]);
		if (intMonth == 2) { 
			var intYear = parseInt(arrayDate[2]);
			if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
				return true; //Feb. had valid number of days
		}
	}
	return false; //any other values, bad date
}




