		var formValidateMessages = {
			required: {prefix: 'Du skal udfylde feltet "', postfix: '"!'},
			email: {prefix: 'Du skal udfylde feltet "', postfix: '" med en valid email adresse!'},
			date: {prefix: 'Du skal udfylde feltet "', postfix: '" med en valid dato (dd-mm-yyy)'}
			};
	
		function Validate(formName, options)
		{
			var picEle = document.getElementById('galleryPicture');
			//alert(picEle.value);
			
		
			var errMsg = FormValidate(formName, options);
			if(errMsg != null)
			{
				alert(errMsg);
				return false;
			}
			return true;
		}
	
		function FormValidateSetMessages(newMessages)
		{
			formValidateMessages = newMessages;
		}
	
		function FormValidate(formName, validationOptions)
		{
			var formObj = document.getElementById(formName);
			if(formObj == null)
				return 'Form could not be found';
				
			// validationOptions is a hashlist of the fieldnames to validate along with their validation-requirements.  Could for instance be
			//
			//{
			//	name: {required:true},
			//	email:{required: true, email: true}
			//}
								
			for(var fieldName in validationOptions)
			{
				var field = FormValidateGetField(fieldName, formObj);
				var fieldVal = null;
				
				if(field != null)
				{	
					switch(field.type)
					{						
						case 'text':
						case 'textarea':								
						case 'file':
							fieldVal = field.value;
							break;
						case 'select-one':
							fieldVal = field.options[field.selectedIndex].value;							
							break;							
					}
					
					if(fieldVal != null)
					{				
						var fieldOptions = validationOptions[fieldName];						
						if(fieldOptions['required'])
						{
								var emptyChk = new RegExp("^\s*$");
								if(fieldVal.replace(emptyChk, '') == '')
								{
									field.focus();
									return FormValidateGetErrorMsg(field, 'required');
								}
						}
						if(fieldOptions['date'])
						{
							if(!FormValidateDate(fieldVal))
							{
								field.focus();
								return FormValidateGetErrorMsg(field, 'date');
							}
						}
						if(fieldOptions['email'])
						{
							if(!FormValidateEmail(fieldVal))
							{
								field.focus();
								return FormValidateGetErrorMsg(field, 'email');
							}
						}
					}
				}
			}
			return null;
		}
	

		function FormValidateGetField(fieldName, formObj)
		{
			try
			{
				var field = document.getElementById(fieldName);
				if(field != null)
					return field;
			}
			catch(e){}			
			
			for(var i=0;i<formObj.elements.length;i++)
			{
				var field = formObj.elements[i];
				if(field.name == fieldName)
					return field;
			}
			return null;
		}				
		
		function FormValidateGetErrorMsg(field, validationtype)
		{
			var msg = formValidateMessages[validationtype]['prefix'];
			var label = document.getElementById(field.id + '_lbl');
			if(label != null)
			{
				var filterHtml = RegExp("<[^>]*>[^<]*</[^>]*>")				
				msg += label.innerHTML.replace(filterHtml, '');
			}
			if(formValidateMessages[validationtype] != null)
				msg += formValidateMessages[validationtype]['postfix'];
			return msg;
		}
		
		function FormValidateDate(strDate)
		{
			// Validates according to the format yyy-mm-dd 
			// (leapyear is not handled atm.)
			var numChk = ("^[0-9]+$");
			var split = strDate.split('-');
			if(split.length != 3)
			{
				return false;
			}
			
			var year = split[2];
			var month = split[1];
			var day = split[0];
			var number = year + month + day;
			if(number.match(numChk) == null)
			{
				return false;
			}
			if(year.length != 4 || month.length != 2 || day.length != 2)
				return false;
			
			if(month > 12)
				return false;
			
			// february (leapyear not handled atm)
			if(month == 2 && day > 28) 
			{
				return false;
			}// uneven months before 08 have 31 days	
			else if(month < 8 && (month % 2 > 0))
			{
				if(day > 31)
					return false;					
			} // even months after 08 have 31 days
			else if (month >= 8 && (month % 2 == 0))
			{
				if(day > 31)
					return false;
			}// all other months have 30 days
			else if(day > 30)
				return false;				
			
			return true;
		}
		
		function FormValidateEmail(strEmail)
		{
			var mailChk = new RegExp("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$");
			return (strEmail.match(mailChk) != null);
		}		
		
		
		function OpenWindow(height, width, url)
		{
			window.open(url, '', 'resizable=1,status=0,toolbar=0,height=' + height + ',width=' + width );
			return false;
		}		
		
		function OpenWindowWithScroll(height, width, url)
		{
			window.open(url, '', 'resizable=1,status=0,toolbar=0,scrollbars=1,height=' + height + ',width=' + width );
			return false;
		}						
