function toggleField( element, enabled )
{
	element.disabled = !enabled;
	
	//ALEEN ALS CUSTOM ELEMENTS AAN STAAN	
	//Roep een functie aan in customelements.js om de custom elements juist weer te geven.
//	if(element.type == "radio" || element.type == "checkbox")
//	{
//		ce_toggleElements(element, element.disabled);
//	}
//	else
//	{
		if(element.disabled == true)
		{
			var labelName = element.name.replace(/(\[)(.+?)(\])$/, '');
			document.getElementById('in_'+labelName+'_label').style.display = 'none';
			
			element.style.display = 'none';
			
			var errorExists = document.getElementById('in_'+labelName+'_error');
			if(errorExists != null)
			{
				document.getElementById('in_'+labelName+'_error').style.display = 'none';
			}
			
			
			if(element.type == "radio"){
			   document.getElementById(element.id+'_label').style.display = 'none';
			}
		}
		else
		{
			var labelName = element.name.replace(/(\[)(.+?)(\])$/, '');
			document.getElementById('in_'+labelName+'_label').style.display = 'inline';
			element.style.display = 'inline';
			
			var errorExists = document.getElementById('in_'+labelName+'_error');
			if(errorExists != null)
			{
				document.getElementById('in_'+labelName+'_error').style.display = 'inline';
			}
			
			if(element.type == "radio"){
			   document.getElementById(element.id+'_label').style.display = 'inline';
			}
		}
		
//	}
	
}

function checkFieldStatus( form, fields, enabled )
{
	postRun = new Array();
	for( i =0; i < fields.length; i++ )
	{
		//dit is lastig, hebben we een enkel form field, een jsarray (bv meerdere checkboxes met dezelfde naam), of een phparray veld (datum)

		//phparray
		if( form.elements[fields[i]] == null )
		{
			//we gaan door de form iteraten om te kijken of er elementen zijn die beginnen met de opgegeven naam
			for( j = 0; j < form.elements.length; j++ )
			{
				//naan van veld moet beginnen met opgegeven naam, en char daarna moet een [ zijn.
				if( form.elements[j].name.indexOf( fields[i] ) == 0 && form.elements[j].name.charAt( fields[i].length ) == "[" )
				{
					toggleField( form.elements[j], enabled );
				}
			}
		}
		//normaal
		else if( form.elements[fields[i]].name != null )
		{
			toggleField( form.elements[fields[i]], enabled );
		}
		//jsarray
		else if( form.elements[fields[i]].length != null )
		{
			for( j = 0; j < form.elements[fields[i]].length; j++ )
			{
				toggleField( form.elements[fields[i]][j], enabled );
				if( form.elements[fields[i]][j].onclick != null)
				{
					if( form.elements[fields[i]][j].checked )
					{
						postRun.unshift( form.elements[fields[i]][j] );
					}
				}
			}
		}
	}

	for( i = 0; i < postRun.length; i++ )
	{
		postRun[i].click( );
	}
	
	
	
}

function triggerEnableField( id )
{
	document.getElementById( id ).click( );
}

function createFieldSubmit( frm )
{
	inp = document.createElement( "input" );
	inp.type = "hidden";
	inp.name = "__--fieldsubmit--__";
	inp.value = "1";
	frm.appendChild( inp );
}

var form_fields = Array();
var form_conditions = Array();
var form_field_subfields = Array();
var form_field_multipleValuesAllowed = Array();
var form_field_mulValues = Array();
var form_field_currentFocus = null;

function form_field_onkeypress(e)
{
	var key = 0;

	if (document.layers)
	{
		key = e.which;
	}
	else if (document.all)
	{
		key = window.event.keyCode;
	}
	else
	{
		key = e.which;
	}

	/* keyID = no special char  */
	if (form_field_currentFocus != null)
	{
		var field = document.forms['defaultForm'].elements[form_field_currentFocus];
		var value = field.value;
		var newValue = value;

		if (key == 8) /* Backspace */
		{
			if (value.length == 1)
			{
				value = '';
			}
		}
		else if (key >= 32) /* 32 = spacebar */
		{
			value += 'a';
		}

		/* This even is called before */
		form_value_changed(field, form_field_currentFocus, value, -1);
	}
}

document.onkeypress = form_field_onkeypress; 

function form_field_blur(fieldname)
{
	if (form_field_currentFocus == fieldname)
	{
		form_field_currentFocus = null;
	}
}

function form_field_focus(fieldname)
{
	form_field_currentFocus = fieldname;
}

function form_value_changed(field, fieldname, newValue, newIndex, isChecked)
{
	var form_fieldsEnabled = Array();

	var strValue = new String(newValue);

	var regex = / |\t|\r|\n/g;

	while(strValue.search(regex) != -1)
	{
		strValue = strValue.replace(" ", "");
		strValue = strValue.replace("\t", "");
		strValue = strValue.replace("\r", "");
		strValue = strValue.replace("\n", "");
	}

	if (form_field_multipleValuesAllowed[fieldname])
	{
		form_field_mulValues[fieldname][newIndex] = isChecked;
	}

	if (form_conditions[fieldname] /*&& form_conditions[fieldname].length > 0*/)
	{
		var index = newIndex;

		for(i in form_conditions[fieldname])
		{
			var condition = form_conditions[fieldname][i];

			if (condition['comparisonMethod'] == 'option_by_value')
			{
				if (
					(form_field_multipleValuesAllowed[fieldname] && form_field_mulValues[fieldname][condition['comparisonValue']])
				||	(!form_field_multipleValuesAllowed[fieldname] && newValue == condition['comparisonValue'])
				)
				{
					form_field_toggle(condition['destField'], true);

					form_fieldsEnabled[condition['destField']] = true;
				}
				else if (!form_fieldsEnabled[condition['destField']])
				{
					form_field_toggle(condition['destField'], false);
				}
			}
			else if (condition['comparisonMethod'] == 'no_value')
			{
				if (form_field_multipleValuesAllowed[fieldname])
				{
					strValue = '';

					for(subIndex in form_field_mulValues[fieldname])
					{
						if (form_field_mulValues[fieldname][subIndex] == true)
						{
							strValue = 'aan';
						}
					}
				}
				
				if (strValue == '')
				{
					form_field_toggle(condition['destField'], true);

					form_fieldsEnabled[condition['destField']] = true;
				}
				else if (!form_fieldsEnabled[condition['destField']])
				{
					form_field_toggle(condition['destField'], false);
				}
			}
			else if (condition['comparisonMethod'] == 'got_value')
			{
				if (form_field_multipleValuesAllowed[fieldname])
				{
					strValue = '';

					for(subIndex in form_field_mulValues[fieldname])
					{
						if (form_field_mulValues[fieldname][subIndex] == true)
						{
							strValue = 'aan';
						}
					}
				}

				if (strValue != '')
				{
					form_field_toggle(condition['destField'], true);

					form_fieldsEnabled[condition['destField']] = true;
				}
				else if (!form_fieldsEnabled[condition['destField']])
				{
					form_field_toggle(condition['destField'], false);
				}
			}
			else if (condition['comparisonMethod'] == 'option_by_number')
			{
				if (
					(form_field_multipleValuesAllowed[fieldname] && form_field_mulValues[fieldname][condition['comparisonValue']])
				||	(!form_field_multipleValuesAllowed[fieldname] && newIndex == condition['comparisonValue'])
				)
				{
					form_field_toggle(condition['destField'], true);

					form_fieldsEnabled[condition['destField']] = true;
				}
				else if (!form_fieldsEnabled[condition['destField']])
				{
					form_field_toggle(condition['destField'], false);
				}
			}
			else
			{
				alert( 'Unknown comparison method: ' + condition['comparisonMethod'] );
			}
		}
	}
}

function form_add_condition(sourceField, destField, comparisonMethod, comparisonValue)
{
	var condition = Array();
	condition['sourceField'] = sourceField;
	condition['destField'] = destField;
	condition['comparisonMethod'] = comparisonMethod;
	condition['comparisonValue'] = comparisonValue;

	if (!form_conditions[sourceField])
	{
		form_conditions[sourceField] = Array();
	}
	
	form_conditions[sourceField].push(condition);
/*	alert( 	condition['comparisonMethod'] +' toevoegen aan : ' + sourceField);*/
}

function form_field_setSubIndexValue(fieldname, index, value)
{
	if (!form_field_mulValues[fieldname])
	{
		form_field_mulValues[fieldname] = Array();
	}

	form_field_mulValues[fieldname][index] = value;
}

function form_field_setAllowMultipleValues(name)
{
	form_field_multipleValuesAllowed[name] = true;
	
	if (!form_field_mulValues[name])
	{
		form_field_mulValues[name] = Array();
	}
}

function form_field_add_subfield(name, subName, value)
{
	if (!form_field_subfields[name])
	{
		form_field_subfields[name] = Array();
	}

	form_field_subfields[name].push(subName);
}

var form_field_grabbingStates = true;
var form_field_initialState = Array();

function form_fields_done()
{
	form_field_grabbingStates = false;
}

function form_fields_reset()
{
	var name;

	for(name in form_field_initialState)
	{
		form_field_toggle(name, form_field_initialState[name]);
	}
}

function form_field_toggle(name, state, depth, parentName, optionID)
{
	if (!depth)
		depth = 1;

	parentName = parentName ? parentName : name;

	if (form_field_subfields[name] && depth != 2)
	{
		for (x in form_field_subfields[name])
		{
			form_field_toggle(form_field_subfields[name][x], state, depth+1, parentName, x);
		}
	}
	else
	{
		if (form_field_grabbingStates)
		{
			form_field_initialState[name] = state;
		}

		var form = document.getElementById('defaultForm');
		var elm = document.getElementById('in_' + name);

		if (elm)
		{
			if (state)
			{
				elm.disabled = false;
				elm.style.backgroundColor = '';
			}
			else
			{
				elm.value = '';
				elm.selectedIndex = -1;
				elm.disabled = true;
				elm.checked = false;
				elm.style.backgroundColor = '#EEEEEE';

				switch(elm.type)
				{
					case "text":
							form_value_changed(elm, name, elm.value, -1);
						break;

					case "select-one":
							form_value_changed(elm, name, elm.value, -1);
						break;

					case "radio":
							form_value_changed(elm, name, elm.value, -1, false);
						break;

					case "checkbox":
							form_value_changed(document.getElementById(parentName), parentName, '', optionID, false);
						break;

					default:
						break;
				}
			}
		}
	}
}

/* Tooltip functions: */

var cX = 0; 
var cY = 0;

function UpdateCursorPosition(e)
{ 
	cX = e.pageX; 
	cY = e.pageY;
}

function UpdateCursorPositionDocAll(e)
{ 
	cX = event.clientX; 
	cY = event.clientY;
}

if(document.all) 
{ 
	document.onmousemove = UpdateCursorPositionDocAll; 
}
else 
{ 
	document.onmousemove = UpdateCursorPosition; 
}

function AssignPosition(d) 
{
	d.style.left = (cX+10) + "px";
	d.style.top = (cY+10) + "px";
}

function HideContent(d) 
{
	if(d.length < 1) 
	{ 
		return; 
	}
	
	document.getElementById(d).style.display = "none";
}

function ShowContent(d) 
{
	if(d.length < 1) 
	{ 
		return; 
	}

	var dd = document.getElementById(d);
	AssignPosition(dd);
	dd.style.display = "";
}

function remark(d) 
{
	if(d.length < 1) 
	{ 
		return; 
	}
	
	var dd = document.getElementById(d);
	AssignPosition(dd);
	
	if (dd.style.display == "block") 
	{ 
		dd.style.display = "none"; 
	}
	else 
	{ 
		dd.style.display = "block"; 
	}
}		
