//
// !COPYRIGHT!
//                      Copyright (c) 2007 Teleformix LLC
//                       All Rights Reserved
// 
// 
// THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF TELEFORMIX LLC
// 
// The copyright notice above does not evidence any
// actual or intended publication of such source code.
// 
// This document contains trade secret data and proprietary information of
// Teleformix, LLC and should be treated as confidential.  No part of this 
// information may be copied, or disclosed in part or in whole as permitted 
// by Teleformix LLC
// 
// Copyright (c) 1998-2007 by Teleformix, LLC.
// All Rights Reserved
// 
// $Id: validate.js,v 1.3 2007/10/04 16:25:13 pdiverde Exp $
// !!
//


//
// isBlank(): Detects blank strs
//
//	Params: 	str - The str to be tested
//
//	Return: 	true if blank
//				false if not blank
//
function isBlank(s)
{
	if (typeof(s) == "string")
	{
		if ((s == null) || (s == "" ))
		{
			return true;
		}

		for (var i = 0; i < s.length; i++)
		{
			if (! isWhiteSpace(s.charAt(i)))
			{
				return false;
			}
		}

		return true;
	}
	else
	{
		return ((s == null) || (s.length == 0))
	}
}


function isNBlank(s)
{
	return (isBlank(s) || (s == "NULL"))
}


//
// ishiteSpace(): Detects white space chars
//
//	Params: 	ch - The str to be tested
//
//	Return: 	true if 1 character of whitespace
//				false if not 1 character of whitespace
//
function isWhiteSpace(ch)
{
	// Non strings aren't WS and more than single element strings aren't chars
	if ((typeof(ch) != "string") || (ch.length != 1))
	{
		return false;
	}

	return ((ch == " ")  || (ch == "\n") || (ch == "\t") ||
			(ch == "\r") || (ch == "\f"))
}


//
// isDigit(): Checks if a single digit is numeric
//
//	Params: 	character - The character to be tested
//
//	Return: 	true if the character is numeric
//				false if it is not numeric
//
function isDigit(character)
{
	return ((character >= '0') && (character <= '9') && character.length == 1)
}


//
// isInt(): Detects integers
//
//	Params:		str - The str to be tested as an integer
//
//	Return:		true if the str represents an integer
//				false if not an integer
//
function isInt(str)
{
	var c;

	if (isBlank(str))
	{
		return false;
	}

	for (i = 0; i < str.length; i++)
	{
		c = str.charAt(i);

		if (! isDigit(c))
		{
			return false;
		}
	}

	return true;
}


//
// isNumeric(): wrapper for isInt()
//
//	Params:		str - the str to be tested as an integer
//
//	Return: 	true if it the str represents a number
//				false if the str is not a number
//
function isNumeric(str)
{
	return isInt(str);
}


//
// isFloat(): detects floating-point numbers
//
//	Params: 	str - The str to be tested as a floating-point number
//
//	Return: 	true if str represents a floating-point number
//				false if not
//
function isFloat(str)
{
	var c;
	var i;
	var hasDecimal = false;

	if (isBlank(str))
	{
		if (isFloat.arguments.length == 1)
		{
			return NaN;
		}
		else
		{
			return isFloat.arguments[1];
		}
	}

	if (str == '.')
	{
		return false;
	}

	for (i = 0; i < str.length; i++)
	{
		c = str.charAt(i);

		if ((c == '.') && (! hasDecimal))
		{
			hasDecimal = true;
		}
		else if (!isDigit(c))
		{
			return false;
		}
	}

	return true;
}

//
// isDecimal(): Detects decimal numbers
//
//	Params: 	str - The str to be tested as a decimal
//				(floating-point) number
//
//	Return:		true if str represents a decimal number
//				false if not
//
//
function isDecimal(str)
{
	return isFloat(str);
}


//
//
// isMoney(): Detects valid money values
//
//	Params: 	str - The str to be tested as a money value
//
//	Return:		true if str is an integer or is a floating-point number which
//				 contains exactly 2 digits right of the decimal point
//				false if not
//
function isMoney(str)
{
	if (isInt(str))
	{
		return true;
	}
	else if (isFloat(str))
	{
		return (str.charAt(str.length - 3) == '.');
	}
	else
	{
		return false;
	}
}


//
//
// isAlpha(): Detects strs consisting solely of alphabetic characters
//
//	Params: 	str - The str to be tested
//
//	Return: 	true if str consists solely of alphabetic characters
//				false if not
//
function isAlpha(str)
{
	var c;

	if (isBlank(str))
	{
		return false;
	}

	for (var i = 0; i < str.length; i++)
	{
		c = str.charAt(i);

		if (!isLetter(c))
		{
			return false;
		}
	}

	return true;
}


//
// isAlphaNumeric(): Detects alphanumeric strings
//
//	Params: 	str - The str be tested
//
//	Return: 	true if str consists only of alphabetic and numeric characters
//				false if it does not
//
function isAlphaNumeric(str)
{
	var c;

	if (isBlank(str))
	{
		return false;
	}

	for (var i = 0; i < str.length; i++)
	{
		c = str.charAt(i);

		if ((! isLetter(c)) && (! isDigit(c)))
		{
			return false;
		}
	}

	return true;
}


//
// isLetter(): Checks if a single character is alphabetic
//
//	Params: 	character - The character to be tested
//
//	Return: 	true if the character is alphabetic
//				false if the character is not alphabetic or if a
//				multi-character str is passed
//
function isLetter(c)
{
	if ((isBlank(c)) || (c.length != 1))
	{
		return false;
	}

	return (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))
}


//
// isUpperCase(): Detects strs consisting solely of upper-case letters
//
//	Params: 	str - The str to be tested
//
//	Return: 	true if the str consists only of uppercase alphabetic chars
//				false if it does not
//
function isUpperCase(s)
{
	var c;

	if (isBlank(s))
	{
		return false;
	}

	for (var i = 0; i < s.length; i++)
	{
		c = s.charAt(i);

		if (! isUpper(c))
		{
			return false;
		}
	}

	return true;
}


//
// isUpper(): Checks if a single character is an upper-case letter
//
//	Params: 	character - The character to be tested
//
//	Return: 	true if the character is an upper-case letter
//				false if it is not an upper-case letter
//
function isUpper(c)
{
	if (isBlank(c) || (c.length != 1))
	{
		return false;
	}

	return ((c >= 'A') && (c <= 'Z'));
}


//
// isPunctuation(): Checks if a single character is a punctuation character
//
//	Params: 	character - The character to be tested
//
//	Return: 	true if c is a punctuation character
//				false if c is not a single punctuation character
//
function isPunctuation(c)
{
	if (isBlank(c) || (c.lenth != 1))
	{
		return false;
	}

	return ((c == '`') 	|| (c == '~') || (c == '!')	 ||
			(c == '@') 	|| (c == '#') || (c == '$')	 ||
			(c == '%')	|| (c == '^') || (c == '&')	 ||
			(c == '*') 	|| (c == '(') || (c == ')')	 ||
			(c == '-') 	|| (c == '_') || (c == '=')	 ||
			(c == '+') 	|| (c == '[') || (c == '{')	 ||
			(c == ']') 	|| (c == '}') || (c == '\\') ||
			(c == ';') 	|| (c == ':') || (c == '\'') ||
			(c == '\"') || (c == ',') || (c == '<')	 ||
			(c == '.') 	|| (c == '>') || (c == '/')	 ||
			(c == '?'));
}


//
// isValidIP(): Checks if input is a valid IP address. No submask support.
//
// 	Params:		ip_address - The IP to check
//
//	Return:		true if ip_address is a valid IPv4 addrss
//				false if ip_address is not a valid IPv4 addrss
//
function isValidIP(ip_address)
{
	var octets;

	if (isBlank(ip_address))
	{
		return false;
	}

	if ((ip_address == "0.0.0.0") || (ip_address == "255.255.255.255"))
	{
		return false;
	}

	octets = ip_address.split(".");

	if (octets.length != 4)
	{
		return false;
	}

	if (octets[0] == "0")
	{
		return false;
	}

	for (var i = 0; i < 4; i++)
	{
		if ((! isInt(octets[i])) || (parseInt(octets[i]) > 255) ||
			(parseInt(octets[i]) < 0))
		{
			return false;
		}
	}

	return true;
}


//
// getGenericDate(): Converts a date and mask to a yyyy-dd-mm format
//
//	Params:	date - The date string to be converted
//
//			mask - 	A mask representing the format of the date. The mask
//					must use the character "y" to represent	a space for the
//					year, the character "m" to represent a space for the month,
//					and the character "d" to represent a space for the date.
//
//					The year must consist of four characters.
//					The month may consist of either two or three characters. If
//					the month consists of three characters, it is assumed to
//					represent an abbreviation of the name; (e.g. Jan, Feb ...).
//					If the month consists of two characters, it is assumed to
//					be a numerical representation of the month. The date may
//					only consist of two charcaters. The year, month, and date
//					sections must be seperated by a one-character delimiter,
//					which may or may not be the the same in both places.
//
//	Return: The given date in the format yyyy-mm-yy if mask and date are valid
//			The str "BAD DATE" if either date or mask are not valid,
//			or if the date does not fit the mask
//
function getGenericDate(str, mask)
{
	var returnDate = "BAD DATE";

	var c;
	var delimiter1 		= '0';
	var delimiter2		= '0';
	var yearPosition;
	var yearLength		= 0;
	var monthPosition;
	var monthLength		= 0;
	var datePosition;
	var dateLength		= 0;
	var parsePosition	= 0;
	var currentElement	= "none";
	var element			= new Array();
	var n;

	for (n = 0; n < mask.length; n++)
	{
		c = mask.charAt(n);
		switch (c)
		{
			case 'y':
			case 'Y':
				if (currentElement != "year")
				{
					currentElement = "year";
					element[parsePosition] = "year";
					yearPosition = parsePosition++;
					yearLength = 0;
				}
				yearLength++;
				break;
			case 'd':
			case 'D':
				if (currentElement != "date")
				{
					currentElement = "date";
					element[parsePosition] = "date";
					datePosition = parsePosition++;
					dateLength = 0;
				}
				dateLength++;

				break;
			case 'm':
			case 'M':
				if (currentElement != "month")
				{
					currentElement = "month";
					element[parsePosition] = "month";
					monthPosition = parsePosition++;
					monthLength = 0;
				}
				monthLength++;

				break;
			default:
				if (delimiter1 == '0')
				{
					// initialize delimiter
					delimiter1 = c;
				}
				else if (delimiter2 == '0')
				{
					delimiter2 = c;
				}
				else
				{
					// c does not match a valid mask charcater
					// or previously found delimiter
					return returnDate;

				}
		}
	}

	if ((yearLength != 2 && yearLength != 4)   ||
		(monthLength != 3 && monthLength != 2) ||
		(dateLength != 2))
	{
		return returnDate;
	}

	var tempArray = new Array();
	var finalArray  = new Array();

	if (delimiter1 == delimiter2)
	{
		tempArray = str.split(delimiter1);
		finalArray = tempArray;

		if (finalArray.length != 3)
		{
			return returnDate;
		}
	}
	else
	{
		tempArray = str.split(delimiter1);

		if (tempArray.length != 2)
		{
			return returnDate;
		}
		else
		{
			finalArray[0] = tempArray[0];
			var tempArray2 = new Array(); //  first element parsed
			tempArray2 = tempArray[1].split(delimiter2);
			if (tempArray2.length != 2)
			{
				return returnDate;
			}
			else
			{
				finalArray[1] = tempArray2[0];
				finalArray[2] = tempArray2[1];
			}
		}
	}

	var year;
	var month;
	var date;

	for (n = 0; n < 3; n++)
	{
		switch (element[n])
		{
			case "year":
				year = finalArray[n];
				if ((!isInt(year)) || (year.length != yearLength))
				{
					return returnDate;
				}
				if (yearLength == 4)
				{
					if (year < "1890" || year > "2100")
					{
						return returnDate;
					}
				}
				else  //  Two digit year
				{
					if (year < "20")
					{
						year = "20" + year;
					}
					else
					{
						year = "19" + year;
					}
				}
				break;
			case "month":
				month = finalArray[n];
				if (monthLength == 3)
				{
					month = month.toUpperCase();
					switch (month)
					{
						case "JAN":
							month = "1";
							break;
						case "FEB":
							month = "2";
							break;
						case "MAR":
							month = "3";
							break;
						case "APR":
							month = "4";
							break;
						case "MAY":
							month = "5";
							break;
						case "JUN":
							month = "6";
							break;
						case "JUL":
							month = "7";
							break;
						case "AUG":
							month = "8";
							break;
						case "SEP":
							month = "9";
							break;
						case "OCT":
							month = "10";
							break;
						case "NOV":
							month = "11";
							break;
						case "DEC":
							month = "12";
							break;
						default:
							return returnDate;
					}
				}
				else
				{
					if (month.charAt(0) == '0')
					{
						month = month.charAt(1);
					}
					switch (month)
					{
						case "1":
						case "2":
						case "3":
						case "4":
						case "5":
						case "6":
						case "7":
						case "8":
						case "9":
						case "10":
						case "11":
						case "12":
							break;
						default:
							return returnDate;
					}
				}
				break;
			case "date":
					date = finalArray[n];
					if (date.charAt(0) == '0')
					{
						date = date.charAt(1);
					}
				break;
		}
	}

	if (validDate(month, date, year))
	{
		returnDate = year + "-" + month + "-" + date;
	}

	return returnDate;
}


//
//
//
function validDate(month, date, year)
{
	switch (month)
	{
		case "1": // Jan
		case "3": // Mar
		case "5": // May
		case "7": // Jul
		case "8": // Aug
		case "10":// Oct
		case "12": // Dec
			if ((date > 0) && (date <= 31))
			{
				return true;
			}
			break;
		case "4": // Apr
		case "6": // Jun
		case "9": // Sep
		case "11": // Nov
			if ((date > 0) && (date <= 30))
			{
				return true;
			}
			break;
		case "2": // Feb
			var daysInFeb;
			if (year % 4 != 0)
			{
				daysInFeb = 28;
			}
			else if (year % 400 == 0)
			{
				daysInFeb = 29;
			}
			else if (year % 100 == 0)
			{
				daysInFeb = 28;
			}
			else
			{
				daysInFeb = 29;
			}
			if ((date > 0) && (date <= daysInFeb))
			{
				return true;
			}
			break;
	}

	return false;
}


//
//isDate(): Detects if a given date is valid under the given mask
//
//	Params: 	date - The date to be tested
//				mask - Optional, assumed to be yyyy-mm-dd if not supplied
//
//	Return: 	true if the date fits the mask and is a valid date
//				false for all other cases
//
function isDate(str)
{
	var mask;

	if (isBlank(str))
	{
		false;
	}

	if (isDate.arguments.length == 2)
	{
		mask = isDate.arguments[1];
	}
	else
	{
		mask = "yyyy-mm-dd";
	}

	var test = getGenericDate(str, mask);

	return (test != "BAD DATE");
}


//
// ageBetween(): Detects if an age falls within the given range
//
//	Params: 	minAge - Minimum allowable age
//				maxAge - Maximum allowable age
//
//				date - Birth Date
//				mask - Date format to be applied to birth date.
//					   Assumed to be yyyy-mm-dd if not supplied
//
//	Return: 	true if the calculated age falls within the given range
//				false if the calculated age falls outside the range
//
function ageBetween(minYears, maxYears, date)
{
	var mask;

	if (arguments.length < 3)
	{
		return false;
	}
	else if (ageBetween.arguments.length == 4)
	{
		mask = ageBetween.arguments[3];
	}
	else
	{
		mask = "yyyy-mm-dd";
	}

	if (! isDate(date, mask))
	{
		return false;
	}

	var age = getAge(date, mask);

	return ((age >= minYears) && (age <= maxYears));
}


//
//
// getAge(): Calculates age given a date and mask
//
//	Params: 	birthday - Birth date
//				mask - Mask to applied to birthday.
//					   Assumed to be yyyy-mm-dd if not supplied
//
//	Return:		age if date and mask are valid
//				0 (zero) if date and mask are not avlid
//
function getAge(birthDay)
{
	var mask;

	if (isBlank(birthDay))
	{
		return false;
	}

	if (getAge.arguments.length == 2)
	{
		mask = getAge.arguments[1];
	}
	else
	{
		mask = "yyyy-mm-dd";
	}

	var	now 		= new Date();
	var	year 		= now.getFullYear();
	var	month 		= now.getMonth() + 1;
	var	date 		= now.getDate();
	birthDay 		= getGenericDate(birthDay, mask);

	if (birthDay == "BAD DATE")
	{
		return 0;
	}
	var birth 		= birthDay.split("-");
	var birthYear	= birth[0];
	var birthMonth 	= birth[1];
	var birthDate  	= birth[2];

	var age	= year - birthYear;

	if (month < birthMonth)
	{
		age--;
	}
	else if (birthMonth == month && date < birthDate)
	{
		age--;
	}

	return age;
}


//
//mod10(): Performs mod 10 verification on a credit card number
//
//	Params: 	ccNumber - Credit card number
//
//	Return: 	true if it is a valid credit card number
//				false if it is not valid
//
function mod10(ccNumber)
{
	var doubleIt = false;
	var total = 0;

	if (! isInt(ccNumber))
	{
		return false;
	}

	for (var i = (ccNumber.length - 1); i >= 0; i--)
	{

		var currentDigit = ccNumber.charAt(i);

		if (doubleIt)
		{
			total += parseInt(sumDigits(2 * currentDigit));
			doubleIt = false;
		}
		else
		{
			total += parseInt(currentDigit);
			doubleIt = true;
		}
	}
	return (total % 10 == 0);
}


//
//
//
function sumDigits(n)
{
	var total = 0;

	while(n > 0)
	{
		total += n % 10;
		n = Math.round((n / 10) - .5);
	}

	return total;
}


//
// isCardMatch(): Checks if a credit card is a valid cardType
//
//	Params: 	cardType -  Is one of "Visa", "Mastercard", "AmericanExpress",
//							"Discover", "JCB", "Diners", "CarteBlanche",
//							"Enroute". This parameter is case-insensitive.
//				number - 	The credit card number to be checked
//
//	Return: 	true if the credit card number is valid for the given company
//				false if the number is not valid
//
function isCardMatch (cardType, cardNumber)
{
	if ((isBlank(cardType)) || (isBlank(cardNumber)))
	{
		return false;
	}

	cardType = cardType.toUpperCase();

	if (cardType == "VISA")
	{
		return (isVisa(cardNumber));
	}

	if (cardType == "MASTERCARD")
	{
		return (isMasterCard(cardNumber));
	}

	if ((cardType == "AMERICAN EXPRESS") ||
		(cardType == "AMERICANEXPRESS") ||
		(cardType == "AMEREX") ||
		(cardType == "AMEX"))
	{
		return (isAmericanExpress(cardNumber));
	}

	if (cardType == "DISCOVER")
	{
		return (isDiscover(cardNumber));
	}

	if (cardType == "JCB")
	{
		return (isJCB(cardNumber));
	}

	if ((cardType == "DINERS") || (cardType == "CARTEBLANCHE"))
	{
		return  (isDinersClub(cardNumber))
	}

	if (cardType == "ENROUTE")
	{
		return (isEnRoute(cardNumber));
	}

	return false;
}


//
// mod11(): Performs mod 11 verification on a Mileage Plus account number
//
//	Params: 	number - account number
//
//	Return: 	true if it is a valid Mileage Plus number
//				false if it is not valid
//
function mod11(acctNumber)
{
	var count1;
	var count2;
	var remainder;

	if ((! isInt(acctNumber)) || (acctNumber.length != 11))
	{
		return false;
	}

	count1 = ((parseInt(acctNumber.charAt(0)) * 5) +
			  (parseInt(acctNumber.charAt(1)) * 4) +
			  (parseInt(acctNumber.charAt(2)) * 3) +
			  (parseInt(acctNumber.charAt(3)) * 2) +
			  (parseInt(acctNumber.charAt(4)) * 7) +
			  (parseInt(acctNumber.charAt(5)) * 6) +
			  (parseInt(acctNumber.charAt(6)) * 5) +
			  (parseInt(acctNumber.charAt(7)) * 4) +
			  (parseInt(acctNumber.charAt(8)) * 3) +
			  (parseInt(acctNumber.charAt(9)) * 2));

	remainder = count1 % 11;

	count2 = (remainder == 0) ? 0 : (11 - remainder);

	if (count2 == 10)
	{
		count2 = 0;
	}

	return (count2 == acctNumber.charAt(10));
}


//
// isVisa(): Checks if a credit card number is a valid Visa number
//
//	Params: 	ccNumber - The number to be tested
//
//	Return: 	true if it is a valid Visa card number
//				false if it is not
//
function isVisa(cc)
{
	if (isBlank(cc))
	{
		return false;
	}

	if (((cc.length == 16) || (cc.length == 13)) && (cc.substr(0, 1) == "4"))
	{
		return mod10(cc);
	}

	return false;
}


//
// isMasterCard(): Checks if a credit card number is a valid MasterCard number
//
//	Params: 	ccNumber - The number to be tested
//
//	Return: 	true if it is a valid Visa card number
//				false if it is not
//
function isMasterCard(cc)
{
	if ((isBlank(cc)) || (cc.length != 16))
	{
		return false;
	}

	var firstdig = cc.substr(0, 1);
	var seconddig = cc.substr(1, 1);

	if ((firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)))
	{
		return mod10(cc);
	}
	return false;

}


//
// isAmericanExpress(): Checks if a credit card number is a valid AmEx Number
//
//	Params: 	ccNumber - The number to be tested
//
//	Return: 	true if it is a valid Visa card number
//				false if it is not
//
function isAmericanExpress(cc)
{
	if ((isBlank(cc)) || (cc.length != 15))
	{
		return false;
	}

	var firstdig = cc.substr(0, 1);
	var seconddig = cc.substr(1, 1);

	if ((firstdig == 3) && ((seconddig == 4) || (seconddig == 7)))
	{
		return mod10(cc);
	}

	return false;
}

//
// isDinersClub(): Checks if a credit card number is a valid Diners Club number
//
//	Params: 	ccNumber - The number to be tested
//
//	Return: 	true if it is a valid Visa card number
//				false if it is not
//
function isDinersClub(cc)
{
	if ((isBlank(cc)) || (cc.length != 14))
	{
		return false;
	}

	var firstdig = cc.substr(0, 1);
	var seconddig = cc.substr(1, 1);

	if ((firstdig == 3) &&
		((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
	{
		return mod10(cc);
	}

	return false;
}


//
// isCarteBlanche(): Checks if a credit card number is a valid Carte Blanche
//
//	Params: 	ccNumber - The number to be tested
//
//	Return: 	true if it is a valid Visa card number
//				false if it is not
//
function isCarteBlanche(cc)
{
	return isDinersClub(cc);
}


//
// isDiscover(): Checks if a credit card number is a valid Discover number
//
//	Params: 	ccNumber - The number to be tested
//
//	Return: 	true if it is a valid Visa card number
//				false if it is not
//
function isDiscover(cc)
{
	if ((isBlank(cc)) || (cc.length != 16))
	{
		return false;
	}

	var first4digs = cc.substr(0, 4);

	if (first4digs == "6011")
	{
		return mod10(cc);
	}

	return false;
}


//
// isEnroute(): Checks if a credit card number is a valid Enroute number
//
//	Params: 	ccNumber - The number to be tested
//
//	Return: 	true if it is a valid Visa card number
//				false if it is not
//
function isEnRoute(cc)
{
	if ((isBlank(cc)) || (cc.length != 15))
	{
		return false;
	}

	var first4digs = cc.substr(0, 4);

	if ((first4digs == "2014") || (first4digs == "2149"))
	{
		return mod10(cc);
	}

	return false;
}


//
// isJCB(): Checks if a credit card number is a valid JCB number
//
//	Params: 	ccNumber - The number to be tested
//
//	Return: 	true if it is a valid Visa card number
//				false if it is not
//
function isJCB(cc)
{
	if ((isBlank(cc)) || (cc.length != 16))
	{
		return false;
	}

	var first4digs = cc.substr(0,4);

	if ((first4digs == "3088") ||
		(first4digs == "3096") ||
		(first4digs == "3112") ||
		(first4digs == "3158") ||
		(first4digs == "3337") ||
		(first4digs == "3528"))
	{
		return mod10(cc);
	}

	return false;
}


//
// stripNonNumeric(): Returns only numberic characters in a str
//
//	Params: 	s - the input str
//
//	Return: 	numeric character in input str or empty str
//				if s was null or empty
//
function stripNonNumeric(s)
{
	var returnstr = "";
	var c;

	if (s == null)
	{
		return "";
	}

	for (var i = 0; i < s.length; i++)
	{
		c = s.charAt(i);

		if (isInt(c))
		{
			returnstr += c;
		}
	}

	return returnstr;
}

//
// stripNonAlphaNumeric(): Returns only alpha-numeric characters in a str
//
//  Params:		s - the input str
//
//  Return:		alpha numeric character in input str or empty str
//				if s was null or empty
//
function stripNonAlphaNumeric(s)
{
	var returnstr = "";
	var c;

	if (s == null)
	{
		return "";
   	}

	for (var i = 0; i < s.length; i++)
	{
		c = s.charAt(i);

		if (isAlphaNumeric(c))
		{
			returnstr += c;
		}
	}

	return returnstr;
}


//
// stripNonFloats(): Returns only umeric characters in a str
//
//  Params:		s - the input str
//
//  Return:		numeric characters in input str or empty str
//				if s was null or empty
//
function stripNonFloat(s)
{
	var returnstr = "";
	var c;
	var have_dec  = false;

	if (s == null)
	{
		return "";
	}

	for (var i = 0; i < s.length; i++)
	{
		c = s.charAt(i);

		if (isInt(c))
		{
			returnstr += c;
		}
		else if ((c == ".") && (! have_dec))
		{
			returnstr += ".";
			have_dec = true;
		}
	}

	return returnstr;
}


//
// isValidState()
//
function isValidState(str)
{
	if (isBlank(str) || (str.length != 2))
	{
		return false;
	}

	str = str.toUpperCase();

	switch (str)
	{
		case "AL":
		case "AK":
		case "AS":
		case "AZ":
		case "AR":
		case "CA":
		case "CO":
		case "CT":
		case "DE":
		case "DC":
		case "FM":
		case "FL":
		case "GA":
		case "GU":
		case "HI":
		case "ID":
		case "IL":
		case "IN":
		case "IA":
		case "KS":
		case "KY":
		case "LA":
		case "ME":
		case "MH":
		case "MD":
		case "MA":
		case "MI":
		case "MN":
		case "MS":
		case "MO":
		case "MT":
		case "NE":
		case "NV":
		case "NH":
		case "NJ":
		case "NM":
		case "NY":
		case "NC":
		case "ND":
		case "MP":
		case "OH":
		case "OK":
		case "OR":
		case "PW":
		case "PA":
		case "PR":
		case "RI":
		case "SC":
		case "SD":
		case "TN":
		case "TX":
		case "UT":
		case "VT":
		case "VI":
		case "VA":
		case "WA":
		case "WV":
		case "WI":
		case "WY":
		case "AE":
		case "AA":
		case "AE":
		case "AE":
		case "AP":
		return true;
	}

	return false;
}


//
// isValidSSN()
//
function isValidSSN(str)
{
	var lastChar;
	var diff		= false;

	if ((! isNumeric(str)) || (str.length != 9))
	{
		return false;
	}

	lastChar = str.charAt(0);

	for (var n = 1; n < str.length; n++)
	{
		if (lastChar != str.charAt(n))
		{
			diff = true;
			break;
		}

		lastChar = str.charAt(n);
	}

	if (! diff)
	{
		return false;
	}

	return (! (str.substr(5, 4) == "0000"));
}


//
// isValidPhoneNumber()
//
function isValidPhoneNumber(str)
{
	var lastChar;

	if ((! isNumeric(str)) || (str.length != 10))
	{
		return false;
	}

	for (var n = 0; n < str.length; n++)
	{
		if (n != 0)
		{
			if (lastChar != str.charAt(n))
			{
				return true;
			}
		}

		lastChar = str.charAt(n);
	}

	return false;
}


//
// getPercent(): parses percentage from a bt fee
//
function getPercent(btfee)
{
	var n;

	if (isBlank(btfee))
	{
		return -1;
	}

	n = btfee.indexOf("%");

	if (n == -1)
	{
		return -1;
	}
	else
	{
		return btfee.substr(0, n + 1);
	}
}


//
//getMin(): parses the minimum dollar value from a bt fee field
//
// Params: btfee - the field containing the minimum dollar amount
//
// Return: the minimum dollar amount, including the dollar sign
// -1 if there is no minimum amount present in the field
//
function getMin(btfee)
{
	if (isBlank(btfee))
	{
		return -1;
	}

	var dollarSignIndex = btfee.indexOf("$");

	if (dollarSignIndex == -1)
	{
		return -1;
	}

	//  The value of min in the str is between
	//  the dollar sign and the next space
	var spaceIndex = btfee.indexOf(" ", dollarSignIndex);

	if (spaceIndex == -1)
	{
		return -1;
	}

	return btfee.substr(dollarSignIndex, spaceIndex - dollarSignIndex);
}


//
//getMax(): parses the maximum dollar value from a bt fee field
//
// Params: btfee - the field containing the maximum dollar amount
//
// Return: the maximum dollar amount, including the dollar sign
// -1 if there is no maximum amount present in the field
//
function getMax(btfee)
{
	if (isBlank(btfee))
	{
		return -1;
	}

	//  Find first dollar sign
	var dollarSignIndex = btfee.indexOf("$");

	if (dollarSignIndex == -1)
	{
		return -1;
	}

	//  Find second dollar sign
	dollarSignIndex = btfee.indexOf("$", dollarSignIndex + 1);

	if (dollarSignIndex == -1)
	{
		return -1;
	}

	//  The value of max in the str is between the
	//  second dollar sign and the next space
	var spaceIndex = btfee.indexOf(" ", dollarSignIndex);

	if (spaceIndex == -1)
	{
		spaceIndex = btfee.length;
	}

	return btfee.substr(dollarSignIndex, spaceIndex - dollarSignIndex);
}


//
// reformat: Very simple numeric string reformatter
//
//  Params: input - The String to be reformated
//			mask  - The mask to validate against. '#' represents numbers.
//					All non-'#' characters in mask are added to input
//					All non-numeric characters in input are stripped
//
// Returns: The formated string or "" on error
//
function reformat(input, mask)
{
	var inputPos = 0;
	var maskPos = 0;
	var outstr = "";
	var c;
	var in_c;

	input = stripNonNumeric(input);

	if ((input == "") && (mask.charAt(0) != '#'))
	{
		return "";
	}

	while ((inputPos <= input.length) && (maskPos < mask.length))
	{
		c = mask.charAt(maskPos);

		if (c != '#')
		{
			outstr += c;
		}
		else
		{
			in_c = input.charAt(inputPos);
			outstr += in_c;
			inputPos++;
		}

		maskPos++;
	}

	return outstr;
}

//
// setCursorPosition(): Sets the cursor location in a text box
//
// Params:	Control - the textbox object
//			Position - integer position to move cursor to
//
// Returns:	void
//
function setCursorPosition(Control, Position)
{
	tr = Control.createTextRange();
	size = Control.value.length;

	tr.moveEnd("character", Position - size);
	tr.moveStart("character" , Position  );

	tr.select();
	tr.collapse(true);
}


//
// doReformat(): Used in a timer in autoFormat. Do not invoke directly.
//
function doReformat(el, mask)
{
	var inval		= el.value;
	var outval;

	outval = reformat(inval, mask);
	el.value = outval;
}


//
// autoFormat(): Event Handler to force formatted input in text boxes
//
//  Params:		mask - the mask to force (See reformat())
//
//  Returns:	void
//
//	Example: (Phone Number)
//
//			<INPUT TYPE="text" SIZE=14 MAXLENGTH=14
//			 onKeyDown='autoFormat("(###) ###-####")'
//			 onClick='setCursorPosition(this, this.value.length + 1)'>
//
function autoFormat(mask)
{
	srcElement	= event.srcElement;
	var keyCode = event.keyCode;

	if ((keyCode == 39) || (keyCode == 37))
	{
		srcElement.keyCode = 0;
	}

	if (keyCode != 8)
	{
		setTimeout('doReformat(srcElement, "' + mask + '")', 0);
	}
}


//
// calcRate(): Calculates a formula of the form P+x%
//
//  Params:		formula  = "P+x.xx%"
//				P = some number
//
//	Returns: 	A formated 2 decimal place percentage with sign or formula on
//				unmatching formula
//
function calcRate(formula, P)
{
	var	value;

	if (formula.substring(0, 2) != "P+")
	{
		return formula;
	}

	P = parseFloat(P);

	if (isNaN(P))
	{
		return formula;
	}

	value = parseFloat(stripNonFloat(formula)) + P;

	return value.toFixed(2) + "%";
}


function trim(str)
{
	// It's debatable what we want to do here
	if ((str == null) || (typeof(str) != "string"))
	{
		return null;
	}

	// Rip space off front
	while (isWhiteSpace(str.charAt(0)))
	{
		str = str.substring(1, str.length);
	}

	// Rip space off end
	while (isWhiteSpace(str.charAt(str.length - 1)))
	{
		str = str.substring(0, str.length - 1);
	}

	return str;
}


function forceNumeric()
{
	var keyCode	= event.keyCode;
	var char	= String.fromCharCode(keyCode)

	return (isDigit(char))
}


function forceAlpha()
{
	var keyCode	= event.keyCode;
	var char	= String.fromCharCode(keyCode)

	return (isLetter(char))
}


function isEmail(address)
{
	var idx;
	var domain;


	if (isBlank(address))
	{
		return false;
	}

	idx = address.indexOf('@');

	if ((idx == -1) || (address.charAt(0) == "@") ||
		(address.indexOf('@', idx + 1) != -1))
	{
		return false;
	}

	domain = address.substr(idx + 1);

	if ((isBlank(domain)) || (domain.indexOf("..") != -1) ||
		(domain.indexOf(".") == -1) || (domain.charAt(0) == ".") ||
		(domain.charAt(domain.length - 1) == "."))
	{
		return false;
	}

	return true;
}

function formatPhone(inphone)
{
	if ((isBlank(inphone)) || (inphone.length != 10))
	{
		return inphone;
	}

	return "(" + inphone.substr(0, 3) + ") " + inphone.substr(3, 3) + "-" +
		   inphone.substr(6, 4);
}
