//RRK 2005 - input controllers through JavaScript


//RRK - This function accepts only specified input.
function KeepOnly (Target)
{
	//Pass an object or a string.
	if (Target.toString() != "[object]")
	{
		//RRK 07-08-2008 accept new keyhandlers without replacing code
		//if (Target.indexOf(String.fromCharCode(parseInt(window.event.keyCode))) == -1)
		if (Target.indexOf(String.fromCharCode(parseInt(window.event.keyCode))) == -1 || window.event.keyCode == 8 || window.event.keyCode == 46 || window.event.keyCode == 0/* || window.event.ctrlKey || window.event.shiftKey || window.event.modifiers == 2*/)
		{
			window.event.keyCode = 0;
		}
	}
	else
	{
		//RRK 07-08-2008 accept new keyhandlers without replacing code
		//if (Target.KeepOnly.indexOf(String.fromCharCode(parseInt(window.event.keyCode))) == -1)
		if (Target.KeepOnly.indexOf(String.fromCharCode(parseInt(window.event.keyCode))) == -1 || window.event.keyCode == 8 || window.event.keyCode == 46 || window.event.keyCode == 0/* || window.event.ctrlKey || window.event.shiftKey || window.event.modifiers == 2*/)
		{
			window.event.keyCode = 0;
		}
	}
}
//RRK - This function cancels only specified input.
function LoseOnly (Target)
{
	//Pass an object or a string.
	if (Target.toString() != "[object]")
	{
		//RRK 07-08-2008 accept new keyhandlers without replacing code
		//if (Target.indexOf(String.fromCharCode(parseInt(window.event.keyCode))) != -1)
		if (Target.indexOf(String.fromCharCode(parseInt(window.event.keyCode))) != -1 || window.event.keyCode == 8 || window.event.keyCode == 46 || window.event.keyCode == 0/* || window.event.ctrlKey || window.event.shiftKey || window.event.modifiers == 2*/)
		{
			window.event.keyCode = 0;
		}
	}
	else
	{
		//RRK 07-08-2008 accept new keyhandlers without replacing code
		//if (Target.LoseOnly.indexOf(String.fromCharCode(parseInt(window.event.keyCode))) != -1)
		if (Target.LoseOnly.indexOf(String.fromCharCode(parseInt(window.event.keyCode))) != -1 || window.event.keyCode == 8 || window.event.keyCode == 46 || window.event.keyCode == 0/* || window.event.ctrlKey || window.event.shiftKey || window.event.modifiers == 2*/)
		{
			window.event.keyCode = 0;
		}
	}
}
//RRK - This function enforces cut&paste rules.
function OnLeaving (Target, KeyList1, KeyList2, NoEnds, Casing, Sequences)
{
	//OnLeaving(object/string, accept/null, cancel/null, keeptrailingspaces, upper/lower, arrayofbadstrings);
	if (Target.toString() != "[object]")
	{
		if (KeyList2 != null && KeyList2.length != 0)
		{
			for (var Counter = 0; Counter < document.getElementById(Target).value.length; Counter++)
			{
				if (KeyList2.indexOf(document.getElementById(Target).value.charAt(Counter)) != -1)
				{
					document.getElementById(Target).value = document.getElementById(Target).value.substring(0, Counter) + document.getElementById(Target).value.substring(Counter + 1, document.getElementById(Target).value.length);
					Counter--;
				}
			}
			//for (var Counter = 0; Counter < KeyList2.length; Counter++)
			//{
			//	document.getElementById(Target).value = document.getElementById(Target).value.replace(new RegExp(KeyList2.charAt(Counter), "g"), "");
			//}
			//Regular expressions have...issues...with some characters
		}
		if (KeyList1 != null && KeyList1.length != 0)
		{
			for (var Counter = 0; Counter < document.getElementById(Target).value.length; Counter++)
			{
				if (KeyList1.indexOf(document.getElementById(Target).value.charAt(Counter)) == -1)
				{
					document.getElementById(Target).value = document.getElementById(Target).value.substring(0, Counter) + document.getElementById(Target).value.substring(Counter + 1, document.getElementById(Target).value.length);
					Counter--;
				}
			}
			//for (var Counter = 0; Counter < document.getElementById(Target).value.length; Counter++)
			//{
			//	if (KeyList1.indexOf(document.getElementById(Target).value.charAt(Counter)) == -1)
			//	{
			//		document.getElementById(Target).value = document.getElementById(Target).value.replace(new RegExp(document.getElementById(Target).value.charAt(Counter), "g"), "");
			//		Counter--;
			//	}
			//}
			//Regular expressions have...issues...with some characters
		}
		if (NoEnds == true)
		{
			while (document.getElementById(Target).value.substring(0, 1) == " ")
			{
				document.getElementById(Target).value = document.getElementById(Target).value.substring(1, document.getElementById(Target).value.length);
			}
			while (document.getElementById(Target).value.substring(document.getElementById(Target).value.length - 1, document.getElementById(Target).value.length) == " ")
			{
				document.getElementById(Target).value = document.getElementById(Target).value.substring(0, document.getElementById(Target).value.length - 1);
			}
		}
		switch (Casing)
		{
			case "U":
			{
				document.getElementById(Target).value = document.getElementById(Target).value.toUpperCase();
			}
			break;
			case "L":
			{
				document.getElementById(Target).value = document.getElementById(Target).value.toLowerCase();
			}
			break;
		}
		//if (Sequences != null)
		//{
		//	for (var Counter = 0; Counter < Sequences.length; Counter++)
		//	{
		//		document.getElementById(Target).value = document.getElementById(Target).value.split(Sequences[Counter]).join("");
		//	}
		//}
	}
	else
	{
		if (Target.LoseOnly != null && Target.LoseOnly.length != 0)
		{
			for (var Counter = 0; Counter < Target.value.length; Counter++)
			{
				if (Target.LoseOnly.indexOf(Target.value.charAt(Counter)) != -1)
				{
					Target.value = Target.value.substring(0, Counter) + Target.value.substring(Counter + 1, Target.value.length);
					Counter--;
				}
			}
			//for (var Counter = 0; Counter < Target.LoseOnly.length; Counter++)
			//{
			//	Target.value = Target.value.replace(new RegExp(Target.LoseOnly.charAt(Counter), "g"), "");
			//}
			//Regular expressions have...issues...with some characters
		}
		if (Target.KeepOnly != null && Target.KeepOnly.length != 0)
		{
			for (var Counter = 0; Counter < Target.value.length; Counter++)
			{
				if (Target.KeepOnly.indexOf(Target.value.charAt(Counter)) == -1)
				{
					Target.value = Target.value.substring(0, Counter) + Target.value.substring(Counter + 1, Target.value.length);
					Counter--;
				}
			}
			//for (var Counter = 0; Counter < Target.value.length; Counter++)
			//{
			//	if (Target.KeepOnly.indexOf(Target.value.charAt(Counter)) == -1)
			//	{
			//		Target.value = Target.value.replace(new RegExp(Target.value.charAt(Counter), "g"), "");
			//		Counter--;
			//	}
			//}
			//Regular expressions have...issues...with some characters
		}
		if (NoEnds == true)
		{
			while (Target.value.substring(0, 1) == " ")
			{
				Target.value = Target.value.substring(1, Target.value.length);
			}
			while (Target.value.substring(Target.value.length - 1, Target.value.length) == " ")
			{
				Target.value = Target.value.substring(0, Target.value.length - 1);
			}
		}
		switch (Casing)
		{
			case "U":
			{
				Target.value = Target.value.toUpperCase();
			}
			break;
			case "L":
			{
				Target.value = Target.value.toLowerCase();
			}
			break;
		}
		//if (Sequences != null)
		//{
		//	for (var Counter = 0; Counter < Sequences.length; Counter++)
		//	{
		//		Target.value = Target.value.split(Sequences[Counter]).join("");
		//	}
		//}
	}
}

function IsInteger (Possible, SendAlert)
{
	if (Possible.toString() != "[object]")
	{
		var Integers = "0123456789";
		if (Possible == null || Possible.length == 0)
		{
			if (SendAlert == true)
			{
				window.alert("Null provided.");
			}
			return false;
		}
		for (var Counter = 0; Counter < Possible.length; Counter++)
		{
			if (Integers.indexOf(Possible.charAt(Counter)) < 0)
			{
				if (SendAlert == true)
				{
					window.alert("Bad character.");
				}
				return false;
			}
		}
		return true;
	}
	else
	{
		var Integers = "0123456789";
		if (Possible.value == null || Possible.value.length == 0)
		{
			if (SendAlert == true)
			{
				window.alert("Null provided.");
			}
			return false;
		}
		for (var Counter = 0; Counter < Possible.value.length; Counter++)
		{
			if (Integers.indexOf(Possible.value.charAt(Counter)) < 0)
			{
				if (SendAlert == true)
				{
					window.alert("Bad character.");
				}
				return false;
			}
		}
		return true;
	}
}


function IsDate (f)
{
	return true;
}





function IsMoney (Possible, SendAlert)
{
	if (Possible.toString() != "[object]")
	{
		var Accepted = "0123456789.";
		var Decimals = 0;
		if (Possible == null || Possible.length == 0)
		{
			if (SendAlert == true)
			{
				window.alert("Null provided.");
			}
			return false;
		}
		for (var Counter = 0; Counter < Possible.length; Counter++)
		{
			if (Accepted.indexOf(Possible.charAt(Counter)) < 0)
			{
				if (SendAlert == true)
				{
					window.alert("Bad character.");
				}
				return false;
			}
			Decimals += (Possible.charAt(Counter) == '.' ? 1 : 0);
		}
		if (Decimals > 1)
		{
			if (SendAlert == true)
			{
				window.alert("Too many decimals.");
			}
			return false;
		}
		return true;
	}
	else
	{
		var Accepted = "0123456789.";
		var Decimals = 0;
		if (Possible == null || Possible.value.length == 0)
		{
			if (SendAlert == true)
			{
				window.alert("Null provided.");
			}
			return false;
		}
		for (var Counter = 0; Counter < Possible.value.length; Counter++)
		{
			if (Accepted.indexOf(Possible.value.charAt(Counter)) < 0)
			{
				if (SendAlert == true)
				{
					window.alert("Bad character.");
				}
				return false;
			}
			Decimals += (Possible.value.charAt(Counter) == '.' ? 1 : 0);
		}
		if (Decimals > 1)
		{
			if (SendAlert == true)
			{
				window.alert("Too many decimals.");
			}
			return false;
		}
		return true;
	}
}


function IsTelephone (f)
{
	return true;
}
function IsEMailAddress (f)
{
	return true;
}



function ToInteger (Target, Default, Format)
{
	if (Target.toString() != "[object]")
	{
		if (IsInteger(Target, false) == false)
		{
			return ToInteger(Default, 0, null);
		}
		else
		{
			var Old = parseInt(Target) + "";
			return (Format == null ? parseInt(Old) : Old);
		}
	}
	else
	{
		if (IsInteger(Target, false) == false)
		{
			return ToInteger(Default, 0, null);
		}
		else
		{
			var Old = parseInt(Target.value) + "";
			return (Format == null ? parseInt(Old) : Old);
		}
	}
}


function ToMoney (Target, Default, Format)
{
	if (Target.toString() != "[object]")
	{
		if (IsMoney(Target, false) == false)
		{
			return ToMoney(Default, 0, null);
		}
		else
		{
			var Old = (Math.round(parseFloat(Target) * 100) / 100) + "";
			Old += (Old.indexOf(".") < 0 ? "." : "");
			while (Old.length - Old.indexOf(".") < 3)
			{
				Old += "0";
			}
			return (Format == null ? parseFloat(Old) : Old);
		}
	}
	else
	{
		if (IsMoney(Target, false) == false)
		{
			return ToMoney(Default, 0, null);
		}
		else
		{
			var Old = (Math.round(parseFloat(Target.value) * 100) / 100) + "";
			Old += (Old.indexOf(".") < 0 ? "." : "");
			while (Old.length - Old.indexOf(".") < 3)
			{
				Old += "0";
			}
			return (Format == null ? parseFloat(Old) : Old);
		}
	}
}

