/*
 * Flaire Form Validation - v1.12
 *
 * LICENSE: This script is commercial software.
 * You may not copy, redistribute and/or modify it.
 *
 * Owner: ............ Flaire V.O.F.
 * Author: ........... Steven Voorn <svoorn@flaire.nl>
 * License: .......... Commercial
 * URL: .............. http://www.flaire.nl
 */

var _translation_c = function()
{
	this.form_o = {};
	this.language_s = 'nl';

	this.translation_a = [];

	this.translation_a['nl'] = [];
	this.translation_a['en'] = [];

	this.translation_a['nl']['notice_prefix'] = 'De volgende fouten zijn aangetroffen:';
	this.translation_a['en']['notice_prefix'] = 'The following errors occurred:';

	this.translation_a['nl']['notice_suffix'] = 'Corrigeer de fouten en probeer het a.u.b. opnieuw.';
	this.translation_a['en']['notice_suffix'] = 'Please, correct the errors and try again.';

	this.translation_a['nl']['notice_required_element_prefix'] = 'Het veld \' ';
	this.translation_a['en']['notice_required_element_prefix'] = 'The field \' ';

	this.translation_a['nl']['notice_required_element_suffix'] = ' \' is leeg.';
	this.translation_a['en']['notice_required_element_suffix'] = ' \' is empty.';

	this.translation_a['nl']['notice_required_email_prefix'] = 'Het veld \' ';
	this.translation_a['en']['notice_required_email_prefix'] = 'The field \' ';

	this.translation_a['nl']['notice_required_email_suffix'] = ' \' bevat een onjuist email adres.';
	this.translation_a['en']['notice_required_email_suffix'] = ' \' contains an incorrect email address.';

	/**/

	this.setLanguage = function()
	{
		this.language_s = (typeof this.form_o['__language__'] != 'undefined') ? this.form_o.__language__.value : this.language_s;
	};

	this.setFormObject = function(form_o)
	{
		this.form_o = form_o;
	};

	this.getTranslation = function(item_s)
	{
		return (typeof this.translation_a[this.language_s] != 'undefined') ? this.translation_a[this.language_s][item_s] : '';
	};
};

var _notification_c = function()
{
	this.form_o = {};
	this.translation_o = {};

	this.notice_a = [];

	/**/

	this.getNoticeCount = function()
	{
		return this.notice_a.length;
	};

	this.getNoticeNameByElement = function(element_o)
	{
		return (typeof element_o.getAttribute != 'undefined') ? (element_o.getAttribute('alt') ? element_o.getAttribute('alt') : element_o.name) : element_o.name;
	};

	/**/

	this.setNoticeLine = function(message_s)
	{
		this.notice_a[this.notice_a.length] = message_s;
	};

	this.setNoticeByRequiredElement = function(element_o)
	{
		this.setNoticeLine(this.translation_o.getTranslation('notice_required_element_prefix') + this.getNoticeNameByElement(element_o) + this.translation_o.getTranslation('notice_required_element_suffix'));
	};

	this.setNoticeByRequiredEmail = function(element_o)
	{
		this.setNoticeLine(this.translation_o.getTranslation('notice_required_email_prefix') + this.getNoticeNameByElement(element_o) + this.translation_o.getTranslation('notice_required_email_suffix'));
	};

	/**/

	this.setAlert = function()
	{
		if (this.getNoticeCount())
		{
			var alert_s = '';

				alert_s += this.translation_o.getTranslation('notice_prefix') + '\n';
				alert_s += '_________________________________________\n';
				alert_s += '\n';
				alert_s += this.notice_a.join('\n');
				alert_s += '\n';
				alert_s += '_________________________________________\n';
				alert_s += '\n';
				alert_s += this.translation_o.getTranslation('notice_suffix');

			window.alert(alert_s);

			return false;
		}

		return true;
	};

	/**/

	this.setTranslationObject = function(translation_o)
	{
		this.translation_o = translation_o;
	};

	this.setFormObject = function(form_o)
	{
		this.form_o = form_o;
	};
};

var _validation_c = function()
{
	this.notification_o = {};
	this.form_o = {};

	/**/

	this.getValidEmailAddress = function(address_s)
	{
		var regex_o = new RegExp('^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$');

		return regex_o.test(address_s);
	};

	/**/

	this.setValidateTextElement = function(element_o)
	{
		if (!element_o.value)
			this.notification_o.setNoticeByRequiredElement(element_o);
	};

	this.setValidateRadioElement = function(element_o)
	{
		if (!element_o.checked)
			this.notification_o.setNoticeByRequiredElement(element_o);
	};

	this.setValidateRadioGroup = function(element_o)
	{
		var checked_b = false;

		for (var index_i = 0; index_i < element_o.length; index_i++)
			if (element_o[index_i].checked)
				checked_b = true;

		if (!checked_b)
			this.notification_o.setNoticeByRequiredElement(element_o[0]);
	};

	this.setValidateSelectElement = function(element_o)
	{
		if (!element_o.options[element_o.selectedIndex].value)
			this.notification_o.setNoticeByRequiredElement(element_o);
	};

	this.setValidateCheckBoxElement = function(element_o)
	{
		if (!element_o.checked)
			this.notification_o.setNoticeByRequiredElement(element_o);
	};

	this.setValidateTextAreaElement = function(element_o)
	{
		if (!element_o.value)
			this.notification_o.setNoticeByRequiredElement(element_o);
	};

	this.setValidateArrayElement = function(name_s)
	{
		var element_o = false;
		var cached_element_o = false;
		var filled_b = false;

		for (i_i = 0; i_i < this.form_o.elements.length; i_i++)
		{
			element_o = this.form_o.elements[i_i];

			if (element_o.name.substr(0, name_s.length + 1) == name_s + '[')
			{
				if (element_o.name.charAt(element_o.name.length - 1) == ']')
				{
					cached_element_o = element_o;

					if (typeof element_o.nodeName != 'undefined')
					{
						switch (element_o.nodeName.toLowerCase())
						{
							case 'select': if (element_o.options[element_o.selectedIndex].value) filled_b = true; break;
							case 'textarea': if (element_o.value) filled_b = true; break;
							case 'input':

								switch (element_o.type.toLowerCase())
								{
									case 'password':
									case 'text': if (element_o.value) filled_b = true; break;
									case 'checkbox': if (element_o.checked) filled_b = true; break;
									case 'radio': if (element_o.checked) filled_b = true; break;
								}

								break;
						}
					}

					else if (element_o.length)
						if (element_o[0].type)
							if (element_o[0].type.toLowerCase() == 'radio')
								for (var radio_i = 0; radio_i < element_o.length; radio_i++)
									if (element_o[radio_i].checked)
										filled_b = true;
				}
			}
		}

		if (!filled_b)
			if (cached_element_o)
				this.notification_o.setNoticeByRequiredElement(cached_element_o.length ? cached_element_o[0] : cached_element_o);
	};

	/**/

	this.setEmailValidation = function()
	{
		if (typeof this.form_o.__sender_email__ != 'undefined')
		{
			var email_o = this.form_o[this.form_o.__sender_email__.value];
			var valid_b = this.getValidEmailAddress(email_o.value);

			if (!valid_b)
				this.notification_o.setNoticeByRequiredEmail(email_o);
		}
	};

	this.setRequiredElementValidation = function()
	{
		var element_name_s = '';
		var element_o = {};

		if (typeof this.form_o.__required__ != 'undefined')
		{
			var element_name_a = this.form_o.__required__.value.split(',');

			for (var i_i = 0; i_i < element_name_a.length; i_i++)
			{
				element_name_s = element_name_a[i_i].replace(/^[\s]+/g, '').replace(/[\s]+$/g, '');

				if (typeof this.form_o[element_name_s] != 'undefined')
				{
					element_o = this.form_o[element_name_s];

					if (typeof element_o.nodeName != 'undefined')
					{
						switch (element_o.nodeName.toLowerCase())
						{
							case 'select': this.setValidateSelectElement(element_o); break;
							case 'textarea': this.setValidateTextAreaElement(element_o); break;
							case 'input':

								switch (element_o.type.toLowerCase())
								{
									case 'password':
									case 'text': this.setValidateTextElement(element_o); break;
									case 'checkbox': this.setValidateCheckBoxElement(element_o); break;
									case 'radio': this.setValidateRadioElement(element_o); break;
								}

								break;
						}
					}

					else if (element_o.length)
						if (element_o[0].type)
							if (element_o[0].type.toLowerCase() == 'radio')
								this.setValidateRadioGroup(element_o);
				}
				else
				{
					this.setValidateArrayElement(element_name_s);
				}
			}
		}
	};

	/**/

	this.setNotificationObject = function(notification_o)
	{
		this.notification_o = notification_o;
	};

	this.setFormObject = function(form_o)
	{
		this.form_o = form_o;
	};
};

var _mutation_c = function()
{
	this.form_o = {};

	this.setReferrer = function()
	{
		if (typeof this.form_o['__referrer__'] == 'undefined')
		{
			var input_o = document.createElement('input');
				input_o.setAttribute('name', '__referrer__');
				input_o.setAttribute('type', 'hidden');
				input_o.setAttribute('value', document.location);

			this.form_o.appendChild(input_o);
		}
	};

	this.setSpamCheck = function()
	{
		if (typeof this.form_o['__spam_check__'] != 'undefined')
		{
			if (this.form_o['__spam_check__'])
			{
				var input_o = document.createElement('input');
					input_o.setAttribute('name', '__spam_check_result__');
					input_o.setAttribute('type', 'hidden');
					input_o.setAttribute('value', 'valid');

				this.form_o.appendChild(input_o);
			}
		}
	};

	this.setFormObject = function(form_o)
	{
		this.form_o = form_o;
	};
};

var getValidation = function(form_o)
{
	var translation_o = new _translation_c();
		translation_o.setFormObject(form_o);
		translation_o.setLanguage();

	var notification_o = new _notification_c();
		notification_o.setTranslationObject(translation_o);
		notification_o.setFormObject(form_o);

	var validation_o = new _validation_c();
		validation_o.setNotificationObject(notification_o);
		validation_o.setFormObject(form_o);
		validation_o.setEmailValidation();
		validation_o.setRequiredElementValidation();

	var mutation_o = new _mutation_c();
		mutation_o.setFormObject(form_o);
		mutation_o.setReferrer();
		mutation_o.setSpamCheck();

	return notification_o.setAlert();
};
