// Email address
function ValidateEmail(FormInfo,ApplyFocus)
{

	if (ApplyFocus == null)
	{ApplyFocus = true}
	
	FixEmail(FormInfo)
	
	re = /^[^@\s]+@[^@\s]+(\.[a-z]{2,})$/i
	
	if	( !re.test(FormInfo.value) )
	{
	alert('Enter a valid email address.')
	if (ApplyFocus) {FormInfo.focus()}
	return false
	}
	
	return true

}


function FixEmail(FormInfo)
{

	FormInfo.value = FormInfo.value.toLowerCase().replace(/[^@\.\w]/g,"")
	
}

// ****************************************************************

// Phone
function ValidatePhone(FormInfo,ApplyFocus)
{

	if (ApplyFocus == null)
	{ApplyFocus = true}
	
	FixPhone(FormInfo)
	
	re = /^([1-9]\d{2})\-([1-9]\d{2}\-\d{4})$/
		
	if (!re.test(FormInfo.value))
	{
	alert("Enter a valid phone number in '123-456-7890' format.")
	if (ApplyFocus) {FormInfo.focus()}
	return false
	}
		
	return true
	
}


function FixPhone(FormInfo)
{

	EditedData = FormInfo.value.replace(/[^\d]/g,"")
	
	if (EditedData.length == 10)
	{FormInfo.value = EditedData.substr(0,3) + "-" + EditedData.substr(3,3) + "-" + EditedData.substr(6,4)}
	
	else
	{FormInfo.value = FormInfo.value.replace(/[^\d\-]/g,"")}

}

// ****************************************************************

// Zip
function ValidateZip(FormInfo,ApplyFocus)
{

	if (ApplyFocus == null)
	{ApplyFocus = true}
	
	FixZip(FormInfo)
	
	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 code in '12345' or '12345-6789' format.")
	if (ApplyFocus) {FormInfo.focus()}
	return false
	}
		
	return true
	
}


function FixZip(FormInfo)
{

	EditedData = FormInfo.value.replace(/[^\d]/g,"")
	
	if (EditedData.length == 5)
	{FormInfo.value = EditedData}
	
	else if (EditedData.length == 9)
	{FormInfo.value = EditedData.substr(0,5) + "-" + EditedData.substr(5,4)}
	
	else
	{FormInfo.value = FormInfo.value.replace(/[^\d\-]/g,"")}

}

// ****************************************************************

// File
function ValidateFileName(FormInfo,IsRequired,AllowedExtensions,DisallowedExtensions,ApplyFocus)
{

	if (ApplyFocus == null)
	{ApplyFocus = true}
	
	FileName = FormInfo.value.toString().replace(/^\s+/,"").replace(/\s+$/,"")
	
	if (FileName == "")
	{
	
		FormInfo.value = ""
	
		if (IsRequired)
		{
			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}
	
	FixNumber(FormInfo)

	// 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
			}
		}
	
	}
	
	
	// If number equals zero
	else 
	{
	
		if (DecimalPlaces != "")
		{
		
			FormInfo.value = "0."
			
			for (i=1; i<=DecimalPlaces; i++)
			{FormInfo.value = FormInfo.value + "0"}
			
		}
		
		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
	
}


function FixNumber(FormInfo)
{

	EditedData = FormInfo.value.replace(/[^\d\.\-]/g,"")
	EditedData = EditedData.replace(/\.+$/g,"")

	FormInfo.value = EditedData

}

// ****************************************************************

// Date
function ValidateDate(FormInfo,ErrorMessage,ApplyFocus)
{

	if (ErrorMessage == null)
	{ErrorMessage = ""}
	
	if (ApplyFocus == null)
	{ApplyFocus = true}
	
	FixDate(FormInfo)
	
	FormInfo.value = FormInfo.value.replace(/^0/,"")
	FormInfo.value = FormInfo.value.replace(/(\/)0(\d+)/g,"$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

}


function FixDate(FormInfo)
{

	FormInfo.value = FormInfo.value.replace(/[^\d\/]/g,"")

}

// ****************************************************************

// 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

}