ui.validation = {
	cell_types: [
		"A", "A1", "A2", "A3", "A4", "A5", "A6",
		"N", "NL", "N2", "N3", "N4", "NE", "NE2",
		"P", "P2", "P3", "P4", "M", "M2", "M3", "M4",
		"D", "T", "K", "S", "IP4", "PW2", "EM", "WS"
	],
	
	needsValidCheck: function(type)
	{
		for(var i in ui.validation.cell_types)
		{
			if(ui.validation.cell_types[i] == type)
			{
				return true;
			}
		}
		
		return false;
	},
	
	isValidInputKey: function(keycode)
	{
		return !((keycode == 35) || (keycode == 36) || (keycode == 37) || (keycode == 39) || (keycode == 9) || (keycode == 13) || (keycode == 16))
	},
	
	isValid: function(str, type, arg1, arg2)
	{
		var result;
		var regexp;
		
		if(arg1 != null)
		{
			arg1 = parseInt(arg1 + "");
		}
		if(arg2 != null)
		{
			arg2 = parseInt(arg2 + "");
		}
		
		switch(type.toUpperCase())
		{
			//The input string must be a valid date. (Format: DD-MM-YYYY)
			case "D":
				regexp = "(\\d{1,2}-\\d{1,2}-\\d{0,4})|(\\d{1,2}-\\d{0,3})|(\\d{0,3})";
				break;
			
			//The input string must be a valid IPv4 address. (Format: xxx.xxx.xxx.xxx, 1-255)
			case "IP4":
				regexp = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.)|(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(\\d{1,3}\\.\\d{1,3}\\.)|(\\d{1,3}\\.\\d{1,3})|(\\d{1,3}\\.)|(\\d{1,3})";
				break;
			
			//The input string must be a valid string containing only letters.
			//arg1: The maximum length of the string.
			case "A":
				regexp = "([a-zA-Z]){0," + arg1 + "}";
				break;
			
			//The input string must be a valid alphanumerical string.
			//arg1: The maximum length of the string.
			case "A1":
			case "A2":
			case "A3":
			case "A4":
			case "A6":
			case "PW2":
				regexp = "(\\w|\\s|\\W){0," + arg1 + "}";
				break;
			
			//The input string may contain letters, numbers, underscores, the pipe symbol (|) and dashes (-).
			case "A5":
				regexp = "^([a-zA-Z0-9_]|-)*";
				break;
			
			//The input string must be a positive number.
			//arg1: The amount of digits before the decimal sign.
			//arg2: The amount of digits after the decimal sign.
			case "N":
			case "NL":
				if(arg2 > 0)
				{
					regexp = "((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
		  		else
		  		{
		  			regexp = "\\d{0," + arg1 + "}";
		  		}
				break;
			
			//The input string must be a number (positive or negative).
			//arg1: The amount of digits before the decimal sign.
			//arg2: The amount of digits after the decimal sign.
			case "N2":
				if(arg2 > 0)
				{
					regexp = "((-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(-?\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
				else
				{
					regexp = "-?\\d{0," + arg1 + "}";
				}
				break;
			
			//?
			case "N3":
				if(arg2 > 0)
				{
					regexp = "((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
				else
				{
					regexp = "\\d{0," + arg1 + "}";
				}
				break;
			
			//?
			case "N4":
				if(arg2 > 0)
				{
					regexp = "((-\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(-?\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
				else
				{
					regexp = "-?\\d{0," + arg1 + "}";
				}
				break;
			
			//The input string must be a valid amount.
			//arg1: The amount of digits before the decimal sign.
			//arg2: The amount of digits after the decimal sign.
			case "NE":
				if(arg2 > 0)
				{
					regexp = "((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
				else
				{
					regexp = "\\d{0," + arg1 + "}";
				}
				break;
			
			//The input string must be a valid amount. Negative numbers allowed.
			//arg1: The amount of digits before the decimal sign.
			//arg2: The amount of digits after the decimal sign.
			case "NE2":
				if(arg2 > 0)
				{
					regexp = "((-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(-?\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
				else
				{
					regexp = "-?\\d{0," + arg1 + "}";
				}
				break;
			
			//The input string must be a valid string.
			//For "M" this is a text field.
			//For "M2" this is a HTMLArea (or TinyMCE) field.
			case "M":
			case "M2":
				regexp = "(\\w|\\s|\\W)*";
				break;
			
			//?
			case "B":
				regexp = "^j$|^J$|^n$|^N$";
				break;
			
			//The input string must be a valid Dutch car license plate. (Format: XX-YY(Y)-ZZ or XX-(YYY) or XX)
			case "K":
				regexp = "(\\w{2}-\\w{2,3}-\\w{0,2})|(\\w{2}-\\w{0,3})|(\\w{0,2})";
				break;
			
			//The input string must be a valid Dutch BSN number. Format: 123456789
			case "S":
				regexp = "\\d{0,9}";
				break;
			
			//The input string must be a valid e-mail address.
			//TODO: Change the regular expression so that it validates the e-mail address.
			case "EM":
				regexp = "(\\w|\\s|\\W)*";
				break;
			
			//The input string must be a valid URI.
			//TODO: Change the regular expression so that it validates a website URI.
			case "WS":
				regexp = "(\\w|\\s|\\W)*";
				break;
			
			//?
			case "CB":
				regexp = "(\\w|\\s|\\W)*";
				break;
			
			//The input string must be a percentage.
			//arg1: The amount of digits before the decimal sign.
			//arg2: The amount of digits after the decimal sign.
			case "P":
				if(arg2 > 0)
				{
					regexp = "((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
				else
				{
					regexp = "\\d{0," + arg1 + "}";
				}
				break;
			
			//The input string must be a percentage. Negative numbers allowed.
			//arg1: The amount of digits before the decimal sign.
			//arg2: The amount of digits after the decimal sign.
			case "P2":
				if(arg2 > 0)
				{
					regexp = "((-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(-?\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
				else
				{
					regexp = "-?\\d{0," + arg1 + "}";
				}
				break;
			
			//The input string must be a percentage. The maximum value is 1000%
			//arg1: The amount of digits before the decimal sign.
			//arg2: The amount of digits after the decimal sign.
			case "P3":
				if(arg2 > 0)
				{
					regexp = "((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
				else
				{
					regexp = "\\d{0," + arg1 + "}";
				}
				break;
			
			//The input string must be a percentage. Negative numbers allowed. The maximum value is 1000%
			//arg1: The amount of digits before the decimal sign.
			//arg2: The amount of digits after the decimal sign.
			case "P4":
				if(arg2 > 0)
				{
					regexp = "((-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "})(\\,|\\.)(\\d{0," + arg2 + "}))|(-?\\d{0," + (arg1 + 1).toString() + "}(\\.|\\,)*)";
				}
				else
				{
					regexp = "-?\\d{0," + arg1 + "}";
				}
				break;
			
			//The input string must be a valid timestamp. Format: hh:mm:ss)
			case "T":
				regexp = "(\\d{1,2}:\\d{1,2}:\\d{0,3})|(\\d{1,2}:\\d{1,2})|(\\d{1,2}:\\d{0,3})|(\\d{0,3})|(\\d{1,2}:\\d{1,2}:\\d{0,3})";
				break;
		}
		
		try
		{
			regexp = new RegExp(regexp, "g");
			result = regexp.exec(str);
		}
		catch(e)
		{
			alert(e.message);
		}
		
		return result != null && result[0] == str;
	},
	
	transform: function(str, type, arg1, arg2)
	{
		if(arg1 != null)
		{
			arg1 = parseInt(arg1 + "");
		}
		if(arg2 != null)
		{
			arg2 = parseInt(arg2 + "");
		}
		
		/*
		//DEBUG: Uncomment this to check the validation type of the field you're editing.
		if(typeof(ui.validation.found) == "undefined" || ui.validation.found != type)
		{
			ui.validation.found = type;
			alert("New validation used: " + type);
		}
		*/
		
		//ALT-255
		str = str.replace(/\u00A0/g, "");
		
		switch(type.toUpperCase())
		{
			//The input string must become a valid date (DD-MM-YYYY)
			case "D":
				str = str.replace(/^(\d{1})-$/, "0$1-");
				str = str.replace(/^(\d{2})-(\d{1})-$/, "$1-0$2-");
				
				str = str.replace(/^(\d{2})(\d)/, "$1-$2");
				str = str.replace(/^(\d{2}-\d{2})(\d)/, "$1-$2");
				break;
			
			//The input string must become an IPv4 address.
			case "IP4":
				// Zet puntjes neer als gebruiker nul intypt
				str = str.replace(/^([0])(\d)$/, "$1.$2");
				str = str.replace(/^(\d{1,3}\.[0]{1})(\d)$/, "$1.$2");
				str = str.replace(/^(\d{1,3}\.\d{1,3}\.[0]{1})(\d)$/, "$1.$2");
				
				// Zet puntjes neer na drie getallen
				str = str.replace(/^(\d{3})(\d)$/, "$1.$2");
				str = str.replace(/^(\d{1,3}\.\d{3})(\d)$/, "$1.$2");
				str = str.replace(/^(\d{1,3}\.\d{1,3}\.\d{3})(\d)$/, "$1.$2");
				
				str = str.replace(/(\d{3})(\d{1,15})/, "$1");      // Vervang blokken van meer dan drie getallen
				str = str.replace(/(\.)\./, "$1");                 // Vervang twee puntjes na elkaar
				str = str.replace(/(0)(0{1,15})/, "$1");           // Vervang meerdere nullen in het laatste blok
				str = str.replace(/(\.[0]{1,3})(\d{1,3})/, ".$2"); // Vervang nullen voor getallen
				str = str.replace(/^0(\d{1,3})/, "$1");            // Vervang nullen voor getallen (eerste blok)
				break;
			
			//Everything is permitted; nothing is true.
			case "A":
			case "A1":
			case "A5":
			case "N3":
			case "B":
			case "S":
			case "EM":
			case "WS":
			case "CB":
				break;
			
			//The input string must turn into uppercase.
			case "A2":
			case "A4":
			case "PW2":
				str = str.toUpperCase();
				break;
			
			//The input string must turn into lowercase.
			case "A6":
				str = str.toLowerCase();
				break;
			
			//The input string must start with an uppercase letter, but the rest must be lowercase.
			case "A3":
				var prefix = str.substring(0, 1);
				var suffix = str.substring(1);
				str = prefix.toUpperCase(prefix) + suffix;
				break;
			
			//The input string must become a valid number. Decimals allowed.
			case "N":
			case "NL":
				if(arg2 > 0)
				{
					var regexp = new RegExp("^(\\d{" + (arg1 - arg2 - 1).toString() +  "})(\\d)","g");
					str = str.replace(regexp, "$1,$2");
					str = str.replace(/^(\,|\.)(\d*)/, "0,$2");
					str = str.replace(/\./, ",");
				}
				break;
			
			//The input string must become a valid number. Decimals allowed. Negative numbers allowed.
			case "N2":
				if(arg2 > 0)
				{
					var regexp = new RegExp("^(-?\\d{" + (arg1 - arg2 - 1).toString() +  "})(\\d)","g");
					str = str.replace(regexp, "$1,$2");
					str = str.replace(/^(\,|\.)(\d*)/, "0,$2");
					str = str.replace(/\./, ",");
				}
				break;
			
			//The input string must become a valid number. Decimals allowed. Negative numbers required.
			case "N4":
				if(arg2 > 0)
				{
					var regexp = new RegExp("^(-\\d{" + (arg1 - arg2 - 1).toString() +  "})(\\d)","g");
					str = str.replace(regexp, "$1,$2");
					str = str.replace(/^(\,|\.)(\d*)/, "0,$2");
					str = str.replace(/\./, ",");
				}
				break;
			
			//The input string must become a valid number. ?
			case "NE":
				if(arg2 > 0)
				{
					var regexp = new RegExp("^(\\d{" + (arg1 - arg2 - 1).toString() +  "})(\\d)","g");
					str = str.replace(regexp, "$1,$2");
					str = str.replace(/^(\,|\.)(\d*)/, "0,$2");
					str = str.replace(/\./, ",");
				}
				break;
			
			//The input string must become a valid number. Negative numbers allowed.
			case "NE2":
				if(arg2 > 0)
				{
					var regexp = new RegExp("^(-?\\d{" + (arg1 - arg2 - 1).toString() +  "})(\\d)","g");
					str = str.replace(regexp, "$1,$2");
					str = str.replace(/^(\,|\.)(\d*)/, "0,$2");
					str = str.replace(/\./, ",");
				}
				break;
			
			//The input string must become a percentage.
			case "P":
				if(arg2 > 0)
				{
					var regexp = new RegExp("^(\\d{" + (arg1 - arg2 - 1).toString() +  "})(\\d)","g");
					str = str.replace(regexp, "$1,$2");
					str = str.replace(/^(\,|\.)(\d*)/, "0,$2");
					str = str.replace(/\./, ",");
				}
				break;
			
			//The input string must become a percentage. Negative numbers allowed.
			case "P2":
				if(arg2 > 0)
				{
					var regexp = new RegExp("^(-?\\d{" + (arg1 - arg2 - 1).toString() +  "})(\\d)","g");
					str = str.replace(regexp, "$1,$2");
					str = str.replace(/^(\,|\.)(\d*)/, "0,$2");
					str = str.replace(/\./, ",");
				}
				break;
			
			//The input string must become a percentage. Maximum value: 1000%
			case "P3":
				if(arg2 > 0)
				{
					var regexp = new RegExp("^(\\d{" + (arg1 - arg2 - 1).toString() +  "})(\\d)","g");
					str = str.replace(re1, "$1,$2");
					str = str.replace(/^(\,|\.)(\d*)/, "0,$2");
					str = str.replace(/\./, ",");
				}
				break;
			
			//The input string must become a percentage. Negative numbers allowed. Maximum value: 1000%
			case "P4":
				if(arg2 > 0)
				{
					var regexp = new RegExp("^(-?\\d{" + (arg1 - arg2 - 1).toString() +  "})(\\d)","g");
					str = str.replace(regexp, "$1,$2");
					str = str.replace(/^(\,|\.)(\d*)/, "0,$2");
					str = str.replace(/\./, ",");
				}
				break;
			
			//The input string must be converted to uppercase and become a word pairing. (ABC-DEF)
			case "K":
				str = str.toUpperCase();
				str = str.replace(/^(\w{2})(\w)/, "$1-$2");
				str = str.replace(/^(\w{2}-\w{3})(\w)/, "$1-$2");
				break;
			
			//The input string must become a valid timestamp. (hh:mm:ss)
			case "T":
				str = str.replace(/^(\d{1}):$/, "0$1:");
				str = str.replace(/^(\d{2})(\d)/, "$1:$2");
				str = str.replace(/^(\d{2}):(\d{2}$)/, "$1:$2:00");
				break;
		}
		
		if(str.length > arg1 && type != "M" && type != "M2" && type != "M3")
		{
			str = str.substring(0, arg1);
		}
		
		return str;
	}
};


function trim(st)
{	
	return rTrim(lTrim(st));
}

function lTrim(st)
{	
	var tempSt = st;
	tempSt = tempSt.replace(/^\s+/,'');
	return tempSt;
}

function rTrim(st)
{
	var tempSt = st;
	tempSt = tempSt.replace(/\s+$/, '');
	return tempSt;
}

function transform(st, tp, arg1, arg2)
{
	return ui.validation.transform(st, tp, arg1, arg2);
}

function isValid(st, tp, arg1, arg2)
{
	return ui.validation.isValid(st, tp, arg1, arg2)
}

function isCorrectType(st, tp, arg1, arg2)
{
	var reSt="";
	if (arg1!=null) arg1=parseInt(arg1);
	if (arg2!=null) arg2=parseInt(arg2);
	var b = true;

	switch(tp.toUpperCase())
  {
  	case "D":
  	if (trim(st)!="")
  	{
  		st = st.replace(/^(\d{1})-/,"0$1-");
		st = st.replace(/^(\d{2})-(\d{1})-/,"$1-0$2-");
  	  	reSt = "\\d{2}-\\d{2}-\\d{4}";
  	  	b = isProperDate(st);
  	}
  	else reSt = "\\d{0,1}";
  	break;
  	case "IP4":
  	reSt = "(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.)|(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(\\d{1,3}\\.\\d{1,3}\\.)|(\\d{1,3}\\.\\d{1,3})|(\\d{1,3}\\.)|(\\d{1,3})";
  	var iptemp = st.split(".");
  	if (iptemp.length != 4)
  		b = false;
  	else
  	{
  		b = true;
  		try
  		{
  			var temp;
	  		for(var i = 0 ; i < 4 ; i++)
	  		{
	  			temp = parseInt(iptemp[i]);
	  			if (temp < 0 || temp > 255) b = false;
	  		}
	  	}
	  	catch (e) { b = false; }
  	}
  	break;
  	case "A":
  	reSt = "([a-zA-Z])*";
  	break;
  	case "A1":
  	reSt = "(\\w|\\W|\\s)*";
  	break;
  	case "A2":
  	reSt = "[^a-z]*";
  	break;
  	case "A3":
  	reSt = "(^[^a-z]+(\\w|\\s|\\W)*)|(^[^a-z]?)";
  	break;
  	case "A4":
  	reSt = "[^a-z]*";
  	break;
  	case "A5":
  	reSt = "^([a-zA-Z0-9_]|-)*";
  	break;
  	case "A6":
  	reSt = "(\\w|\\W|\\s)*";
  	break;
  	case "PW2":
  	reSt = "[^a-z]*";
  	break;
  	case "N":
  	case "NL":
  	if (arg2>0) reSt="^((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{1," + arg2 + "})|(\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  	else reSt = "\\d{0," + arg1 + "}";
  	break;
   	case "N2":
  	if (arg2>0) reSt="^((-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{1," + arg2 + "})|(-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  	else reSt = "-?\\d{0," + arg1 + "}";
  	break;
  	case "N3":
  	if (arg2>0) reSt="^((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{0," + arg2 + "})|(\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  	else reSt = "\\d{0," + arg1 + "}";
  	break;
   	case "N4":
   	if (trim(st) != "")
   	{
   		var f = parseFloat(st);
   		if (isNaN(f) || f != 0)
   		{
  			if (arg2>0) reSt="^((-\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{1," + arg2 + "})|(-\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  			else reSt = "-\\d{0," + arg1 + "}";
  		}
  		else
  		{
  			reSt = "(\\w|\\W|\\s)*";
  		}
  	}
  	else
  	{
  		reSt = "(\\w|\\W|\\s)*";
  	}
  	break;
  	case "NE":
  	if (arg2>0) reSt="^((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{1," + arg2 + "})|(\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  	else reSt = "\\d{0," + arg1 + "}";
  	break;
   	case "NE2":
  	if (arg2>0) reSt="^((-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{1," + arg2 + "})|(-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  	else reSt = "-?\\d{0," + arg1 + "}";
  	break;
  	case "M":
  	reSt = "(\\w|\\W|\\s)*";
  	break;
  	case "M2":
  	reSt = "(\\w|\\W|\\s)*";
  	break;
  	case "B":
  	reSt = "^j$|^J$|^n$|^N$";
  	break;
 	case "S":
	if (trim(st)!="")
	{
	  	reSt = "\\d{9}";
	  	b = bValidateSofinummer(st)
	}
  	break;
  	case "EM":
  		reSt = "(\\w+(((\\-\\w+)|(\\.\\w+))*|(\\w+))@(\\w)+(((\\-\\w+)|(\\.\\w+))*|(\\w+))\\.(\\w)+(\\.*\\w+)*){0,255}";
   	break;
   	case "WS":
   	reSt = "(\\w|\\W|\\s)*";
   	break;
   	case "CB":
   	reSt = "(\\w|\\W|\\s)*";
   	break;
  	case "K":
  	if (trim(st)!="")
  	{
  		reSt = "(\\w{2}-\\w{2,3}-\\w{1,2})";
  	}
  	else reSt = "\\w{0,1}";
  	break;
  	case "P":
    if (arg2>0) reSt="^((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{1," + arg2 + "})|(\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  	else reSt = "\\d{0," + arg1 + "}";
  	if (trim(st)!="") {i = parseFloat(st.replace(/,/,'.')); b = (i>=0 && i<=100);}
  	break;
  	case "P2":
    if (arg2>0) reSt="^((-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{1," + arg2 + "})|(-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  	else reSt = "-?\\d{0," + arg1 + "}";
  	if (trim(st)!="")
  	{
  		if (st == "-") i = 0;
  		else i = parseFloat(st.replace(/,/,'.'));
  		b = (i>=-100 && i<=100);
  	}
  	break;
  	case "P3":
    if (arg2>0) reSt="^((\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{1," + arg2 + "})|(\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  	else reSt = "\\d{0," + arg1 + "}";
  	if (trim(st)!="") {i = parseFloat(st.replace(/,/,'.')); b = (i>=0 && i<=1000);}
  	break;
  	case "P4":
    if (arg2>0) reSt="^((-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\,|\\.)\\d{1," + arg2 + "})|(-?\\d{0," + (parseInt(arg1) - parseInt(arg2)).toString() + "}(\\.|\\,)?))";
  	else reSt = "-?\\d{0," + arg1 + "}";
  	if (trim(st)!="")
  	{
  		if (st == "-") i = 0;
  		else i = parseFloat(st.replace(/,/,'.'));
  		b = (i>=-1000 && i<=1000);
  	}
  	break;
  	case "T":
  	if (trim(st)!="")
  	{
  	  	reSt = "\\d{2}:\\d{2}(:\\d{2})?";
  	  	b = isProperTime(st);
  	}
  	else reSt = "\\d{0,1}";
  	break;

  }
  var re = new RegExp(reSt,"g");
  result = re.exec(st);

  b = b && (result!=null && result[0]==st);

  return  b;
}

function isProperTime(st)
{
	var time = st.split(":");
	var hours = parseInt(time[0]);
	var minutes = parseInt(time[1]);
	var seconds = parseInt(time[2]);
	var b = true;
	if(hours > 23) b = false;
	if(minutes > 59) b = false;
	if(seconds > 59) b = false;
	return b;
}

function bValidateSofinummer(obj)
{
	var reSofi = /(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/;
	var bValid = false;
	var array;
	var lSom = 0;
	if ((obj.length == 0) || (obj == null)) bValid = true;
	else
	{
		bValid = (reSofi.test(obj));
		if (bValid)
		{
			// 11 proef
			array = (obj.match(reSofi));
			for (i=1;i<9;i+=1) lSom += (array[i] * (9-(i-1)));
			bValid = (((lSom -= array[9]) % 11) == 0)
		}
	}

	return (bValid);
}

function validateBankRek(rek)
{
	var s 		= "" + rek;
	var total 	= 0;
	var j 		= s.length;
	
	if (j  > 10 && j <= 34) // iban nr
	{
		//var landcod			= getAttribute(document.getElementById("landcod"), "value");
		//BOY: volgens wikipedia moeten de eerste twee posities van het iban nr gelijk zijn aan de landcode
		//maar dat laten we nu nog even achterwege omdat mensen bijvoorbeeld in nederland kunnen wonen en in
		//belgie een bankrekening hebben
		var ibanlandcod			= s.substring(0,2);
		var controlegetal		= s.substring(2,4); // BOY: controle getal moet 2 cijferig zijn
		if (isValid(ibanlandcod, "A", 2) && isValid(controlegetal, "N", 2, 2))
			return true;
		else
			return false;
	}
	else if (j == 9 || j == 10) // banken
	{	
		for (var i = 0; i < s.length; i++ ) 
		{
			total 	= total + (s.charAt(i) * j);
			j 		= j - 1;
		}
	
		return ((total % 11) == 0)
	}
	else if (j <= 7) return true;	// postbank
	else return false;
}

function transformOnBlur(st, tp, arg1, arg2)
{
	if (arg1!=null) arg1=parseInt(arg1);
	if (arg2!=null) arg2=parseInt(arg2);

  	if ( st!="" && (tp == "N" || tp == "N2" || tp == "P") && arg2!=null && arg2>0)
  	{
  		//nullen en/of komma toevoegen.
  		i = st.indexOf(",") + 1;
  		if (i==0) {st = st + ","; i = st.length;}
  		st = st + generateZero(arg2 - (st.length - i));
    }
  return st;
}

function generateZero(n)
{
	var z ="";
	for(i=0;i<n;i++) z = z + "0";
	return z;
}

function twoDecimals(s)
{
	if (s == "") s = "0,00";
	else if (s.indexOf(",") == -1) s = s + ",00";
	else if (s.indexOf(",") == s.length - 2) s = s + "0";
	return s;
}

String.prototype.left = function(str)
{
	var i = this.indexOf(str);
	if (i > -1) return this.substring(0, i);
	else        return this;
}

String.prototype.right = function(str)
{
	var i = this.indexOf(str);
	if (i > -1) return this.substring(i + str.length);
	else        return this;
}

String.prototype.endsWith = function(str) 
{
	return (this.match(str + "$") == str);
}

String.prototype.startsWith = function(str) 
{
	return (this.match(str) == str);
}

String.prototype.trim = function(str) 
{
	return (trim(this));
}

String.prototype.backwardsright = function(str)
{
	var i = this.lastIndexOf(str);
	if(i > -1) return this.substring(i + str.length);
	else return this;
}

function validateZIPCode(zipcodeitem, countrycodeitem, housenumberitem)
{
	if (mode != MODE_READ)
	{
		var zipcode					= $(zipcodeitem).val();
		var countrycode				= $(countrycodeitem).val();
		var housenumber				= $(housenumberitem).val();
		var maskzipcode				= "";
		var maskzipcodemsg			= "";
		var maskhousenumber			= "";
		var maskhousenumbermsg		= "";
		var validzipcode			= true;
		var validhousenumber		= true;
		
		if (countrycode.toUpperCase() == "NL" || countrycode.toUpperCase() == "NEDERLAND")
		{
			maskzipcode				= "\\d{4}\\s\\w{2}";
			maskzipcodemsg			= "0000 AA";
			
			maskhousenumber			= "\\d+";
			maskhousenumbermsg		= "123";
			
			zipcodeitem.setAttribute("req", "true");
			housenumberitem.setAttribute("req", "true");
		}
		else
		{
			maskzipcode				= ".*";
			maskzipcodemsg			= "";
			
			maskhousenumber			= "\\d*";
			maskhousenumbermsg		= "";
			
			zipcodeitem.setAttribute("req", "false");
			housenumberitem.setAttribute("req", "false");
		}
		
		if (maskzipcode != "")
		{
			var re 					= new RegExp(maskzipcode, "g");
		  	var result				= re.exec(zipcode);
	
		  	validzipcode			= (result != null && result[0] == zipcode);
		}
	
		if (maskhousenumber != "")
		{
			var re 					= new RegExp(maskhousenumber, "g");
		  	var result				= re.exec(housenumber);
	
		  	validhousenumber		= (result != null && result[0] == housenumber);
		}
		
		if (!validzipcode)
		{
			zipcodeitem.setAttribute("valid", "false");
			zipcodeitem.errmsg	= "Postcode moet van de vorm \"" + maskzipcodemsg + "\" zijn.";
		}
		else zipcodeitem.setAttribute("valid", "true");
	
		if (!validhousenumber)
		{
			housenumberitem.setAttribute("valid", "false");
			housenumberitem.errmsg	= "Huisnummer moet van de vorm \"" + maskhousenumbermsg + "\" zijn.";
		}
		else housenumberitem.setAttribute("valid", "true");
		
		setErrorClass(zipcodeitem);
		setErrorClass(housenumberitem);
	
		return validzipcode && validhousenumber;
	}
	else return true;
}
