/*
function validate()
The function validates form elements. The elements must be passed as arguments to the function.

If validation fails, an alert box will be shown with a description of the errors.
The text in the alert box is Danish.

When the user clicks OK to the alert, the first field, where validation fails, will have focus.

Arguments must be in the following format:

1: 	ID of element(s) to validate.
		If argument no. 3 is 'oneRequired', then this argument must be a comma delimited list
		containing the fields, out of which one must be filled in.
		I.E.: 'Field_1,Field_2,Field_3', 'Fields 1-3', 'oneRequired'
		This will test wheter one of the fields is filled in, and display the test 'Fields 1-3' if validation fails.
2: 	Text to display, if validation fails. Leave empty to show ID of element.
3: 	Test to perform on value of field(s).

Argument No. 3 must be one of the following:
R								: Value is required.
									Can be placed in front of any other validation test, so that the value is required, and also must pass the other test.
oneRequired			: One of the fields passed in argument no. 1, ID, must be filled in.
allIfAny				: All the fields passed in argument no. 1, ID, must be filled in, if any of the fields is filled in.
compareTo:Field	: Value must be the same as Field.Value
isEmail					: Value must be a valid email address.
isNum						: Value must be only numbers.
inRangeMin:Max	: Value must be a number in the range: min <= number <= max.
isDateSeparator	:	Value must be a valid date in the format 'dd[Separator]mm[Separator]yyyy', i.e. 'dd-mm-yyyy'.
									If Separator is omitted, '-' is used.
*/
function validate()
{
	var nm;
	var test;
	var errors			= '';
	var args				= validate.arguments;
	var focusField	= '';

  for (var i = 0; i < args.length - 1; i += 3)
	{
		nm		= args[i+1];
		test	= args[i+2];

		if (test.indexOf('oneRequired') != -1)
		{
			fields = args[i].split(',');
			
			if (fields.length > 0 && nm == '')
			{
				nm = fields[0].name;
			}
			
			var success = false;

			for (var j = 0; j < fields.length; j++)
			{
				field = MM_findObj(fields[j]);
				if (field.value != '')
				{
					success = true;
				}
				else
				{
					if (focusField == '')
					{
						focusField = field;
					}
				}
			}
			if (!success)
			{									
				errors += '- Et af felterne under \'' + nm + '\' skal være udfyldt.\n';
				/*
				if (focusField == '')
				{
					focusField = field;
				}
				*/
			}
		}
		else if (test.indexOf('allIfAny') != -1)
		{
			fields = args[i].split(',');
			
			if (fields.length > 0 && nm == '')
			{
				nm = fields[0].name;
			}
			
			var testForAll = false;
			
			for (var j = 0; j < fields.length; j++)
			{
				field = MM_findObj(fields[j]);
				if (field.value != '')
				{
					testForAll = true;
				}
			}

			var success = true;

			if (testForAll)
			{
				for (var j = 0; j < fields.length; j++)
				{
					field = MM_findObj(fields[j]);
					if (field.value == '')
					{
						success = false;
					}
					else
					{
						if (focusField == '')
						{
							focusField = field;
						}
					}
				}
			}

			if (!success)
			{									
				errors += '- Alle felterne under \'' + nm + '\' skal være udfyldt.\n';
				/*
				if (focusField == '')
				{
					focusField = field;
				}
				*/
			}
		}
		else
		{
			field	= MM_findObj(args[i]);
	
			if (nm == '')
			{
				nm = field.name;
			}
				
			if (field)
			{
				val = field.value;
				
				if (val != "")
				{
					if (test.indexOf('isEmail') != -1)
					{
						if (!validEmail(val))
						{
							errors += '- ' + nm + ' skal indeholde en gyldig email.\n';
							if (focusField == '')
							{
								focusField = field;
							}
						}
					}
					else if (test.indexOf('isNum') != -1)
					{
						if (isNaN(val))
						{
							errors += '- ' + nm + ' skal indeholde et tal.\n';
							if (focusField == '')
							{
								focusField = field;
							}
						}
					}
					else if (test.indexOf('inRange') != -1)
					{
						num = parseFloat(val);
						p = test.indexOf(':');
						min = test.substring(8, p);
						max = test.substring(p + 1);
						if (num < min || num > max)
						{
							errors += '- ' + nm + ' skal indeholde et nummer mellem \'' + min + '\' og \'' + max + '\'.\n';
							if (focusField == '')
							{
								focusField = field;
							}
						}
					}
					else if (test.indexOf('compareTo') != -1)
					{
						val;
						p = test.indexOf(':');
						field2 = test.substring(p + 1);
						
						nm = nm.split(',');
						
						field2 = MM_findObj(field2);
						
						if (field2)
						{
							if (val != field2.value)
							{
								errors += '- \'' + nm[1] + '\' skal indeholde samme værdi som \'' + nm[0] + '\'.\n';
								if (focusField == '')
								{
									focusField = field2;
								}
							}
						}
					}
					else if	(test.indexOf('isDate') != -1)
					{
						var separator = test.substring(7);
						if (separator = '')
						{
							separator = '-'
						}
						
						if (!validDate(val, separator))
						{
							errors += '- ' + nm + ' skal indeholde en gyldig dato med formatet dd' + separator + 'mm' + separator + 'yyyy.\n';
							if (focusField == '')
							{
								focusField = field;
							}
						}
					}
				}
				else if (test.indexOf('R') == 0)
				{
					errors += '- ' + nm + ' er obligatorisk.\n';
					if (focusField == '')
					{
						focusField = field;
					}
				}
			}
		}
	}

	if (errors != '')
	{
		alert('Disse fejl opstod:\n' + errors);
		document.getElementById(focusField.name).focus();
	}
  return (errors == '');
}

function validEmail(str)
{
	var atsign = str.indexOf('@') // get position of @ sign in string
	var dot = str.lastIndexOf('.')

	if ((atsign < 1) ||                    // '@' cannot be in first position
	    (dot <= atsign + 1) ||             // Must be at least one valid char btwn '@' and '.'
	    (str.charAt(dot - 1) == '.') ||	   // Two dots can not appear in consecutive positions
	    (dot == (str.length - 1)) ||       // Must be at least one valid char after '.'
	    (str.indexOf(' ')  != -1) ||       // No empty spaces permitted
	    (str.indexOf(',')  != -1) ||       // No commas permitted
	    (str.indexOf('"')  != -1) ||       // No double quotes permitted
	    (str.indexOf('\'')  != -1))        // No single quotes permitted
	   {  
	   return false;
	}
	return true;
}

function validDate(date, separator)
{
	var fejl = false;

	if (date.length == 10)
	{
		if (date.substring(2,3) == separator && date.substring(5,6) == separator)
		{
			var day	  = date.substring(0,2);
			var month = date.substring(3,5);
			var year  = date.substring(6,10);
			
			if (isNaN(day) || isNaN(month) || isNaN(year))
			{
				// dag, måned og år skal være tal
				fejl = true;
			}
			else
			{
				if (month >= 1 && month <= 12)
				{
					if (day >= 1 && day <= 31)
					{
						if (day > 28)
						{
							if ((month == 4 || 
									month == 6 || 
									month == 9 || 
									month == 11) && day == 31)
							{
								// 31 dage i de "korte" måneder
								fejl = true;
							}
							else if (month == 2)
							{
								// for mange dage i februar måned
								fejl = true;
								
								skudaar = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
								
								if (skudaar && day == 29)
								{
									// Hvis det er skudår, må det gerne være den 29. februar
									fejl = false;
								}
							}
						}
					}
					else
					{
						// dag mindre end 1 eller større end 31
						fejl = true;
					}
				}
				else
				{
					// måned er mindre end 1 eller større end 12
					fejl = true;
				}
			}
		}
		else
		{
			// forkert separator
			fejl = true;
		}
	}
	else
	{
		// forkert længde
		fejl = true;
	}
	return !fejl;
}

function MM_findObj(n, d)
{
  var p;
	var i;
	var x;
	
	if (!d)
		d = document;
	
	if ((p = n.indexOf("?")) > 0 && parent.frames.length)
	{
    d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}

  if (!(x = d[n]) && d.all)
		x = d.all[n];

	for (i = 0; !x && i < d.forms.length; i++)
		x = d.forms[i][n];
  
	for (i = 0; !x && d.layers && i < d.layers.length; i++)
		x = MM_findObj(n,d.layers[i].document);

  if(!x && d.getElementById)
		x = d.getElementById(n);
	
	return x;
}



