
/* This script is designed for the use of input validation 
   The following Methods are supported:
	
		MinLength(strValue, intLength)
			- determines if the value is at least 
			the requested length		
		
		MaxLength(strValue, intLength)
			- determines if the value is no longer
			than the requested length
		
		AbsoluteLength(strValue, intLength)
			- determines if the value is exactly the
			requested length
			
		ValidNumber(numNumber, bolAllowEmpty)
			- determines if the value supplied is a number
			or if the value is empty
		
		ValidTime(timTime, bolAllowEmpty)
			- determines if the value supplied is a time in 
			HH:MM:SS AM/PM format, where seconds are optional,
			or if the value is empty
			
		ValidDate(datDate, bolAllowEmpty)
			- determines if the value supplied is a date in
			(m)m/(d)d/yyyy format, or if the value is empty
		
		ValidAlpha(strAlpha, bolAllowEmpty)
			- determines if the value supplied contains all letters
			  or if the value is empty
			  
		ValidZipCode(strZip, intZipType, bolAllowEmpty)
			- determines if the value supplied is a valid Zip Code or
			  if the value is empty.  There are 3 modes for Zip Type:
				0 = Allow standard zip codes only
				1 = Allow extended zip codes only
				2 = Allow standard or extended zip codes
		
	All of these functions return a boolean value depending on
	the values supplied

*/


function MinLength(strValue, intLength)	{
	
	if (strValue.length < intLength)	{
		return false;
	}
	
	return true;
}


function MaxLength(strValue, intLength)	{
	
	if (strValue.length > intLength)	{
		return false;
	}
	
	return true;
}


function AbsoluteLength(strValue, intLength)	{

	if (strValue.length != intLength)	{
		return false;
	}
	
	return true;
}


function ValidNumber(numNumber, bolAllowEmpty)	{

	// Check for Empty String
	if (numNumber == "")	{
		return bolAllowEmpty;
	}
	
	if (isNaN(numNumber))	{
		return false;
	}
	
	return true;
}

	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds are optional.
function ValidTime(timTime, bolAllowEmpty) {

	// Check for Empty String
	if (timTime == "")	{
		return bolAllowEmpty;
	}
	
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?( (AM|am|PM|pm))$/;
	var matchArray = timTime.match(timePat);
	
	// Check for invalid characters
	if (matchArray == null) {
		return false;
	}
	
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];

	if (hour < 1  || hour > 12) {
		return false;
	}
	if (minute < 0 || minute > 59) {
		return false;
	}
	if (second != "" && (second < 0 || second > 59)) {
		return false;
	}
	return true;
}


	// Makes sure that dates are in the format (m)m/(d)d/yyyy
function ValidDate(datDate, bolAllowEmpty) {	
	
	// Check for Empty String
	if (datDate == "")	{
		return bolAllowEmpty;
	}
	
	// Periods cause problems
	if (datDate.indexOf(".",0) > 0)	{
		return false;
	}
	
	start = 0;
	end = datDate.indexOf("/",start);
	month = datDate.slice(start, end);
	start = end + 1;
	end = datDate.indexOf("/",start);
	day = datDate.slice(start, end);
	start = end + 1;
	year = datDate.slice(start); 

	if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12) )	{
		if ( day >= 1 && day <= 31) {
			if ( year >= 1000 && year <= 3000 ) {
				return true;
			}
		}
	}
	else if ((month == 4) || (month == 6) || (month == 9) || (month == 11) ) {
		if ( day >= 1 && day <= 30) {
			if ( year >= 1000 && year <= 3000 ) {
				return true;
			}
		}
	}
	else if (month == 2)	{
		if ( year >= 1000 && year <= 3000 && (year % 4 > 0)) {
			if ( day >= 1 && day <= 28) {
				return true;
			}
		}
		else if ( year >= 1000 && year <= 3000 && (year % 4 == 0)) {
			if ( day >= 1 && day <= 29) {
				return true;
			}
		}
	}
	//Did not find a valid date
	return false; 
}

function ValidAlpha(strAlpha, bolAllowEmpty)	{
	var alphaPat = /^([A-Za-z]*)$/;

	if (strAlpha == "")	{
		return bolAllowEmpty;
	}
	
	var matchArray = strAlpha.match(alphaPat);
	
	// Check for invalid characters
	if (matchArray == null) {
		return false;
	}

	return true;
}

function ValidZipCode(strZip, intZipType, bolAllowEmpty)	{
	var zipPatStandard = /^(\d{5})$/;
	var zipPatExtended = /^(\d{5})-(\d{4})$/;
	var zipPatEither = /^(\d{5})(-(\d{4}))?$/;
	
	if (strZip == "")	{
		return bolAllowEmpty;
	}
	
	switch (intZipType){
		case 0: 
			var matchArray = strZip.match(zipPatStandard);
			break;
		case 1: 
			var matchArray = strZip.match(zipPatExtended);
			break;
		case 2: 
			var matchArray = strZip.match(zipPatEither);
			break;
		default : 
			var matchArray = strZip.match(zipPatStandard);
	}
	
	// Check for invalid characters
	if (matchArray == null) {
		return false;
	}

	return true;
}


