function ValidateForm (frm, fields)	{
	var vf = Validate(fields);
	var cf = CheckForm(frm);

	if (cf && vf) { return true; }
	else { return false; }
}
function ValidateRegisterForm (frm, fields)	{
	var vf = Validate(fields);
	var cf = CheckRegisterForm(frm, 'register');

	if (cf && vf) { return true; }
	else { return false; }
}

function ValidateProfileForm (frm, fields)	{
	var vf = Validate(fields);
	var cf = CheckRegisterForm(frm, 'profile');

	if (cf && vf) { return true; }
	else { return false; }
}

function ValidateChangeAddressForm (frm, fields)	{
	var vf = Validate(fields);
	var cf = CheckChangeAddressForm(frm);

	if (cf && vf) { return true; }
	else { return false; }
}

function ValidateRegisterEventForm (frm, fields)	{
	var vf = Validate(fields);
	var cf = CheckRegisterEventForm(frm);

	if (cf && vf) { return true; }
	else { return false; }
}


missings = new Array();
missings['geen_emailadres'] = 'U heeft geen email adres ingevuld.';
missings['geen_geldig_emailadres'] = 'Het ingevulde email adres is geen geldig email adres.';
missings['ongeldige_tekens_voor'] = 'Het emailadres bevat ongeldige tekens voor de @.';
missings['ongeldige_tekens_na'] = 'Het emailadres bevat ongeldige tekens na de @.';
missings['geen_naam'] = 'U heeft geen naam ingevuld.';
missings['geen_voorletters'] = 'U heeft geen voorletters ingevuld.';
missings['geen_voornaam'] = 'U heeft geen voornaam ingevuld.';
missings['geen_achternaam'] = 'U heeft geen achternaam ingevuld.';
missings['geen_telefoon'] = 'U heeft geen telefoonnummer ingevuld.';
missings['geen_paard'] = 'U heeft geen naam van uw paard ingevuld.';
missings['geen_vader_paard'] = 'U heeft geen naam van de vader van uw paard ingevuld.';
missings['geen_klasse'] = 'U heeft geen klasse ingevuld.';
missings['geen_wachtwoord'] = 'U heeft geen wachtwoord ingevuld.';
missings['geen_wachtwoord2'] = 'U heeft uw wachtwoord niet bevestigd.';
missings['onvolledig_factuuradres'] = 'U heeft het factuuradres niet compleet ingevuld.';
missings['onvolledig_afleveradres'] = 'U heeft het afleveradres niet compleet ingevuld.';
missings['onvolledig_adres'] = 'U heeft uw adres niet compleet ingevuld.';

function CheckForm (theForm) {
	if (theForm.NB_contactpersoon.value.trim() == "") {
		alert(missings['geen_naam']);
		theForm.NB_contactpersoon.focus();
		return (false);
	}
	if (!emailCheck(theForm.E_emailadres)){
		theForm.E_emailadres.focus();
		return (false);
	}
	return (true);
}

function CheckRegisterForm (theForm, theAction) {
	if (theForm.initials.value.trim() == "") {
		alert(missings['geen_voorletters']);
		theForm.initials.focus();
		return (false);
	}
	if (theForm.lastname.value.trim() == "") {
		alert(missings['geen_achternaam']);
		theForm.lastname.focus();
		return (false);
	}
	if (theForm.phone.value.trim() == "") {
		alert(missings['geen_telefoon']);
		theForm.phone.focus();
		return (false);
	}
	if (!emailCheck(theForm.loginname)){
		theForm.loginname.focus();
		return (false);
	}
	if(theAction == 'register'){
		if (theForm.password.value.trim() == "") {
			alert(missings['geen_wachtwoord']);
			theForm.password2.focus();
			return (false);
		}
		if (theForm.password2.value.trim() == "") {
			alert(missings['geen_wachtwoord2']);
			theForm.password2.focus();
			return (false);
		}
	}
	if (theForm.post_address.value.trim() == "") {
		alert(missings['onvolledig_factuuradres']);
		theForm.post_address.focus();
		return (false);
	}
	if (theForm.post_zipcode.value.trim() == "") {
		alert(missings['onvolledig_factuuradres']);
		theForm.post_zipcode.focus();
		return (false);
	}
	if (theForm.post_city.value.trim() == "") {
		alert(missings['onvolledig_factuuradres']);
		theForm.post_city.focus();
		return (false);
	}
	if (theForm.post_country.options[theForm.post_country.selectedIndex].value.trim() == "") {
		alert(missings['onvolledig_factuuradres']);
		theForm.post_country.focus();
		return (false);
	}
	if (theForm.delivery_address.value.trim() == "") {
		alert(missings['onvolledig_afleveradres']);
		theForm.delivery_address.focus();
		return (false);
	}
	if (theForm.delivery_zipcode.value.trim() == "") {
		alert(missings['onvolledig_afleveradres']);
		theForm.delivery_zipcode.focus();
		return (false);
	}
	if (theForm.delivery_city.value.trim() == "") {
		alert(missings['onvolledig_afleveradres']);
		theForm.delivery_city.focus();
		return (false);
	}
	if (theForm.delivery_country.options[theForm.delivery_country.selectedIndex].value.trim() == "") {
		alert(missings['onvolledig_afleveradres']);
		theForm.delivery_country.focus();
		return (false);
	}
	return (true);
}

function CheckRegisterEventForm (theForm) {
	if (theForm.voornaam.value.trim() == "") {
		alert(missings['geen_voornaam']);
		theForm.voornaam.focus();
		return (false);
	}
	if (theForm.achternaam.value.trim() == "") {
		alert(missings['geen_achternaam']);
		theForm.achternaam.focus();
		return (false);
	}
	if (theForm.adres.value.trim() == "") {
		alert(missings['onvolledig_adres']);
		theForm.adres.focus();
		return (false);
	}
	if (theForm.postcode.value.trim() == "") {
		alert(missings['onvolledig_adres']);
		theForm.postcode.focus();
		return (false);
	}
	if (theForm.woonplaats.value.trim() == "") {
		alert(missings['onvolledig_adres']);
		theForm.woonplaats.focus();
		return (false);
	}
	if (theForm.telefoon.value.trim() == "") {
		alert(missings['geen_telefoon']);
		theForm.telefoon.focus();
		return (false);
	}
	if (theForm.naam_paard1.value.trim() == "") {
		alert(missings['geen_paard']);
		theForm.naam_paard1.focus();
		return (false);
	}
	if (theForm.vader_paard1.value.trim() == "") {
		alert(missings['geen_vader_paard']);
		theForm.vader_paard1.focus();
		return (false);
	}
	if (theForm.klasse1.value.trim() == "") {
		alert(missings['geen_klasse']);
		theForm.klasse1.focus();
		return (false);
	}
	return (true);
}

function CheckChangeAddressForm (theForm) {
	if (theForm.voornaam.value.trim() == "") {
		alert(missings['onvolledig_afleveradres']);
		theForm.delivery_address.focus();
		return (false);
	}
	if (theForm.delivery_zipcode.value.trim() == "") {
		alert(missings['onvolledig_afleveradres']);
		theForm.delivery_zipcode.focus();
		return (false);
	}
	if (theForm.delivery_city.value.trim() == "") {
		alert(missings['onvolledig_afleveradres']);
		theForm.delivery_city.focus();
		return (false);
	}
	if (theForm.delivery_country.options[theForm.delivery_country.selectedIndex].value.trim() == "") {
		alert(missings['onvolledig_afleveradres']);
		theForm.delivery_country.focus();
		return (false);
	}
	return (true);
}

function trim(str){
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}

function Validate(check)
{
	var pass = true;
	var fields = new Array();
	var i = 0;
	var f = '';

	fields = check.split(',');

	for (i=0;i<fields.length;i++) {
		f = trim(fields[i]);
		/*
		This sets the "missing" class to the field instead of the parent (P element)
		document.getElementById(fields[i]).className = document.getElementById(fields[i]).className.replace('missing', '');
		*/
		document.getElementById(fields[i]).parentNode.className = document.getElementById(fields[i]).parentNode.className.replace('missing', '');

		/* Remove the added textual indication of an error */
		var els = document.getElementById(fields[i]).parentNode.getElementsByTagName('strong');
		for (j=0; j<els.length; j++)
		{
			document.getElementById(fields[i]).parentNode.removeChild(els[j]);
		}

		var errTag = document.createElement('strong');
		var errTxt = document.createTextNode(' !');
		/* End: Remove the added... */

		if (document.getElementById(fields[i]).value.trim() == '' || (document.getElementById(fields[i]).tagName == 'select' && document.getElementById(fields[i]).options[document.getElementById(fields[i]).selectedIndex].value)) {
			/*
			This sets the "missing" class to the field instead of the parent (P element)
			document.getElementById(fields[i]).className = document.getElementById(fields[i]).className + ' missing';
			*/
			document.getElementById(fields[i]).parentNode.className = document.getElementById(fields[i]).parentNode.className + ' missing';

			/* Add a textual indication of an error */
			document.getElementById(fields[i]).parentNode.appendChild(errTag);
			errTag.appendChild(errTxt);
			pass = false;
		}

		// Safety
		if (i>50) { break; }
	}
	return pass;
}
//--><!]]>


function emailCheck (obj) {
emailObj=obj;
emailStr = emailObj.value;
if(emailStr == "")
{
    alert ( missings['geen_emailadres'] );
    return false;
}
var checkTLD=0;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
alert(missings['geen_geldig_emailadres']);
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert(missings['ongeldige_tekens_voor']);
return false;
}
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert(missings['ongeldige_tekens_na']);
return false;
}
}
if (user.match(userPat)==null) {
alert(missings['geen_geldig_emailadres']);
return false;
}
/*
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Het IP adres klopt niet.");
return false;
}
}
return true;
}
*/
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert(missings['geen_geldig_emailadres']);
return false;
}
}
if (checkTLD && domArr[domArr.length-1].length!=2 &&
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert(missings['geen_geldig_emailadres']);
return false;
}
if (len<2) {
alert(missings['geen_geldig_emailadres']);
return false;
}
return true
}


