
// checks field input
// note: adds state flag with value 1 if input is accepted
function form_input_check(field_element, info_type)
{	
	
	info_state = false;
	
	// email
	if (info_type == 'email')
	{
	
		// step 1: replace illegal characters
		// note: allows typing of alphanumeric, hyphens, underscores, dots and at (@)
		while (field_element.value.match(/[^-._@0-9a-zA-Z]+/) != null)
		{																																
		  field_element.value = field_element.value.replace(/[^-._@0-9a-zA-Z]+/g, '');
		}
		
		// step 2: validate as proper email address
		//if(field_element.value.match(/^[-._0-9a-zA-Z]+@[-._0-9a-zA-Z]+\.[a-zA-Z]{2,6}$/) != null)
		if(field_element.value.match(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.[a-zA-Z]{2,6}$/i) != null)
		{         
 			info_state = true;
		}

	}
	
	// phone
	if (info_type == 'phone')
	{
	
		// step 1: replace illegal characters
		// note: allows typing of numeric, hyphens, plus-sign, spaces and brackets (())
		while (field_element.value.match(/[^-+ ()0-9]+/) != null)
		{																																
		  field_element.value = field_element.value.replace(/[^-+ ()0-9]+/g, '');
		}
		
		// step 2: validate as proper phone number
		// note: any ten numbers will do
		if(field_element.value.replace(/[^0-9]+/g, '').length == 10)
		{         
 			info_state = true;
		}

	}
	
	// text
	if (info_type == 'text')
	{
	
		// validate as alphanumeric string of 1 character or more
		// note: this ignores non-alphanumeric, assuming there is at least 1 normal number or letter in the text
		if(field_element.value.replace(/[^0-9a-z]+/ig, '').length > 0)
		{         
 			info_state = true;
		}

	}
	
	
	// add flags
	field_element.flag_info_state = 0;
	
	
	
	// update field appearance and set field status
	if (info_state == true)
	{
		// set border and background color of field
		field_element.style.borderColor = 'green';
		field_element.style.backgroundColor = '#d6fac0';
		
		// adjust the state field
		//document.getElementById(field_element.name + '_state').value = '1';
		
		// adjust the state flag
		field_element.flag_info_state = 1;
	}
	else
	{
		// set border and background color of field
		field_element.style.borderColor = 'red';
		field_element.style.backgroundColor = '#fdd3d3';
		
		// adjust the state flag
		field_element.flag_info_state = 0;
	}
	
}



// submit a form
// note: submittin is blocked when a required field has no state flag of 1
// note: this means a required field must always have an input check or the form cannot be submitted
function form_submit(form_id, required_field_id_list)
{

	// check if form exists
	if (!document.getElementById(form_id))
	{
		alert ('Geen formulier gevonden met id "' + form_id + '"!');
	}
	
	// check if all required fields are set
	required_field_id_list = required_field_id_list.split(' ');
	
	if (required_field_id_list.length > 0)
	{
		var all_fields_state_check = true;
		
		for (var i in required_field_id_list)
		{
			if(!(document.getElementById(required_field_id_list[i]) && document.getElementById(required_field_id_list[i]).flag_info_state && document.getElementById(required_field_id_list[i]).flag_info_state == 1)) all_fields_state_check = false;
		}
		
		// block submmit
		if(all_fields_state_check == false)
		{
			alert('Niet alle informatie is goed ingevuld!');
			return false;
		}
		
	}
	
	// submit form
	document.getElementById(form_id).submit();
	
}



