// <!--- Validate.js -- Javascript functions to validate form fields --->

// 18 Apr 2000  CAL  Written.
// 21 Jun 2000  CAL  Add uppercase option to checkText.
// 23 Jun 2000  CAL  Add function to validate state abbreviation.
// 				  		A number is not required unless both min and max values are not 0.
// 28 Jun 2000  CAL  Return true or false to indicate result for each function
// 18 Oct 2000  CAL  Add SkipFields function for tabbing through forms.
// 10 Nov 2000  CAL  Modify checkDate to use regular expressions to validate dates.
// 14 Dec 2000  CAL  Allow decimal points in numeric fields.
// 20 Jul 2001  CAL  Add use of field.select() with field.focus()
// 07 Aug 2001  CAL  Add checkNumberHex function for RRGGBB color values
// 08 Aug 2001  CAL  Replace all alert error messages with use of the alertError function
// 09 Aug 2001  CAL  Add checkSelect function for Selection Lists
// 24 Jun 2002  CAL  Add checkEmail function for e-mail addresses
// 01 Jul 2002  CAL  Add check for range of dates in checkDate
// 08 Jul 2002  CAL  Add option to checkText to allow "<" and ">" for HTML tags
// 16 Jul 2002  CAL  Add truncateField function (for limiting TextArea input lengths)
// 17 Jul 2002  CAL  Change checkText to automatically replace "&" with "and"
// 20 Jul 2002  CAL  Allow ";", strip extra white space characters (Tab, Space) in text fields.
// 17 Oct 2002  CAL  Add function checkFileName to validate filename to upload
// 23 Oct 2002  CAL  In alertError use select() only for certain types of input fields
// 21 FEb 2003  CAL  Add login to checkFileName: allow list of valid extensions as an
//					 argument, make filename extensions case insensitive.
//					 Add FormfieldLength function.
// 06 Mar 2003  CAL  Add logic to checkSelect for a required input for "select-one" fields
// 12 Mar 2003  CAL  Don't allow apostrophe in a file name.
// 20 Mar 2003  CAL  Add checkURL function for validating hypertext links.
// 21 Mar 2003  CAL  Change the Date display format from MM/DD/YYYY to mm-dd-yyyy.
// 03 Apr 2003  CAL  Add to checkNumber-- don't allow only "-", without digits.
// 17 Apr 2003  CAL  Allow "#" in a text field if HTML is allowed.
// 28 Jun 2003  CAL  Add an argument to checkFile for the hidden NameOfFile field.
// 30 Oct 2003  CAL  Modify checkDate-- if start date has a time but end date has no time,
//                   assume the end date time is "11:59:59" (not "00:00:00")
//					 Add the listFind function-- find a value in a list
// 24 Feb 2004  CAL  Allow "mailto:" address in checkURL.
// 02 Mar 2004  CAL  Allow "https:" in checkURL.
// 10 May 2004  CAL  Allow &, ^, [], and # characters in text fields.
// 28 Sep 2004  CAL  Add decimalPlaces argument to checkNumber-- format number with correct
//                   number of digits to right of decimal point and give error if too
//                   many digits have been entered. Digits to the right are not allowed
//                   unless decimalPlaces argument is used-- if argument is missing
//					 assume number must be an integer.
//                   checkNumber: convert input value from string to a number in order
//					 to check if value is within a range of numbers.
// 06 Oct 2004  CAL  Change required field edit in checkNumber-- if minvalue is < 0,
//                   meaning "0" is an allowable value, then the field is not required.
// 22 Jul 2005  CAL  Improve error message for apostrophe in file name.
// 13 Sep 2005  CAL  Add checkText options to preserve or strip HTML and line breaks.
// 19 Oct 2005  CAL  Allow apostrophe in filename, as this is should now be stripped
//					 out, along with other punctuation, by the program uploading the file.
// 16 Nov 2005  CAL  In checkURL, allow "?subject=...." for a mailto: address.
//					 Note that this is not allowed in checkEmail.

// * Validation functions are called by: (1) <form ... onSubmit="return ValidateFields()">
//   or (2) <input ... onchange="checkXXXX(this...) onblur="if (errorFieldName!=null)...">
// * The validation functions return "false" so that the ValidateFields function
//   will return "false" and cancel the form submission
// * If an error is found, errorFieldName is set so the onblur event for the <input>
//   can restore focus back to the input field

// Global variable to indicate if an entry error has been detected
var errorFieldName = null

// <!--- ------------------ --->
// <!--- Show error message --->
// <!--- ------------------ --->

function  alertError(Entryfield, errorMessage) {
	errorFieldName = Entryfield.name
// * Highlight the offending field-- only for file, password, input, and textarea fields
	if (Entryfield.type == "file" || Entryfield.type == "password" || Entryfield.type == "text" || Entryfield.type == "text") 
		Entryfield.select()   
	alert(errorMessage + " (" + Entryfield.name + ")")
	Entryfield.focus()    // * If called from a form submit, return to the bad field
}

// <!--- -------------------------------------------------------------------- --->
// <!--- Validate Date and Time in format: MM-DD-YYYY HH:MM AM/PM or MMDDYYYY --->
// <!--- -------------------------------------------------------------------- --->

function  checkDate(Datefield, reqinput, DateStart) {

	errorFieldName = null
	var str = Datefield.value
//	Check if input is required for this field
	if (str.length == 0) {
		if (reqinput == "Y") { 
			alertError(Datefield, "This field is required. Data must be entered for this field." )
			return false
		}
		return true
	}
//	Initialize return field parts
	var year = 0
	var month = 0
	var day = 0
	var hour = ""
	var minute = ""
	var ampm = ""
//  Search for pattern match: MM-DD-YYYY hh:mm am/pm format, time is optional
	if (str.indexOf(":",1) > 0) {
		if (str.search(/(\d{1,2})([-\/]?)(\d{1,2})([^-\/]?\2)(\d{2,4})\s+(\d{1,2}):(\d{2})\s?([a|A|p|P]?)/) < 0) {
			alertError(Datefield, "Date/Time must be entered in MM-DD-YYYY hh:mm AM/PM format.")
			return false
		}
		month = RegExp.$1
		day = RegExp.$3
		year = RegExp.$5
		hour = RegExp.$6
		minute = RegExp.$7
		ampm = RegExp.$8
		if (ampm.length == 1) {
			if (ampm.toUpperCase() == "A") { ampm = "AM" }
			if (ampm.toUpperCase() == "P") { ampm = "PM" }
		}
		else {
//			Allow hour to be entered in military format
			if (hour > 12 && hour <=24) {
				hour = hour - 12
				hour = hour.toString()
				ampm = "PM"
			}
			else { ampm = "AM" }
		}
	}
	else {
		if (str.search(/(\d{1,2})([-\/]?)(\d{1,2})([^-\/]?\2)(\d{2,4})/) < 0) {
 			alertError(Datefield, "Date must be entered in MM-DD-YYYY format.")
 			return false
		}
		month = RegExp.$1
		day = RegExp.$3
		year = RegExp.$5
	}
//  Allow two digit years
	if (year < 100) {
		if (year >= 70) { year = "19" + year }
		else { year = "20" + year }
	}
		
//	Validate reasonableness of date parts
	if (year < 1970 || year > 2069) {
 		alertError(Datefield, "The year must be between 1970 and 2069.")
 		return false
	}
	if (month < 1 || month > 12) {
 		alertError(Datefield, "The month must be between 1 and 12.")
 		return false
	}
//	Validate days in the month
	var maxday = 0
	if (month == 1) { maxday = 31 } 
	if (month == 2) {
		maxday = 28
		if ( (Math.floor(year/4) * 4) == year ) { maxday = 29 }
	}
	if (month == 3) { maxday = 31 } 
	if (month == 4) { maxday = 30 } 
	if (month == 5) { maxday = 31 } 
	if (month == 6) { maxday = 30 } 
	if (month == 7) { maxday = 31 } 
	if (month == 8) { maxday = 31 } 
	if (month == 9) { maxday = 30 } 
	if (month == 10) { maxday = 31 } 
	if (month == 11) { maxday = 30 } 
	if (month == 12) { maxday = 31 } 
	if (day < 1 || day > maxday) {
 		alertError(Datefield, "The day of the month must be between 1 and " + maxday + ".")
 		return false
	}
//	Validate hours, minutes, and time of day
	if (hour != "") {
		if (hour < 1 || hour > 12) {
 			alertError(Datefield, "The hour of the day must be between 1 and 12.")
 			return false
		}
		if (minute < 0 || minute > 59) {
 			alertError(Datefield, "The minute of the hour must be between 0 and 59.")
 			return false
		}
		ampm = ampm.toUpperCase()
		if (ampm != "AM" && ampm != "PM") {
 			alertError(Datefield, "Enter AM or PM after the time to indicate the time of day")
 			return false
		}
	}
	
//	Redisplay the user's input in the recommended format
	var returnDate = ((month.length == 1) ? "0" : "") + month + "-" + ((day.length == 1) ? "0" : "") + day + "-" + year
	if (hour != "") { 
		returnDate = returnDate + " " + ((hour.length == 1) ? "0" : "") + hour + ":" + minute + " " + ampm
	}
	Datefield.value = returnDate

//  Check for range of dates if necessary
	if (arguments.length == 3) {
		endDate = new Date(Datefield.value)
		startDate = new Date(DateStart.value)
		// if start date has a time but end date has no time, give end date a time
		if ( (endDate.getHours()==0 && endDate.getMinutes()==0 && endDate.getSeconds()==0) && (startDate.getHours()>0 || startDate.getMinutes()>0 || startDate.getSeconds()>0) ) {
			endDate.setHours(11);
			endDate.setMinutes(59);
			endDate.setSeconds(59);
		}
		if ( endDate.getTime() - startDate.getTime() < 0 ) {
			alertError (Datefield,"The date must be on or after " + DateStart.value + ".")
			return false
		}
	}
	return true
}


// <!--- ----------------------- --->
// <!--- Validate E-mail Address --->
// <!--- ----------------------- --->

function  checkEmail(Textfield, reqinput) {

	errorFieldName = null
	var str = Textfield.value
	if (str.length == 0) {
		if (reqinput == "Y") { 
 			alertError(Textfield, "This field is required. Data must be entered for this field." )
 			return false
		}
		return true
	}

// Determine if pattern is valid-- NOTE: this pattern is also in checkURL for mailto:
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	if (!re.test(str)) {
 		alertError(Textfield, 'E-mail address is not valid. This should be in a format like "username@organization.com"' )
 		return false
	}
	return true
}


// <!--- --------------------------- --->
// <!--- Validate FileName to Upload --->
// <!--- --------------------------- --->

// validExtensions is a comma-delimited list of filename extensions, not sensitive to case
// This sets a hidden form field, "NameOfFile", to copy of the filename selected

function  checkFileName(FileName, RequiredInput, validExtensions, NameOfFileField) {

	errorFieldName = null
	var str = FileName.value
	if (str == "") {
		if (RequiredInput == "Y") {
 			alertError(FileName, "Enter a file name or click Browse... to select from a list of files.")
			return false
		}
		else {
			if (arguments.length < 4) {
				document.ThisForm.NameOfFile.value = str
			}
			else {
				NameOfFileField.value = str
			}
			return true
		}
	}
	strPos = str.search(/[.](\w{1,4})$/)
	var extension = RegExp.$1
	if (extension == "") {
		alertError(FileName, "The file name must have an extension.")
		return false
	}
	/*
	if (str.search(/'/) >= 0) {
		alertError(FileName, "An apostrophe (') is not allowed in the file name.\rPlease rename the file (remove the apostrophe) \rand then select the file again.")
		return false
	}
	*/
//  If a list of valid extensions is not supplied, provide a basic list
	if (arguments.length < 3) {
		var validExtensions = "aif|aiff|au|doc|gif|htm|html|jpeg|jpg|mov|pdf|qt|rtf|txt|xls"
	}
//  Convert both the extension of this file and the list to lowercase
//  Also, make sure the extension list is delimited by "|" for the ReqExp to treat as OR
	var extension = extension.toLowerCase()
	var extensionList = validExtensions.toLowerCase()
	var REpattern = new RegExp(extensionList.replace(/\,/g, "|"))
	if (extension.search(REpattern) == -1) {
 		alertError(FileName, 'A file name with an extension of "' + extension + '" is not allowed.\rValid file name extensions are:\r' + extensionList.replace(/\|/g, ", ") + "\r")
 		return false
	}
	if (!checkText(FileName,'Y','N')) {return false}
// Save the filename in a hidden field on the form
	if (arguments.length < 4) {
		document.ThisForm.NameOfFile.value = str
	}
	else {
		NameOfFileField.value = str
	}
	return true
}


// <!--- --------------- --->
// <!--- Validate Number --->
// <!--- --------------- --->

function  checkNumber(Numberfield, minvalue, maxvalue, decimalPlaces) {

	errorFieldName = null
	var str = Numberfield.value
//	Check if input is required for this field
	if (str.length == 0) {
		if (minvalue > 0 && maxvalue != 0) { 
 			alertError(Numberfield, "This field is required. Data must be entered for this field." )
 			return false
		}
		return true
	}
// Determine if each character is a number
	if (str.charAt(0) == "-" && str.length == 1)  {
 		alertError(Numberfield, "A negative sign must be followed by one or more digits.")
 		return false
	}
	var allnum = true
	var numpoints = 0
	var numplaces = 0
	var i = 0
	if (str.charAt(0) == "-") { i = 1 }
	for (i; i < str.length; i++) {
		var strchar = str.charAt(i)
		if (strchar < "0" || strchar > "9") {
			if (strchar == ".") { numpoints++ }
			else {
				allnum = false
				break
			}
		}
		else {
			if (numpoints == 1) numplaces++ ;
		}
	}
	if (allnum == false) {
 		alertError(Numberfield, "Only numbers may be entered into this field.")
 		return false
	}
	// Edits based on decimal point and number of digits to the right
	if (numpoints > 1) {
 		alertError(Numberfield, "Only one decimal point may be entered into this field.")
 		return false
	}
	if (arguments.length == 4 && numplaces > decimalPlaces) {
 		alertError(Numberfield, "Only " + decimalPlaces + " digits to the right of the decimal point are allowed.")
 		return false
	}
	if (arguments.length < 4 && numplaces > 0) {
 		alertError(Numberfield, "Digits to the right of the decimal point are NOT allowed.")
 		return false
	}
//	Check for range of values
	if (minvalue != 0 || maxvalue != 0) {
		var numvalue = parseFloat(str)
		if (numvalue < minvalue || numvalue > maxvalue) { 
 			alertError(Numberfield, "The number entered must be between " + minvalue + " and " + maxvalue + ".")
 			return false
		}
	}
// Format number with correct number of digits to the right of the decimal point
	if (arguments.length == 4 && decimalPlaces > numplaces) {
		if (numpoints == 0) str = str + "." ;
		for (i=0; i < decimalPlaces - numplaces; i++) {
			str = str + "0"
		}
		Numberfield.value = str
	}
	
	return true
}

// <!--- ----------------------------- --->
// <!--- Validate Number in Hex Format --->
// <!--- ----------------------------- --->

function  checkNumberHex(Numberfield, minlength, maxlength) {

	errorFieldName = null
	var str = Numberfield.value
	var str = str.toUpperCase()
//	Check if input is required for this field
	if (str.length == 0) {
		if (minlength != 0 && maxlength != 0) { 
			alertError(Numberfield, "This field is required. Data must be entered for this field.")
			return false
		}
		return true
	}
// Only hex characters are allowed
	if (str.search(/[^0123456789ABCDEF]/) >= 0) {
		alertError(Numberfield, "The value must be entered in hex code format\n(numbers 0 - 9 and letters A - F).")
		return false
	}

//	Check for length
	if (minlength != 0 || maxlength != 0) {
		if (str.length < minlength || str.length > maxlength) { 
			if (str.length < minlength) {
				alertError(Numberfield, "The value entered must be at least " + minlength + " hex characters long.")
			}
			if (str.length > maxlength) {
				alertError(Numberfield, "The value entered must be less than " + maxlength + " hex characters long.")
			}
			return false
		}
	}
// Make sure alphabetic characters are capitalized
	Numberfield.value = str
	return true
}


// <!--- ----------------- --->
// <!--- Validate Password --->
// <!--- ----------------- --->

function  checkPassword(Passwordfield, reqinput) {

	errorFieldName = null
	var str = Passwordfield.value
//	Check if input is required for this field
	if (str.length == 0) {
		if (reqinput == "Y") { 
 			alertError(Passwordfield, "This field is required. Data must be entered for this field." )
 			return false
		}	
		return true
	}
	if (str.length < 6) {
 		alertError(Passwordfield, "A password must be at least 6 characters long." )
 		return false
	}
// Passwords cannot be all numbers or all letters
	var countnum = 0
	for (var i = 0; i < str.length; i++) {
		var strchar = str.charAt(i)
		if (strchar >= "0" && strchar <= "9") { countnum = countnum + 1 }
	}
	if (countnum == 0) {
 		alertError(Passwordfield, "Please enter at least one number in the password.")
 		return false
	}
	if (countnum == str.length) {
 		alertError(Passwordfield, "Please enter at least one letter in the password.")
 		return false
	}
	return true
}


// <!--- ----------------------- --->
// <!--- Validate Selection List --->
// <!--- ----------------------- --->

function  checkSelect(Selectfield, reqinput) {

	errorFieldName = null
	if (arguments.length == 2 && reqinput == "Y") {
		if (Selectfield.selectedIndex < 0) {
	 		alertError(Selectfield, "Please select an item from this list." )
	 		return false
		}
		if (Selectfield.options[Selectfield.selectedIndex].value == "0" || Selectfield.options[Selectfield.selectedIndex].value == "") {
	 		alertError(Selectfield, "Please select an item other than a blank line or a *** Heading Line ***." )
	 		return false
		}
	}
	return true
}


// <!--- -------------- --->
// <!--- Validate State --->
// <!--- -------------- --->

function  checkState(Statefield, reqinput) {

	errorFieldName = null
	var str = Statefield.value
	if (str.length == 0) {
		if (reqinput == "Y") { 
 			alertError(Statefield, "This field is required. Data must be entered for this field." )
 			return false
		}
		return true
	}
	if (str.length != 2) {
 		alertError(Statefield, "State abbreviation is two characters." )
 		return false
	}
// Determine if invalid characters have been entered
	var goodchar = true
	for (var i = 0; i < str.length; i++) {
		var strchar = str.charAt(i)
		if (strchar == "|") { goodchar = false }
		if (goodchar == false) { 
 			alertError(Statefield, "This field must contain only letters." )
 			return false
		}
	}
// Make sure field is in uppercase
	var str = str.toUpperCase()

// Determine if state abbreviation is valid
	var REpattern = new RegExp(str)
	var strStates = "AL|AK|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|PR|RI|SC|SD|TN|TX|UT|VA|VI|VT|WA|WI|WV|WY"
	if (strStates.search(REpattern) == -1) {
 		alertError(Statefield, "State abbreviation is not valid." )
 		return false
	}
	Statefield.value = str
	return true
}


// <!--- ------------------------------------------------------------------------ --->
// <!--- Validate Telephone number in format: AAA-PPP-SSSS/XXXX or AAAPPPSSSSXXXX --->
// <!--- ------------------------------------------------------------------------ --->

function  checkTelephone(Phonefield, reqinput, extensionOK) {

	errorFieldName = null
	var str = Phonefield.value
//	Check if input is required for this field
	if (str.length == 0) {
		if (reqinput == "Y") { 
 			alertError(Phonefield, "This field is required. Data must be entered for this field." )
 			return false
		}
		return true
	}
// Determine if entered as all numbers or numbers with extra characters
	var allnum = true
	for (var i = 0; i < str.length; i++) {
		var strchar = str.charAt(i)
		if (strchar < "0" || strchar > "9") {
			allnum = false
			break
		}
	}
// Parse elements of the field depending on whether or not this is all numbers
	if (allnum == true) {
		if (str.length < 10) {
 			alertError(Phonefield, "You may enter the telephone number in AAAPPPSSSS format.")
 			return false
		}
		var areacode = str.substr(0,3)
		var prefix = str.substr(3,3)
		var suffix = str.substr(6,4)
		var extension = str.substr(10,4)
	}
	else {
		if (str.length < 12) {
 			alertError(Phonefield, "Enter the area code, prefix, and suffix in the format AAA-PPP-SSSS.")
 			return false
		}
//		Verify that extra characters are in the correct positions
		var correctformat = true
		if (str.charAt(3) != "-" && str.charAt(3) != ".") { correctformat = false }
		if (str.charAt(7) != "-" && str.charAt(7) != ".") { correctformat = false }
		if (str.length > 12 && str.charAt(12) != "-" && str.charAt(12) != "/") {
			correctformat = false }
		if (correctformat == false) {
 			alertError(Phonefield, "This field must be entered in the format AAA-PPP-SSSS/XXXX.")
 			return false
		}
//		Verify that each part of the field is numeric
		var areacode = str.substr(0,3)
		var prefix = str.substr(4,3)
		var suffix = str.substr(8,4)
		var extension = str.substr(13,4)
		var str = areacode + prefix + suffix + extension
		var allnum = true
		for (var i = 0; i < str.length; i++) {
			var strchar = str.charAt(i)
			if (strchar < "0" || strchar > "9") {
				allnum = false
				break
			}
		}
		if (allnum == false) {
 			alertError(Phonefield, "The area code, prefix, suffix, and extension must be all numbers.")
 			return false
		}
		if (extensionOK != "Y" && extension.length > 0) {
 			alertError(Phonefield, "An extension may not be entered for this telephone number.")
 			return false
		}

	}
//	Redisplay the user's input in the recommended format
	var returnPhone = areacode + "-" + prefix + "-" + suffix
	if (extension != "") { returnPhone = returnPhone + "/" + extension }
	Phonefield.value = returnPhone
	return true
}


// <!--- ------------- --->
// <!--- Validate Text --->
// <!--- ------------- --->

function  checkText(Textfield, reqinput, toUpper, htmlAllowed) {

	errorFieldName = null
	var str = Textfield.value
// Debug:	ShowString(str)
	if (str.length == 0) {
		if (reqinput == "Y") { 
 			alertError(Textfield, "This field is required. Data must be entered for this field." )
 			return false
		}
		return true
	}
// 'N': HTML is not allowed with error message and CRLF stripped (default)
// 'S': HTML is stripped quietly and CRLF stripped  
// 'P': HTML is stripped quietly and CRLF preserved
// 'Y': HTML is allowed and CRLF preserved
	var noHtml = true
	var stripHtml = false
	var stripCrlf = true
	if (arguments.length == 4) {
		if (htmlAllowed == "S") { noHtml = false; stripHtml = true; stripCrlf = true; }
		if (htmlAllowed == "P") { noHtml = false; stripHtml = true; stripCrlf = false; }
		if (htmlAllowed == "Y") { noHtml = false; stripHtml = false; stripCrlf = false; }
	}
// Determine if invalid characters have been entered
	var goodchar = true
	var badchar = null
	for (var i = 0; i < str.length; i++) {
		var strchar = str.charAt(i)
		if (strchar == "{") { badchar = strchar; goodchar = false}
		if (strchar == "}") { badchar = strchar; goodchar = false}
// Only allow < and > if html tags can be entered in this text field
		if (noHtml) {
			if (strchar == "<") { badchar = strchar; goodchar = false}
			if (strchar == ">") { badchar = strchar; goodchar = false}
		}
		if (goodchar == false) { 
 			alertError(Textfield, 'This field cannot contain the character "' + badchar + '".\r\rNone of the following characters are allowed: { }' + (noHtml ? ' < >' : '') + '\r')
 			return false
		}
	}
// Strip HTML tags if requested
	if (stripHtml) { str = str.replace(/<\/?[^<]+>/g," ") }
// Strip CR, LF, and Tab characters if necessary
	if (stripCrlf) { str = str.replace(/[\r\n\t]/g," ") }
// Automatically strip extra blanks spaces before, within, and after the string text
	str = str.replace(/ {2,}/g," ")
	if (str.charAt(0) == " ") { str = str.substr(1) }
	if (str.charAt(str.length-1) == " ") { str = str.substr(0,str.length-1) }
// Convert to uppercase if requested
	if (toUpper == "Y") { str = str.toUpperCase() }
	Textfield.value = str
// Debug:	ShowString(str)
	return true

}


// <!--- ------------------- --->
// <!--- Validate URL (Link) --->
// <!--- ------------------- --->

function  checkUrl(Textfield, reqinput) {

	errorFieldName = null
	if (!checkText(Textfield, reqinput)) return false
	var str = Textfield.value
	if (str.length == 0 && reqinput != "Y") return true
// Determine if pattern is valid
	var re = /^http[s]?:\/\/\S+\.\S+$/i
	var reMail = /^mailto:\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+([\?]subject=[^\n]+)?$/
	if (!re.test(str) && !reMail.test(str)) {
 		alertError(Textfield, 'URL is not valid. This should be in a format like \r"http://www.organization.com" or \r"mailto:username@organization.com"\r' )
 		return false
	}
	return true
}

// <!--- ---------------------------- --->
// <!--- Length of a form input field --->
// <!--- ---------------------------- --->

function  FormfieldLength(Formfield) {
	var str = Formfield.value
	return str.length
}

// <!--- -------------------------------------- --->
// <!--- Find a value in a comma-delimited list --->
// <!--- -------------------------------------- --->

function listFind (valueSelected,valueList) {
	var REpattern = new RegExp(valueSelected.replace(/\,/g,"|"))
	var str = valueList 
	if (str.search(REpattern) == -1) 
		return (false) ;
	else 
		return (true) ; 
}

// <!--- ------------------- --->
// <!--- Show a string field --->
// <!--- ------------------- --->

function  ShowString(str) {
	var stringValue = ""
	for (var i = 0; i < str.length; i++) {
		var strnum = str.charCodeAt(i)
		if (strnum < 33) { 
			switch(strnum) {
			case 9: stringValue = stringValue + "<TAB>"; break;
			case 10: stringValue = stringValue + "<LF>"; break;
			case 13: stringValue = stringValue + "<CR>"; break;
			case 32: stringValue = stringValue + "_"; break;
			default: stringValue = stringValue + "<" + strnum + ">"; break;
			}
		} else { stringValue = stringValue + str.charAt(i) }
	}	
	alert("String: (length is " + str.length + ", '_' is a blank space)\r" + stringValue) 
}

// <!--- ----------------------------------------------------------- --->
// <!--- Skip a group of form fields if the base field is left blank --->
// <!--- ----------------------------------------------------------- --->

function  SkipFields(BaseField, NextBaseField) {
	baseField = BaseField.value
	if (baseField.search(/\S/) == -1) {
		NextBaseField.focus()
		return
	}
	return
}

// <!--- ---------------- --->
// <!--- Truncate a field --->
// <!--- ---------------- --->

function  truncateField(Entryfield, fieldLength) {
	var str = Entryfield.value
	if (str.length > fieldLength) { 
		str = str.substr(0,fieldLength) 
		Entryfield.value = str
		alert("The text in this field has been truncated to the maximum of " + fieldLength + " characters (" + Entryfield.name + ")")
	}
	return
}



