function valid_state(pov_state,plv_required)
{
if (!plv_required)
{
	if (pov_state.value == "")
	{
	return (true);
	}
}

lcv_state = jtrim(pov_state.value.toUpperCase());
pov_state.value = lcv_state;
lca_state = new Array("AK","AL","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID",
"IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM",
"NV","NY","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY");
llv_found = false;
lnv = 0;
for (lnv = 0; lnv <= lca_state.length; lnv++) {
	if (lca_state[lnv] == lcv_state)
		{
		llv_found = true;
		break;
		}
	}
if  (!llv_found)
	{
	alert("Invalid " + format_name(pov_state.name) + ":  " + pov_state.value);
	pov_state.focus();
	}
return (llv_found)
}

//***********************************************************

function not_blank(pov_field)
{
pov_field.value = jtrim(pov_field.value)
if (pov_field.value == "")
	{
	alert(format_name (pov_field.name) + " cannot be blank.");
	pov_field.focus();
	return (false);
	}
else
	{
	return (true);
	}
}

//***********************************************************

function is_blank(pov_field)
{
pov_field.value = jtrim(pov_field.value)
if (pov_field.value != "")
	{
	alert(format_name (pov_field.name) + " must be blank.");
	pov_field.focus();
	return (false);
	}
else
	{
	return (true);
	}
}

//***********************************************************

function tonum(str)
{
	var nstr = "";

	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if ((ch >= "0" && ch <= "9") || ch == '.')
		{
			nstr += ch;
		}
	}
	return parseFloat(nstr);
}

//***********************************************************

function valid_date(pov_field, plv_required)
{
	if (!plv_required)
	{
		if (jtrim(pov_field.value) == "")
			{
			return (true);
			}
	}

	var str = pov_field.value;
	var ncnt = 0;
	var cm = "";
	var cd = "";
	var cy = "";
	var nm = 0;
	var nd = 0;
	var ny = 0;

	if (str.length == 0 || str == "" || str == null)
	{
		alert (format_name (pov_field.name) + " cannot be blank.") ;
		pov_field.focus();
		return (false);
	}

	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1)
		if (ch >= "0" && ch <= "9")
		{
			if (ncnt == 0)
			{
				cm += ch;
			}
			if (ncnt == 1)
			{
				cd += ch;
			}
			if (ncnt == 2)
			{
				cy += ch;
			}
		}
		if (ch == "/" || ch == "-")
		{
			ncnt++;
		}
	}
	nm = tonum(cm);
	nd = tonum(cd);
	ny = tonum(cy);
	if (nm < 1 || 12 < nm)
	{
		alert (format_name (pov_field.name) + ": " + pov_field.value + " is not a valid date.  Bad month.");
		pov_field.focus();
		return (false);
	}
	if (cy.length != 4 && cy.length != 2)
	{
		alert (format_name (pov_field.name) + ": " + pov_field.value + " is not a valid date.  Bad year");
		pov_field.focus();
		return (false);
	}
	if (cy.length == 4)
	{
		if (ny < 1900)
		{
			alert(cy.length);
			alert (format_name (pov_field.name) + ": " + pov_field.value + " is not a valid date.  Year cannot be less than 1900.");
			pov_field.focus();
			return (false);
		}
	}
	if ((nm == 1 || nm == 3 || nm == 5 || nm == 7 || nm == 8 || nm==10 || nm==12) && (nd < 1 || 31 < nd))
	{
		alert (format_name (pov_field.name) + ": " + pov_field.value + " is not a valid date.  Wrong number of days in month.") ;
		pov_field.focus();
		return (false);
	}
	if (nm == 2 && (ny=96 || ny==1996 || ny==00 || ny==2000 || ny==04 || ny==2004 ) && (nd < 1 || 29 < nd))
	{
		alert (format_name (pov_field.name) + ": " + pov_field.value + " is not a valid date.  Wrong number of days in month.") ;
		pov_field.focus();
		return (false);
	}
	if (nm == 2 && ny!=96 && ny!=1996 && ny!=0 && ny!=2000 && ny!=4 && ny!=2004 && (nd < 1 || 28 < nd))
	{
		alert (format_name (pov_field.name) + ": " + pov_field.value + " is not a valid date.  Wrong number of days in month.") ;
		pov_field.focus();
		return (false);
	}
	if ((nm == 4 || nm == 6 || nm == 9 || nm == 11) && (nd < 1 || 30 < nd))
	{
		alert (format_name (pov_field.name) + ": " + pov_field.value + " is not a valid date.  Wrong number of days in month.") ;
		pov_field.focus();
		return (false);
	}
	var rtn = "";
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1)
        if (ch >= "0" && ch <= "9")
		{
			rtn += ch;
		}
		if (ch == "/" || ch == "-")
		{
			rtn += "/";
		}
	}
	pov_field.value = rtn
	return (true);
}

//***********************************************************

function format_name(pcv_name)
{
var lcv_name = pcv_name
while (lcv_name.search("_") > 0)
	{
	lcv_name = lcv_name.replace("_"," ");
	}
return(lcv_name);
}

//***********************************************************

function jtrim(pcv_string)
{
if (pcv_string == "") {return ("")}
	
var lcv_reply = "";

for (var lnv = pcv_string.length - 1; lnv >= 0; lnv--)
	{
	if (pcv_string.substr (lnv, 1) != " ")
		{
		lcv_reply = pcv_string.substr (0, lnv + 1);
		break;
		}
	}
return (lcv_reply);
}

//***********************************************************

function digits_only (pov_field, plv_required)
{
var lcv_value = jtrim (pov_field.value);
var llv_reply = true;
pov_field.value = lcv_value;

if (lcv_value == "")
	{
	if (!plv_required)	
		{
		return (true);
		}
	else
		{
		alert (format_name(pov_field.name) + " can only contain digits (0 - 9).");
		pov_field.focus ();
		return (false);
		}
	}

for (var lnv = lcv_value.length - 1; lnv >= 0; lnv--)
	{
	if (lcv_value.substr (lnv, 1) < "0" || lcv_value.substr (lnv, 1) > "9")
		{
		alert (format_name(pov_field.name) + " can only contain digits (0 - 9).");
		pov_field.focus ();
		llv_reply = false;
		break;
		}
	}
return (llv_reply);
}

function min_length (pov_field, pnv_length)
{
var lcv_field = jtrim(pov_field.value);
pov_field.value = lcv_field;
if (lcv_field.length < pnv_length)
	{
	alert(format_name(pov_field.name) + " must be at least " + pnv_length.toString() + " positions long.");
	pov_field.focus();
	return (false);
	}
else
	{
	return (true);
	}
}

function max_length (pov_field, pnv_length)
{
var lcv_field = jtrim(pov_field.value);
pov_field.value = lcv_field;
if (lcv_field.length > pnv_length)
	{
	alert(format_name(pov_field.name) + " cannot be more than " + pnv_length.toString() + " positions long.");
	pov_field.focus();
	return (false);
	}
else
	{
	return (true);
	}
}

function check_sex (pov_field, plv_required)
{
var lcv_sex = jtrim (pov_field.value.toUpperCase())
pov_field.value = lcv_sex;
if (!plv_required)
{
	if (lcv_sex == "")
	{
	return (true);
	}
}
if (lcv_sex != "M" && lcv_sex != "F")
	{
	alert(format_name(pov_field.name) + " must be M(ale) or F(emale).");
	pov_field.focus();
	return (false);
	}
else
	{
	return (true)
	}
}

function check_marital_status (pov_field, plv_required)
{
var lcv_marital_status = jtrim(form1.marital_status.value.toUpperCase ());
pov_field.value = lcv_marital_status;
if (!plv_required)
{
	if (lcv_marital_status == "")
		{
		return (true);
		}
}
if (lcv_marital_status == "D" || lcv_marital_status == "M" || lcv_marital_status == "P" || lcv_marital_status == "S" || lcv_marital_status == "W")
	{
	return (true);
	}
else
	{
	alert(format_name(pov_field.name) + " must be (S)ingle, (M)arried), (D)ivorced, (W)idowed, or se(P)arated.");
	form1.marital_status.focus();
	return (false);
	}
}

//***********************************************************
// Check if passed comparison date is in range              *
//***********************************************************
function date_in_range (pov_compdate, pnv_dayspast, pnv_daysfuture, plv_required, pov_basedate)
{

// First check if date is required; if not and value is blank, return true
if (!plv_required)
	{
	if (jtrim(pov_compdate.value) == "")
		{
		return (true);
		}
	}
	
// Check to see if pov_field is a valid date; if not, return false
llv = valid_date(pov_compdate, true);
if (llv == false)
	{
	return (false);
	}
	
// Set the initial comparison date and retrieve the mm/dd/yyyy
compdate = new Date(pov_compdate.value);
mm = compdate.getMonth();
dd = compdate.getDate();
yyyy = compdate.getFullYear();

// Isolate the year passed and determine if 2 or 4 digits
var str = pov_compdate.value;
var ncnt = 0;
var cy = "";
for (var i = 0; i < str.length; i++)
	{
	var ch = str.substring(i, i + 1);
	if (ch >= "0" && ch <= "9" && ncnt == 2)
		{
		cy += ch;
		}
	if (ch == "/" || ch == "-")
		{
		ncnt++;
		}
	}

//...Javascript assumes 2 digit dates are 1900's; if between 00 & 24, force to 2000's
if (cy.length == 2 && cy >= "00" && cy <= "24")
	{
	var yyyy = yyyy + 100;
	}

//...Set the full comparison date
compdate = new Date(yyyy,mm,dd,23,59,59,999);

// Check to see if a base date is passed; if not passed or blank, 
// use current date as the base date for comparison
if (pov_basedate == null || jtrim(pov_basedate.value) == "")
	{
	basedate = new Date();
	}
else
	{
	
	// Set the initial base date and retrieve the mm/dd/yyyy. **Note**: This 
	// date is assumed to be valid already and is not validated. It must be passed
	// as an object, such as thru a form field.
	basedate = new Date(pov_basedate.value);
	mm = basedate.getMonth();
	dd = basedate.getDate();
	yyyy = basedate.getFullYear();
	
	// Isolate the year passed and determine if 2 or 4 digits
	var str = pov_basedate.value;
	var ncnt = 0;
	var cy = "";
	for (var i = 0; i < str.length; i++)
		{
		var ch = str.substring(i, i + 1);
		if (ch >= "0" && ch <= "9" && ncnt == 2)
			{
			cy += ch;
			}
		if (ch == "/" || ch == "-")
			{
			ncnt++;
			}
		}

	//...Javascript assumes 2 digit dates are 1900's; if between 00 & 24, force to 2000's
	if (cy.length == 2 && cy >= "00" && cy <= "24")
		{
		var yyyy = yyyy + 100;
		}
	basedate = new Date(yyyy,mm,dd,23,59,59,999);
	}

// Calculate the days between the comparison date and base date
msPerDay = 24 * 60 * 60 * 1000; 
daysLeft = (compdate.getTime() - basedate.getTime()) / msPerDay;
daysLeft = Math.round(daysLeft);

// See if the number of days is between the passed range; if not, send alert
if (daysLeft < (pnv_dayspast * -1) || daysLeft > pnv_daysfuture)
	{
	lowdate = new Date((basedate.getTime()) - (pnv_dayspast * msPerDay));
	mm = lowdate.getMonth() + 1;
	dd = lowdate.getDate();
	yyyy = lowdate.getFullYear();
	
	highdate = new Date((basedate.getTime()) + (pnv_daysfuture * msPerDay));
	mm2 = highdate.getMonth() + 1;
	dd2 = highdate.getDate();
	yyyy2 = highdate.getFullYear();
	
	alert("Date must be between \n" + mm + "/" + dd + "/" + yyyy + " and " + mm2 + "/" + dd2 + "/" + yyyy2)
	pov_compdate.focus();
	return (false);
	}

// If you get here, all is good
return (true);	
}

//******************************************************
// Prompt to enter a comment/rejection reason          *
//******************************************************
function prompt_comment (pov_comment, pnv_max, pcv_row, plv_edit, pov_flag)
{
var lnv_index = pcv_row - 1;
var llv_one_row = (typeof(pov_comment.length) == "undefined");

if (llv_one_row)
	{var lcv_comment = pov_comment.value;}
else
	{var lcv_comment = pov_comment[lnv_index].value;}

if (plv_edit)	
	{
	lcv_reason=prompt("Please enter a reason/comment/note (" + pnv_max + " characters max):", lcv_comment);
	// if cancel is pressed, just return
	if (typeof(lcv_reason)=="object")
		{return;}
	// if OK is pressed, trim the reason and chop off characters
	// after 50 (max length)
	lcv_reason = jtrim(lcv_reason);
	llv_set_flag = typeof(pov_flag)=="object"
	if (llv_one_row)
		{
		pov_comment.value = lcv_reason.substring(0,pnv_max);
		if (llv_set_flag) {pov_flag.value = true;}
		}
	else
		{
		pov_comment[lnv_index].value = lcv_reason.substring(0,pnv_max);
		if (llv_set_flag) {pov_flag[lnv_index].value = true;}
		}
	}
else
	{
	if (lcv_comment == "")
		{
		lcv_comment = "(no comments entered)";
		}
	alert(lcv_comment);
	}
}

//***********************************************************
// Toggle sets of checkbox values
//***********************************************************
function uncheck (pov_not_checked, pnv_row, pov_flag)
{
// If the following statement is true, there is not an array of values (only 1 checkbox)
var llv_one_row = (typeof(pov_not_checked.length) == "undefined");
if (llv_one_row)
	{
	pov_flag.value = true;
	pov_not_checked.checked = false;
	}
// Otherwise, there is an array of values (multiple checkboxes)
else
	{
	pov_flag[pnv_row - 1].value = true;
	pov_not_checked[pnv_row - 1].checked = false;
	}
}

//***********************************************************
// Toggle all sets of checkbox values
//***********************************************************
function toggle_all (pov_checked, pov_not_checked, pov_flag)
{
lnv_length = pov_checked.length;
llv_one_row = (typeof(lnv_length) == "undefined");
// If the following statement is true, there is not an array of values (only 1 checkbox)
if (llv_one_row)
	{
	pov_flag.value = true;
	pov_not_checked.checked = false;
	pov_checked.checked = true;
	}
// Otherwise, there is an array of values (multiple checkboxes)
else
	{
	for (lnv_row = 0; lnv_row < lnv_length; lnv_row++)
		{
		pov_flag[lnv_row].value = true;
		pov_not_checked[lnv_row].checked = false;
		pov_checked[lnv_row].checked = true;
		}
	}
}

//***********************************************************

function real_number (pov_field, plv_required)
{
var lcv_value = jtrim (pov_field.value);
var llv_reply = true;
pov_field.value = lcv_value;

if (lcv_value == "")
	{
	if (!plv_required)	
		{
		return (true);
		}
	else
		{
		alert (format_name(pov_field.name) + ": " + lcv_value + " can only contain digits (0 - 9).");
		pov_field.focus ();
		return (false);
		}
	}

for (var lnv = lcv_value.length - 1; lnv >= 0; lnv--)
	{
	if ((lcv_value.substr (lnv, 1) < "0" || lcv_value.substr (lnv, 1) > "9") && (lcv_value.substr (lnv, 1) != "."))
		{
		alert (format_name(pov_field.name) + ": " + lcv_value + " can only contain digits (0 - 9) or a decimal point.");
		pov_field.focus ();
		llv_reply = false;
		break;
		}
	}
return (llv_reply);
}

//***********************************************************************************************

function valid_pw (pov_field)
{
var lcv_value = pov_field.value;
var llv_reply = true;

if (lcv_value == "")
	{
	alert ("Password cannot be empty.");
	pov_field.focus ();
	return (false);
	}
	
if (lcv_value.length < 4 || lcv_value.length > 10)
	 {
	 alert("Password must be between 4 and 10 characters long.");
	 pov_field.focus ();
	 return (false);
	 }

for (var lnv = lcv_value.length - 1; lnv >= 0; lnv--)
	{
	if (lcv_value.charCodeAt(lnv) < 33 || lcv_value.charCodeAt(lnv) > 126)
		{
		alert ("Password can only contain digits, letters and punctuation other than double quotes (''), single quotes (') and square brackets ([]).");
		pov_field.focus ();
		llv_reply = false;
		break;
		}
		else
		{
		if (lcv_value.charCodeAt(lnv) == 34 || lcv_value.charCodeAt(lnv) == 39 || lcv_value.charCodeAt(lnv) == 91 || lcv_value.charCodeAt(lnv) == 93)
			{
			alert ("Password cannot contain double quotes (''), single quotes (') or square brackets ([]).");
			pov_field.focus ();
			llv_reply = false;
			break;
			}
		}
	}
return (llv_reply);
}

//***********************************************************************************************

function is_last_day (pov_field)
{

var lcv_value = pov_field.value;
var llv_reply = true;
var ldv_date1 = new Date();
var ldv_date2 = new Date();
ldv_date1.setTime(Date.parse(lcv_value));
var lnv_month1 = ldv_date1.getMonth();
ldv_date2 = ldv_date1
ldv_date2.setDate(ldv_date2.getDate()+1);
var lnv_month2 = ldv_date2.getMonth();
		
// month is 0 based, Jan = 0, Dec = 11
if(lnv_month1==11)
  {
  if(lnv_month2!=0)
		{
	  alert (format_name(pov_field.name) + ": " + lcv_value + " must be the last day of the month.");
  	pov_field.focus ();
	 	return (false);
		}
  }
  else
  {
    if((lnv_month2-lnv_month1)!=1)
  	{
		 	alert (format_name(pov_field.name) + ": " + lcv_value + " must be the last day of the month.");
  		pov_field.focus ();
	 		return (false);
  	}
  }
return (llv_reply);
}
//************************************************************************************************