// JS
function ClassFormChecker(oForm)
{
	this.Form = oForm
	
	this.Initialize = function()
	{
		this.SetRegExp()
		this.SetErrorMessages()
		this.DebugOn = false
		this.Error = ''
	}
	
	this.SetErrorMessages = function()
	{
		this.ErrorMessages = new Array()
		this.ErrorMessages['VUL_IN'] = 'Fill in your <*omschrijving*>.\n'
		this.ErrorMessages['TE_KORT'] = '<*omschrijving*> is too short.\n'
		this.ErrorMessages['NIET_CORRECT'] = '<*omschrijving*> was not filled in correctly.\n'
		this.ErrorMessages['SELECTEER_EEN'] = 'Select a <*omschrijving*>.\n'
		this.ErrorMessages['VINK_AAN'] = 'Select at least <*omschrijving*>.\n'
	}
	
	this.SetRegExp = function()
	{
		this.RegExp = new Array()
		this.RegExp['Tekst'] = /^[a-zA-Z \.\,\-]{1,255}$/
		this.RegExp['Email'] = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
		this.RegExp['Telefoon'] = /^[0-9 \-]{10,11}$/
		this.RegExp['Adres'] = /^[a-zA-Z0-9]{4,100}$/
		this.RegExp['Postcode'] = /^[0-9]{4}[ ]*[a-zA-Z]{2}$/
		this.RegExp['Datum'] = /^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{2,4}$/
	}
	
	this.CheckInput = function(sName, sOmschrijving, sRegExp, nMinLength)
	{
		try
		{
			var sErrorLocal = ''
			oInput = this.Form[sName]
			
			if ( oInput.value == '' && sErrorLocal == '' && nMinLength )
				sErrorLocal += this.CreateErrorMessage('VUL_IN', sName, sOmschrijving)

			if ( oInput.value.length < nMinLength && sErrorLocal == '' )
				sErrorLocal += this.CreateErrorMessage('TE_KORT', sName, sOmschrijving)

			if ( sRegExp != null && sRegExp != '' )
				if ( oInput.value != '' && this.RegExp[sRegExp].exec(oInput.value)+'' == 'null' && sErrorLocal == '' )
					sErrorLocal += this.CreateErrorMessage('NIET_CORRECT', sName, sOmschrijving)

			if ( sErrorLocal != '' )
				this.AddError(sErrorLocal)
		}
		catch(e)
		{
			this.Debug(e.message, 'CheckInput', sName, sOmschrijving, sRegExp, nMinLength)
		}
	}
	
	this.CheckSelect = function(sName, sOmschrijving)
	{
		try
		{
			var sErrorLocal = ''
			oInput = this.Form[sName]
			if ( oInput.value == '' && sErrorLocal == '' )
				sErrorLocal += this.CreateErrorMessage('SELECTEER_EEN', sName, sOmschrijving)

			if ( sErrorLocal != '' )
				this.AddError(sErrorLocal)

		}
		catch(e)
		{
			this.Debug(e.message, 'CheckSelect', sName, sOmschrijving)
		}
	}
	
	this.CheckRadio = function(sName, sOmschrijving)
	{
		try
		{
			var sErrorLocal = ''
			oInput = this.Form[sName]
			bChecked = false
			if ( oInput.length )
			{
				for (var i = 0; i<oInput.length; i++ )
				{
					if ( oInput[i].checked == true ) bChecked = true
				}
			}
			else
			{
				bChecked = oInput.checked
			}
			if ( bChecked == false && sErrorLocal == '' )
				sErrorLocal += this.CreateErrorMessage('VINK_AAN', sName, sOmschrijving)

			if ( sErrorLocal != '' )
				this.AddError(sErrorLocal)

		}
		catch(e)
		{
			this.Debug(e.message, 'CheckSelect', sName, sOmschrijving)
		}
	}

	this.CheckCheckbox = function(sName, sOmschrijving, nMinimaalAantal)
	{
		try
		{
			if ( !nMinimaalAantal ) nMinimaalAantal = 1
			var sErrorLocal = ''
			oInput = this.Form[sName]
			if ( oInput.length )
			{
				var nChecked = 0
				for (var i = 0; i<oInput.length; i++ )
				{
					if ( oInput[i].checked == true ) nChecked ++
				}
				bChecked = ( nChecked >= nMinimaalAantal )
			}
			else
			{
				bChecked = oInput.checked
			}
			if ( bChecked == false && sErrorLocal == '' )
				sErrorLocal += this.CreateErrorMessage('VINK_AAN', sName, sOmschrijving)

			if ( sErrorLocal != '' )
				this.AddError(sErrorLocal)

		}
		catch(e)
		{
			this.Debug(e.message, 'CheckSelect', sName, sOmschrijving)
		}
	}
	
	this.CreateErrorMessage = function(sMessageKey, sName, sOmschrijving)
	{
		var sMessage = ''

		if ( this.ErrorMessages[sName + '_' + sMessageKey] )
			sMessage = this.ErrorMessages[sName + '_' + sMessageKey]
		else
			sMessage = this.ErrorMessages[sMessageKey]

		return this.Replace(sMessage, '<*omschrijving*>', sOmschrijving)
	}
	
	this.AddError = function(sError)
	{
		this.Error += sError
	}
	
	this.GetErrors = function()
	{
		if ( this.Error != '' )
		{
			alert( this.Error )
			return false
		}
		else
		{
			return true
		}
	}

	this.Debug = function(sDebug)
	{
		if ( this.DebugOn == true )
		{
			if ( sDebug == "'this.RegExp[...]' is null or not an object" ) sDebug += '\n\nJe geeft de verkeerde regexp aan (derde parameter van de Check functie)'
			if ( sDebug == "'oInput.value' is null or not an object" ) sDebug += '\n\nJe geeft de verkeerde inputname aan (eerste parameter van de Check functie)'
			if ( arguments.length > 1 )
			{
				sDebug += '\n\n' + arguments[1] + '('
				if ( arguments.length > 2 )
				{
					sKomma = ''
					for(i=2;i<arguments.length;i++)
					{
						if ( arguments[i] ) 
						{
							sDebug += sKomma + (this.isInteger(arguments[i]) ? arguments[i] : '\'' + arguments[i] + '\'')
							sKomma = ', '
						}
					}
				}
				sDebug += ')'
			}
			alert('Debug info:\n' + sDebug)
		}
	}

	this.isInteger = function(s)
	{
		var i
		s += ''
		for (i = 0; i < s.length; i++)
		{   
			var c = s.charAt(i)
			if (c < "0" || c > "9") return false
		}
		return true
	}
	
	this.Replace = function(string, find, replace)
	{
		return string.split(find).join(replace)
	}
	
	this.Initialize()
}

	
function CheckSubmit(oForm)
{
	if ( CheckForm(oForm) == true ) oForm.submit()
}