function userform_init()
{
	if (!document.getElementById) return false;

	if ($('userform'))
	{	
		form = $('userform');
		form.onsubmit = function() { return validate_form(this) };
	}
}

// needs rewrite
function validate_form(form)
{
	var i, string, error, is_filled, checked_names, exists, text, tmp, target;

	error = [];
	checked_names = [];
	for(i = 0; i < form.elements.length; i++)
	{
		string = form.elements[i].className;
		this_el = form.elements[i];
		if (string.indexOf('required') != -1)
		{
			el = this_el;
			if (el.nodeName.toLowerCase() == 'input')
			{
				if (el.type == 'text' || el.type == 'file')
				{
					if (el.value == '')
					{
						el = crawl_to_span(el);
						error.push('Verplicht veld niet ingevuld: ' + el.lastChild.nodeValue);
					}

				}
				else if (el.type == 'checkbox' || el.type == 'radio')
				{
					exists = false;
					for(k = 0; k < checked_names.length; k++)
					{
						if (checked_names[k] == el.name)
						{
							exists = true;
						}
					}

					if (exists == true)
					{
						continue;
					}

					checked_names.push(el.name);

					while(el.nodeName.toLowerCase() != 'p' && el.parentNode)
					{
						el = el.parentNode;
					}

					text = crawl_to_span(el);
					is_filled = false;
					inputs = el.getElementsByTagName('input');
					for (j = 0; j < inputs.length; j++)
					{						
						if (inputs[j].checked == true)
						{
							is_filled = true;
						}
					}

					if (is_filled == false)
					{
						error.push('Verplicht veld niet ingevuld: ' + text.lastChild.nodeValue);
					}
				}
			}
			else if (el.nodeName.toLowerCase() == 'textarea' && el.value == '')
			{
				el = crawl_to_span(el);
				error.push('Verplicht veld niet ingevuld: ' + el.lastChild.nodeValue);
			}
			else if (el.nodeName.toLowerCase() == 'select' && el.value == '')
			{
				el = crawl_to_span(el);
				error.push('Verplicht veld niet ingevuld: ' + el.lastChild.nodeValue);
			}
		}
		
		if (string.indexOf('email') != -1 && this_el.value != '')
		{
			if (!this_el.value.match(/^[a-z0-9.!\#$%&'*+-\/=?^_`{|}~]+@([0-9.]+|([a-z0-9._-]+\.+[a-z]{2,4}))$/i))
			{
				tmp = crawl_to_span(this_el);
				error.push('Ongeldige waarde ingevoerd bij: ' + tmp.lastChild.nodeValue + '```' + 'Dit veld moet een geldig e-mailadres zijn');
			}
		}
		
		if (string.indexOf('text_only') != -1 && this_el.value != '')
		{
			if (!this_el.value.match(/^[+a-z,.! -]+$/i))
			{
				tmp = crawl_to_span(this_el);
				error.push('Ongeldige waarde ingevoerd bij: ' + tmp.lastChild.nodeValue + '```' + 'Dit veld mag alleen tekst bevatten');
			}
		}

		if (string.indexOf('digits_only') != -1 && this_el.value != '')
		{
			if (!this_el.value.match(/^[0-9]+$/))
			{
				tmp = crawl_to_span(this_el);
				error.push('Ongeldige waarde ingevoerd bij: ' + tmp.lastChild.nodeValue + '```' + 'Dit veld mag alleen cijfers bevatten');
			}
		}
	}

	if (error.length)
	{
		target = document.getElementById('form-errorbox');

		if (document.getElementById('form-errors'))
		{		
			tmp = document.getElementById('form-errors');
			tmp.parentNode.removeChild(tmp);
		}

		ul = Construct.node('ul', {className:'content warn', id:'form-errors'});

		for(i = 0; i < error.length; i++)
		{			
			tmp = error[i].split('```');
			if (tmp.length == 2)
			{
				//alert(tmp[1]);
				ul.appendChild(Construct.node('li', tmp[0], [
					Construct.node('p',	[
						Construct.node('small', tmp[1])
						])
					])
				);
			}
			else
			{
				ul.appendChild(Construct.node('li', error[i]));
			}
		}

		target.appendChild(ul);
		window.scrollTo(0,-4000);

		return false;
	}

	return true;
}

addEvent(window, "load", userform_init);
