//	CheckArrow checks for keypresses of arrow keys in form fields at runtime,
//	and if an arrow is pressed, moves the cursor to an adjacent field.
//
//	It should be the OnKeyUp function for each field.
//
//	onKeyUp="CheckArrow('Left', 'Right', 'Up', 'Down')"
//
//	Left		field to go to when left arrow is pressed
//	Right		field to go to when right arrow is pressed
//	Up			field to go to when up arrow is pressed
//	Down		field to go to when down arrow is pressed
//
//	fields may be null or invalid - if so, the keypress does !hing

function CheckArrow(Left, Right, Up, Down)
{
var keynum;
var NextField;
var thisForm;
var thisElt;
if (navigator.appName.indexOf("Microsoft") != -1)
	{
	keynum = window.event.keyCode;
	thisElt = window.event.srcElement;
	}
	else 
	{
	return true;
	}
if (keynum == 37) NextField = Left;
	else if (keynum == 38) NextField = Up;
	else if (keynum == 39) NextField = Right;
	else if (keynum == 40) NextField = Down;
	else return true;
if (NextField == null) return true;
thisForm = thisElt.form;
if (!thisForm(NextField)) return true;
if (keynum == 37 || keynum == 39)
	{
	if (thisElt.value != '') return true;
	}
thisForm(NextField).focus();
if (thisForm(NextField).type == 'text' || thisForm(NextField).type == 'textarea') 
	thisForm(NextField).select();
return false;
}

//	CheckLength checks for maximum length in a textarea field.  When a 
//	textarea field is to be stored in a database field of fixed length
//	(typically 255), we need a way to make the user stop typing when
//	he reaches max length.  html can't do it.  This function can.
//
//	Call from the OnKeypress event:
//
//	Onkeypress="CheckLength(255)"
//
//	If the field length is greater than allowed, then the keystroke is
//	cancelled unless it's a arrow or control character.

function CheckLength(maxlen)
{
var keynum;
var thisElt;
if (navigator.appName.indexOf("Microsoft") != -1)
	{
	keynum = window.event.keyCode;
	thisElt = window.event.srcElement;
	}
	else 
	{
	keynum = event.which;
	thisElt = event.target;
	}
if (keynum <= 40) return true;
if (thisElt.value.length > maxlen) 
	{
	if (navigator.appName.indexOf("Microsoft") != -1) window.event.returnValue = false;
	return false;
	}
return true;
}


//	DoSelect selects the current field, if the browser is Microsoft.
//	It is the onfocus function for most fields.  It does !hing
//	if Netscape, because Netscape delivers onfocus everytime you 
//	click in the field and thus you can't type anything.

function DoSelect()
{
if (navigator.appName.indexOf("Microsoft") != -1)
	window.event.srcElement.select();
return true;
}


//	ValDate validates a date field on the form.
//
//	result = ValDate(fieldname, title, nullflag, occurs)
//
//	fieldname	base name of field (e.g., 'MudDate')
//	title		printable title of field (e.g., 'Date Mud Applied')
//	nullflag	if true, field may be null/blank/empty
//	occurs		if omitted, the field occurs once and its name is 
//				fieldname.  If given, field occurs 1 to occurs times, 
//				and each one's name is fieldname_occurence (MudDate_7)
//
//	If the field fails validation, issues error message, moves
//	focus to defective field, and returns false.  Otherwise
//	returns true.

function ValDate(fieldname, title, nullflag, occurs)
{
var val;
var i;
var fname;
var maxoccur;
var dateitem;
if (occurs == null) maxoccur = 1; else maxoccur = occurs;
for (i = 1; i <= maxoccur; i++)
	{
	fname = fieldname;
	if (occurs != null) fname = fname + "_" + i;
	val = document.WellForm[fname].value;
	if (val == null || val == '')
		{
		if (nullflag != true) 
			{
			alert(title + ' is required.');
			document.WellForm[fname].focus();
			return false;
			}
		}
		else
		{
		dateitem = new Date(val);
		if (isNaN(dateitem.getFullYear())) 
			{
			alert(title + ' must be a valid date.');
			document.WellForm[fname].focus();
			return false;
			}
		}
	}
return true;
}

//	ValNumber validates a number field on the form.
//
//	result = ValNumber(fieldname, title, nullflag, occurs)
//
//	fieldname	base name of field (e.g., 'Temperature')
//	title		printable title of field (e.g., 'Mud Temperature')
//	nullflag	if true, field may be null/blank/empty
//	occurs		if omitted, the field occurs once and its name is 
//				fieldname.  If given, field occurs 1 to occurs times, 
//				and each one's name is fieldname_occurence (Temperature_7)
//
//	If the field fails validation, issues error message, moves
//	focus to defective field, and returns false.  Otherwise
//	returns true.

function ValNumber(fieldname, title, nullflag, occurs)
{
var val;
var i;
var fname;
var maxoccur;
if (occurs == null) maxoccur = 1; else maxoccur = occurs;
for (i = 1; i <= maxoccur; i++)
	{
	fname = fieldname;
	if (occurs != null) fname = fname + "_" + i;
	val = document.WellForm[fname].value;
	if (val == null || val == '')
		{
		if (nullflag != true) 
			{
			alert(title + ' is required.');
			document.WellForm[fname].focus();
			return false;
			}
		}
		else
		{
		if (!isFinite(val)) 
			{
			alert(title + ' must be a valid number.');
			document.WellForm[fname].focus();
			return false;
			}
		}
	}
return true;
}

//	ValString - check string field on form.
//
//	The only reason to call this thing is to ensure a required
//	field is present.
//
//	result = ValString(fieldname, title, occurs)
//	fieldname	base name of field (e.g., 'CostCode')
//	title		printable title of field (e.g., 'Cost Code')
//	occurs		if omitted, the field occurs once and its name is 
//				fieldname.  If given, field occurs 1 to occurs times, 
//				and each one's name is fieldname_occurence (Temperature_7)
//
//	If the field is empty, issues error message, moves
//	focus to defective field, and returns false.  Otherwise
//	returns true.

function ValString(fieldname, title, occurs)
{
var val;
var i;
var fname;
var maxoccur;
if (occurs == null) maxoccur = 1; else maxoccur = occurs;
for (i = 1; i <= maxoccur; i++)
	{
	fname = fieldname;
	if (occurs != null) fname = fname + "_" + i;
	val = document.WellForm[fname].value;
	if (val == null || val == '')
			{
			alert(title + ' is required.');
			document.WellForm[fname].focus();
			return false;
			}
	}
return true;
}

