if (!Array.prototype.each)
{
	Array.prototype.formFormEach = function(fun)
	{
		var len = this.length;

		if (typeof fun != "function")
		{
			throw new TypeError();
		}

		var thisp = arguments[1];

		for (var i = 0; i<len; i++)
		{
			if (i in this)
			{
				fun.call(thisp, this[i], i, this);
			}
		}
	};
}

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;

				var iterator = function(condition)
				{
					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'] );
					}
				}
			
				if (Array.prototype.each) /* for use with the protoype javascript library */
				{
					form_conditions[fieldname].each(iterator);
				}
				else /* without prototype */
				{
					form_conditions[fieldname].formFormEach(iterator);
				}
			}
		}

		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":
									elm.value = '';
									form_value_changed(elm, name, elm.value, -1);
								break;

							case "select-one":
									elm.value = '';
									form_value_changed(elm, parentName, elm.value, -1);
								break;

							case "radio":
									form_value_changed(elm, parentName, 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"; 
			}
		}		

