
	
	// on dom ready
	window.addEvent('domready', function(){

		// make the form work with AJAX and make it perform a check on the entered values before submit
		if (document.location.href.replace(/.*\/([^\/]*)$/, '$1') == 'contact.html')
		{
			ContactForm.initialize();
		}

	});


	// create the Contact object
	// works with one single form using a single submit button
	// will contact the server through AJAX and perform a check on the entered values before submit
	var ContactForm = {

		adaptorPath: 'scripts/contactAdaptor/',
// no localization implemented
//		languageCode: 'nl',
		fieldRequiredClassName: 'required',
		fieldEmailClassName: 'email',
		fieldErrorClassName: 'error',
		fieldEmptyOnClickClassName: 'emptyOnClick',
		fieldCanBeDefaultClassName: 'canBeDefault',
		onInitialize: window.contactFormOnInitialize,
		onRequest: window.contactFormOnRequest,
		onComplete: window.contactFormOnComplete,


		// initialize
		initialize: function()
		{
			/*
no localization implemented
			// reading language
			tempNode = document.createElement('script');
			tempNode.type = 'text/javascript';
			tempNode.src = this.adaptorPath + 'locale/' + this.languageCode + '.js';
			document.getElementsByTagName('head')[0].appendChild(tempNode);
			*/

			$('contactForm').addEvent('submit', this.submit.bind(this));

			// set form elements' click and blur events
			$$('#contactForm .' + this.fieldEmptyOnClickClassName).each(function(element){
				element.onclick = function(){if(this.defaultValue == this.value){this.value = ''}};
				element.onblur = function(){if(this.value.trim() == ''){this.value = this.defaultValue}};
			});

			if (typeof(this.onInitialize) == 'function')
			{
				this.onInitialize.run('', this);
			}
		},


		submit: function(event)
		{
			var errorMssg, formElements, postData = {};

			$E('input[type=image]').blur();

			if (errorMssg = this.checkForm())
			{
				alert(errorMssg);
				return false;
			}

			formElements = $E('fieldset').getElements('[name!=]');
			formElements.each(function(formElement){
					postData[formElement.name] = formElement.value;
			});
			postData.action = 'sendMail';

			new Request.JSON({
				url: this.adaptorPath + 'contactAdaptor.php',
				method: 'post',
				data: postData,
				onRequest: function(){
					if (typeof(this.onRequest) == 'function')
					{
						this.onRequest.run('', this);
					}
				}.bind(this),
				onComplete: function(responseJSON){
					if (typeof(this.onComplete) == 'function')
					{
						this.onComplete.run(responseJSON, this);
					}
					else
					{
						alert(responseJSON.message);
					}
				}.bind(this)
			}).send();

			return false;
		},


		// check whether the required fields have been filled in
		checkForm: function()
		{
			var errorMssg, email;
			var requiredElement, requiredElements = $$('.' + this.fieldRequiredClassName);
			var alternatives, alternative, alternativeIsSet = false;

			for (i = 0; requiredElement = requiredElements[i]; i++)
			{
				if (requiredElement.value.trim() == '' || (requiredElement.value == requiredElement.defaultValue && !requiredElement.hasClass(this.fieldCanBeDefaultClassName)))
				{
					alternatives = requiredElement.className.match(/.*alternatives=\[([^\]]*)\].*/);
					if (alternatives)
					{
						alternatives = alternatives[1].split(',');

						for (j = 0; alternative = $(alternatives[j]); j++)
						{
							if (!(alternative.value.trim() == '' || (alternative.value == alternative.defaultValue && !alternative.hasClass(this.fieldCanBeDefaultClassName))))
							{
								alternativeIsSet = true;
								break;
							}
						}

						if (alternativeIsSet)
						{
							requiredElement.removeClass(this.fieldErrorClassName);
						}
						else
						{
							requiredElement.addClass(this.fieldErrorClassName);
							errorMssg = 'U hebt nog niet alle relevante velden ingevuld.';
						}
					}
					else
					{
						requiredElement.addClass(this.fieldErrorClassName);
						errorMssg = 'U hebt nog niet alle relevante velden ingevuld.';
					}
				}
				else
				{
					requiredElement.removeClass(this.fieldErrorClassName);
				}
			}

			if (errorMssg)
			{
				return errorMssg;
			}

			if (email = $E('.' + this.fieldEmailClassName))
			{
				if (email.value != "" && email.value.search(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/) == -1)
				{
					email.addClass(this.fieldErrorClassName);
					errorMssg = 'Het opgegeven e-mail adres is ongeldig.';
				}
				else
				{
					email.removeClass(this.fieldErrorClassName);
				}
			}

			return errorMssg;
		}


	};


	// onInitialize function for the ContactForm
	// scope (this) is the ContactForm object
	function contactFormOnInitialize()
	{
		this.ajaxLoadImage = new Asset.image('images/ajaxLoadBgWhite.gif', {id: 'ajaxLoadImage'});
	}


	// onRequest function for the ContactForm
	// scope (this) is the ContactForm object
	function contactFormOnRequest()
	{
		this.targetFieldset = $E('fieldset');
		this.fieldsetFadeFx = new Fx.Tween(this.targetFieldset, 'opacity', {'duration': 700, transition: Fx.Transitions.Quad.easeInOut});

		this.targetFieldset.setStyle('height', this.targetFieldset.getSize().y - this.targetFieldset.getStyle('padding-top').toInt() - this.targetFieldset.getStyle('padding-bottom').toInt()); // fix height
		this.fieldsetFadeFx.start(0).chain(function(){
			this.targetFieldset.empty();
// IE throws error, so harcoding it
//			this.ajaxLoadImage.setStyle('margin-top', (this.targetFieldset.getSize().y - this.ajaxLoadImage.getStyle('height').toInt()) / 2);
			this.ajaxLoadImage.setStyle('margin-top', 50);
			this.ajaxLoadImage.inject(this.targetFieldset);
			this.fieldsetFadeFx.start(1);
		}.bind(this));
	}


	// onComplete function for the ContactForm
	// scope (this) is the ContactForm object
	function contactFormOnComplete(responseJSON)
	{
		// wrapping in anonymous function to apply delay
		(function(){

			this.fieldsetFadeFx.start(0).chain(function(){
				this.targetFieldset.set('html', (responseJSON ? responseJSON.message : 'Uw bericht kon niet worden verstuurd. Probeer a.u.b. opnieuw. Mocht de fout zich herhalen, contacteer ons dan a.u.b. via de e-mail link.'));
				(new Fx.Tween(this.targetFieldset, 'height', {'duration': 700, transition: Fx.Transitions.Quad.easeInOut})).start(20).chain(function(){
					this.fieldsetFadeFx.start(1);
				}.bind(this));
			}.bind(this));

		}).delay(1500, this);
	}

