// $Id: profile.js,v 1.1.1.1 2009/07/31 20:58:34 cmanley Exp $
// This is included in the register and account/edit pages.
// Requires FormCtl.js and FormCtlSite.js

xAddEventListener(this, 'load', function() {
	var f = document.getElementById('FormProfile');
	var opts = {
		email: {
			func: FormCtl.prototype.Site.validateEmail,
			re: FormCtl.prototype.Site.reEmail
		},
		geslacht: {},
		voorletters: {},
		achternaam: {},
		adres: {},
		postcode: {
			chars: FormCtl.prototype.Site.charsPostalcodeNL,
			re: FormCtl.prototype.Site.rePostalcodeNL
		},
		plaats: {},
		gebdatum: {
			chars: FormCtl.prototype.Site.charsDateNL,
			func: FormCtl.prototype.Site.validateBirthDateNL
		},
		apotheek_naam: {},
		apotheek_plaats: {},
		acties: {},
		voorkeur: {}
	};
	if (f.elements['password']) {
		opts.password = {
			func: FormCtl.prototype.Site.validatePassword
		};
		opts.password2 = {
			func: FormCtl.prototype.Site.validatePassword
		};
	}
	new FormCtl(f, opts,
		{
			errorMsgRequired: FormCtl.prototype.Site.errorMsgRequired,
			errorMsgSyntax: FormCtl.prototype.Site.errorMsgSyntax,
			useEventHandlers: true,
			validateOnInit: true
		}
	);
});


var Actions = new (function() {
	this.post = function(f) {
		if (f.elements['password']) {
			if (f.elements['password'].value != f.elements['password2'].value) {
				f.elements['password'].value = '';
				f.elements['password2'].value = '';
				alert('De wachtwoorden zijn niet identiek. Vul ze nogmaals in aub');
				return false;
			}
		}
		if (!FormCtl.prototype.Site.formOnSubmit(f)) {
			return false;
		}
		if (f.elements['gebdatum']) {
			if (!f.elements['gebdatum'].value.match(FormCtl.prototype.Site.reDateISOStrict) && f.elements['gebdatum'].value.match(FormCtl.prototype.Site.reDateISO)) {
				var date = new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3)
				f.elements['gebdatum'].value = date.format('yyyy-mm-dd');
			}
		}
		/*
		if (!Postcode.checkPostcodeHnum(f)) {
			return false;
		}
		*/
		return true;
	}

})();


