<!--
//Format the passed text to the required type (0 - Date, 1 - NIN, 2 - Upper, 3 - Proper, 4 - Each Word Proper)
function fmt (str, typ)
{
	//Dates
	if (typ == 0)
	{
		//Extract numbers only
		newstr = "";
		for (x=0; x<str.length; x++)
		{
			if (str.substr(x,1) >= '0' && str.substr(x,1) <= '9') newstr = newstr + str.substr(x,1);
		}

		if (newstr.length == 0) return "";

		//Check for valid length
		if (newstr.length >= 5 && newstr.length <= 8)
		{
			//DMMYY
			if (newstr.length == 5)
			{
				if (newstr.substr(0,1) == '0' || newstr.substr(1,2)*1 < 1 || newstr.substr(1,2)*1 > 12)
				{
					alert ('Invalid date format. Please make sure you enter 2 digits for the month and either 2 or 4 digits for the year');
					return "";
				} else {
					if (newstr.substr(3,2) * 1 < 10) {
						newstr = newstr.substr(0,1) + '/' + newstr.substr(1,2) + '/' + '20' + newstr.substr(3);
					} else {
						newstr = newstr.substr(0,1) + '/' + newstr.substr(1,2) + '/' + '19' + newstr.substr(3);
					}
				}
			}
			
			//DDMMYY
			if (newstr.length == 6)
			{
				if (newstr.substr(0,2)*1 < 1 || newstr.substr(0,2)*1 > 31 || newstr.substr(2,2)*1 < 1 || newstr.substr(2,2)*1 > 12)
				{
					alert ('Invalid date format. Please make sure you enter 2 digits for the month and either 2 or 4 digits for the year');
					return "";
				} else {
					if (newstr.substr(4,2) * 1 < 10) {
						newstr = newstr.substr(0,2) + '/' + newstr.substr(2,2) + '/' + '20' + newstr.substr(4);
					} else {
						newstr = newstr.substr(0,2) + '/' + newstr.substr(2,2) + '/' + '19' + newstr.substr(4);
					}
				}
			}

			//DMMYYYY
			if (newstr.length == 7)
			{
				if (newstr.substr(0,1) == '0' || newstr.substr(1,2)*1 < 1 || newstr.substr(1,2)*1 > 12)
				{
					alert ('Invalid date format. Please make sure you enter 2 digits for the month and either 2 or 4 digits for the year');
					return "";
				} else {
					if (newstr.substr(3,2) * 1 < 10) {
						newstr = newstr.substr(0,1) + '/' + newstr.substr(1,2) + '/' + newstr.substr(3);
					} else {
						newstr = newstr.substr(0,1) + '/' + newstr.substr(1,2) + '/' + newstr.substr(3);
					}
				}
			}
			
			//DDMMYYYY
			if (newstr.length == 8)
			{
				if (newstr.substr(0,2)*1 < 1 || newstr.substr(0,2)*1 > 31 || newstr.substr(2,2)*1 < 1 || newstr.substr(2,2)*1 > 12)
				{
					alert ('Invalid date format. Please make sure you enter 2 digits for the month and either 2 or 4 digits for the year');
					return "";
				} else {
					if (newstr.substr(4,2) * 1 < 10) {
						newstr = newstr.substr(0,2) + '/' + newstr.substr(2,2) + '/' + newstr.substr(4);
					} else {
						newstr = newstr.substr(0,2) + '/' + newstr.substr(2,2) + '/' + newstr.substr(4);
					}
				}
			}			
			
			if (newstr.length == 9) newstr = '0'+newstr;
			return newstr;

		}
		else
		{
			alert ('Invalid date format. Please make sure you enter 2 digits for the month and either 2 or 4 digits for the year');
			return "";
		}
	}

	//National Insurance Numbers
	if (typ == 1)
	{

		str = str.toUpperCase();
		//Strip out any non-letters or non-digits
		newstr = "";
		for (x=0; x < str.length; x++)
		{
			if ((str.substr(x,1) >= 'A' && str.substr(x,1) <= 'Z') || (str.substr(x,1) >= '0' && str.substr(x,1) <= '9')) newstr = newstr + str.substr(x,1); 
		}
		
		prob = 0;
		if (newstr.length >= 9)
		{
			if (newstr.substr(0,1) < 'A' || newstr.substr(0,1) > 'Z') prob = 1;
			if (newstr.substr(1,1) < 'A' || newstr.substr(1,1) > 'Z') prob = 1;
			if (newstr.substr(2,1) < '0' || newstr.substr(2,1) > '9') prob = 1;
			if (newstr.substr(3,1) < '0' || newstr.substr(3,1) > '9') prob = 1;
			if (newstr.substr(4,1) < '0' || newstr.substr(4,1) > '9') prob = 1;
			if (newstr.substr(5,1) < '0' || newstr.substr(5,1) > '9') prob = 1;
			if (newstr.substr(6,1) < '0' || newstr.substr(6,1) > '9') prob = 1;
			if (newstr.substr(7,1) < '0' || newstr.substr(7,1) > '9') prob = 1;
			if (newstr.substr(8,1) < 'A' || newstr.substr(8,1) > 'Z') prob = 1;
		}
		else
		{
			prob = 1;
		}
		
		if (prob == 0)
		{
			return newstr.substr(0,2) + '/' + newstr.substr(2,2) + '/' + newstr.substr(4,2) + '/' + newstr.substr(6,2) + '/' + newstr.substr(8,1);
		}
		else
		{
			alert ('Invalid National Insurance Number format');
			return "";
		}
	}
	
	//Upper case whole string
	if (typ == 2)
	{
		return str.toUpperCase();
	}

	//Proper case whole string (Initial letter capital, rest of sring lower case,   *** Added bit to capitalize initial letters of new 
	if (typ == 3)
	{
		status = 1;
		newstr='';
		//For every character in string...
		for (x=0; x<str.length; x++)
		{
			if (status == 1 && str.substr(x,1) != ' ')
			{
				newstr = newstr + str.substr(x,1).toUpperCase();
				status = 0;
			}
			else
			{
				newstr = newstr + str.substr(x,1).toLowerCase();
				if (str.substr(x,1) == '.') status = 1;
			}
		}
		return newstr;
	}
	
	//Proper case each word within string
	if (typ == 4)
	{
		space = 1;
		newstr = "";
		
		for (x=0; x<str.length; x++)
		{
			if (str.substr(x,1) == " ")
			{
				space = 1;
				newstr = newstr + " ";
			}
			else
			{
				if (space == 1) {newstr = newstr + str.substr(x,1).toUpperCase(); space = 0;}
				else newstr = newstr + str.substr(x,1).toLowerCase();
			}
		}
		return newstr;
	}
	
	//Currency formatting
	if (typ == 5)
	{
		dt = -1;
		newstr = '';
		//Strip out non digits and any repeated . s
		for (x=0; x<str.length; x++)
		{
			if (str.substr(x,1) >= '0' && str.substr(x,1) <= '9') newstr = newstr + str.substr(x,1);
			if (str.substr(x,1) == '.' && dt == -1)
			{
				if (newstr.length == 0) newstr = newstr + '0';
				newstr = newstr + '.';
				dt = newstr.length-1;
			}
		}
		
		if (newstr.length > 0)
		{
			if (dt == newstr.length-1) newstr = newstr + '00';
			if (dt == newstr.length-2 && newstr.length != 1) newstr = newstr + '0';
			if (dt == -1) {newstr = newstr + '.00'; dt = newstr.length-3;}

			if (newstr.length > dt + 3) newstr = newstr.substr(0,dt+3);
		
			//Now have a number ending in .00  work backwards thru and add , thousand groupings
			cnt = 0;
			for (x=dt-1; x--; x>=0)
			{
				cnt++;
				if (cnt == 3)
				{
					newstr = newstr.substr(0,x+1) + ',' + newstr.substr(x+1);
					cnt = 0;
				}									

			}

			return "£" + newstr;
		}
		else
		{
			return '£0.00';
		}
	}
	
	//Telephone numbers (for now have 4 digits, space, then rest
	if (typ == 6)
	{
		newstr = '';
		//Strip out non digits
		for (x=0; x<str.length; x++)
		{
			if (str.substr(x,1) >= '0' && str.substr(x,1) <= '9') newstr = newstr + str.substr(x,1);
		}	
		if (newstr.length > 5) return newstr.substr(0,5) + " " + newstr.substr(5);
		else return newstr;
	}
	
	//Just nos
	if (typ==7)
	{
		newstr = '';
		//Strip out non digits
		for (x=0; x<str.length; x++)
		{
			if (str.substr(x,1) >= '0' && str.substr(x,1) <= '9') newstr = newstr + str.substr(x,1);
		}
		return newstr;
	}
}
//-->
