
//Confirmes right format for dates
function pp_date(field)
{
	var destination = document.getElementById(field);
	if (destination != null)
	{
		var formatedValue = destination.value;
		
		if (formatedValue.length == 0)
		{
			return;
		}
		
		formatedValue = formatedValue.replace(/[ ]+/g, '-');
		formatedValue = formatedValue.replace(/[.]+/g, '-');
		
		//1. If it contains only numbers
		//	1B. check length to see if there are trailing zeros
		//	1C. check if the year is in four digit format, add digits if not
		//	1D. Re-format; insert - between DD.MM.YYYY
		//OR
		//2. If it contains other stuff
		//	2B. Return
		
		for (var i = 0; i < formatedValue.length; i++)
		{
			if (formatedValue.charAt(i) >= 0 && formatedValue.charAt(i) <= 9 && formatedValue.charAt(i) != ' ')
			{
			
			}
			else
			{
				var li = formatedValue.lastIndexOf("-");
				var year = formatedValue.substr(li + 1);
				
				if (year.length < 4) 
				{
					if (year > 30)
						year = "19" + year;
					else
						year = "20" + year;
				}
				
				if (li != 0)
				{
					formatedValue = formatedValue.substr(0, li + 1) + year;
				}
				
				destination.value = formatedValue;
				
				return;
			}
		}
		
		if (formatedValue.length < 6)
		{
			alert("For at datoer skal tolkes riktig, må dager og måneder skrives med 0 forann, samt at årstallet må være med.");
			return;
		}
		
		var day = formatedValue.substr(0, 2);
		var month = formatedValue.substr(2, 2);
		var year = formatedValue.substr(4);
		
		if (day > 31)
			day = 31;
		if (month > 12) 
			month = 12;
		
		if (year.length < 4) 
		{
			if (year > 30)
				year = "19" + year;
			else
				year = "20" + year;
		}
		//Add separators
		formatedValue = day + "-" + month + "-" + year;
		destination.value = formatedValue;
	}
}

//Formats to proper time of day in 24-hour format (00:00)
function pp_time(field)
{
	var destination = document.getElementById(field);
	if (destination != null)
	{
		var formatedValue = destination.value;
		
		//1. If there are more than 5, discard the extras
		//2. Trim all non-number characters
		//3. If there are 3 or less, add zeros
		//4. Add : between the hour and the minute
		

		formatedValue = formatedValue.replace(/[^0-9]+/g, '');
		
		if (formatedValue.length > 5)
		{
			formatedValue = formatedValue.substr(0, 4);
		}
		
		if (formatedValue.length == 3)
		{
			formatedValue = "0" + formatedValue;
		}
		
		var hour = formatedValue.substr(0, 2);
		var min = formatedValue.substr(2,2);
		
		if (hour == 24)
			hour = "00";
		if (hour > 23)
			hour = 23;
			
		if (min > 59)
			min = 59;
		
		if (hour == null || hour == "")
			hour = "00";
		
		if (min == null || min == "")
			min = "00";
		
		formatedValue =  hour + ":" + min;
		
		//Set the new text
		destination.value = formatedValue;
		return formatedValue;
	}
	return "00:00";
}

//Trims away everything but numbers
function pp_numbersOnly(field)
{
	var destination = document.getElementById(field);
	if (destination != null)
	{
		var formatedValue = destination.value;
		//destination.value = destination.value.replace(/[^0-9]+/g, '');
	}
}

//Fixes mobile phone numbers
function pp_phoneno(field)
{
	var destination = document.getElementById(field);
	if (destination != null)
	{
		var formatedValue = destination.value;
		if (formatedValue != null && formatedValue != "")
		{
			formatedValue = formatedValue.replace(/[^0-9]+/g, '');
			destination.value = formatedValue;
			var first = formatedValue.substr(0,1);
			if ( (first != '4' && first != '9') || formatedValue.length < 8)
			{
				alert("Telefonnummer må begynne på 4 eller 9, og må være minst 8 siffer!");
			}
		}
	}
}
