// ValidationScripts.js - Robert Kovach

/********************************************************************
*
*		Basic Data Validation Functions:
*	isEmpty (strSource)
*	isCurrency (strSource, [EmptyOk])
*	isFloat (strSource, [EmptyOk])
*	isInt (strSource, [EmptyOk])
*	isPercent (strSource, [EmptyOk])
*
*		Advanced Data Validation Functions:
*	isDate (strSource, [EmptyOk])
*	isEmail (strSource, [EmptyOk])
*	isSSN (strSource, [EmptyOk])
*	isUSPhone (strSource, [EmptyOk])
*	isUSStateCode (strSource, [EmptyOk])
*	isZIPCode (strSource, [EmptyOk])
*
*		Credit Card Data Validation Functions:
*	isCreditCard (strSource, [EmptyOk], [RemoveChars])
*	isAmericanExpress (strSource, [EmptyOk], [RemoveChars], [CheckCC])
*	isCarteBlanche (strSource, [EmptyOk], [RemoveChars], [CheckCC])
*	isDinersClub (strSource, [EmptyOk], [RemoveChars], [CheckCC])
*	isDiscover (strSource, [EmptyOk], [RemoveChars], [CheckCC])
*	isEnRoute (strSource, [EmptyOk], [RemoveChars], [CheckCC])
*	isJBC (strSource, [EmptyOk], [RemoveChars], [CheckCC])
*	isMasterCard (strSource, [EmptyOk], [RemoveChars], [CheckCC])
*	isVisa (strSource, [EmptyOk], [RemoveChars], [CheckCC])
*	isCardMatch (strSource)
*	CreditCardName (strSource)
*
*		Formatting Functions:
*	FormatCurrency (strSource, [NumDigitsAfterDecimal], [IncludeLeadingZero], [UseParensForNegativeNumbers])
*	FormatDate (strSource, [DateSeparator], [IncludeLeadingZeros], [DisplayFullYear])
*	FormatSSN (strSource)
*	FormatUSPhone (strSource)
*	FormatZipCode (strSource)
*
*		Utility Functions:
*	ContainsExcept (strSource, strChars, [IgnoreCase])
*	ContainsOnly (strSource, strChars, [IgnoreCase])
*	RemoveChars (strSource, strChars, [IgnoreCase])
*	RemoveOtherChars (strSource, strChars, [IgnoreCase])
*	RemoveWhitespace (strSource)
*	ReplaceChars (strSource, strChars, strReplace, [IgnoreCase])
*	ReplaceOtherChars (strSource, strChars, strReplace, [IgnoreCase])
*	TrimChars (strSource, strChars, [IgnoreCase])
*	TrimOtherChars (strSource, strChars, [IgnoreCase])
*	TrimWhitespace (strSource)
*	LTrimChars (strSource, strChars, [IgnoreCase])
*	LTrimOtherChars (strSource, strChars, [IgnoreCase])
*	LTrimWhitespace (strSource)
*	RTrimChars (strSource, strChars, [IgnoreCase])
*	RTrimOtherChars (strSource, strChars, [IgnoreCase])
*	RTrimWhitespace (strSource)
*
********************************************************************/


	// Global Default Constants

	// defaultEmptyOk - Evalute empty strings as true
if (defaultEmptyOk == undefined)
	var defaultEmptyOk = true;
	// defaultDateSeparator - Character to separate fields in a date string when formatting
if (defaultDateSeparator == undefined)
	var defaultDateSeparator = "/";
	// defaultDateLeadingZeros - Add leading zeros when formatting dates
if (defaultDateLeadingZeros == undefined)
	var defaultDateLeadingZeros = false;
	// defaultCurrencyLeadingZeros - Add leading zeros when formatting currencies
if (defaultCurrencyLeadingZeros == undefined)
	var defaultCurrencyLeadingZeros = true;
	// defaultFullYear - Display four-digit year when formatting
if (defaultFullYear == undefined)
	var defaultFullYear = true;
	// defaultNumDecimalDigits - Number of decimal digits to use when formatting
if (defaultNumDecimalDigits == undefined)
	var defaultNumDecimalDigits = 2;
	// defaultNegativeParens - Use parenthesis for negative numbers when formatting
if (defaultNegativeParens == undefined)
	var defaultNegativeParens = false;
	// defaultIgnoreCase - Ignore case for matching character sets
if (defaultIgnoreCase == undefined)
	var defaultIgnoreCase = false;
	// defaultCheckCC - Evaluate isCreditCard when evaluating specific credit card functions
if (defaultCheckCC == undefined)
	var defaultCheckCC = true;
	// defaultRemoveChars - Remove extra characters from strings when checking contents
if (defaultRemoveChars == undefined)
	var defaultRemoveChars = true;


	
	
	
	

	// ContainsExcept - Checks if a given string contains any characters except the characters in a given character set.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: boolean True/False

function ContainsExcept(strSource, strChars)
{
	return (strSource.search(RegExp("^[^" + strChars + "]+$", ((ContainsExcept.arguments.length > 2) ? ((ContainsExcept.arguments[2] == true) ? "i" : "") : ((defaultIgnoreCase == true) ? "i" : "")))) != -1);
}


	// ContainsOnly - Checks if a given string contains only the characters in a given character set.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: boolean True/False

function ContainsOnly(strSource, strChars)
{
	return (strSource.search(RegExp("^[" + strChars + "]+$", ((ContainsOnly.arguments.length > 2) ? ((ContainsOnly.arguments[2] == true) ? "i" : "") : ((defaultIgnoreCase == true) ? "i" : "")))) != -1);
}


	// DaysInFebruary - Calculates the number of days in February on a given year.
	//	Parameters: integer iYear
	//	Return Value: integer 28/29

function DaysInFebruary(iYear)
{
	return (((iYear % 4 == 0) && ((iYear % 100 != 0) ||  (iYear % 400 == 0))) ? 29 : 28);
}


	// EscapeChars - Escapes special JavaScript string characters (', ", \) in a given string.
	//	Parameters: string strSource
	//	Return Value: string with (', ", \) replaced as (\', \", \\) respectively

function EscapeChars(strSource)
{
	return strSource.replace(RegExp("[^\\\\][\'\"\\\\]", "g"), "\$1");
}


	// isEmpty - Check whether a given string is empty.
	//	Parameters: string strSource
	//	Return Value: boolean True/False

function isEmpty(strSource)
{
	return ((strSource == null) || (strSource.length == 0));
}


	// isInt - Checks whether a given string is an integer.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isInt(strSource)
{
	if (isEmpty(strSource))
		return ((isInt.arguments.length == 1) ? defaultEmptyOk : (isInt.arguments[1] == true));
	
	return (strSource.search(/^[\+\-]?\d+$/) != -1);
}


	// isFloat - Checks whether a given string is a floating-point number.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isFloat(strSource)
{
	if (isEmpty(strSource))
		return ((isFloat.arguments.length == 1) ? defaultEmptyOk : (isFloat.arguments[1] == true));
	
	return (strSource.search(/^[\+\-]?(\d*\.\d+|\d+)(e\d+|\*10\^\d+|\*e\^\d+)?$/i) != -1);
}


	// isUSPhone - Checks whether a given string is a US phone number.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isUSPhone(strSource)
{
	if (isEmpty(strSource))
		return ((isUSPhone.arguments.length == 1) ? defaultEmptyOk : (isUSPhone.arguments[1] == true));
	
	return (strSource.search(/^(1[\-\.\s]?)?(\(\d{3}\)[\s\-\.]?|\d{3}[\s\-\.]?)?\d{3}[\-\.\s]?\d{4}(\s?x\d+)?$/) != -1);
}


	// isZIPCode - Checks whether a given string is a ZIP code.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isZIPCode(strSource)
{
	if (isEmpty(strSource))
		return ((isZIPCode.arguments.length == 1) ? defaultEmptyOk : (isZIPCode.arguments[1] == true));
	
	return (strSource.search(/^\d{5}([\-\s]?\d{4})?$/) != -1);
}


	// isSSN - Checks whether a given string is a social security number.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isSSN(strSource)
{
	if (isEmpty(strSource))
		return ((isSSN.arguments.length == 1) ? defaultEmptyOk : (isSSN.arguments[1] == true));
	
	return (strSource.search(/^\d{3}([\-\.\s]?)\d{2}\1\d{4}$/) != -1);
}


	// isDate - Check whether a given string is a valid date.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isDate(strSource)
{
	if (isEmpty(strSource))
	{
		return ((isDate.arguments.length == 1) ? defaultEmptyOk : (isDate.arguments[1] == true));
	}
	
	var re = /^(0?[1-9]|1[0-2])([\/\-\.\s]?)(0?[1-9]|[12]\d|3[01])\2(\d{2}|\d{4})$/;
	
	if (!re.test(strSource))
		return false;
	
	re.exec(strSource);
	var iFebDays = DaysInFebruary(RegExp.$4);
	
	if ((RegExp.$1 == 2)  && (RegExp.$3 > iFebDays))
		return false;
	
	if (((RegExp.$1 == 4) || (RegExp.$1 == 6) || (RegExp.$1 == 9) || (RegExp.$1 == 11)) && (RegExp.$3 > 30))
		return false;

	return true;
}


	// isEmail - Checks whether a given string is an e-mail address.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isEmail(strSource)
{
	if (isEmpty(strSource))
		return ((isEmail.arguments.length == 1) ? defaultEmptyOk : (isEmail.arguments[1] == true));
	
	return (strSource.search(/^[a-z\d][\w\-\.]*\@([a-z\d][\w\-]*\.)+[a-z]{2,4}$/i) != -1);
}


	// isUSStateCode - Checks whether a given string is a US state code.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isUSStateCode(strSource)
{
	if (isEmpty(strSource))
		return ((isUSStateCode.arguments.length == 1) ? defaultEmptyOk : (isUSStateCode.arguments[1] == true));
	
	return (strSource.search(/^(a[lkrsz]|c[aot]|d[ec]|f[lm]|g[au]|hi|i[adln]|k[sy]|la|m[aedhinopst]|n[cdehjmvy]|o[hkr]|p[arw]|ri|s[cd]|t[nx]|ut|v[ait]|w[aivy])$/i) != -1);
}


	// isCurrency - Checks whether a given string is a currency.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isCurrency(strSource)
{
	if (isEmpty(strSource))
		return ((isCurrency.arguments.length == 1) ? defaultEmptyOk : (isCurrency.arguments[1] == true));
	
	return (strSource.search(/^\-?\$?((\d+|\d{1,3}(,\d{3})*)(\.\d+)?|\.\d+)$/i) != -1);
}


	// isPercent - Checks whether a given string is a percentage.
	//	Parameters: string strSource, boolean EmptyOk (optional)
	//	Return Value: boolean True/False

function isPercent(strSource)
{
	if (isEmpty(strSource))
		return ((isPercent.arguments.length == 1) ? defaultEmptyOk : (isPercent.arguments[1] == true));
	
	return (strSource.search(/^((\d+|\d{1,3}(,\d{3})*)(\.\d+)?|\.\d+)%?$/i) != -1);
}


	// isCreditCard - Checks whether a given string is a credit card number.
	//	Parameters: string strSource, boolean EmptyOk (optional), boolean RemoveChars (optional)
	//	Return Value: boolean True/False

function isCreditCard(strSource)
{
	if (isEmpty(strSource))
		return ((isCreditCard.arguments.length == 1) ? defaultEmptyOk : (isCreditCard.arguments[1] == true));
	
	if (((isCreditCard.arguments.length > 2) && (isCreditCard.arguments[2] == true)) || ((isCreditCard.arguments.length <= 2) && (defaultRemoveChars)))
		strSource = RemoveOtherChars(strSource, "0-9");
	
	if ((strSource.length < 13) || (strSource.length > 19))
		return false;
	
	var iDigit = 0;
	var iIndex = 0;
	var iLength = strSource.length
	var iSum = 0;
	
	for (iIndex = 0; iIndex < iLength; iIndex++)
	{
		iDigit = parseInt(strSource.substring(iLength - iIndex - 1, iLength - iIndex), 10) * ((iIndex % 2) + 1);
		iSum += ((iDigit >= 10) ? (iDigit % 10 + 1) : (iDigit));
	}
	
	return ((iSum % 10) == 0);
}


	// isVisa - Checks whether a given string is a Visa credit card number.
	//	Parameters: string strSource, boolean EmptyOk (optional), boolean CheckCC (optional), boolean RemoveChars (optional)
	//	Return Value: boolean True/False

function isVisa(strSource)
{
	if (isEmpty(strSource))
		return ((isVisa.arguments.length == 1) ? defaultEmptyOk : (isVisa.arguments[1] == true));
	
	if (((isVisa.arguments.length > 3) && (isVisa.arguments[3] == true)) || ((isVisa.arguments.length <= 3) && (defaultRemoveChars)))
		strSource = RemoveOtherChars(strSource, "0-9");
	
	return (((strSource.length == 16) || (strSource.length == 13)) && (strSource.search(/^4/) != -1) && ((((isVisa.arguments.length > 2) && (isVisa.arguments[2] == true)) || ((isVisa.arguments.length == 2) && (defaultCheckCC))) ? (isCreditCard(strSource)) : true));
}


	// isMasterCard - Checks whether a given string is a MasterCard credit card number.
	//	Parameters: string strSource, boolean EmptyOk (optional), boolean CheckCC (optional), boolean RemoveChars (optional)
	//	Return Value: boolean True/False

function isMasterCard(strSource)
{
	if (isEmpty(strSource))
		return ((isMasterCard.arguments.length == 1) ? defaultEmptyOk : (isMasterCard.arguments[1] == true));
	
	if (((isMasterCard.arguments.length > 3) && (isMasterCard.arguments[3] == true)) || ((isMasterCard.arguments.length <= 3) && (defaultRemoveChars)))
		strSource = RemoveOtherChars(strSource, "0-9");
	
	return ((strSource.length == 16) && (strSource.search(/^5[1-5]/) != -1) && ((((isMasterCard.arguments.length > 2) && (isMasterCard.arguments[2] == true)) || ((isMasterCard.arguments.length == 2) && (defaultCheckCC))) ? (isCreditCard(strSource)) : true));
}


	// isAmericanExpress - Checks whether a given string is an American Express credit card number.
	//	Parameters: string strSource, boolean EmptyOk (optional), boolean CheckCC (optional), boolean RemoveChars (optional)
	//	Return Value: boolean True/False

function isAmericanExpress(strSource)
{
	if (isEmpty(strSource))
		return ((isAmericanExpress.arguments.length == 1) ? defaultEmptyOk : (isAmericanExpress.arguments[1] == true));
	
	if (((isAmericanExpress.arguments.length > 3) && (isAmericanExpress.arguments[3] == true)) || ((isAmericanExpress.arguments.length <= 3) && (defaultRemoveChars)))
		strSource = RemoveOtherChars(strSource, "0-9");
	
	return ((strSource.length == 15) && (strSource.match(/^3[47]/)) && ((((isAmericanExpress.arguments.length > 2) && (isAmericanExpress.arguments[2] == true)) || ((isAmericanExpress.arguments.length == 2) && (defaultCheckCC))) ? (isCreditCard(strSource)) : true));
}


	// isDiscover - Checks whether a given string is a Discover credit card number.
	//	Parameters: string strSource, boolean EmptyOk (optional), boolean CheckCC (optional), boolean RemoveChars (optional)
	//	Return Value: boolean True/False

function isDiscover(strSource)
{
	if (isEmpty(strSource))
		return ((isDiscover.arguments.length == 1) ? defaultEmptyOk : (isDiscover.arguments[1] == true));
	
	if (((isDiscover.arguments.length > 3) && (isDiscover.arguments[3] == true)) || ((isDiscover.arguments.length <= 3) && (defaultRemoveChars)))
		strSource = RemoveOtherChars(strSource, "0-9");
	
	return ((strSource.length == 16) && (strSource.match(/^6011/)) && ((((isDiscover.arguments.length > 2) && (isDiscover.arguments[2] == true)) || ((isDiscover.arguments.length == 2) && (defaultCheckCC))) ? (isCreditCard(strSource)) : true));
}


	// isDinersClub - Checks whether a given string is a Diner's Club credit card number.
	//	Parameters: string strSource, boolean EmptyOk (optional), boolean CheckCC (optional), boolean RemoveChars (optional)
	//	Return Value: boolean True/False

function isDinersClub(strSource)
{
	if (isEmpty(strSource))
		return ((isDinersClub.arguments.length == 1) ? defaultEmptyOk : (isDinersClub.arguments[1] == true));
	
	if (((isDinersClub.arguments.length > 3) && (isDinersClub.arguments[3] == true)) || ((isDinersClub.arguments.length <= 3) && (defaultRemoveChars)))
		strSource = RemoveOtherChars(strSource, "0-9");
	
	return ((strSource.length == 14) && (strSource.match(/^3[068]/)) && ((((isDinersClub.arguments.length > 2) && (isDinersClub.arguments[2] == true)) || ((isDinersClub.arguments.length == 2) && (defaultCheckCC))) ? (isCreditCard(strSource)) : true));
}


	// isCarteBlanche - Checks whether a given string is a Carte Blanche credit card number.
	//	Parameters: string strSource, boolean EmptyOk (optional), boolean CheckCC (optional), boolean RemoveChars (optional)
	//	Return Value: boolean True/False

function isCarteBlanche(strSource)
{
	if (isEmpty(strSource))
		return ((isCarteBlanche.arguments.length == 1) ? defaultEmptyOk : (isCarteBlanche.arguments[1] == true));
	
	if (((isCarteBlanche.arguments.length > 3) && (isCarteBlanche.arguments[3] == true)) || ((isCarteBlanche.arguments.length <= 3) && (defaultRemoveChars)))
		strSource = RemoveOtherChars(strSource, "0-9");
	
	return ((strSource.length == 14) && (strSource.match(/^3[068]/)) && ((((isCarteBlanche.arguments.length > 2) && (isCarteBlanche.arguments[2] == true)) || ((isCarteBlanche.arguments.length == 2) && (defaultCheckCC))) ? (isCreditCard(strSource)) : true));
}


	// isEnRoute - Checks whether a given string is a EnRoute credit card number.
	//	Parameters: string strSource, boolean EmptyOk (optional), boolean CheckCC (optional), boolean RemoveChars (optional)
	//	Return Value: boolean True/False

function isEnRoute(strSource)
{
	if (isEmpty(strSource))
		return ((isEnRoute.arguments.length == 1) ? defaultEmptyOk : (isEnRoute.arguments[1] == true));
	
	if (((isEnRoute.arguments.length > 3) && (isEnRoute.arguments[3] == true)) || ((isEnRoute.arguments.length <= 3) && (defaultRemoveChars)))
		strSource = RemoveOtherChars(strSource, "0-9");
	
	return ((strSource.length == 15) && (strSource.match(/^(2014|2149)/)) && ((((isEnRoute.arguments.length > 2) && (isEnRoute.arguments[2] == true)) || ((isEnRoute.arguments.length == 2) && (defaultCheckCC))) ? (isCreditCard(strSource)) : true));
}


	// isJBC - Checks whether a given string is a JBC credit card number.
	//	Parameters: string strSource, boolean EmptyOk (optional), boolean CheckCC (optional), boolean RemoveChars (optional)
	//	Return Value: boolean True/False

function isJBC(strSource)
{
	if (isEmpty(strSource))
		return ((isJBC.arguments.length == 1) ? defaultEmptyOk : (isJBC.arguments[1] == true));
	
	if (((isJBC.arguments.length > 3) && (isJBC.arguments[3] == true)) || ((isJBC.arguments.length <= 3) && (defaultRemoveChars)))
		strSource = RemoveOtherChars(strSource, "0-9");
	
	return ((strSource.length == 16) && (strSource.match(/^(3088|3096|3112|3158|3337|3528)/)) && ((((isJBC.arguments.length > 2) && (isJBC.arguments[2] == true)) || ((isJBC.arguments.length == 2) && (defaultCheckCC))) ? (isCreditCard(strSource)) : true));
}


	// isCardMatch - Checks whether a given string is a valid credit card number of a specified type.
	//	Parameters: string strSource, string strCardType
	//	Return Value: boolean True/False

function isCardMatch(strSource, strCardType)
{
	strCardType = RemoveOtherChars(strCardType, "a-z", true);
	
	switch (strCardType.toUpperCase())
	{
		case "VISA":
			return isVisa(strSource, false, true);
			break;
		case "MASTERCARD":
			return isMasterCard(strSource, false, true);
			break;
		case "MC":
			return isMasterCard(strSource, false, true);
			break;
		case "AMERICANEXPRESS":
			return isAmericanExpress(strSource, false, true);
			break;
		case "AMEX":
			return isAmericanExpress(strSource, false, true);
			break;
		case "DISCOVER":
			return isDiscover(strSource, false, true);
			break;
		case "DISC":
			return isDiscover(strSource, false, true);
			break;
		case "CARTEBLANCHE":
			return isCarteBlanche(strSource, false, true);
			break;
		case "DINERSCLUB":
			return isDinersClub(strSource, false, true);
			break;
		case "ENROUTE":
			return isEnRoute(strSource, false, true);
			break;
		case "JBC":
			return isJBC(strSource, false, true);
			break;
		default:
			return false;
	}
}


	// CreditCardName - Determines credit card type name for a given string.
	//	Parameters: string strSource
	//	Return Value: string credit card type name ("Visa"/"MasterCard"/"AmericanExpress"/"Discover"/"DinersClub"/"CarteBlanche"/"EnRoute"/"JBC"/"Nothing"/"Unknown")

function CreditCardName(strSource)
{
	if (!isCreditCard(strSource, false))
		return "Nothing";
	
	if (isVisa(strSource, false, false))
		return "Visa";
	
	if (isMasterCard(strSource, false, false))
		return "MasterCard";
	
	if (isAmericanExpress(strSource, false, false))
		return "AmericanExpress";
	
	if (isDiscover(strSource, false, false))
		return "Discover";
	
	if (isDinersClub(strSource, false, false))
		return "DinersClub";
	
	if (isCarteBlanche(strSource, false, false))
		return "CarteBlanche";
	
	if (isEnRoute(strSource, false, false))
		return "EnRoute";
	
	if (isJBC(strSource, false, false))
		return "JBC";
	
	return "Unknown";
}


	// Format - Formats a given string based on additional parameters.
	//	Parameters: string strSource
	//	Return Value: string

function Format(strSource)
{
	var arg;
	var sPos = 0;
	var strResult = "";
	
	for (var i = 1; i < Format.arguments.length; i++)
	{
		arg = Format.arguments[i];
		
		if (i % 2 == 1)
			strResult += arg;
		else
		{
			strResult += strSource.substring(sPos, sPos + arg);
			sPos += arg;
		}
	}
	
	return strResult;
}


	/* FormatCurrency - Formats a given string as a currency.
   Syntax:

   FormatCurrency(num[,unpure])
   num:     The string or number to format.
   unpure:  true/false (optional)
            If true, dollar signs and commas
            are removed before formatting. */

function FormatCurrency(num,unpure) {
/*
   Author: Robert K. Davis
   Website: http://www.solidscripts.com
*/
   if (unpure) num = num.replace(/\,/g,"").replace(/^\$/g,"");
   num = !isNaN(num) ? Math.round(num * 100) / 100 : 0;
   if (num.toString().indexOf(".") == -1) num += ".";
   while (/\.\d{0,1}$/.test(num)) num += "0";
   num = num.toString().split(".");
   var objRegExp  = new RegExp('(-?\[0-9]+)([0-9]{3})');
   while (objRegExp.test(num[0])) num[0] = num[0].replace(objRegExp,'$1,$2');
   return "$" + num.join(".");
}


function FormatCurrency2(num, dad) {
/*
   Author: Robert K. Davis
   Website: http://www.solidscripts.com
*/
   if (typeof num == "string") num = num.replace(/[^0-9\.]/g,"");
   if (typeof dad == "string") dad = isInt(dad) ? parseInt(dad, 10) : 0;
   if (isNaN(dad) || (dad < 0)) dad = 0;
   var mult = Math.pow(10, dad);
   num = !isNaN(num) ? Math.round(num * mult) / mult : 0;
   num = num.toFixed(dad);
   var objRegExp  = new RegExp("(-?[0-9]+)([0-9]{3})");
   num = num.toString().split(".");
   while (objRegExp.test(num[0])) num[0] = num[0].replace(objRegExp,'$1,$2');
   return (dad > 0) ? "$" + num.join(".") : "$" + num[0];
}


function FormatPercent(num, dad) {
/*
   Author: Robert K. Davis
   Website: http://www.solidscripts.com
*/
   if (typeof num == "string") num = num.replace(/[^0-9\.]/g,"");
   if (typeof dad == "string") dad = isInt(dad) ? parseInt(dad, 10) : 0;
   if (isNaN(dad) || (dad < 0)) dad = 0;
   var mult = Math.pow(10, dad);
   num = !isNaN(num) ? Math.round(num * mult) / mult : 0;
   num = num.toFixed(dad);
   var objRegExp  = new RegExp("(-?[0-9]+)([0-9]{3})");
   num = num.toString().split(".");
   while (objRegExp.test(num[0])) num[0] = num[0].replace(objRegExp,'$1,$2');
   return (dad > 0) ? num.join(".") + "%" : num[0] + "%";
}


function FormatNumber(num, dad) {
/*
   Author: Robert K. Davis
   Website: http://www.solidscripts.com
*/
   if (typeof num == "string") num = num.replace(/[^0-9\.]/g,"");
   if (typeof dad == "string") dad = isInt(dad) ? parseInt(dad, 10) : 0;
   if (isNaN(dad) || (dad < 0)) dad = 0;
   var mult = Math.pow(10, dad);
   num = !isNaN(num) ? Math.round(num * mult) / mult : 0;
   num = num.toFixed(dad);
   var objRegExp  = new RegExp("(-?[0-9]+)([0-9]{3})");
   num = num.toString().split(".");
   while (objRegExp.test(num[0])) num[0] = num[0].replace(objRegExp,'$1,$2');
   return (dad > 0) ? num.join(".") : num[0];
}



	// FormatDate - Formats a given string as a date.
	//	Parameters: string strSource
	//	Return Value: string date ("mm/dd/yyyy")

function FormatDate(strSource)
{
	var dtCurrDate = new Date();
	var iCurrYear = dtCurrDate.getYear() % 100;
	var iCurrCentury = (dtCurrDate.getYear() - iCurrYear) / 100;
	var iInputYear;
	var strSeparator = ((FormatDate.arguments.length > 1) ? FormatDate.arguments[1] : defaultDateSeparator);
	
	strSource = strSource.replace(/^(0?[1-9]|1[0-2])([\/\-\.\s]?)(0?[1-9]|[12]\d|3[01])\2(\d{2}|\d{4})$/, "$1" + strSeparator + "$3" + strSeparator + "$4");
	iInputYear = RegExp.$4;
	
	if (((FormatDate.arguments.length > 2) ? (FormatDate.arguments[2]) : (defaultDateLeadingZeros)))
		strSource = strSource.replace(/(\b|\D)([1-9])(\D)/g, "$10$2$3");
	else
		strSource = strSource.replace(/(\b|\D)0([1-9])(\D)/g, "$1$2$3");
	
	if (((FormatDate.arguments.length > 3) ? (FormatDate.arguments[3]) : (defaultFullYear)))
		strSource = strSource.replace(/(\D)(\d{2})$/, "$1" + ((iInputYear < iCurrYear + 20) ? (iCurrCentury) : (iCurrCentury - 1)) + "$2");
	else
		strSource = strSource.replace(/(\D)\d{2}(\d{2})$/, "$1$2");
	
	return strSource;
}


	// FormatSSN - Formats a given string as a social security number.
	//	Parameters: string strSource
	//	Return Value: string social security number ("###-###-####")

function FormatSSN(strSource)
{
	return strSource.replace(/^(\d{3})([\-\.\s]?)(\d{3})\2(\d{4})$/, "$1-$3-$4");
}


	// FormatUSPhone - Formats a given string as a US phone number.
	//	Parameters: string strSource
	//	Return Value: string US phone number ("(###) ###-####")

function FormatUSPhone(strSource)
{
	return strSource.replace(/^(1[\-\.\s]?)?(\(?(\d{3})\)?[\s\-\.]?)?(\d{3})[\-\.\s]?(\d{4})(\s?x(\d+))?$/, "($3) $4-$5 x$7").replace(/^\(\) /, "").replace(/ x$/, "");
}


	// FormatZIPCode - Formats a given string as a ZIP code.
	//	Parameters: string strSource
	//	Return Value: string ZIP code ("#####-####")

function FormatZIPCode(strSource)
{
	return strSource.replace(/^(\d{5})[\-\s]?(\d{4})$/, "$1-$2");
}


	// ReplaceChars - Replaces all characters in a given character set in a given source string with a given replacement string.
	//	Parameters:	string strSource, string strChars, string strReplace, boolean IgnoreCase (optional)
	//	Return Value: string

function ReplaceChars(strSource, strChars, strReplace)
{
	if (ReplaceChars.arguments.length > 3)
		return strSource.replace(RegExp("[" + strChars + "]", ((ReplaceChars.arguments[3] == true) ? "gi" : "g")), strReplace);
	else
		return strSource.replace(RegExp("[" + strChars + "]", ((defaultIgnoreCase == true) ? "gi" : "g")), strReplace);
}


	// ReplaceOtherChars - Replaces all characters not in a given character set in a given source string with a given replacement string.
	//	Parameters:	string strSource, string strChars, string strReplace, boolean IgnoreCase (optional)
	//	Return Value: string

function ReplaceOtherChars(strSource, strChars, strReplace)
{
	if (ReplaceOtherChars.arguments.length > 3)
		return strSource.replace(RegExp("[^" + strChars + "]", ((ReplaceOtherChars.arguments[3] == true) ? "gi" : "g")), strReplace);
	else
		return strSource.replace(RegExp("[^" + strChars + "]", ((defaultIgnoreCase == true) ? "gi" : "g")), strReplace);
}


	// RemoveChars - Removes all characters in a given character set from a given string.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: string

function RemoveChars(strSource, strChars)
{
	if (RemoveChars.arguments.length > 2)
		return strSource.replace(RegExp("[" + strChars + "]", ((RemoveChars.arguments[2] == true) ? "gi" : "g")), "");
	else
		return strSource.replace(RegExp("[" + strChars + "]", ((defaultIgnoreCase == true) ? "gi" : "g")), "");
}


	// RemoveOtherChars - Removes all characters not in a given character set from a given string.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: string

function RemoveOtherChars(strSource, strChars)
{
	if (RemoveOtherChars.arguments.length > 2)
		return strSource.replace(RegExp("[^" + strChars + "]", ((RemoveOtherChars.arguments[2] == true) ? "gi" : "g")), "");
	else
		return strSource.replace(RegExp("[^" + strChars + "]", ((defaultIgnoreCase == true) ? "gi" : "g")), "");
}


	// RemoveWhitespace - Removes all whitespace from a given string.
	//	Parameters: string strSource
	//	Return Value: string

function RemoveWhitespace(strSource)
{
	return strSource.replace(/\s/g, "");
}


	// TrimChars - Removes all characters in a given character set from the extremities of a given string.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: string

function TrimChars(strSource, strChars)
{
	if (TrimChars.arguments.length > 2)
		return strSource.replace(RegExp("^[" + strChars + "]*(.*?)[" + strChars + "]*$", ((TrimChars.arguments[2] == true) ? "i" : "")), "$1");
	else
		return strSource.replace(RegExp("^[" + strChars + "]*(.*?)[" + strChars + "]*$", ((defaultIgnoreCase == true) ? "i" : "")), "$1");
}


	// TrimOtherChars - Removes all characters not in a given character set from the extremities of a given string.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: string

function TrimOtherChars(strSource, strChars)
{
	if (TrimOtherChars.arguments.length > 2)
		return strSource.replace(RegExp("^[^" + strChars + "]*(.*?)[^" + strChars + "]*$", ((TrimOtherChars.arguments[2] == true) ? "i" : "")), "$1");
	else
		return strSource.replace(RegExp("^[^" + strChars + "]*(.*?)[^" + strChars + "]*$", ((defaultIgnoreCase == true) ? "i" : "")), "$1");
}


	// TrimWhitespace - Removes all whitespace from the extremities of a given string.
	//	Parameters: string strSource
	//	Return Value: string

function TrimWhitespace(strSource)
{
	return strSource.replace(/^\s*(.*?)\s*$/, "$1");
}


	// LTrimChars - Removes all characters in a given character set from the left of a given string.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: string

function LTrimChars(strSource, strChars)
{
	if (LTrimChars.arguments.length > 2)
		return strSource.replace(RegExp("^[" + strChars + "]+", ((LTrimChars.arguments[2] == true) ? "i" : "")), "");
	else
		return strSource.replace(RegExp("^[" + strChars + "]+", ((defaultIgnoreCase == true) ? "i" : "")), "");
}


	// LTrimOtherChars - Removes all characters not in a given character set from the left of a given string.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: string

function LTrimOtherChars(strSource, strChars)
{
	if (LTrimOtherChars.arguments.length > 2)
		return strSource.replace(RegExp("^[^" + strChars + "]+", ((LTrimOtherChars.arguments[2] == true) ? "i" : "")), "");
	else
		return strSource.replace(RegExp("^[^" + strChars + "]+", ((defaultIgnoreCase == true) ? "i" : "")), "");
}


	// LTrimWhitespace - Removes all whitespace from the left of a given string.
	//	Parameters: string strSource
	//	Return Value: string

function LTrimWhitespace(strSource)
{
	return strSource.replace(/^\s+/, "");
}


	// RTrimChars - Removes all characters in a given character set from the right of a given string.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: string

function RTrimChars(strSource, strChars)
{
	if (RTrimChars.arguments.length > 2)
		return strSource.replace(RegExp("[" + strChars + "]+$", ((RTrimChars.arguments[2] == true) ? "i" : "")), "");
	else
		return strSource.replace(RegExp("[" + strChars + "]+$", ((defaultIgnoreCase == true) ? "i" : "")), "");
}


	// RTrimOtherChars - Removes all characters not in a given character set from the right of a given string.
	//	Parameters: string strSource, string strChars, boolean IgnoreCase (optional)
	//	Return Value: string

function RTrimOtherChars(strSource, strChars)
{
	if (RTrimOtherChars.arguments.length > 2)
		return strSource.replace(RegExp("[^" + strChars + "]+$", ((RTrimOtherChars.arguments[2] == true) ? "i" : "")), "");
	else
		return strSource.replace(RegExp("[^" + strChars + "]+$", ((defaultIgnoreCase == true) ? "i" : "")), "");
}


	// RTrimWhitespace - Removes all whitespace from the right of a given string.
	//	Parameters: string strSource
	//	Return Value: string

function RTrimWhitespace(strSource)
{
	return strSource.replace(/\s+$/, "");
}
