/*
'*********************************************************************
'		FileName:		FormValidation.asp
'		Author:			Christopher Cerruto
'		DateCreated:	7/31/1998
'		Description:	This script contains the client-side Javascript functions
'						necessary to properly validate all form elements throughout
'						the application.
'*********************************************************************
*/

var strFormat;

function ValidateElement (	strLabelValue, intFormNumber, intElementNumber, strElementValue, strElementType,
							boolCanBeBlank,intMinLength, intMaxLength, strLessThanValue, strEqualToValue, strGreaterThanValue,
							boolAllowSpaces, boolFutureDate, boolDisplayMessage)

{
	strInvalidChars = "'<>\"";

	//If it's okay for the field to be blank and it is blank, it is validated
	if ((boolCanBeBlank  == "T") && (strElementValue.length == 0))
		return true;

	//Validate Element Length
	if (! IsValidLength(strElementValue, intMinLength, intMaxLength))
	{
		if ((strElementValue.length == 0) && (boolCanBeBlank != "T"))
			strErrorMsg = "The " + strLabelValue + " cannot be left blank.";
		else
			if (intMinLength == intMaxLength)
				strErrorMsg = "The " + strLabelValue + " must be " + intMinLength +" characters long.";
			else
				strErrorMsg = "The " + strLabelValue + " requires a length between " + intMinLength + " and " + intMaxLength + " characters.";
		if (boolDisplayMessage =="T")
			ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
		return false;
	}			

	//Validate Element Type
	if (! IsValidType(strElementValue, strElementType, strInvalidChars, boolAllowSpaces, boolFutureDate))
	{
		if (boolAllowSpaces == "F")
			if (boolFutureDate == "T")
				strErrorMsg = "The " + strLabelValue +  " must be a valid " + strElementType + " in the following format [" + strFormat + "] with no spaces.  This date must also be greater than today's date.";			
			else
				strErrorMsg = "The " + strLabelValue +" must be a valid " + strElementType + " in the following format [" + strFormat + "]"+" with no spaces.";
		else
			if (boolFutureDate == "T")
				strErrorMsg = "The " + strLabelValue +  " must be a valid " + strElementType + " in the following format [" + strFormat + "].  This date must also be greater than today's date.";			
			else
				strErrorMsg = "The " + strLabelValue +  " must be a valid " + strElementType + " in the following format [" + strFormat + "]";
		if (boolDisplayMessage =="T")				
			ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
		return false;
	}
	
	//If provided, validate whether element is less than another value
	if (strLessThanValue != "NULL")
		{
		if (! IsLessThan(strElementValue, strLessThanValue, strElementType))
		{
			strErrorMsg = "The " + strLabelValue + " cannot be greater than " + strLessThanValue;		
			if (boolDisplayMessage =="T")			
				ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
			return false;
		}
		}
	//If provided, validate whether element is equal to another value
	if (strEqualToValue != "NULL")
		if (! IsEqualTo(strElementValue, strEqualToValue, strElementType))
		{
			strErrorMsg = "The " + strLabelValue + " must be equal to " + strEqualToValue;		
			if (boolDisplayMessage =="T")
				ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
			return false;
		}

	//If provided, validate whether element is greater than another value
	if (strGreaterThanValue != "NULL")
		if (! IsGreaterThan(strElementValue, strGreaterThanValue, strElementType))
		{
			strErrorMsg = "The " + strLabelValue + " must be greater than " + strGreaterThanValue;		
			if (boolDisplayMessage =="T")			
				ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
			return false;
		}

	// if the validation was successful, then just return the true value.  this line won't be called if it failed anywhere during the
	// way because it will have returned already.
	return true;
}


//this function is a reworked version of the master function above.  The main difference is that this function
//accepts a string of invalid characters that can be used in validating strings
function IsValidElement (	strLabelValue, intFormNumber, intElementNumber, strElementValue, strElementType,
							strInvalidChars, boolCanBeBlank,intMinLength, intMaxLength, strLessThanValue, strEqualToValue, strGreaterThanValue,
							boolAllowSpaces, boolFutureDate, boolDisplayMessage)

{
	//If it's okay for the field to be blank and it is blank, it is validated
	if ((boolCanBeBlank  == "T") && (strElementValue.length == 0))
		return true;

	//Validate Element Length
	if (! IsValidLength(strElementValue, intMinLength, intMaxLength))
	{
		if ((strElementValue.length == 0) && (boolCanBeBlank != "T"))
			strErrorMsg = "The " + strLabelValue + " cannot be left blank.";
		else
			if (intMinLength == intMaxLength)
				strErrorMsg = "The " + strLabelValue + " must be " + intMinLength +" characters long.";
			else
				strErrorMsg = "The " + strLabelValue + " requires a length between " + intMinLength + " and " + intMaxLength + " characters.";
		if (boolDisplayMessage =="T")
			ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
		return false;
	}			

	//Validate Element Type
	if (! IsValidType(strElementValue, strElementType, strInvalidChars, boolAllowSpaces, boolFutureDate))
	{
		if (boolAllowSpaces == "F")
			if (boolFutureDate == "T")
				strErrorMsg = "The " + strLabelValue +  " must be a valid " + strElementType + " in the following format [" + strFormat + "] with no spaces.  This date must also be greater than today's date.";			
			else
				strErrorMsg = "The " + strLabelValue +" must be a valid " + strElementType + " in the following format [" + strFormat + "]"+" with no spaces.";
		else
			if (boolFutureDate == "T")
				strErrorMsg = "The " + strLabelValue +  " must be a valid " + strElementType + " in the following format [" + strFormat + "].  This date must also be greater than today's date.";			
			else
				strErrorMsg = "The " + strLabelValue +  " must be a valid " + strElementType + " in the following format [" + strFormat + "]";
		if (boolDisplayMessage =="T")				
			ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
		return false;
	}
	
	//If provided, validate whether element is less than another value
	if (strLessThanValue != "NULL")
		{
		if (! IsLessThan(strElementValue, strLessThanValue, strElementType))
		{
			strErrorMsg = "The " + strLabelValue + " cannot be greater than " + strLessThanValue;		
			if (boolDisplayMessage =="T")			
				ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
			return false;
		}
		}
	//If provided, validate whether element is equal to another value
	if (strEqualToValue != "NULL")
		if (! IsEqualTo(strElementValue, strEqualToValue, strElementType))
		{
			strErrorMsg = "The " + strLabelValue + " must be equal to " + strEqualToValue;		
			if (boolDisplayMessage =="T")
				ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
			return false;
		}

	//If provided, validate whether element is greater than another value
	if (strGreaterThanValue != "NULL")
		if (! IsGreaterThan(strElementValue, strGreaterThanValue, strElementType))
		{
			strErrorMsg = "The " + strLabelValue + " must be greater than " + strGreaterThanValue;		
			if (boolDisplayMessage =="T")			
				ValidationFailed(strErrorMsg, intFormNumber, intElementNumber)
			return false;
		}

	// if the validation was successful, then just return the true value.  this line won't be called if it failed anywhere during the
	// way because it will have returned already.
	return true;
}


// **************************************************************************************
// 	This routine will print out the appropriate error message for the corresponding error and
//	then put focus on the box being validated.
function ValidationFailed(strErrorMessage, intFormNumber, intElementNumber)
{
	alert(strErrorMessage);
	document.forms[intFormNumber].elements[intElementNumber].focus();
	document.forms[intFormNumber].elements[intElementNumber].select();
}

// **************************************************************************************
// 	This routine is used to ensure that the Element is of a certain type and meets
//	that type's requirements
function IsValidType(strElementValue, strElementType, strInvalidChars, boolAllowSpaces, boolFutureDate)
{
	// define element types
	// string, integer, real, date, email
	
	if (strElementType == "string")
		return IsValidString(strElementValue, strInvalidChars, boolAllowSpaces);
	if (strElementType == "integer")
		return IsValidInteger(strElementValue);
	if (strElementType == "real")
		return IsValidReal(strElementValue);
	if (strElementType == "date")
		return IsValidDate(strElementValue, boolFutureDate);
	if (strElementType == "shortdate")
		return IsValidShortDate(strElementValue);
	if (strElementType == "phoneno")
		return IsValidPhoneNo(strElementValue);
	if (strElementType == "zipcode")
		return IsValidZipCode(strElementValue);
	if (strElementType == "email")
		return IsValidEmail(strElementValue);
}

// **************************************************************************************
// 	This routine is used to ensure that the Element is a valid string by checking for 
//	invalid characters and blank spaces
function IsValidString(strElementValue, strInvalidChars, boolAllowSpaces)
{
	var boolAllSpaces;
	
	boolAllSpaces = true;

	if (boolAllowSpaces == "F")
		strInvalidChars = strInvalidChars + " ";

	strFormat = "does not contain any of the following characters:  " + strInvalidChars + " and cannot consist entirely of spaces";
			
	for (i=0; i < strElementValue.length; i++)
	{
		chrElementChar = strElementValue.substring(i, i+1);
		if (strInvalidChars.indexOf(chrElementChar, 0) != -1)
				return false;
		if (chrElementChar != " ")
			boolAllSpaces = false;
	}
	
	if (boolAllSpaces)
		return false;
		
	return true;
}

// **************************************************************************************
//	This routine is used to ensure that the Element is a valid integer
function IsValidInteger(strElementValue)
{
	strFormat = "contains only digits 0-9";

	for (i=0; i < strElementValue.length; i++)
	{
		chrElementChar = strElementValue.substring(i, i+1);
		if ((chrElementChar < "0") || (chrElementChar > "9"))
			return false;
	}
	return true;
}

// **************************************************************************************
//	This routine is used to ensure that the Element is a valid real number
function IsValidReal(strElementValue)
{
	strFormat = "xxx.xx";

	intPeriodsFound=0;
	for (i=0; i < strElementValue.length; i++)
	{
		chrElementChar = strElementValue.substring(i, i+1);
		if (((chrElementChar < "0") || (chrElementChar > "9")) && (chrElementChar != "."))
			return false;
		if (chrElementChar == ".")
			intPeriodsFound=intPeriodsFound+1;
	}
	if (intPeriodsFound > 1)
		return false;
	return true;
}

// **************************************************************************************
//	This routine is used to ensure that the Element is a valid date
function IsValidDate(strElementValue, boolFutureDate)
{

	strFormat = "mm/dd/yyyy";

	strInvalidChars="`~!@#$%^&*()=+[]{}|;,.<>?:";
	strValidChars="0123456789-/";
	strValidNumbers="0123456789";
	
	if (strElementValue.indexOf("-",0) == -1)
		chrSep = "/";
	else
		chrSep = "-";
	
	intFirstSep=strElementValue.indexOf(chrSep);
	if ((intFirstSep == 0) || (intFirstSep > 2) || (intFirstSep == -1))
		return false;
	intMonth=strElementValue.substring(0,intFirstSep);
	for (i=0; i < intMonth.length; i++)
	{
		chrElementChar = intMonth.substring(i, i+1);
		if ((strInvalidChars.indexOf(chrElementChar) != -1))
				return false;
		else
			if (strValidNumbers.indexOf(chrElementChar) == -1)
				return false; 
	}
	if ((intMonth > 12) || (intMonth < 1))
		return false;


	strElementValue=strElementValue.substring(intFirstSep+1,strElementValue.length);
	intSecondSep = strElementValue.indexOf(chrSep);	
	if ((intSecondSep == 0) || (intSecondSep > 2) || (intSecondSep == -1))
		return false;
	intDay=strElementValue.substring(0,intSecondSep);	
	for (i=0; i < intDay.length; i++)
	{
		chrElementChar = intDay.substring(i, i+1);
		if ((strInvalidChars.indexOf(chrElementChar) != -1))
				return false;
		else
			if (strValidNumbers.indexOf(chrElementChar) == -1)
				return false; 
	}

	strElementValue=strElementValue.substring(intSecondSep+1,strElementValue.length);
	intYear=strElementValue.substring(0,strElementValue.length);
//	if ((strElementValue.length != 2) && (strElementValue.length != 4))
//		return false;
	if (strElementValue.length != 4)
		return false;

	for (i=0; i < intYear.length; i++)
	{
		chrElementChar = intYear.substring(i, i+1);
		if ((strInvalidChars.indexOf(chrElementChar) != -1))
				return false;
		else
			if (strValidNumbers.indexOf(chrElementChar) == -1)
				return false; 
	}
	if (intYear > 3000)
		return false;

	if ((intMonth == 1) || (intMonth == 3) || (intMonth == 5) || (intMonth == 7) || (intMonth == 8) || (intMonth == 10) || (intMonth == 12))
		if ((intDay > 31) || (intDay < 1))
			return false;

	if ((intMonth == 4) || (intMonth == 6) || (intMonth == 9) || (intMonth == 11))
		if ((intDay > 30) || (intDay < 1))
			return false;

	if (intMonth == 2) 
		if ((intYear % 4) != 0)
		{
			if ((intDay > 28) || (intDay < 1))
				return false;
		}
		else
		{
			if ((intDay > 29) || (intDay < 1))
				return false;
		}
		
	if (boolFutureDate == "T")
	{
		strCurrentDate = new Date();
		intCurrentYear = strCurrentDate.getYear() * 1;
		intCurrentMonth = (strCurrentDate.getMonth() * 1) + 1;
		intCurrentDay = strCurrentDate.getDate() * 1;
		if (intCurrentYear < 100)
			intCurrentYear = intCurrentYear + 1900;

		if (intYear < intCurrentYear)
			return false;

		if ((intYear == intCurrentYear) && (intMonth < intCurrentMonth))
			return false;

		if ((intYear == intCurrentYear) && (intMonth == intCurrentMonth) && (intDay <= intCurrentDay))
			return false;
	}
		
	return true;
}


// **************************************************************************************
//	This routine is used to ensure that the Element is a valid date
function IsValidShortDate(strElementValue)
{

	strFormat = "mm/yy";

	strInvalidChars="`~!@#$%^&*()=+[]{}|;,.<>?:";
	strValidChars="0123456789-/";
	strValidNumbers="0123456789";
	
	if (strElementValue.indexOf("-",0) == -1)
		chrSep = "/";
	else
		chrSep = "-";
	
	intFirstSep=strElementValue.indexOf(chrSep);
	if ((intFirstSep == 0) || (intFirstSep > 2) || (intFirstSep == -1))
		return false;
	intMonth=strElementValue.substring(0,intFirstSep);
	for (i=0; i < intMonth.length; i++)
	{
		chrElementChar = intMonth.substring(i, i+1);
		if ((strInvalidChars.indexOf(chrElementChar) != -1))
				return false;
		else
			if (strValidNumbers.indexOf(chrElementChar) == -1)
				return false; 
	}
	if ((intMonth > 12) || (intMonth < 1))
		return false;

	strElementValue=strElementValue.substring(intFirstSep+1,strElementValue.length);
	intYear=strElementValue.substring(0,strElementValue.length);
	if (strElementValue.length != 2)
		return false;

	for (i=0; i < intYear.length; i++)
	{
		chrElementChar = intYear.substring(i, i+1);
		if ((strInvalidChars.indexOf(chrElementChar) != -1))
				return false;
		else
			if (strValidNumbers.indexOf(chrElementChar) == -1)
				return false; 
	}
	if ((intYear < 98) && (intYear > 50))
		return false;

	strCurrentDate = new Date();
	intCurrentMonth = (strCurrentDate.getMonth() * 1) + 1;	
	
	if ((intYear == 98) && (intMonth < intCurrentMonth))
		return false;

	return true;
}


// **************************************************************************************
//	This routine is used to ensure that the Element is a valid Email Address
function IsValidEmail(strElementValue)
{
	strFormat = "xxxxx@xxxxx.xxx";
	
	strInvalidChars="`~!#$%^&@*()=+[]{}|;,<>?:";
	strInvalidBodyChars="`~!#$%^&@*()=+[]{}|;,.<>?:";	

	intFirstSep=strElementValue.indexOf("@");
	if ((intFirstSep == 0) || (intFirstSep == -1))
		return false;

	strName=strElementValue.substring(0,intFirstSep);

	for (i=0; i < strName.length; i++)
	{
		chrElementChar = strName.substring(i, i+1);
		if (strInvalidChars.indexOf(chrElementChar) != -1)
				return false;
	}

	strElementValue=strElementValue.substring(intFirstSep+1,strElementValue.length);
	intSecondSep = -1;
	for (i=0; i< strElementValue.length; i++)
	{
		chrElementChar = strElementValue.substring(i, i+1);
		if (chrElementChar == ".")
			intSecondSep = i;
	}
	
	if ((intSecondSep == 0) || (intSecondSep == -1))
		return false;
	strLocation=strElementValue.substring(0,intSecondSep);	
	
	for (i=0; i < strLocation.length; i++)
	{
		chrElementChar = strLocation.substring(i, i+1);
		if ((strInvalidChars.indexOf(chrElementChar) != -1))
				return false;
	}

	strElementValue=strElementValue.substring(intSecondSep+1,strElementValue.length);
	strExtension=strElementValue.substring(0,strElementValue.length);
	
//	if (strExtension.length != 3)
//		return false;
	for (i=0; i < strExtension.length; i++)
	{
		chrElementChar = strExtension.substring(i, i+1);
		if ((strInvalidBodyChars.indexOf(chrElementChar) != -1))
				return false;
	}
	return true;
}


// **************************************************************************************
//	This routine is used to ensure that the Element is of a valid length.  This checks
//	minimum and maximum parameters
function IsValidLength(strElementValue, intMinLength, intMaxLength)
{
	if ((strElementValue.length <= intMaxLength) && (strElementValue.length >= intMinLength))
		return true;
	else
		return false;
}

// **************************************************************************************
//	This routine is used to ensure that the Element is less than a passed value
function IsLessThan(strElementValue, strLessThanValue, strElementType)
{
	if (strElementType == "integer")
	{
		strElementValue=strElementValue*1;
		strLessThanValue=strLessThanValue*1;
	}	
	if (strElementType == "real")
	{
		strElementValue=strElementValue*1.0;
		strLessThanValue=strLessThanValue*1.0;
	}			
	
	if (strElementValue <= strLessThanValue)
		return true;
	else
		return false;
}

// **************************************************************************************
//	This routine is used to ensure that the Element is equal to a passed value
function IsEqualTo(strElementValue, strEqualToValue, strElementType)
{
	if (strElementType == "integer")
	{
		strElementValue=strElementValue*1;
		strEqualToValue=strEqualToValue*1;
	}	
	if (strElementType == "real")
	{
		strElementValue=strElementValue*1.0;
		strEqualToValue=strEqualToValue*1.0;
	}			

	if (strElementValue == strEqualToValue)
		return true;
	else
		return false;
}

// **************************************************************************************
//	This routine is used to ensure that the Element is greater than a passed value
function IsGreaterThan(strElementValue, strGreaterThanValue, strElementType)
{
	if (strElementType == "integer")
	{
		strElementValue=strElementValue*1;
		strGreaterThanValue=strGreaterThanValue*1;
	}	
	if (strElementType == "real")
	{
		strElementValue=strElementValue*1.0;
		strGreaterThanValue=strGreaterThanValue*1.0;
	}			
	
	if (strElementValue > strGreaterThanValue)
		return true;
	else
		return false;
}

//	End of routines

