function set_focus(the_field) {
	if (!the_field.disabled && the_field.type != 'hidden') {
		if (the_field.type != 'select-one'){ //accomodate listboxes
			the_field.select();
		}
		the_field.focus();
	}	
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}


function validate(form_name){
// Copyright Michael Baker
// 2001 All rights reserved
// you may use this code freely but must not remove these credits and you must
// include notes in this section as to whether you have modified it from the original version.
// mbaker@kayapo.com
// v2002.11.22
// altered reformatting of phone number
// v2003.01.15
// changed from checking by id to checking by id OR name
// v2003.01.30
// changed date verification method - more flexable
// v2003.09.20
// changed date verification method - even more flexable
// v2004.03.24
// changed date verification method - put more strict critera check on date check
// v2006.01.10
// added supports for integer and length checking.
// The new format for names is (type identifier)[size](_fieldname). 
// Ex. T20_bigmama, which indicates a required text field with a maximum size 20 characters
// The functionallity is backwards compatible with previous naming schemes, which excluded size.
//v2006.04.20
// Added code to ignore field names with "_trigger" in them because it is a special field used by calendar object

	var i, ident, nam, val, cookie, phone, row, search_by, field_size;
	var fields = document.forms[form_name].elements;
	var sep = '/';
	var emailPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	var fieldSizePattern = /^\w([0-9]*)_[\w\s]+$/;
	var stripFieldSizePattern = /^(\w)[0-9]*(_[\w\s]+)$/;
	var cleanNumber = /[^\d\.]/;
	var triggerField = /_trigger/; //this field used for calendar obj and not form processing
	
	for (i = 0; i < fields.length; i++)  {

			ident  = fields[i].id
			nam = fields[i].name
			val = fields[i].value
			
			if (ident !='') {
				search_by = ident
			}
			if (nam !='') {
				search_by = nam
			}	
			
			if(search_by==''){
					alert('This form cannot be validated. Processing cannot contiue. Please contact the webmaster.');
					return false;
			}
			
		if(search_by.match(fieldSizePattern) && !(search_by.match(triggerField) )) {	
			//check to see if a field size is indicated
			field_size = search_by.replace(fieldSizePattern,"$1");
			//if field size is indicated make sure that the value in the field is not too big
			if (field_size !="") {
				//eliminate size identifier
				search_by = search_by.replace(stripFieldSizePattern,"$1$2");
				if ( field_size < val.length  && search_by.substr(0,1).toUpperCase()!='I') {//if it is 'i' then it's an integer and the field size is actually the exponent that 2 is raised to
					alert("The maximum length for data in this field is " + field_size);
					set_focus(fields[i]);
					return false;
				}
			}
			
			switch (search_by.substr(0,2)) {
				case "T_" : //then the data should be textural and is required
					if (val == '') {
						alert("A required field has been left blank. Please correct to continue.");
						set_focus(fields[i]);
						return  false;
					}
					break;
					
				case "N_" ://then the data should be numeric and is required
					if (val == '' || isNaN(val)) {
						alert("An answer that is expected to be a number is not. Please correct to continue.");
						set_focus(fields[i]);
						return  false;
					}
					break;
					
				case "n_" ://then the data should be numeric and is not requried
					if (val != "") {
						if (isNaN(val)) {
							alert("An answer that is expected to be a number is not. Please correct to continue.");
							set_focus(fields[i]);
							return  false;
						}
					}
					break;
					
				case "I_" ://then the data should be an integer and is required
					//val = val.replace(/[+\-,]/g,''); //get rid of commas and any signs
					if (val == '' || isNaN(val) || parseInt(val,10).toString()!=val.toString()) {
						alert("A number that is expected to be a integer is not. Please correct to continue.");
						set_focus(fields[i]);
						return  false;
					}
					//verify length - SQL int = VBscript long, SQL smallint = VBscript int. Cant accomodate SQL Bigint even with VBscript Dbl (17 chars) so don't even try
					if(val.length > 16) {
							alert("An answer that is expected to be an integer is TOO BIG. It must be less than 17 Digits.");
							set_focus(fields[i]);
							return  false;
					}
					if(field_size!='') {
						if((Math.pow(2,field_size)-1)<val) {
							alert("An integer is out of range. It must be less than +/-" + (Math.pow(2,field_size)-1));
							set_focus(fields[i]);
							return  false;
						}
					}
					break;
					
				case "i_" ://then the data should be an integer and is optional
					if (val != "") {
						//val = val.replace(/[+\-,]/g,''); //get rid of commas and any signs
						if (isNaN(val) || parseInt(val,10).toString()!=val.toString()) {
							alert("An answer that is expected to be a integer is not. Please correct to continue.");
							set_focus(fields[i]);
							return  false;
						}
						//verify length - SQL int = VBscript long, SQL smallint = VBscript int. Cant accomodate SQL Bigint even with VBscript Dbl (17 chars) so don't even try
						if(val.length > 16) {
								alert("An answer that is expected to be an integer is TOO BIG. It must be less than 17 Digits.");
								set_focus(fields[i]);
								return  false;
						}
						if(field_size!='') {
							if((Math.pow(2,field_size)-1)<val) {
								alert("An integer is out of range. It must be less than +/-" + (Math.pow(2,field_size)-1));
								set_focus(fields[i]);
								return  false;
							}
						}
					}
					break;
					
				case "L_" ://then the object is a list box and cannot = "" (Select One) required
					if (val == "") {
						alert("A selection has not been made in a dropdown listbox. Please make a selection to continue.");
						set_focus(fields[i]);		
						return  false;
					}
					break;
					
					case "D_" ://then the data should be in a date format of mm/dd/yyyy and is required
						if (val != '') {
							if (chkdate(fields[i]) == false) {
								alert("That date is invalid.  Please try again.");
								set_focus(fields[i]);	
								return false;
							}
						}
						else {
							alert("A date is required, but is missing. Please correct to continue.");
							set_focus(fields[i]);	
							return false;
						}
						break;
						
					case "d_" ://then the data should be in a date format of mm/dd/yyyy but is NOT required
						if (val != '') {
							if (chkdate(fields[i]) == false) {
								alert("That date is invalid.  Please try again.");
								set_focus(fields[i]);	
								return false;
							}
						}	
						break;
											
					case "P_" ://then the data should be a phone number and is required
						//strip to only numeric numbers
						phone =val.replace(/[^0-9]/g, '');
						if (val == "" || phone.length != 10) {//if the value is empty or the number stirpped of all formatting characters is less than or more than 10 digits then there is an error
							alert("You must enter a valid phone Number that includes the Area Code and 7 digit number. Please correct to continue.");
							set_focus(fields[i]);		
							return  false;
						}
						else {
							//reformat number in appropriate format
							//changed the following to strip any non-numeric characters from number
							//val = '('+val.substr(0,3)+')'+val.substr(3,3)+'-'+val.substr(6,4);
							fields[i].value = phone;
						}
						break;
						
					case "p_" ://then the data should be a phone number but is not required
						//strip to only numeric numbers
						phone =val.replace(/[^0-9]/g, '');
						if (val.length > 0) {
							if (val == "" || phone.length != 10) {  //if the value is empty or the number stirpped of all formatting characters is less than or more than 10 digits then there is an error
								alert("You must enter a valid phone Number that includes the Area Code and 7 digit number. Please correct to continue.");
								set_focus(fields[i]);		
								return  false;
							}
							else {
								//reformat number in appropriate format
								//changed the following to strip any non-numeric characters from number
								//val = '('+val.substr(0,3)+')'+val.substr(3,3)+'-'+val.substr(6,4);
								fields[i].value = phone;
							}
						}
						break;
					
					case "E_" ://then email address required
						if (!emailPattern.test(val)) {  //test against the regular expression format defined at beginning of function
							alert("You must enter a valid email address in the format of username@domain.com. Please correct to continue.");
							set_focus(fields[i]);			
							return  false;
						}	 
						break;
						
					case "e_" ://email address optional	
						if (val != '') {
							if (!emailPattern.test(val)) {  //test against the regular expression format defined at beginning of function
								alert("You must enter a valid email address in the format of username@domain.com. Please correct to continue.");
								set_focus(fields[i]);			
								return  false;
							}	 
						}
						break;
						
					default : // The element was not required to be validated or is not a form field
			} //end switch
		}
	} //end for
	
	//the function below exists in the app that calls this validation function. it provides the ability to customize this generic validation routine if this validation is passed.
	return custom_validation(form_name);
} //end validate function


function chkdate(val) {
	var YearMillenniumBreakPt = 15 //determines the decade year where the millennium is decided on a 2 digit year
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var strTemp;
	var booFound = false;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var datefield = val;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 6) {
		return false;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
		}
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}


	intYear = parseInt(strYear, 10);	
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}	
	if (strYear.length > 4 || intYear < 0 ) {
		return false;
	}
	if (strYear.length != 4) {
		if (intYear < YearMillenniumBreakPt) {
			intYear = 2000 + intYear;
		}
		else {
			intYear = 1900 + intYear;
		}
		if (!confirm('A new year has been created from the one you provided. Is this year - ' + intYear + ' - what you intended? \n Click Cancel if not.')) {
			err = 4;
			return false;
		}
		strYear = String(intYear)
	}
	//make sure date is within range
	if (intYear < 1900 || intYear > 2078) {	
		if (!confirm('Valid years are between 1900 and 2078 and the date entered is outside of this range. Is this year - ' + intYear + ' - what you intended? \n Click Cancel if not.')) {
			err = 4;
			return false;
		}
	}
	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
		}
	}

	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
	if (strDatestyle == "US") {
		datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else {
		datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
	datefield.value = strMonth + '/' + strDay + '/'  + strYear;
	//alert(val.name);
	//alert(val.value);
	return true;
}