function alertFocus( fieldObject, errorMessage ) {
// called from checkEmptyField
	// showID( "All" );   // display all fields
	fieldObject.focus();   // if fieldObject is not displayed, this will fail - center the field in the brower before alert
	alert( "Error: " + errorMessage );
	fieldObject.focus();   // re-focus after alert
	return true;
}   // alertFocus

function checkEmptyField( fieldName, errorMessage ) {
	var f = document.forms[0][ fieldName ];
	// "select-one", "select-multiple", "text", "radio", "textarea"
	if ( null == f ) {
		// skip
	} else if ( f.type == "text" ) {
		if ( isBlank( f.value ) ) {
			return alertFocus( f, errorMessage );
		}
	} else if ( f.type == "password" ) {
		if ( isBlank( f.value ) ) {
			return alertFocus( f, errorMessage );
		}
	} else if ( f.type == "textarea" ) {
		if ( isBlank( f.value ) ) {
			return alertFocus( f, errorMessage );
		}
	} else if ( f.type == "select-one" ) {
		if ( f.selectedIndex < 1 ) {   // -1 means no selection, 0 means first (---select something---) element
			return alertFocus( f, errorMessage );
		}
	} else if ( f.type == "select-multiple" ) {
		for ( i = 1, iMax = f.length; i < iMax; i++ ) {   // skip 0th element of list (---select something---)
			if ( f.options[ i ].selected == true ) return false;   // exit early
		}
		return alertFocus( f, errorMessage );
	} else if ( ( null != f [ 0 ] ) && ( f[ 0 ].type == "radio" ) ) {
		for ( i = 0, iMax = f.length; i < iMax; i++ ) {
			if ( f[ i ].checked == true ) return false;   // exit early
		}
		return alertFocus( f[ 0 ], errorMessage );
	} else {
		//alert( "unchecked type:" + fieldName + ":" + f + ":" + f.type );   ///zzzz
		////zzzz
	}
	return false;
}   // checkEmptyField()


function checkNumericField( fieldName, errorMessage ) {
	var f = document.forms[0][ fieldName ];
	if ( null == f ) {
		// skip
	} else if ( f.type == "text" ) {
		if ( ! isNumeric( f.value ) ) return alertFocus( f, errorMessage );
	}
	return false;
}   // checkNumericField()


function formatPhone( textbox ) {
// called from onchange event of phone fields of investigation form 
	var newPhone='';
	var c='';
	for ( pos = 0; pos < textbox.value.length; pos++ ) {
		c=textbox.value.charAt( pos );
		newPhone=( isNaN( c ) | c == ' ' ) ? newPhone : newPhone + c;
	}  // end for
	if ( newPhone.length < 10 && newPhone != '' ) {
		alert( 'Phone Numbers must contain at least 10 digits.  Please re-enter this phone number or leave the field blank.' );
		textbox.focus();
		textbox.select();
		return false;
	}  // end if
	var finalPhone = '';
	for ( count = 0; count < newPhone.length; count++ ) {
		if ( count == 3 ) finalPhone = finalPhone + '-';
		if ( count == 6 ) finalPhone = finalPhone + '-';
		if ( count == 10 ) finalPhone = finalPhone + ' x';
		finalPhone = finalPhone + newPhone.charAt( count );
	}  // end for
	textbox.value = finalPhone;
	return true;
}   // formatPhone()

function isBlank( s ) { return( "" == s ); }   // function isBlank

function checkDateField( fieldName, errorMessage ) {
	var fieldObj = document.forms[0].elements[fieldName];
	if (fieldObj.value == '') return false;
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = fieldObj.value.match( datePat ); // is the format ok?
	if ( matchArray == null ) {
		return alertFocus( fieldObj, errorMessage + "  Please enter date as mm/dd/yyyy." );
	}
	day = matchArray[3];
	month = matchArray[1];
	year = matchArray[5];
	if ( year < 1996 || year > 2018 ) {
		return alertFocus( fieldObj,  errorMessage + "  Year (" + year + ") seems to be out of range." );
	}
	if ( month < 1 || month > 12 ) { // check month range
		return alertFocus( fieldObj, errorMessage + "  Month (" + month + ") must be between 1 and 12." );
	}
	if ( day < 1 || day > 31 ) {
		return alertFocus( fieldObj, errorMessage + "  Day (" + day + ") must be between 1 and 31." );
	}
	if ( ( month == 4 || month == 6 || month == 9 || month == 11 ) && ( day == 31 ) ) {
		return alertFocus( fieldObj, errorMessage + "  Month " + month + " does not have 31 days." )
	}
	if ( month == 2 ) {
		var isleap = ( year % 4 == 0 && (year % 100 != 0 || year % 400 == 0 ) );
		if ( day > 29 || (day == 29 && ! isleap ) ) {
			alertFocus( fieldObj, errorMessage + "  February " + year + " does not have " + day + " days." );
		}
	}
	return false;
}   // function checkDateField

function isNumeric( s ) {
	var validChars = "0123456789.-";   // 0-9, decimal point, and negative sign
	var isNumber = true;
	var c;
	for ( i = 0; i < s.length && isNumber == true; i++ ) {
		c = s.charAt( i );
		if ( validChars.indexOf( c ) == -1) isNumber = false;
	}
	return isNumber;
}   // function isNumeric

function formatLatLong(textbox, type) {
	//Check for a decimal point in the string, then separate the number into right and left sections, 
	//remove any non number characters, and make it negative.
	
	if (textbox.value=='' | textbox.value=='0.0' | textbox.value == '0.00000000') return true;
	var pointPos = textbox.value.indexOf('.');

	// if not found, then the number is only positive. don't get a right text.
	if (pointPos == -1) {   //check for decimal point
		var newLeft='';	
		 //  loop through each character and remove anything that is not a number.
		for (pos=0;pos<textbox.value.length;pos++)  {
			var c=textbox.value.charAt(pos);
			var newLeft=(isNaN(c) | c==' ')?newLeft:newLeft+c;
		}  // end for
		if ( (textbox.name.toLowerCase()).indexOf('longitude') >= 0  ) {
			finalTude = "-" + newLeft + ".000000";
		} else {
			finalTude=newLeft + '.000000';
		}  // end if
	} else {
		var rightText = textbox.value.substring(pointPos + 1 );
		var leftText = textbox.value.substring( 0 , pointPos);	
		var tempLeft='';	
		var tempRight='';	
		//  loop through each character and remove anything that is not a number.
		for (pos=0;pos<textbox.value.length;pos++)   {
			var c=leftText.charAt(pos);
			var tempLeft=(isNaN(c) | c==' ')?tempLeft:tempLeft+c;

			var d=rightText.charAt(pos);
			var tempRight=(isNaN(d) | d==' ')?tempRight:tempRight+d;
		}  // end for
		leftText = tempLeft;
		rightText = tempRight;
		if(rightText.length < 6 ) {  //pad with zeros to 6 decimal points
			var diff = 6 - rightText.length
			for (var i = 0 ; i < diff ; i++) {
				rightText = rightText + "0"
			} // end for  
		} // end  pad with zeros
		if(rightText.length >6 ) {
			rightText = rightText.substring(0,7);
		}  // end if
		if ( (textbox.name.toLowerCase()).indexOf('longitude') >= 0  ) {
			finalTude = "-" + leftText + "." + rightText;
		} else {
			finalTude=leftText + "." + rightText
		}  // end if
	}  //end decimal check

	if ( (textbox.name.toLowerCase()).indexOf('longitude') >= 0 ) {
		// continental US, Alaska, Hawaii
		// if ( (parseFloat(finalTude)>-66 || parseFloat(finalTude)<-125) || (parseFloat(finalTude)>-129 || parseFloat(finalTude)<-179) ) {
		if ( !(parseFloat(finalTude)<=-66 && parseFloat(finalTude)>=-125) && !(parseFloat(finalTude)<=-129 && parseFloat(finalTude)>=-179) ) {
			alert('Longitude must be between -125.000 and -66.000 for continental US or between -179.000 and -129.000 for Alaska and Hawaii');
			textbox.focus();
			textbox.select();
			return false;
		} // end if
	} else {
		// continental US, Alaska, Hawaii
		// if ( (parseFloat(finalTude)>50 || parseFloat(finalTude)<24) || (parseFloat(finalTude)>55 || parseFloat(finalTude)<51.4) || (parseFloat(finalTude)>22 || parseFloat(finalTude)<18.5) ) {
		if ( !(parseFloat(finalTude)<=50 && parseFloat(finalTude)>=24) && !(parseFloat(finalTude)<=55 && parseFloat(finalTude)>=51.4) && !(parseFloat(finalTude)<=22 && parseFloat(finalTude)>=18.5) ) {
			alert('Latitude must be between 24.000 and 50.000 for continential US, between 51.400 and 55.000 for Alaska or between 18.500 and 22.000 for Hawaii.');
			textbox.focus();
			textbox.select();
			return false;
		}  // end if
	} // end if-else

	textbox.value = finalTude;
	
	return true;

}  // end formatLatLong

function changeSelectedView() {
	var f = document.forms[0];
	if (f.selectedFilter) {
		f.selectedFilter.selectedIndex = 0;
	} // end if
	
	document.forms[0].submit()
} // end changeSelectedView()

var GLOBAL_TEXTAREA_MAX_LENGTH = 500;

function checkTextAreaMaxLength( fieldName, maxLength, errorMessage ) {
	var f = document.forms[0][ fieldName ];
	if ( f.value.length > maxLength ) {
		return alertFocus(f, errorMessage + "  Current Length: " + f.value.length);
	}
	return false;
}   // checkTextAreaMaxLength()