<!--
// JAVASCRIPT DATA VALIDATION FUNCTIONS
// Version 1.2
// Last Revision -- Matt Petersen -- 12/2/2002

//-------------------------------------------------------------------

// Define whitespace characters
var whitespace = " \t\n\r";

//-------------------------------------------------------------------

// Check for empty string
function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

//-------------------------------------------------------------------

// Check for whitespace characters
function isWhitespace (s)
{
	var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1)
		{
			return false;
	    }
	}
    return true;
}

//-------------------------------------------------------------------

// Force entry (return focus optional, third parameter)

function forceEntry(objField, FieldName, objFocus)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField))
	{
		alert("An entry is required in the " + FieldName + " field.");
		if (objFocus == 1)
		{
			objField.focus();
			objField.select();
		}
		return false;
	}
	return true;
}

//-------------------------------------------------------------------

// Force numeric value

function forceNumber(objField, FieldName, isReq)
{
	var strField = new String(objField.value);
	if (isReq == 1)
	{
		if (isWhitespace(strField))
		{
			alert("The " + FieldName + " field must contain a valid numeric entry!");
			objField.focus();
			return false;
		}
	}
	else
	{
		if (isWhitespace(strField))
		{
			return true;
		}	
	}
	var i = 0;
	for (i = 0; i < strField.length; i++)
	{
		if (strField.charAt(i) < '0' || strField.charAt(i) > '9')
		{
			alert("The " + FieldName + " field must contain a valid numeric entry!");
			objField.focus();
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------

// Force valid numeric value -- MONTH (MM format)

function forceMonth(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField))
	{
		return true;
	}
	var i = parseInt(strField, 10);
	var n = 0;
	for (n = 0; n < strField.length; n++)
	{
		if (strField.charAt(n) < '0' || strField.charAt(n) > '9')
		{
			alert("The " + FieldName + " field must contain a number between 1-12!");
			objField.focus();
			return false;
		}
		if (i < 1 || i > 12)
		{
			alert("The " + FieldName + " field must contain a number between 1-12!");
			objField.focus();
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------

// Force valid numeric value -- DAY (DD format)

function forceDay(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField))
	{
		return true;
	}
	var i = parseInt(strField, 10);
	var n = 0;
	for (n = 0; n < strField.length; n++)
	{
		if (strField.charAt(n) < '0' || strField.charAt(n) > '9')
		{
			alert("The " + FieldName + " field must contain a number between 1-31!");
			objField.focus();
			return false;
		}
		if (i < 1 || i > 31)
		{
			alert("The " + FieldName + " field must contain a number between 1-31!");
			objField.focus();
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------

// Force valid numeric value -- YEAR (YY format)

function forceYear(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField))
	{
		return true;
	}
	var i = parseInt(strField, 10);
	var n = 0;
	for (n = 0; n < strField.length; n++)
	{
		if (strField.charAt(n) < '0' || strField.charAt(n) > '9')
		{
			alert("The " + FieldName + " field must contain a number between 00-99!");
			objField.focus();
			return false;
		}
		if (i < 0 || i > 99)
		{
			alert("The " + FieldName + " field must contain a number between 00-99!");
			objField.focus();
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------

// Force valid numeric value -- HOUR (HH format)

function forceHour(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField))
	{
		return true;
	}
	var i = parseInt(strField, 10);
	var n = 0;
	for (n = 0; n < strField.length; n++)
	{
		if (strField.charAt(n) < '0' || strField.charAt(n) > '9')
		{
			alert("The " + FieldName + " field must contain a number between 1-12!");
			objField.focus();
			return false;
		}
		if (i < 1 || i > 12)
		{
			alert("The " + FieldName + " field must contain a number between 1-12!");
			objField.focus();
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------

// Force valid numeric value -- MINUTE (MM format)

function forceMinute(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField))
	{
		return true;
	}
	var i = parseInt(strField, 10);
	var n = 0;
	for (n = 0; n < strField.length; n++)
	{
		if (strField.charAt(n) < '0' || strField.charAt(n) > '9')
		{
			alert("The " + FieldName + " field must contain a number between 0-59!");
			objField.focus();
			return false;
		}
		if (i < 0 || i > 59)
		{
			alert("The " + FieldName + " field must contain a number between 0-59!");
			objField.focus();
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------

// Force valid email string

function forceEmail (emailStr,strName)
{
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null)
	{
		alert("The entry in the " + strName + " field is invalid! Please check '@' and '.' characters.")
		return false;
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null)
	{
	    alert("The username in the " + strName + " field is invalid (e.g. username@yourdomain.com)!")
	    return false;
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null)
	{
		for (var i=1;i<=4;i++)
		{
		    if (IPArray[i]>255)
			{
		        alert("The destination IP address in the " + strName + " field is invalid!")
				return false;
		    }
	    }
	    return true;
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null)
	{
		alert("The domain name in the " + strName + " field is invalid!")
	    return false;
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3 || len < 2)
	{ 
	   alert("The entry in the " + strName + " field is invalid! Email addresses must end in a 3-letter domain (e.g. '.com'), or a 2-letter country code (e.g. '.us').")
	   return false;
	}
	return true;
}

//-------------------------------------------------------------------

// Validate email string

function isEmail (emailStr,strName)
{
	if (isWhitespace(emailStr))
	{
		return true;
	}
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null)
	{
		alert("The entry in the " + strName + " field is invalid! Please check '@' and '.' characters.")
		return false;
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null)
	{
	    alert("The username in the " + strName + " field is invalid (e.g. username@yourdomain.com)!")
	    return false;
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null)
	{
		for (var i=1;i<=4;i++)
		{
		    if (IPArray[i]>255)
			{
		        alert("The destination IP address in the " + strName + " field is invalid!")
				return false;
		    }
	    }
	    return true;
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null)
	{
		alert("The domain name in the " + strName + " field is invalid!")
	    return false;
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3 || len < 2)
	{ 
	   alert("The entry in the " + strName + " field is invalid! Email addresses must end in a 3-letter domain (e.g. '.com'), or a 2-letter country code (e.g. '.us').")
	   return false;
	}
	return true;
}

//-------------------------------------------------------------------

// Force valid phone number (3-digit)

function forcePhone3(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField))
	{
		return true;
	}
	var i = parseInt(strField, 10);
	var n = 0;
	for (n = 0; n < strField.length; n++)
	{
		if (strField.charAt(n) < '0' || strField.charAt(n) > '9')
		{
			alert("The " + FieldName + " field must contain a valid telephone number!");
			objField.focus();
			return false;
		}
		if (i < 100 || i > 999)
		{
			alert("The " + FieldName + " field must contain a valid telephone number!");
			objField.focus();
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------

// Force valid phone number (4-digit)

function forcePhone4(objField, FieldName)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField))
	{
		return true;
	}
	var i = parseInt(strField, 10);
	var n = 0;
	for (n = 0; n < strField.length; n++)
	{
		if (strField.charAt(n) < '0' || strField.charAt(n) > '9')
		{
			alert("The " + FieldName + " field must contain a valid telephone number!");
			objField.focus();
			return false;
		}
		if (i < 0 || i > 9999)
		{
			alert("The " + FieldName + " field must contain a valid telephone number!");
			objField.focus();
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------

// Force valid date string
function isDate(dateStr,objField,origStr,isReq)
{
	if (isReq == 0)
	{
		if (isWhitespace(dateStr))
		{
			return true;
		}
	}
	var datePat = /^(\d{1,2})(?:\/|-)(\d{1,2})(?:\/|-)(\d{2,4})$/;
	var matchArray = dateStr.match(datePat);
	if (matchArray == null) 
	{
		alert("Date format must be MM/DD/YYYY");
		objField.focus();
		return false;
	}
	month = matchArray[1];
	day = matchArray[2];
	year = matchArray[3];
	if (year.length == 2)
	{
		if (year > 79)
		{
			year = "19" + year
		}
		else
		{
			year = "20" + year
		}
	}
	if (month < 1  || month > 12) 
	{
		alert("Month must be between 1 and 12.");
		objField.focus();
		return false;		
	}
	if (day < 1  || day > 31) 
	{
		alert("Day must be between 1 and 31.");
		objField.focus();
		return false;
	}
	if (year < 1900  || year > 2079) 
	{
		alert("Year must be between 1900 and 2079.");
		objField.focus();
		return false;
	}
	var strMonths = new Array();
	strMonths[1]  = "January";
	strMonths[2]  = "February";
	strMonths[3]  = "March";
	strMonths[4]  = "April";
	strMonths[5]  = "May";
	strMonths[6]  = "June";
	strMonths[7]  = "July";
	strMonths[8]  = "August";
	strMonths[9]  = "September";
	strMonths[10]  = "October";
	strMonths[11] = "November";
	strMonths[12] = "December";
	if (day > 30 && (month == 9 || month == 4 || month == 6 || month == 11 || month == 11))
	{
		var x = parseInt(month);
		alert(strMonths[x] + " has 30 days.");
		objField.focus();
		return false;
	}
	if ((month == 2 && (year == 2000 || year == 2004 || year == 2008 || year == 2012)) && day > 29)
	{
		alert("February has 29 days in " + year +".");
		objField.focus();
		return false;
	}
	if ((month == 2 && (year != 2000 || year != 2004 || year != 2008 || year != 2012)) && day > 28)
	{
		alert("February has 28 days in " + year +".");
		objField.focus();
		return false;
	}
	if (month.length == 1)
	{
		month = "0" + month;
	}
	if (day.length == 1)
	{
		day = "0" + day;
	}
	objField.value = month + "/" + day + "/" + year;	
	return true;
}

//-------------------------------------------------------------------

// Validate that start date preceeds end date
function isValidRange(startDate, endDate)
{
	var datePat = /^(\d{1,2})(?:\/|-)(\d{1,2})(?:\/|-)(\d{2,4})$/;
	var matchArray1 = startDate.match(datePat);
	var matchArray2 = endDate.match(datePat);
	var startmonth = matchArray1[1];
	var startday = matchArray1[2];
	var startyear = matchArray1[3];
	if (startyear.length == 2)
	{
		if (startyear > 79)
		{
			startyear = "19" + startyear;
		}
		else
		{
			startyear = "20" + startyear;
		}
	}
	var endmonth = matchArray2[1];
	var endday = matchArray2[2];
	var endyear = matchArray2[3];
	if (endyear.length == 2)
	{
		if (endyear > 79)
		{
			endyear = "19" + endyear;
		}
		else
		{
			endyear = "20" + endyear;
		}
	}
	if (startmonth.length == 1)
	{
		startmonth = "0" + startmonth;
	}
	if (startday.length == 1)
	{
		startday = "0" + startday;
	}
	var startString = startmonth + "/" + startday + "/" + startyear;
	if (endmonth.length == 1)
	{
		endmonth = "0" + endmonth;
	}
	if (endday.length == 1)
	{
		endday = "0" + endday;
	}
	var endString = endmonth + "/" + endday + "/" + endyear;
	var startdate = new Date(startString.substring(6,10), startString.substring(0,2)-1, startString.substring(3,5));
	var enddate = new Date(endString.substring(6,10), endString.substring(0,2)-1, endString.substring(3,5));
	if (enddate < startdate)
	{
		alert("The end date must not preceed the start date! Please check your entries.");
		return false;
	}
	else
	{
		return true;
	}
}

//-------------------------------------------------------------------

// Force valid time string

function isTime(dateStr,objField,origStr,isReq)
{
	if (isReq == 0)
	{
		if (isWhitespace(dateStr))
		{
		return true;
		}
	}

	// Validate string format
	var datetimePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = dateStr.match(datetimePat);
	if (matchArray == null) 
	{
		alert("Time format must be HH:MM AM");
		objField.value = origStr;
		objField.focus();
		objField.select();
		return false;
	}
	
	// Date elements
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }
	
	if (hour == 0 || hour == 00 || hour < 1 || hour > 12) 
	{
		alert("Hour must be between 1 and 12.");
		objField.value = origStr;
		objField.focus();
		objField.select();
		return false;
	}
	
	if (ampm == null) 
	{
		alert("You must specify AM or PM.");
		objField.value = origStr;
		objField.focus();
		objField.select();
		return false;
	}
	
	if (minute < 0 || minute > 59) 
	{
		alert ("Minute must be between 0 and 59.");
		objField.value = origStr;
		objField.focus();
		objField.select();
		return false;
	}
	
	var newDateTimeStr = hour + ":" + minute + " " + ampm;
	objField.value = newDateTimeStr;
	objField.focus();
	objField.select();
	return true;
}

//-------------------------------------------------------------------

function formatPhone(objField)
{
	var strField = new String(objField.value);
	if (isWhitespace(strField))
	{
		return true;
	}
	var is_valid = true;
	
	// Replace legal formatting characters
	var strField = strField.replace(" ", "-");
	var strField = strField.replace("\t", "-");
	var strField = strField.replace("\n", "-");
	var strField = strField.replace("\r", "-");
	var strField = strField.replace(".", "-");
	var strField = strField.replace("/", "-");
	var strField = strField.replace(" ", "-");
	var strField = strField.replace("\t", "-");
	var strField = strField.replace("\n", "-");
	var strField = strField.replace("\r", "-");
	var strField = strField.replace(".", "-");
	var strField = strField.replace("/", "-");
	
	// Check length
	if (strField.length < 12 || strField.length > 12)
	{
		var is_valid = false;
	}
	if (is_valid)
	{
		// Split into array
		var aPhone = new Array();
		aPhone[0] = parseInt(strField.substring(0,3), 10);
		aPhone[1] = strField.charAt(3);
		aPhone[2] = parseInt(strField.substring(4,7), 10);
		aPhone[3] = strField.charAt(7);
		aPhone[4] = strField.substring(8,12);
	
		// Check area code and prefix
		if (aPhone[0] < 100 || aPhone[0] > 999 || aPhone[2] < 100 || aPhone[2] > 999)
		{
			var is_valid = false;
		}
		
		// Check last 4 digits
		if (is_valid)
		{
			for (n = 0; n < 4; n++)
			{
				if(isNaN(aPhone[4].charAt(n)))
				{
					var is_valid = false;
				}
			}
		}
		
		// Check dashes
		if (is_valid)
		{
			if (aPhone[1] != "-" || aPhone[3] != "-")
			{
				var is_valid = false;
			}
		}
	}
	// Alert on error
	if (!is_valid)
	{
		alert("Telephone number format must be '111-222-3333'.");
		objField.focus();
		return false;
	}
	else
	{
		objField.value = strField;
		return true;
	}
}
//-->