function validateRequiredSelect(field)
{
	// used to make sure an item is selected from a listbox
	if (field.value == "")
		{
			alert("This is a required field. Please make a selection");
			field.focus();
			return false;
		}
	return true;
}
function validateRequired(field)
{
	// called on the onBlur event to ensure there is an entry
	if (field.value == "")
		{
			alert("This is a required field. Please enter a value");
			field.focus();
			field.select();
			return false;
		}
	return true;
}

function validateNumeric(field)
{
	var valid = "0123456789.-"
	var numeric = true;
	var temp;
	for (var x = 0; x < field.value.length; x++)
		{
			temp = "" + field.value.substring(x, x + 1);
			if (valid.indexOf(temp) == "-1") numeric = false;
		}
	if (!numeric)
		{
		alert("This is a numeric field. Please enter a numeric value.");
		field.focus();
		field.select();
		return false;
		}
	return true;
}

function validateAlpha(field)
{
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-. '"
	var alpha = true;
	var temp;
	for (var x = 0; x < field.value.length; x++)
		{
		temp = "" + field.value.substring(x, x + 1);
		if (valid.indexOf(temp) == "-1") alpha = false;
		if (valid.indexOf(temp) == "'") alpha = true;
		}
	if (!alpha)
		{
		alert("Only alphabetic characters are allowed in this field. Please enter text only.");
		field.focus();
		field.select();
		return false;
		}
	return true;	
}

function validateAlphanumeric(field)
{
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.- '"
	var alphanumeric = true;
	var temp;
	for (var x = 0; x < field.value.length; x++)
		{
		temp = "" + field.value.substring(x, x + 1);
		if (valid.indexOf(temp) == "-1") alphanumeric = false;
		if (valid.indexOf(temp) == "'") alphanumeric = true;
		}
	if (!alphanumeric)
		{
		alert("Only alphanumeric characters are allowed (a-z, A-Z, 0-9). Please try again.");
		field.focus();
		field.select();
		return false;
		}
	return true;
}

function validateAddress(field)
{
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 #-.'"
	var alphanumeric = true;
	var temp;
	for (var x = 0; x < field.value.length; x++)
		{
		temp = "" + field.value.substring(x, x + 1);
		if (valid.indexOf(temp) == "-1") alphanumeric = false;
		}
	if (!alphanumeric)
		{
		alert("Only alphanumeric characters are allowed. Please try again.");
		field.focus();
		field.select();
		return false;
		}
	return true;
}

function validateLength(field, length)
{
	if (field.value.length < length)
		{
		alert("This field must be at least " + length + " characters long. Please try again.");
		field.focus();
		field.select();
		return false;
		}
	return true;
}

function validateMaxLength(field, length)
{
	if (field.value.length > length)
		{
		alert("This field cannot be more than " + length + " characters long. Please try again.");
		field.focus();
		field.select();
		return false;
		}
	return true;
}

function validateZIP(field)
{
	var s = new String(field.value);
	var valid = "0123456789-";
	var hyphencount = 0;

	if (s.length != 5 && s.length != 10)
		{
		// inappropriate length
		alert("Please enter a 5 digit or 5 digit+4 zip code.");
		field.focus();
		field.select();
		return false;
		}
		
	for (var i=0; i < s.length; i++)
		{
		temp = "" + s.substring(i, i+1);
		if (temp == "-")
			{
			hyphencount++;
			}
		if (valid.indexOf(temp) == "-1")
			{
			alert("Invalid characters in your zip code. Please try again.");
			field.focus();
			field.select();
			return false;
			}
		if (hyphencount == 1) 
			{
			if (s.length < 10)
				{
				alert("The hyphen should only be used with a properly formatted 5 digit + four zip code, like '12345-6789'. Please try again.");
				field.focus();
				field.select();
				return false;
				}
			else
				if("" + s.charAt(5) != "-")
					{
					alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
					field.focus();
					field.select();
					return false;
					}
			}
		else
			{
			if (hyphencount > 1)
				{
				alert("Only one hyphen character is allowed in a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
				field.focus();
				field.select();
				return false;
				}
			}
		}
		return true;
}

function validateEmail(field)
{
    var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if(emailReg.test(field.value))
		{
		return true;
		}
	else
		{
		alert("Your Email address appears to be incorrect. Please ensure it is in the proper format (ex. user@domain.com)");
		field.focus();
		field.select();
		return false;
		}
}		

// 12/05/05 Revised to allow the _ character in the first position
//function validateEmail(field)
//{
//	var emailad = field.value;
//	var exclude=/[^@\-\._\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
//	var check=/@[\w\-]+\./;
//	var checkend=/\.[a-zA-Z]{2,3}$/;
//
//	if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1))
//		{
//		alert("Your Email address appears to be incorrect. Please ensure it is in the proper format (ex. user@domain.com)");
//		field.focus();
//		field.select();
//		return false;
//		}
//	return true;
//}



function validateDL(field, state)
{
	switch(state.value)
		{
		case "SC":
			if(!validateNumeric(field))
				{
				return false;
				}
			if(!validateLength(field,5))
				{
				return false;
				}	
			if(!validateMaxLength(field,9))
				{
				return false;
				}	
			break
		case "GA":
			if(!validateNumeric(field))
				{
				return false;
				}
			if(!validateLength(field,5))
				{
				return false;
				}	
			if(!validateMaxLength(field,9))
				{
				return false;
				}	
			break
		default:
			if(!validateAlphanumeric(field))
				{
				return false;
				}
			break
		}
	return true;
}




function validateDate(field)
{
	var s = field.value;
	var delimeter = "/";
	
	split = s.split(delimeter);

	month = eval(split[0]);
	day = eval(split[1]);
	year = eval(split[2]);
	
	// check to make sure the month is valid
	if (month > 0 && month <= 12)
		{
		// ok
		}
	else
		{
		// not a valid month
		alert("The month is invalid. Please ensure you use mm/dd/yy format.");
		field.focus();
		field.select();
		return false;
		}

	// check to make sure the day is valid
	if (day > 0 && day <= 31)
		{
		// check months
		if (month == 4 || month == 6 || month == 9 || month == 11)
			{
			if (day == 31)
				{
				alert("This month cannot have more than 30 days.");
				field.focus();
				field.select();
				return false;
				}
			}
		if (month == 2)
			{
			if (day > 29)
				{
				alert("February can only have 29 days");
				field.focus();
				field.select();
				return false;
				}
			else
				{
				if (day == 29)
					{
					if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
						// this is a leap year so 29 days is ok
						{
						// ok
						}
					else
						{
						alert("This is not a leap year. February can only have 28 days.")
						field.focus();
						field.select();
						return false;
						}
					}
				}
			}	
		}	
	else
		{
		alert("The day is invalid. Please ensure you use mm/dd/yy format.");
		field.focus();
		field.select();
		return false;
		}
		
	// check to make sure the year is valid	
	if (year >= 0 && year < 100)
		{
		// ok
		}
	else
		{
		alert("The year is invalid. Please ensure you use mm/dd/yy format.")
		field.focus();
		field.select();
		return false;
		}
	// this is a valid date	
	return true;
}

function validatePFC(field)
	{
	var valid = "0123456789"
	var numeric = true;
	var temp = field.value;
	var pfcTest;
	var checkDigit;

	for (var x = 0; x < field.value.length; x++)
		{
		temp = "" + field.value.substring(x, x + 1);
		if (valid.indexOf(temp) == "-1") numeric = false;
		}
	if(!numeric)
		{
		alert("This field must be numeric");
		field.focus();
		field.select();
		return false;
		}
		
	if (field.value.length == 12)
		{
		pfcTest = "" + field.value.substring(0, 11);
		checkDigit = "" + field.value.substring(11, 12);
		if(checkDigit_MOD10(pfcTest) == checkDigit)
			{
			return true;
			}
		else
			{
			alert("This is not a valid PFC number");
			field.focus();
			field.select();
			return false;
			}
		}
	return true;
	}

function checkDigit_MOD10(pfcTest)
{
	var y = 0;
	var nbrAccum = 0;
	var result = 0;
	for(var x = 0; x != 11; x++)
	{
		y = pfcTest.substring(x, x + 1);
		switch(x)
		{
			case 0:
				nbrAccum = nbrAccum + (y * 3);
				break;
			case 1:
				nbrAccum = nbrAccum + (y * 1);
				break;
			case 2:
				nbrAccum = nbrAccum + (y * 3);
				break;
			case 3:
				nbrAccum = nbrAccum + (y * 1);
				break;
			case 4:
				nbrAccum = nbrAccum + (y * 3);
				break;
			case 5:
				nbrAccum = nbrAccum + (y * 1);
				break;
			case 6:
				nbrAccum = nbrAccum + (y * 3);
				break;
			case 7:
				nbrAccum = nbrAccum + (y * 1);
				break;
			case 8:
				nbrAccum = nbrAccum + (y * 3);
				break;
			case 9:
				nbrAccum = nbrAccum + (y * 1);
				break;
			case 10:
				nbrAccum = nbrAccum + (y * 3);
				break;
		}
	}
	result = nbrAccum % 10;
	if (result != 0)
		result = 10 - result;
	return result;
}


function validateFullDate(field)
{
	var s = field.value;
	var delimeter = "/";
	
	split = s.split(delimeter);

	month = eval(split[0]);
	day = eval(split[1]);
	year = eval(split[2]);
	
	// check to make sure the month is valid
	if (month > 0 && month <= 12)
		{
		// ok
		}
	else
		{
		// not a valid month
		alert("The month is invalid. Please ensure you use mm/dd/yyyy format.");
		field.focus();
		field.select();
		return false;
		}

	// check to make sure the day is valid
	if (day > 0 && day <= 31)
		{
		// check months
		if (month == 4 || month == 6 || month == 9 || month == 11)
			{
			if (day == 31)
				{
				alert("This month cannot have more than 30 days.");
				field.focus();
				field.select();
				return false;
				}
			}
		if (month == 2)
			{
			if (day > 29)
				{
				alert("February can only have 29 days");
				field.focus();
				field.select();
				return false;
				}
			else
				{
				if (day == 29)
					{
					if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
						// this is a leap year so 29 days is ok
						{
						// ok
						}
					else
						{
						alert("This is not a leap year. February can only have 28 days.")
						field.focus();
						field.select();
						return false;
						}
					}
				}
			}	
		}	
	else
		{
		alert("The day is invalid. Please ensure you use mm/dd/yyyy format.");
		field.focus();
		field.select();
		return false;
		}
		
	// check to make sure the year is valid	
	if ((year >=1 && year < 100) || (year >= 1000 && year < 10000))
		{
		// ok
		}
	else
		{
		alert("The year is invalid. Please ensure you use mm/dd/yyyy format.")
		field.focus();
		field.select();
		return false;
		}
	// this is a valid date	
	return true;
}
function validateBirthDate(field)
{
	// make sure all characters are valid
	var valid = "0123456789/"
	var numericOrSlash = true;
	var temp;
	var currentDate = new Date();
		
	for (var x = 0; x < field.value.length; x++)
		{
			temp = "" + field.value.substring(x, x + 1);
			if (valid.indexOf(temp) == "-1") numericOrSlash = false;
		}
	if (!numericOrSlash)
		{
		alert("Only numbers and the slash character are allowed in Date fields.");
		field.focus();
		field.select();
		return false;
		}

	var s = field.value;
	var delimeter = "/";
	
	split = s.split(delimeter);

	month = eval(split[0]);
	day = eval(split[1]);
	year = eval(split[2]);
	
	// check to make sure the month is valid
	if (month > 0 && month <= 12)
		{
		// ok
		}
	else
		{
		// not a valid month
		alert("The month is invalid. Please ensure you use mm/dd/yyyy format.");
		field.focus();
		field.select();
		return false;
		}

	// check to make sure the day is valid
	if (day > 0 && day <= 31)
		{
		// check months
		if (month == 4 || month == 6 || month == 9 || month == 11)
			{
			if (day == 31)
				{
				alert("This month cannot have more than 30 days.");
				field.focus();
				field.select();
				return false;
				}
			}
		if (month == 2)
			{
			if (day > 29)
				{
				alert("February can only have 29 days");
				field.focus();
				field.select();
				return false;
				}
			else
				{
				if (day == 29)
					{
					if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
						// this is a leap year so 29 days is ok
						{
						// ok
						}
					else
						{
						alert("This is not a leap year. February can only have 28 days.")
						field.focus();
						field.select();
						return false;
						}
					}
				}
			}	
		}	
	else
		{
		alert("The day is invalid. Please ensure you use mm/dd/yyyy format.");
		field.focus();
		field.select();
		return false;
		}
		
	// check to make sure the year is valid	
	if (year >= 1900 && year < 10000)
		{
		// ok
		}
	else
		{
		alert("The year is invalid. Please ensure you use mm/dd/yyyy format and that it is greater than 1899.")
		field.focus();
		field.select();
		return false;
		}
	//check to make sure they were not born after today
	//with (currentDate)
		
	var Stamp=new Date(); 
	var theDate=(Stamp.getMonth()<9)? 
	"0"+(Stamp.getMonth()+1)+"/":(Stamp.getMonth()+1)+"/"; 
	theDate+= (Stamp.getDate()<9)? "0"+Stamp.getDate()+"/":Stamp.getDate()+"/"; 

	theDate+=Stamp.getFullYear(); 
		
	var holdDate = theDate.split(delimeter);
	
	nowMonth = eval(holdDate[0]);
	nowDay = eval(holdDate[1]);
	nowYear = eval(holdDate[2]);
	
	if(year >= nowYear)
		{
			alert("The year must be less than the current year.")
			field.focus();
			field.select();
			return false;
		}
	// this is a valid date	
	return true;
}
