<!---
/*
	File:	dataValidationLib.js
	Date:	2 September 2002
	Author:	LMS
	Revisions:	2 April 2004 - changed function names, added functions, reorganized structure for use in 
								new simAdmin class structure

	Date and Time functions
	isValidDate (obj controlName, string field, bool required)
	isValidTime (obj controlName, string field, bool required)

	Edit Mode function
	isValidEditMode (obj controlName, string field)

	File
	eitherNewOrOldFile (obj controlName1, obj controlName2, string field, bool required)

	General functions
	confirmFields (obj controlName1, obj controlName2, string field) (useful for passwords, email, username)
	requiredField (obj controlName, string field, int minLength, int maxLength)
	
	Index Function
	none at moment

	Numeric functions
	isValidInteger (obj controlName, string field, bool required, int minVal, int maxVal)
	isValidFloat (obj controlName, string field, bool required, float minVal, float maxVal)

	Password function
	isValidPassword (obj controlName, string field, int minLength, int maxLength)

	Radio Button function
	isRadioButtonChecked (obj controlName, string field)

	String functions
	isValidEmail(obj controlName, string field, bool required)
	isValidPhone(obj controlName, string field, bool required)
	isValidSSN(obj controlName, string field, bool required)
	isValidURL(obj controlName, string field, bool required)
	isValidZipcode(obj controlName, string field, bool required)
	formatPhoneNumber (obj controlName)
	trimTextToSize (obj controlName, string field, int maxLength)

*/
function isValidDate (controlName, field, required)
{
var daysInMonth = Array (0,31,28,31,30,31,30,31,31,30,31,30,31);
var monthString = Array ("", "January", "February", "March", "April", "May", "June",
						"July", "August", "September", "October", "November", "December");
var dateString = controlName.value;
var dateParts = dateString.split ("/");
var year = dateParts[2] - 0;
var month = dateParts[0] - 0;
var day = dateParts[1] - 0;
var errorFlag = true;
var datePattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
if (month == 2 &&  ((year%4 == 0) && (!(year%100 == 0) || (year%400 == 0))))
	daysInMonth[2] = 29;

if (!required && dateString.length == 0) // if no date and not required, return true
	return true;

if (dateString.search (datePattern) == -1)
	{
	alert (dateString+" is not a valid date string for "+field+".\n Please enter a date in format MM/DD/YYYY)!");
	errorFlag = false;
	}
else if (month < 1 || month > 12)
	{
	alert (month+" is not a valid month for "+field+".");
	errorFlag = false;
	}
else if (day < 1 || day > daysInMonth[month])
	{
	alert ("There are only "+daysInMonth[month]+" in "+monthString[month]+", "+year+". You entered "+day+" in field "+field+".");
	errorFlag = false;
	}

if (!errorFlag)
	controlName.focus();

return (errorFlag);						// for use by a calling function					
}

function isValidTime (controlName, field, required)
{
var timeString = controlName.value;
var timeParts = timeString.split (":");
var errorFlag = true;
var timePattern = /^((0?[1-9])|(1[0-2])):[0-5][0-9]$/;

if (!required && timeString.length == 0) // if no date and not required, return true
	return true;

if (timeString.search (timePattern) == -1)
	{
	alert (timeString+" is not a valid time string for "+field+".\n Please enter a time in format HH:MM where HH is 1-12 and MM is 00-59!");
	controlName.focus();
	errorFlag = false;
	}
return (errorFlag);
}

function isValidEditMode (controlName)
{
var status = false;
switch (controlName.value.toLowerCase ())
	{
	case 'view':
	case 'copy':
	case 'delete':
	case 'new':
	case 'save':
	case 'login':
		status = true;
		break;
	default:
		alert ("The Edit Mode is invalid.  Please contact the Webmaster.");
	}
return status;
}		

function eitherNewOrOldFile (newFile, oldFile, field, required)
{
var status = false;

if (oldFile.value.length != 0)
	status = true;
else if (required)
	status = requiredField (newFile, field);
else
	status = true;

return status;
}

function confirmFields (first, second, fieldString)
{
var status = false;
if (first.value == second.value)
	status = true;
else
	{
	alert ('The '+fieldString+' and Confirm '+fieldString+' do not agree.  Please re-enter.');
	first.focus();
	}

return status;
}

function requiredField (controlName, field, minLength, maxLength)
{
var status = true;
var fieldValue = controlName.value;
if (fieldValue.length < minLength)
	{
	alert (field+' is a required field and must contain at least '+minLength+' characters!');
	controlName.focus();
	status = false;
	}
else
	trimTextToSize (controlName, field, maxLength);
return (status);
}

function isValidInteger (controlName, field, required, minVal, maxVal)
{
var value = controlName.value;
var status = true;
var integerPattern = /^[+-]?\d+$/;
if (!required && controlName.value.length == 0)
	return true;
if (value.search (integerPattern) == -1)
	{
	alert (value+' is not an integer.  Please enter a valid integer.');
	status = false;
	}
else if (!isNaN (minVal) && value < minVal)
	{
	alert (field+' must be an integer greater than or equal to '+minVal);
	status = false;
	}
else if (!isNaN (maxVal) && value > maxVal)
	{
	alert (field+' must be an integer less than or equal to '+maxVal);
	status = false;
	}

if (!status)
	controlName.focus();

return status;
}

function isValidFloat (controlName, field, required, minVal, maxVal)
{
var value = controlName.value;
var status = true;
var integerPattern = /^[+-]?\d*.\d*$/;

if (!required && controlName.value.length == 0)
	return true;
if (value.search (integerPattern) == -1)
	{
	alert (value+' is not floating point number.  Please enter a valid real number.');
	status = false;
	}
else if (!isNaN (minVal) && value < minVal)
	{
	alert (field+': '+value+' must be a real number greater than or equal to '+minVal);
	status = false;
	}
else if (!isNaN (maxVal) && value > maxVal)
	{
	alert (field+' must be a real number less than or equal to '+maxVal);
	status = false;
	}

if (!status)
	controlName.focus();

return status;
}

function isValidPassword (controlName, field, minLength, maxLength)
{
var status = false;
var passwordPatternString = '^[a-zA-Z0-9]{'+minLength+','+maxLength+'}$';
var passwordPattern = new RegExp (passwordPatternString);
var passLength = controlName.value.length;

searchResult = controlName.value.search (passwordPattern);
if (searchResult != -1)
	status = true;
else if (passLength < minLength || passLength > maxLength)
	{
	alert ('The password must be between '+minLength+' and '+maxLength+' characters long.  You have entered '+passLength+' characters.');
	}
else
	{
	alert ('The password '+controlName.value+' contains invalid characters.  It can only contain letters and numbers, no spaces or punctuation.');
	controlName.focus ();
	}

return status;
}

function isRadioButtonChecked (controlName, field)
{
var status = false;

for (i=0; i<controlName.length; ++i)
	{
	if (controlName[i].checked)
		{
		status = true;
		break;
		}
	}

if (!status)
	{
	alert ('Please choose an option for '+field+'.');
	controlName[0].focus ();
	}

return status;
}

function isValidEmail (controlName, field, required)
{
var EmailOk  = false;
var emailPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
if (!required && controlName.value.length == 0)
	return true;

if (controlName.value.search (emailPattern) == -1)
	{
	alert ('The email address entered ('+controlName.value+') is not valid.')
	controlName.focus ();
	}
else
	EmailOk = true;
return EmailOk;
}

function isValidPhone (controlName, field, required)
{
var phoneOK = true;
var phonePattern = /^(\(\d{3}\)|\d{3}) ?\d{3}( |-)?\d{4}( ?( |x|X)?\d+)?$/;
if (!required && controlName.value.length == 0)
	return true;
if (controlName.value.search (phonePattern) == -1)
	{
	alert ('The phone number entered ('+controlName.value+') is not valid.');
	controlName.focus ();
	phoneOK = false;
	}
else
	formatPhoneNumber (controlName);
return phoneOK;
}

function formatPhoneNumber (controlName)
{
var result = controlName.value.replace (/\D*/g, "");
var length = result.length;
if (length < 10)
	phoneNumber = result.substr(0,3)+'-'+result.substr(3,4);
else
	phoneNumber = '('+result.substr(0,3)+') '+result.substr(3,3)+'-'+result.substr(6,4);
if (length > 10)
	phoneNumber += ' x'+result.substr(10,length-10)
controlName.value = phoneNumber;
}

function isValidSSN (controlName, field, required)
{
var ssnOK = true;
var ssnPattern = /^\d{3}( |-)?\d{2}( |-)?\d{4}$/;
if (!required && controlName.value.length == 0)
	return true;
if (controlName.value.search (ssnPattern) == -1)
	{
	alert ('The Social Security Nnumber entered ('+controlName.value+') is not valid.');
	controlName.focus ();
	ssnOK = false;
	}
else
	formatSSN (controlName);
return ssnOK;
}

function formatSSN (controlName)
{
var result = controlName.value.replace (/\D*/g, "");
var length = result.length;
ssn = result.substr(0,3)+'-'+result.substr(3,2)+'-'+result.substr(5,4);
controlName.value = ssn;
}

function isValidURL (controlName, field, required)
{
var urlOK = true;
var urlPattern = /^http(s)?\:(\/){2}(\w+.)+\w+$/;

if (!required && controlName.value.length == 0)
	return true;

if (controlName.value.search (urlPattern) == -1)
	{
	alert ('The web address entered ('+controlName.value+') is not valid.  It requires http:// or https://.');
	controlName.focus ();
	urlOK = false;
	}
return urlOK;
}

function isValidZipcode (controlName, field, required)
{
var status=true;
var result = controlName.value.search (/^\d{5}( |-)?(\d{4})?$/);
if (!required && controlName.value.length == 0)
	return true;
if (result == -1)
	{
	alert ('The zipcode entered ('+controlName.value+') is not valid for '+field+'.');
	status = false;
	controlName.focus ();
	}
else
	formatZipcode (controlName);
return status;
}

function formatZipcode (controlName)
{
var result = controlName.value.replace (/\D*/g, "");
var length = result.length;
zipcode = result.substr(0,5);
if (length > 5)
	zipcode += ' '+result.substr(5,4);
controlName.value = zipcode;
}

function trimTextToSize (controlName, field, maxLength)
{
if (controlName.value.length > maxLength)
	{
	controlName.value.length = maxLength;
	alert ('Trimmed '+field+' to '+maxLength+' characters!');
	}
return true;
}

function validateCC (ccNumCtrl, ccNumStr, 
					expMonth, expYear, expStr, 
					valCtrl, codeStr)
{
var status = false;

if (validateCCNumber (ccNumCtrl, ccNumStr)
	&& validateCCExpiration (expMonth, expYear, expStr)
	&& validateCCCode (valCtrl, codeStr))
	status = true;

return status;
}

function validateCCNumber (ccNumCtrl, fieldStr)
{
var status = true;

if (ccNumCtrl.value.search (/^\d{4}( |-)?\d{4}( |-)?\d{4}( |-)?\d{4}$/))
	{
	alert ('The credit card number entered ('+ccNumCtrl.value+') is not a valid format.  Please re-enter.');
	ccNumCtrl.select ();
	ccNumCtrl.focus ();
	status = false;
	}

return status;
}

function validateCCExpiration (expMonth, expYear, expStr)
{
var status = true;

today = new Date();
var thisYear = today.getFullYear();
var thisMonth = today.getMonth() + 1;

if (expYear.value == thisYear && expMonth.value < thisMonth)
	{
	alert ('The card expiration date entered has expired.  Please enter a valid expiration date.');
	expMonth.focus ();
	status = false;
	}

return status;
}

function validateCCCode (valCtrl, codeStr)
{
var status = true;
var valCode = valCtrl.value;

if (valCode.search (/^[0-9]{3,4}$/) == -1)
	{
	alert ('Validation code must contain 3 or 4 characters and be a number from 000 to 9999');
	valCtrl.select ();
	valCtrl.focus ();
	status = false;
	}

return status;
}

-->