// Email address
function ValidateEmail(FormInfo,ApplyFocus)
{

if (ApplyFocus == null)
{ApplyFocus = true}

FormInfo.value = FormInfo.value.toLowerCase()

re = /^[^@\s]+@[^@\s]+(\.[a-z]{2,})$/i
re2 = /[^-@\.\w]/g

if	( !re.test(FormInfo.value) || re2.test(FormInfo.value) )
{
alert('Enter a valid email address.')
if (ApplyFocus) {FormInfo.focus()}
return false
}

return true

}

// ****************************************************************

// Phone
function ValidatePhone(FormInfo,IsInternational,PhoneType,ApplyFocus)
{

	if (ApplyFocus == null)
	{ApplyFocus = true}
	
	if (IsInternational == null)
	{IsInternational = false}
	
	if (PhoneType == null)
	{PhoneType = "phone"}

	if (IsInternational)
	{
	
		if (FormInfo.value.split(" ").length > 2)
		{re = /^\d+\s\d+(\sx\d+)$/}
		else
		{re = /^\d+\s\d+$/}
		
		ValidationFormat = "1 234567 x890 (The first part is the country code.  The second part is the phone number.  The third part is the extension.)"
		
	}
		
	else
	{
		
		if (FormInfo.value.split(" ").length > 2)
		{re = /^(\([1-9]\d{2}\))\s([1-9]\d{2}\-\d{4})\sx\d+$/}
		else
		{re = /^(\([1-9]\d{2}\))\s([1-9]\d{2}\-\d{4})$/}
		
		ValidationFormat = "(123) 456-7890 x123"
				
	}
	
	if (!re.test(FormInfo.value))
	{
	alert('Enter a valid ' + PhoneType + ' number in the following format (extension is optional): ' + ValidationFormat)
	if (ApplyFocus) {FormInfo.focus()}
	return false
	}
		
	return true
	
}

// ****************************************************************

// Zip
function ValidateZip(FormInfo,IsInternational,ApplyFocus)
{

	if (IsInternational == null)
	{IsInternational = false}

	if (ApplyFocus == null)
	{ApplyFocus = true}
	
	if (IsInternational)
	{
	FormInfo.value = FormInfo.value.replace(/[^\d\w\-\s]/g,"")
	return true
	}
	
	else
	{
		if (FormInfo.value.length == 5)
		{re = /^\d\d{3}\d$/}
		else
		{re = /^\d{5}\-\d{4}$/}
		
		if (!re.test(FormInfo.value))
		{
		alert('Enter a valid zip\/postal code.')
		if (ApplyFocus) {FormInfo.focus()}
		return false
		}
	}
	
	return true
	
}

// ****************************************************************

// File
function ValidateFileName(FormInfo,IsRequired,AllowedExtensions,DisallowedExtensions,ApplyFocus)
{

	if (ApplyFocus == null)
	{ApplyFocus = true}
	
	FileName = FormInfo.value.toString().replace(/^\s+/,"").replace(/\s+$/,"")
	
	if (IsRequired)
	{
	
		if (FileName == "")
		{
		alert("The file name can not be blank.")
		if (ApplyFocus) {FormInfo.focus()}
		return false
		}
		
	}
	
	re = /^[^<>#"&@'\|\?\*]+(\.\w{2,})$/
	
	if (!re.test(FileName))
	{
	alert("The file name is not valid. Make sure there is a valid file extension. The following invalid characters can not be used in the file name: \|\'\<\>\"\#\&\@\?\*")
	if (ApplyFocus) {FormInfo.focus()}
	return false
	}	

	if (AllowedExtensions != null)
	{

		ValidExtension = false		
		ExtensionArray = AllowedExtensions.toLowerCase().split(",")
		FileNameArray = FileName.toLowerCase().split(".")
		LastIndex = FileNameArray.length - 1
		
		for (i=0; i<ExtensionArray.length; i++)
		{
		
			if ( FileNameArray[LastIndex] == ExtensionArray[i] )
			{ValidExtension = true; break}
			
		}
		
		if (!ValidExtension)
		{
		alert("Only the following file types are permitted: ." + AllowedExtensions.replace(/,/g,", ."))
		if (ApplyFocus) {FormInfo.focus()}
		return false
		}
	
	}
		
	if (DisallowedExtensions != null)
	{

		ValidExtension = true	
		ExtensionArray = DisallowedExtensions.toLowerCase().split(",")
		FileNameArray = FileName.toLowerCase().split(".")
		LastIndex = FileNameArray.length - 1
			
		for (i=0; i<ExtensionArray.length; i++)
		{
		
			if ( FileNameArray[LastIndex] == ExtensionArray[i] )
			{ValidExtension = false; break}
			
		}
		
		if (!ValidExtension)
		{
		alert("The following file types are not permitted: ." + DisallowedExtensions.replace(/,/g,", ."))
		if (ApplyFocus) {FormInfo.focus()}
		return false
		}
	
	}
	
	return true
	
}

// ****************************************************************

// Number
function ValidateNumber(FormInfo,LowerRange,UpperRange,AllowDecimal,DecimalPlaces,ForceNegative,ForcePositive,ApplyFocus)
{

	ExtraText = ""
	ErrorFound = false
	
	if (ApplyFocus == null)
	{ApplyFocus = true}

	// Convert range values to numbers
	if (LowerRange != "")
	{LowerRange = parseFloat(LowerRange)}

	if (UpperRange != "")
	{UpperRange = parseFloat(UpperRange)}

	// If number does not equal zero
	if (parseFloat(FormInfo.value) != 0)

	{

	// Remove leading zero
	FormInfo.value = FormInfo.value.replace(/^0+([\d+|\.])/,"$1")
	
	// Remove parenthesis and replace with negative sign
	FormInfo.value = FormInfo.value.replace(/\(/g,"-")
	FormInfo.value = FormInfo.value.replace(/\)/g,"")
	
	// If not a number
	if (isNaN(FormInfo.value) || FormInfo.value == "")
	{
	ExtraText = " number."
	ErrorFound = true
	}
	
	// If decimal has been passed and is not allowed
	if (FormInfo.value.indexOf(".") != -1 && !AllowDecimal && !ErrorFound)
	{
	ExtraText = " whole number."
	ErrorFound = true
	}
	
	// If decimal has been passed and is too many places places
	if (FormInfo.value.indexOf(".") != -1 && DecimalPlaces != "" && !ErrorFound)
	{
		DecimalString = FormInfo.value.split(".")
		NumberPlaces = DecimalString[1].length
		
		if (NumberPlaces > DecimalPlaces)
		{
		ExtraText = " number with no more than " + DecimalPlaces + " decimal place(s)."
		ErrorFound = true
		}
	}
	
	}
	
	else {FormInfo.value = "0"}
	
	// If number is not in required range
	if (LowerRange.toString() != "" && UpperRange.toString() != "" && !ErrorFound)
	{
		ExtraText = " number between " + LowerRange + " and " + UpperRange + "."
		
		if (FormInfo.value < LowerRange || FormInfo.value > UpperRange)
		{ErrorFound = true}
	}
	
	// If number is too small
	else if (LowerRange.toString() != "" && UpperRange.toString() == "" && !ErrorFound)
	{
		ExtraText = " number greater than or equal to " + LowerRange + "."
		
		if (FormInfo.value < LowerRange)
		{ErrorFound = true}
	}
	
	// If number is too large
	else if (LowerRange.toString() == "" && UpperRange.toString() != "" && !ErrorFound)
	{
		ExtraText = " number less than or equal to " + UpperRange + "."
	
		if (FormInfo.value > UpperRange)
		{ErrorFound = true}
	}
	
	// If number should be negative
	if (!ErrorFound && ForceNegative && FormInfo.value >= 0)
	{
	
		ExtraText = " negative number. Zero can not be entered."
		ErrorFound = true
	
	}
	
	// If number should be positive
	if (!ErrorFound && ForcePositive && FormInfo.value <= 0)
	{
	
		ExtraText = " positive number. Zero can not be entered."
		ErrorFound = true
	
	}
	
	
	// If error has been found
	if (ErrorFound)
	{
	alert('Enter a valid'+ExtraText)
	if (ApplyFocus) {FormInfo.focus()}
	return false
	}
	
	return true
	
}

// ****************************************************************

// Date
function ValidateDate(FormInfo,ErrorMessage,ApplyFocus)
{

if (ErrorMessage == null)
{ErrorMessage = ""}

if (ApplyFocus == null)
{ApplyFocus = true}

FormInfo.value = FormInfo.value.replace(/^0/,"")
FormInfo.value = FormInfo.value.replace(/(\/)0(\d+)/,"$1$2")

re = /^([1-9]\d?)\/([1-9]\d?)\/(\d{4})$/

re.exec(FormInfo.value)

if (
	!re.test(FormInfo.value) || 
	RegExp.$1 > 12 || 
	RegExp.$1 < 1 || 
	RegExp.$2 > 31 || 
	RegExp.$2 < 1 || 
	(RegExp.$1 == 2 && RegExp.$2 > 29) ||
	((RegExp.$1 == 4 || RegExp.$1 == 6 || RegExp.$1 == 9 || RegExp.$1 == 11) && (RegExp.$2 > 30)) ||
	RegExp.$3 < 1901 ||
	RegExp.$3 > 2079
	)

	{
	if (ErrorMessage == "")
	{
	alert("Enter a valid date in m/d/yyyy format.")
	}
	else
	{alert(ErrorMessage)}
	
	if (ApplyFocus) {FormInfo.focus()}
	return false
	}
	
if (RegExp.$1 == 2 && RegExp.$2 == 29)
	{
	if 	(RegExp.$3 % 4 != 0)
		{
		if (ErrorMessage == "")
		{alert(RegExp.$3 + " is not a leap year.")}
		else
		{alert(ErrorMessage + " " + RegExp.$3 + " is not a leap year.")}
		if (ApplyFocus) {FormInfo.focus()}
		return false
		}
	}

return true

}

// ****************************************************************

// Date Range
function ValidateDateRange(StartDate,EndDate,AllowEqualDates,ErrorMessage,ApplyFocus)
{

	if (AllowEqualDates == null)
	{AllowEqualDates = true}

	if (ErrorMessage == null)
	{ErrorMessage = ""}
	
	if (ApplyFocus == null)
	{ApplyFocus = true}
	
	StartYear = StartDate.value.split("/")[2]
	StartMonth = StartDate.value.split("/")[0]
	StartDay = StartDate.value.split("/")[1]
		
	if (StartMonth.length == 1)
	{StartMonth = "0" + StartMonth}
		
	if (StartDay.length == 1)
	{StartDay = "0" + StartDay}
		
	StartString = "" + StartYear + StartMonth + StartDay
		
	EndYear = EndDate.value.split("/")[2]
	EndMonth = EndDate.value.split("/")[0]
	EndDay = EndDate.value.split("/")[1]
		
	if (EndMonth.length == 1)
	{EndMonth = "0" + EndMonth}
		
	if (EndDay.length == 1)
	{EndDay = "0" + EndDay}
		
	EndString = "" + EndYear + EndMonth + EndDay
		
	if (EndString < StartString || (!AllowEqualDates && EndString == StartString) )
	{
	
		if (ErrorMessage == "")
		{
			if (!AllowEqualDates)
			{alert('The end date must be after the start date.')}
			else
			{alert('The end date can not be before the start date.')}
		}
		else
		{alert(ErrorMessage)}
			
		if (ApplyFocus) {EndDate.focus()}
		return false
	
	}
	
	return true

}

// ****************************************************************

// Format price
function FormatPrice(ThePrice)
{

	if (isNaN(ThePrice))
	{return 0}

	if (ThePrice == 0)
	{return 0}
	
	if (ThePrice.toString().indexOf(".") == -1)
	{return ThePrice + ".00"}
	
	PriceArray = ThePrice.toString().split(".")
	Dollars = PriceArray[0]
	Cents = PriceArray[1]
	
	if (Cents.length == 1)
	{return ThePrice + "0"}
	
	if (Cents.length == 2)
	{return ThePrice}
	
	if (Cents.length >= 3)
	{
	
		UseForRounding = parseInt(Cents.charAt(2))
		
		if (Cents.length > 3)
		{
		
			UseForRounding2 = parseInt(Cents.charAt(3))
		
			if (UseForRounding2 == 9)
			{UseForRounding++}
			
		}
		
		if (UseForRounding < 5)
		{return Dollars + "." + Cents.substr(0,2)}
		
		if (UseForRounding >= 5)
		{
		
			NumberToRound = parseInt(Cents.substr(0,2))
			
			if (NumberToRound < 99)
			{	

				RoundedUp = NumberToRound + 1
					
				if (RoundedUp.toString().length == 1)
				{return Dollars + ".0" + RoundedUp}
			
				else
				{return Dollars + "." + RoundedUp}
			
			}
			
			else
			{
	
				Dollars = parseInt(Dollars) + 1
				return Dollars + ".00"
				
			}
		
		}
	
	}
	
}