﻿// Generic function to check the formfields clientside.
// ===================================================================== 
// Posible validators:
//  - required = required field
//  - requiredemail = required valid emailadres
//  - email = valid emailadres
// ----------------------------------------------------------------------
function CheckFormFields(oForm,sValidateSign)
{
	// set check form default ok (true)
	var blnCheckForm = true;

	// set variable for first error initial false
	var blnFirstError = false;
	
	// zet alle elementen in een array.
	var aElements = oForm.elements;

	// loup through all elements
	for (var i = 0; i < aElements.length; i++)
	{
		// try to make object of validator
		var oValidator = document.getElementById(aElements[i].id + "Validator");


		// check if there is a validator
		if (oValidator != null)
		{		
			var blnValidatorCheck = true;
			// set the value of the validator to required sign of empty

			if (oValidator.getAttribute("val").indexOf("required") == 0)
			{
				oValidator.innerHTML = sValidateSign;
			}
			else
			{
				oValidator.innerHTML = "";
			}
			// give the classes of the element and the validator the default classname
			document.getElementById(aElements[i].id).className = "";
			oValidator.className = "validator";

			// read the herefore introduced attribute val for what to be validated
			switch (oValidator.getAttribute("val"))
			{
				case "required":
					if (CheckFilled(aElements[i].value) == false)
					{
						oValidator.innerHTML = "Verplicht veld";
						document.getElementById(aElements[i].id).className = "validatorfield";
						oValidator.className = "validatorerror";
						blnValidatorCheck = false;
						blnCheckForm = false;
					}
					break;
				case "requiredemail":
					if (CheckEmail(aElements[i].value) == false)
					{
						oValidator.innerHTML = "Verplicht en geldig e-mailadres";
						document.getElementById(aElements[i].id).className = "validatorfield";
						oValidator.className = "validatorerror";
						blnValidatorCheck = false;
						blnCheckForm = false;
					}
					break;

				case "email":
					if (CheckFilled(aElements[i].value) == true && CheckEmail(aElements[i].value) == false)
					{
						oValidator.innerHTML = "Geen geldig e-mailadres";
						document.getElementById(aElements[i].id).className = "validatorfield";
						oValidator.className = "validatorerror";
						blnValidatorCheck = false;
						blnCheckForm = false;
					}
					break;

				default:
					// do nothing
			}
			
			if (blnValidatorCheck == false && blnFirstError == false)
			{
				document.getElementById(aElements[i].id).focus();
				blnFirstError = true;
			}
			
		}
	}
	return blnCheckForm;
}

// check if field is filled true=filled false=empty
// ---------------------------------------------------------------------------------------------------------
function CheckFilled(strVariable)
{
	if (strVariable == "")
		return false;
	else
		return true;
}


// Check valid emailadres
// ---------------------------------------------------------------------------------------------------------
function CheckEmail(strEmail)
{
	var emailPat = /^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/;
	var matchArray = strEmail.match(emailPat);
	if (matchArray == null)
	{
		return false;
	}
	var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (IPArray != null)
	{
		for (var i = 1; i <= 4; i++)
		{
			if (IPArray[i] > 255)
			{
				return false;
			}
		}
	}
	return true;
}
