//*********************************************************************
//  The following are generalized error checking functions for forms
//********************************************************************
function isNum(value){
// Check that value is numeric
	if(value==""){
		return false
	}
	for(j=0;j<value.length;j++){
		if(value.charAt(j)<"0" || value.charAt(j)>"9"){
			return false
			}
		}
	return true
	}
//*********************************************************************
// Returns a string with illegal charaters in instr removed!
//*********************************************************************
	function replaceIllegal(inStr) {
		expl = /[#|]/g
		outStr = inStr.replace(expl,"!")
	// replace " with '
		quote = /"/g
		outStr = outStr.replace(quote,"'")
		return outStr
		}
//********************************************************************
// Checks the a date, puts a corrected format in form or generates an error message.
//********************************************************************
function chkDate(frm,elmt,dateValue){ 
// A blank date will be caught later during the submit process
	if(dateValue=="" || dateValue=="NA" || dateValue=="na"){
		return true
		}
	goodDate = validDate(dateValue) // validDate returns false if the slashes are incorrect or an error
					                // message if a value is out of range
	if(!goodDate) {  //Slashes not foun in the correct position
		alert("Invalid Date\n(Date must be in mm/dd/yyyy format)")
		eval("document."+frm + "." + elmt + ".select()") 
		eval("document."+frm + "." + elmt + ".value = dateValue")  
		eval("document."+frm + "." + elmt + ".focus()")  
		return false
	}
	if(goodDate.length>11){  //Error message returned
		eval("document."+frm + "." + elmt + ".select()") 
		eval("document."+frm + "." + elmt + ".value = dateValue")  
		eval("document."+frm + "." + elmt + ".focus()")  
		return false
	}
	eval("document."+frm + "." + elmt + ".value = goodDate")  
	return true
	}

//*********************************************************************
// Checks for valid date, "mm/dd/yyyy",  or mm-dd-yyyy or mm.dd.yyyy and 
// returns the date sring with leading zeros as needed. 
// If any element is invalid the appropriate error message is returned
//*********************************************************************
	function validDate(date){
	dayError="Invalid Day\n(Value must be between 1 and 31)"
	monthError="Invalid Month\n(Value must be between 1 and 12)"
	yrError="Invalid Year\n(Value must be between 1900 and 2099)"

// determine the delimiter
	delim = (date.indexOf("/")>0) ? "/" : ""
	delim = (date.indexOf("-")>0) ? "-" : delim
	delim = (date.indexOf(".")>0) ? "." : delim
	if(delim == "")
		return false
	
	slash1Pos = date.indexOf(delim,0)
	if(slash1Pos<0||slash1Pos>2)
		return false
	slash2Pos = date.indexOf(delim,slash1Pos+1)
	if(slash2Pos<0||slash2Pos>slash1Pos+3)
		return false
	month = date.substr(0,slash1Pos)
	if(!isNum(month))
		return monthError
	if(month<1 || month>12)
		return monthError
	day = date.substr(slash1Pos+1,slash2Pos-slash1Pos-1)
	if(!isNum(day))
		return dayError
	if(day<1 || day>31)
		return dayError
	yr = date.substr(slash2Pos+1,date.length)
	if(!isNum(yr))
		return yrError
	if(yr.length==2)  //only good until 225
	{
	  yr = (yr.length==1) ? yr="0"+yr : yr
	  yr = (yr < 25) ? ("20" + yr) : ("19" + yr);
	}
	if(yr<1900 || yr>2099)
      return yrError
	if(month.length==1)
		month="0"+month
	if(day.length==1)
		day="0"+day
	return month + "/" + day + "/" + yr
	}
// Determines if the field is required
	function isRequired(required) {
		if (required == 1){
			return true
		}
		return false
	}
	
function chkEmail(cell,frm) {
	address = eval('document.'+frm+'.'+cell+'.value')
	//Blank address ok when filling out form
	if(address==""){
		return true
		}
// Check email address for validity.
	if(!validEmail(address)) {
		alert("Oops! " + address + " is an invalid email address.")
		eval("document."+frm+'.'+cell+'.focus()')
		eval("document."+frm+'.'+cell+'.select()')
		return false
	}
	return true
	}
// Validate the Email address passed in email
function validEmail(email){
	invalidChars = " /;,:"
	atPos = email.indexOf("@",1) // Check for one and only one @ charachter
	if(atPos == -1){
		return false
		}
	if(email.indexOf("@",atPos+1)>-1){
		return false
	}
	for(j=0; j<invalidChars.length; j++){ // Check for any invalid Characters in email
		badChar=invalidChars.charAt(j)
		if(email.indexOf(badChar,0)>-1){
			return false
			}
		}
	periodPos = email.indexOf(".",atPos)  //One period and 3 or more chars following it.
	if(periodPos == -1){
		return false
		}
	if(periodPos+3>email.length) {
		return false
	}
	return true
}
// Checks on zip code.  Must be a 5 digit number or a 5 digit number dash a 4 digit number.
// an empty zip is ignored
 
	function chkZip(cell,frm) {
	Zip = eval("document." + frm + "." + cell + ".value")
	if(Zip==""){   			// ignore, it will be caught on submit processing
		return true
		}
	if (Zip.indexOf('-')!=-1){
		Zip1=Zip.substr(0,Zip.indexOf('-'))
		Zip2=Zip.substr(Zip.indexOf('-')+1,Zip.length)
		Zip=Zip1+Zip2
		}
	if(!isNum(Zip)) {
		alert("Postal Code must be a number")
		eval('document.'+frm+'.'+cell+'.select()')
		eval('document.'+frm+'.'+cell+'.focus()')
		return false
		}
	if(Zip.length!=5 && Zip.length!=9) {
		alert("Postal Code must have 5 or 9 digits")
		eval('document.'+frm+'.'+cell+'.select()')
		eval('document.'+frm+'.'+cell+'.focus()')
		return false
		}
	return true
	}
// Checks on phone mumber in form and if OK replaces it with one of the correct form, or generates
// an error message
	function chkPhone(cell,frm,reqd) {
	a = valPhone(eval("document."+frm+'.'+cell+'.value'))	
	if(a=="empty"){
		if(reqd){
			return false  //ok maybe??
			//alert("Please enter your phone number")
			//eval("document."+frm+'.'+cell+'.focus()')
			//return false
			}
		else{
			return true
			}
		}
	if(!a) {
		alert("You must enter a ten digit phone number.")
		eval("document."+frm+'.'+cell+'.focus()')
		eval("document."+frm+'.'+cell+'.select()')
		return false
		}
	eval('document.'+frm+'.'+cell+'.value= a')
	return true
	}
	
// validates Phone numbers and reformats them.  Must be a 10 digit number: Area Code plus number. 
// It can have any delimiter between numbers.  It returns a phone number 
// separated by dashes if the number is good, other wise it returns the string "empty"
	function valPhone(num)  {
	  number = num
	  if(number=="")
	  	return "empty"
	  for(j=number.length;j>=0;j--) { // get rid of anything that's not a number
	    if(number.charAt(j)<"0" || number.charAt(j)>"9")
		  number = number.substring(0,j) + number.substring(j+1,number.length)
	    }
	  if(number.length==10)
	    return "("+ number.substring(0,3)+") "+number.substring(3,6)+"-"+number.substring(6,10)
	return num
	}

// chkDelete puts a confirm box for deleting a record and saves the responce in the deleteOK variable.
// and submits the form.  id is the string name hidden variable that will contain the record id to be 
// deleted The record number is passed in idVal.
	function chkDelete(id,idVal)
	{
		a = confirm("Are You sure you want to delete this record?");
		for(i=0;i<document.forms.length;++i){
			try{
				document.forms[i].deleteOK.value = a;
			}
			catch(e){
				continue
			}
			document.getElementById(id).value = idVal;
			document.forms[i].submit();
			return true;
		}
	}

