function validateEmail(email) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(email))
		return true
	else{
		return false
	}
}

function inArray(p_array, p_item)
{
	var i;
	
	for (i=0; i < p_array.length; i++) {
			if (p_array[i] === p_item)
				return true;
	}
	return false;
}

function validateform()
//validate fields
{
	errors = "";
	var ar_radio = [];
	var ar_fields = [];
	
	$("input, select").removeClass("input_error");
	
	//check required textfields
	$("input[type=text].required, input[type=text].requirednumeriek, input[type=text].requiredemail").each(function() {
			if ($(this).val()=="") {
				errors += "<li>" + this.name + " is een verplicht veld" + "</li>";

					ar_fields[ar_fields.length+1] = this.name;

				$(this).addClass("input_error");
			}
	});
	
	//check required textarea's	
	$(':input[type=textarea].required').each(function() {
			if ($(this).val()=="")
				errors += "<li>" + this.name + " is een verplicht veld" + "</li>";
	});
	
	//check required checkboxes	
	$("input[type=checkbox]:not(:checked).required").each(function() {
			if (!this.checked)
				errors += "<li>" + this.name + " is een verplicht veld" + "</li>";
	});	
	
	//check the required radioboxes
	$("input[type=radio].required, input[type=text].requirednumeriek").each(function() {
			if ( ($("input[name='"+this.name+"']:checked").val() == null) && (!inArray(ar_radio, this.name)) )
				errors += "<li>" + this.name + " is een verplicht veld" + "</li>";
				ar_radio[ar_radio.length+1] = this.name;
	});	
	
	//check the required comboboxes
	$("select.required").each(function() {
			if ( $(this).val() == "" )
				errors += "<li>" + this.name + " is een verplicht veld" + "</li>";	
	});
	
	//check email fields
	$("input[type=text].email, input[type=text].requiredemail").each(function() {
		if ( (!validateEmail( $(this).val())) && (!inArray(ar_fields, this.name)) )
			errors += "<li>'" + $(this).val() + "' is geen geldig email adres" + "</li>";
			ar_fields[ar_fields.length+1] = this.name;				
	});
	
	//check email fields
	$("input[type=text].numeriek").each(function() {
		if ( ($(this).val() != parseFloat($(this).val())) && (!inArray(ar_fields, this.name)) )
			errors += "<li>" + this.name + " mag alleen uit cijfers bestaan" + "</li>";
			ar_fields[ar_fields.length+1] = this.name;				
	});		

	return errors;
}

function submit_generatedform()
{
	//validate field.
	var errors = validateform();
	
	if (errors=="")
	{
		//remove unchecked boxes and create checkboxes for them
		$("input[type=checkbox]:not(:checked)").each(function() {
				alert(this.name);
				this.value = "null";
				this.checked = true;
		});
		
		//remove the send button, we dont want it to store in the data
		$("input[type=submit]").remove();

		return true;
		
	} else {
		if ($('#formerror').length == 0)
		{
			alert(errors);
		} else {
			$("#formerror").fadeIn();
			$("#formerror").html("<ul>" + errors + "</ul>");
		}
		
		return false;
	}
}