//GLOBAR VARS
var error_count = 0;	// ADDED BY STEVE (2007 02-07) --> Keeps track of number of errors that occurred in the form


function validateForm(f,a)
{
	var txtReturn = "T";
	var txtErrorCheck = "F";
	var txtErrorField;
	for (var i=0; i < a.length; i++)
	{
		// ADDED BY STEVE (2006 12-28) TO RESOLVE INSTANCES WHEN 'radio' OR 'checkbox' nodelists are being validated
		// alert("a[i][0] = " + a[i][0] + "; a[i][1] = " + a[i][1] + "; a[i][2] = " + a[i][2] + ";\na[i][0][0] = " + a[i][0][0] + ";\na[i][0] = " + a[i][0]);
		
		if (a[i][2] == "radio")
		{
			e = a[i][0][0];
			var labelID = "lbl"+e.name;
			var labelID2 = "lbl2"+e.name;
		}
		else if (a[i][2] == "checkbox")
		{			
			if(a[i][0][0]) //if >1 chkbx
			{
				e = a[i][0];
				var labelID = "lbl"+e[0].name;
				var labelID2 = "lbl2"+e[0].name;
			}
			else
			{
				e = a[i][0];
				var labelID = "lbl"+e.name;
				var labelID2 = "lbl2"+e.name;
			}
		}
		else
		{
			e = a[i][0];
			var labelID = "lbl"+e.name;
			var labelID2 = "lbl2"+e.name;
		}
		
		//Checks Input types: Text, Select-One, Select-Multiple, TextArea, CheckBox, Radio, File
		switch (e.type)
		{
			case "text":
				if ( a[i][2] )
				{
					//Checks text types: phone-number, date, date2, email, text-only, zip-code, social-security, numeric
					switch ( a[i][2] )
					{
						case "phone-number":
							var phoneNumber = e.value;
							if (phoneNumber.length == 0)
							{
								document.getElementById(labelID).className = "lblError";
        						if (txtReturn=="T")
								{
									txtReturn="F";
									if (txtErrorCheck == "F")
									{
										txtErrorCheck = "T";
										txtErrorField = e;
									}
								}
								focus_on_error(e);
							}
							else
							{
								if (validatePhone(e, a[i][1]))
								{
									document.getElementById(labelID).className = "lblOK";
								}
								else
								{
									document.getElementById(labelID).className = "lblError";
        							if (txtReturn=="T")
									{
										txtReturn="F";
										if (txtErrorCheck == "F")
										{
											txtErrorCheck = "T";
											txtErrorField = e;
										}
									}
									focus_on_error(e);
								}
							}
							break;

						case "date":
							//First check to see if anything was entered into the field.
							//This function call WILL NOT allow dates in the past of present.
							if (e.value.length == 0)
							{ 
								document.getElementById(labelID).className = "lblError";
        						if (txtReturn=="T")
								{
									txtReturn="F";
									if (txtErrorCheck == "F")
									{
										txtErrorCheck = "T";
										txtErrorField = e;
									}
								}
								focus_on_error(e);
      						}
							else
							{
      							//Now Check to see if it is an actual date
      							if (isDate(e.value, a[i][1]))
								{
      								document.getElementById(labelID).className = "lblOK";
      							}
								else
								{
      								document.getElementById(labelID).className = "lblError";
      								if (txtReturn=="T")
									{
										txtReturn="F";
										if (txtErrorCheck == "F")
										{
											txtErrorCheck = "T";
											txtErrorField = e;
										}
									}
									focus_on_error(e);
      							}
      						}
							break;
							
						case "date2":
							//First check to see if anything was entered into the field.
							if (e.value.length == 0)
							{ 
								document.getElementById(labelID).className = "lblError";
        						if (txtReturn=="T")
								{
									txtReturn="F";
									if (txtErrorCheck == "F")
									{
										txtErrorCheck = "T";
										txtErrorField = e;
									}
								}
								focus_on_error(e);
      						}
							else
							{
      							//Now Check to see if it is an actual date
								//This function call will allow dates in the past.
      							if (isDate2(e.value, a[i][1]))
								{
      								document.getElementById(labelID).className = "lblOK";
      							}
								else
								{
      								document.getElementById(labelID).className = "lblError";
      								if (txtReturn=="T")
									{
										txtReturn="F";
										if (txtErrorCheck == "F")
										{
											txtErrorCheck = "T";
											txtErrorField = e;
										}
									}
									focus_on_error(e);
      							}
      						}
							break;
							
						case "email":
							//First check to see if anything was entered into the field.
							if (e.value.length == 0)
							{ 
								document.getElementById(labelID).className = "lblError";
        						if (txtReturn=="T")
								{
									txtReturn="F";
									if (txtErrorCheck == "F")
									{
										txtErrorCheck = "T";
										txtErrorField = e;
									}
								}
								focus_on_error(e);
      						}
							else
							{
      							//Now Check to see if it is an valid email.
      							if (validateEmail(e, a[i][1]))
								{
      								document.getElementById(labelID).className = "lblOK";
      							}
								else
								{
      								document.getElementById(labelID).className = "lblError";
      								if (txtReturn=="T")
									{
										txtReturn="F";
										if (txtErrorCheck == "F")
										{
											txtErrorCheck = "T";
											txtErrorField = e;
										}
									}
									focus_on_error(e);
      							}
      						}
							break;
							
						case "text-only":
							//First check to see if anything was entered into the field.
							var str = e.value;
      						if (str.length ==0)
							{	
								document.getElementById(labelID).className = "lblError";
								if (txtReturn=="T")
								{
									txtReturn="F";
									if (txtErrorCheck == "F")
									{
										txtErrorCheck = "T";
										txtErrorField = e;
									}
								}
								focus_on_error(e);
								
      						}
							else
							{
								document.getElementById(labelID).className = "lblOK";
      						}
							break
							
						case "zip-code":
							//First check to see if anything was entered into the field.
							var zipCode = e.value;
							if (zipCode.length == 0)
							{
								document.getElementById(labelID).className = "lblError";
        						if (txtReturn=="T")
								{
									txtReturn="F";
									if (txtErrorCheck == "F")
									{
										txtErrorCheck = "T";
										txtErrorField = e;
									}
								}
								focus_on_error(e);
							}
							else
							{								
								var re = new RegExp("[0-9][0-9][0-9][0-9][0-9]");
								if (e.value.match(re)) 
								{
									document.getElementById(labelID).className = "lblOK";
								} 
								else 
								{
									document.getElementById(labelID).className = "lblError";
									if (txtReturn=="T")
									{
										txtReturn="F";
										if (txtErrorCheck == "F")
										{
											txtErrorCheck = "T";
											txtErrorField = e;
										}
									}
									focus_on_error(e);
								}
							}
							break;
							
						case "social-security":
							//First check to see if anything was entered into the field.
							var socialSecurity = e.value;
							if (socialSecurity.length == 0)
							{
								document.getElementById(labelID).className = "lblError";
        						if (txtReturn=="T")
								{
									txtReturn="F";
									if (txtErrorCheck == "F")
									{
										txtErrorCheck = "T";
										txtErrorField = e;
									}
								}
								focus_on_error(e);
							}
							else
							{								
								var re = new RegExp("[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]");
								if (e.value.match(re)) 
								{
									document.getElementById(labelID).className = "lblOK";
								} 
								else 
								{
									document.getElementById(labelID).className = "lblError";
									if (txtReturn=="T")
									{
										txtReturn="F";
										if (txtErrorCheck == "F")
										{
											txtErrorCheck = "T";
											txtErrorField = e;
										}
									}
									focus_on_error(e);
								}
							}
							break;
							
						case "numeric":
							//First check to see if anything was entered into the field.
							var number = e.value;
							if (number.length == 0)
							{
								document.getElementById(labelID).className = "lblError";
        						if (txtReturn=="T")
								{
									txtReturn="F";
									if (txtErrorCheck == "F")
									{
										txtErrorCheck = "T";
										txtErrorField = e;
									}
								}
								focus_on_error(e);
							}
							else
							{								
								if (IsNumeric(e))
								{
									document.getElementById(labelID).className = "lblOK";
								} 
								else 
								{
									document.getElementById(labelID).className = "lblError";
									if (txtReturn=="T")
									{
										txtReturn="F";
										if (txtErrorCheck == "F")
										{
											txtErrorCheck = "T";
											txtErrorField = e;
										}
									}
									focus_on_error(e);
								}
							}
							break;
							
						default: 
							break
					}
				}
      			break
				
			case "textarea":
				if (e.value.length == 0 )
				{
					document.getElementById(labelID).className = "lblError";
					if (txtReturn=="T")
					{
						txtReturn="F";
						if (txtErrorCheck == "F")
						{
							txtErrorCheck = "T";
							txtErrorField = e;
						}
					}
					focus_on_error(e);
				}
				else
				{
        			document.getElementById(labelID).className = "lblOK";
      			}
				break
				
      		case "select-one":
      			if ( e.selectedIndex == 0 )
				{
      				document.getElementById(labelID).className = "lblError";
      				if (txtReturn=="T")
					{
						txtReturn="F";
						if (txtErrorCheck == "F")
						{
							txtErrorCheck = "T";
							txtErrorField = e;
						}
					}
					focus_on_error(e);
      			}
				else
				{
      				document.getElementById(labelID).className = "lblOK";
      			}
      			break
				
      		default:
				//Checks Input types: radio, checkbox
				switch (a[i][2])
				{
      				case "radio":
						if(validateRadio(a[i][0], a[i][1]))
						{
							document.getElementById("lbl"+e.name).className = "lblOK";
      					}
						else
						{ 
							document.getElementById("lbl"+e.name).className = "lblError";
      						if (txtReturn=="T")
							{
								txtReturn="F";
								if (txtErrorCheck == "F")
								{
									txtErrorCheck = "T";
									txtErrorField = e;
								}
							}
      					}
      					break
						
      				case "checkbox":					
						if (validateCheckbox(a[i][0], a[i][1]))
						{
							if(e[0]) //when >1 chkbx
								document.getElementById("lbl"+e[0].name).className = "lblOK";
							else
								document.getElementById("lbl"+e.name).className = "lblOK";
      					}
						else
						{
								if(e[0]) //when >1 chkbx
									document.getElementById("lbl"+e[0].name).className = "lblError";
								else
									document.getElementById("lbl"+e.name).className = "lblError";
							
      						if (txtReturn=="T")
							{
								txtReturn="F";
								if (txtErrorCheck == "F")
								{
									txtErrorCheck = "T";
									if(e[0]) //when >1 chkbx
										txtErrorField = e[0];
									else
										txtErrorField = e;
								}
							}
      					}
      					break
						
      				default:
      					break
      			}
				break
      		}
		}

		if (txtErrorCheck == "T")
		{
			alert("There are fields that did not pass validation and are either required or need to be fixed.  \nPlease see the fields with bold red labels and correct the issue.");
		  	//txtErrorField.focus();
			return false;
		}
		else
		{
			return true;
		}
		
		
}


/*********************************************************
focus_on_error()
ADDED BY STEVE (2007 02-07)
This function focuses the form on the FIRST error item
**********************************************************/
function focus_on_error(focus_field)
{
	if(error_count <= 0)
	{
		focus_field.focus();
		scrollBy(0,-16)
	}
	
	error_count++

}


/*********************************************************
validateText()
This function checks to make sure the user has entered
data into a text-only field.
Arguments:
fld = field
errMsg = english name of field for display on error
**********************************************************/
function validateText(fld, errMsg)
{
	if(fld.value=="")
	{
		alert("Please enter a value for '" + errMsg + "'.");
		focus_on_error(fld);
		return false;
	}
	return true;
}


/*********************************************************
validateSelect()
This function checks to make sure the user has selected
an option from a combobox or multi-select list box.
Arguments:
fld = field
errMsg = english name of field for display on error
**********************************************************/
function validateSelect(fld, errMsg)
{
	var idx=fld.selectedIndex;
	if(idx== -1)
	{
		alert("Please choose a value for '" + errMsg + "'.");
		focus_on_error(fld);
		return false;	
	}
	return true;
}


/*********************************************************
validateRadio()
This function checks to make sure the user has entered
an option for a radio button
Arguments:
fld = field
errMsg = english name of field for display on error
**********************************************************/
function validateRadio(fld, errMsg)
{
	for(i=0;i<fld.length;i++)
	{
		if(fld[i].checked==true)
		{
			return true;
		}
	}
	
	//alert("Please select a choice for '" + errMsg + "'.");
	focus_on_error(fld[0]);
	return false;
}


/*********************************************************
validateCheckbox()
This function checks to make sure the user has selected
at least one option
Arguments:
fld = field
errMsg = english name of field for display on error
**********************************************************/
function validateCheckbox(fld, errMsg)
{
	if(fld[0])
	{
		for (index = 0; index < fld.length; index++)
		{
			if (fld[index].checked)
			{
				return true;
			}
		}
		//alert("Please select at least one choice for '" + errMsg + "'.");	
		focus_on_error(fld[0]);
	}
	else
	{
		if (fld.checked)
		{
			return true;
		}
		focus_on_error(fld);
	}
	
	return false;

}


/*********************************************************
validatePhone()
This function checks to make sure the user has entered
a correctly formatted phone number
Arguments:
fld = field
errMsg = english name of field for display on error
**********************************************************/
function validatePhone(fld, errMsg)
{
	// retrieve the value
	var phone = fld.value;	

	if(phone == "")
	{
		return false;
	}
	
	// Phone number entered with dashes or dots
	if (phone.length == 12)
	{
		while (phone.indexOf(".") > -1)
		{
			phone = phone.replace(".", "-");
		}
		fld.value = phone;
		return true;
	}
	
	// Phone entered with no punctuation
	if (phone.length == 10)
	{
		phone = phone.substr(0, 3) + "-" + 
		phone.substr(3, 3) + "-" + phone.substr(6, 4);
		fld.value = phone;
		return true;
	}
	
	//alert(errMsg + " needs to be formatted to 999-999-9999.");
	return false;
}


/*********************************************************
IsNumeric()
This function checks the passed text to see if it is a numeric value and 
returns True if it is, False if it is not.

IsNumeric - true for all numeric, false if not
**********************************************************/
function IsNumeric(fld)
{
	var PNum = new String(fld.value);
	var regex = /[^0-9]/;
	return !regex.test(PNum);
}


/*********************************************************
validateNumber()
This function checks:
1) the user has entered a valid number
2) check to see if the number is less than the minimum
3) check to see if the number is more than the maximum
fld = field
errMsg = english name of field for display on error
min = minimum
max = maximum
**********************************************************/
function validateNumber(fld, errMsg, min, max)
{
	if(fld.value=="")
	{
		alert("Please enter a value for '" + errMsg + "'.");
		focus_on_error(fld);
		return false;	
	}
	
	//check to see if it is a number	
	var n=parseFloat(fld.value);
	if(isNaN(n))
	{
		alert("Please enter a valid number for '" + errMsg + "'.");
		focus_on_error(fld);
		return false;	
	}
	
	// check to see if the number is less than the minimum
	if ( min && n < min )
	{
		alert("Please enter a number greater than " + min + " for '" + errMsg + "'.");
		focus_on_error(fld);
		return false;	
	}
	
	// check to see if the number is more than the maximum
	if ( max && n > max )
	{
		alert("Please enter a number less than " + max + " for '" + errMsg + "'.");
		focus_on_error(fld);
		return false;	
	}
	
	return true;
}


/***********************************************************
validateNumberNoAlert()
This function checks that the value of a field is a number
and, optionally within a certain range.
Arguments:
val = Value to be checked
min = Optional minimum allowed value
max = Optional maximum allowed value
************************************************************/
function validateNumberNoAlert(val, min, max)
{
	if ( isNaN( val ) ) return false;
	if ( min && val < min ) return false;
	if ( max && val > max ) return false;
	return true;
}


/***********************************************************
validateEmail()
This function checks that the value of a field is a valid 
SMTP e-mail address ie x@xx.xx
Arguments:
obj = Input whose value is to be checked
Note: Work in progress = validate SMTP OR Notes Canonical
************************************************************/
function validateEmail(obj, fldName)
{
	var emailStr = obj.value;
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	if ( !reg1.test( emailStr ) && reg2.test( emailStr ) )
	{
		return true;
	}
	else
	{
		//alert(fldName + " needs to be formatted in (Address)@(Company).(Extension)");
	 	return false;
	}
}


/*********************************************************
validateCombobox()
This function checks to make sure the user has selected
an option from a combobox.  (The first option should 
always be "(please select)"
Arguments:
fld = field
errMsg = english name of field for display on error
**********************************************************/
function validateCombobox(fld, errMsg)
{
	var idx=fld.selectedIndex;
	if(idx== -1 || idx==0 )
	{
		alert("Please choose a value for '" + errMsg + "'.");
		focus_on_error(fld);
		return false;	
	}
	return true;
}


/***********************************************************
locateFileUpload()
Returns a handle to the file upload control on a form.
Used to get around the fact that there is no consistent
way to refer to the element cross-browser.
***********************************************************/
function locateFileUpload( f )
{
	for(var i = 0; i < f.elements.length; i ++)
	{
		if( f.elements[i].type=='file' )
		{
			return f.elements[i];
		}
	}
}


/***********************************************************
validateFileType()
This function checks that the type of file being uploaded
is allowed
Arguments:
obj = The File Upload control.
fTyp = Allowed file types
************************************************************/
function validateFileType( obj, fTyp ) 
{
	dots = obj.value.split(".");
	fType = "." + dots[dots.length-1];
	
	if ( fTyp != null && fTyp.indexOf(fType) == -1 )
	{
		return false;
	}
	
	return true;
}


/***********************************************************
validateFileLimit()
This function checks that the value in a file upload
Arguments:
obj = The File Upload control
cur = Current number of file attachments
max = Limit on allowed files
************************************************************/
function validateFileLimit( obj, cur, max )
{
	if ( cur >= max )
	{
		return false;
	}
	
	return true;
}


/***********************************************************
OnFailure()
This function returns the error message to the validation
routine that is appropriate to the error generated.
obj = handle to the field that is being validated.
lbl = field label
msg = string type for field
***********************************************************/
function OnFailure( obj, lbl, msg ){
	var msgs = new Array();
	msgs["text"] = " is a required field. \n\nPlease enter a value.";
	msgs["textarea"] = " is a required field. \n\nPlease enter a value.";
	msgs["select-one"] = " is a required field. \n\nPlease select an entry.";
	msgs["select-multiple"] = " is a required field. \n\nPlease select an entry.";
	msgs["checkbox"] = " is a required field. \n\nPlease select an entry.";
	msgs["file"] = " is a required upload. \n\nPlease select a file.";
	msgs["fileType"] = " requires certain file types. \n\nPlease select a valid file type.";
	msgs["fileLimit"] = " is a limited file upload. \n\nPlease reduce number of attachment(s) first.";
	msgs["radio"] = " is a required field. \n\nPlease select an entry.";
	msgs["number"] = " is a numeric field. \n\nPlease enter a valid number.";
	msgs["date"] = " is a date field. \n\nPlease enter a valid date.";
	msgs["email"] = " is an address field. \n\nPlease enter a valid e-mail address.";
	if(msg[1]	|| msg[2]){ //upper/lower bound ranges have been specified
		if(msg[1]	&& msg[2]){//range
			term = ( msg[0] == "date" )? " ("+msg[3]+")" : "";
			alert(lbl + msgs[msg[0]] + term + " between " + msg[1] + " and " + msg[2]);
		} else if (msg[1]) {//lower bound
			term = ( msg[0] == "number" ) ? " greater than " : " (" + msg[3] + ") after ";
			alert(lbl + msgs[msg[0]] + term + msg[1]);
		} else {//upper bound
			term = ( msg[0] == "number" )? " less than " : " (" + msg[3] + ") before ";
			alert(lbl + msgs[msg[0]] + term + msg[2]);
		}
	} else {//no range given
		alert(lbl + msgs[msg[0]]);
	}
	obj.focus();
	return false;
}


/***********************************************************
isSomethingSelected()
This function is passed an object of type radio group or check
box. It then loops through all options and checks that 
one of them is selected, returning true if so.
Arguments:
obj = Reference to the parent object of the group.
************************************************************/
function isSomethingSelected( obj )
{
	for (var r=0; r < obj.length; r++)
	{
		if ( obj[r].checked ) return true;
	}
	return false;
}


/***********************************************************
validateRequiredFields()
This function is passed an array of fields that are required
to be filled in and iterates through each ensuring they have
been correctly entered.
************************************************************/
function validateRequiredFields( f, a ){
	for (var i=0; i < a.length; i++){
		e = a[i][0];
		//checks input types: "text","select-one","select-multiple","textarea","checkbox","radio","file"
		switch (e.type) {
			case "text":
				if ( trim(e.value) == "" ) return OnFailure(e, a[i][1], ["text"]);
				if ( a[i][2] ) {
					switch ( a[i][2][0] ){
						case "number":
							if ( !validateNumber(e.value, a[i][2][1], a[i][2][2]) ) return OnFailure(e, a[i][1], ["number", a[i][2][1], a[i][2][2]]);
							break
						case "date":
							if ( !valiDate(e, a[i][2][1], a[i][2][2], a[i][2][3]) ) return OnFailure(e, a[i][1], ["date", a[i][2][1], a[i][2][2], a[i][2][3]]);
							break
						case "email":
							if ( !validateEmail(e) ) return OnFailure(e, a[i][1], ["email"]);
							break
						default:
							break
					} //end switch( a[i][2][0] )
				} //end if ( a[i][2] )
				break
			case "file":
				//make sure AT LEAST one file gets attached
				if ( a[i][2][1] == 0 && trim(e.value) == "" ) return OnFailure(e, a[i][1], ["file"]);
				if ( trim(e.value) != "") {
					//check type of file that is being uploaded
					if ( a[i][2][0] != null && validateFileType( e, a[i][2][0] ) == false ) return OnFailure(e, a[i][1], ["fileType"]);
					//check that file limit has not been reached
					if ( a[i][2][2] != null && validateFileLimit( e, a[i][2][1], a[i][2][2] ) == false ) return OnFailure(e, a[i][1], ["fileLimit"]);
				} //end if ( trim(e.value) != "") 
				break
			case "textarea":
				if ( trim(e.value) == "" ) return OnFailure(e, a[i][1], ["textarea"]);
				break
			case "select-one":
				if ( e.selectedIndex == 0 ) return OnFailure(e, a[i][1], ["select-one"]);
				break
			case "select-multiple":
				if (e.selectedIndex == -1) return OnFailure(e, a[i][1], ["select-multiple"]);
				break
			default:
				//must be a checkbox or a radio group if none of above
 				if ( !e[0].type) {  //handle single item group first
					switch (e[0].type) {
						case "checkbox":
							if ( !e.checked )  return OnFailure(e, a[i][1], ["checkbox"]);
							break
						case "radio":
							if ( !e.checked )  return OnFailure(e, a[i][1], ["radio"]);
							break
						default:
							break
					} //end switch(e.type)
				} else { //handle multi-item groups
					switch (e[0].type) {
						case "checkbox":
							if ( !isSomethingSelected( e ) )  return OnFailure(e[0], a[i][1], ["checkbox"]);
							break
						case "radio":
							if ( !isSomethingSelected( e ) )  return OnFailure(e[0], a[i][1], ["radio"]);
							break
						default:
							break
					} //end switch(e[0].type)
				} //end if(!e[0])
				break
		} //end switch (e.type)
	} //end for loop
	return true;
} //end  validateRequiredFields


/******************************************************
isFormComplete()
This function will loop through a form element called
FormName and check each element to make sure
it has been filled in.  Will stop as soon as it fails.
FormName = Name of the form to be validated.
******************************************************/
function IsFormComplete(FormName){
	var x       = 0
	var FormOk  = true
	while ((x < document.forms[FormName].elements.length) && (FormOk)){
		if (document.forms[FormName].elements[x].value == ''){
			alert('Please enter the '+document.forms[FormName].elements[x].name +' and try again.')
        		document.forms[FormName].elements[x].focus()
        		FormOk = false 
     	} //end if.
     	x ++
   	} //end while.
	return FormOk
} //end IsFormComplete


/******************************************************
getSelectedRadio()
returns the array number of the selected radio button or -1 if no button is selected
******************************************************/
function getSelectedRadio(buttonGroup) {
   
	if (buttonGroup[0])
	{ // if the button group is an array (one button is not an array)
		for (var i=0; i<buttonGroup.length; i++)
		{
			if (buttonGroup[i].checked)
			{
				return i;
			}
		}
	}
   	else
	{
		if (buttonGroup.checked)
		{
			return 0;
		} // if the one button is checked, return zero
	}
	
	// if we get to this point, no radio button is selected
	return -1;
}


/******************************************************
getSelectedRadioValue()
returns the value of the selected radio button or "" if no button is selected
******************************************************/
function getSelectedRadioValue(buttonGroup)
{
	var i = getSelectedRadio(buttonGroup);
	if (i == -1)
	{
		return "";
	}
	else
	{
		if (buttonGroup[i])
		{
			// Make sure the button group is an array (not just one button)
			return buttonGroup[i].value;
		}
		else
		{
			// The button group is just the one button, and it is checked
			return buttonGroup.value;
		}
	}
}


/******************************************************
getSelectedCheckbox()
Go through all the check boxes. return an array of all the ones
that are selected (their position numbers). if no boxes were checked,
returned array will be empty (length will be zero)
******************************************************/
function getSelectedCheckbox(buttonGroup)
{
	var retArr = new Array();
	var lastElement = 0;
	
	// if the button group is an array (one check box is not an array)
	if (buttonGroup[0])
	{ 
		for (var i=0; i<buttonGroup.length; i++)
		{
			if (buttonGroup[i].checked)
			{
				retArr.length = lastElement;
				retArr[lastElement] = i;
				lastElement++;
			}
		}
	}
	// There is only one check box (it's not an array)
	else
	{	
		// if the one check box is checked
		if (buttonGroup.checked)
		{ 
			retArr.length = lastElement;
			retArr[lastElement] = 0; // return zero as the only array value
		}
	}
	return retArr;
}


/******************************************************
getSelectedCheckboxValue()
return an array of values selected in the check box group. if no boxes
were checked, returned array will be empty (length will be zero)
******************************************************/
function getSelectedCheckboxValue(buttonGroup)
{

	var retArr = new Array(); // set up empty array for the return values
	var selectedItems = getSelectedCheckbox(buttonGroup);
	
	// if there was something selected
	if (selectedItems.length != 0)
	{ 
		retArr.length = selectedItems.length;
		for (var i=0; i<selectedItems.length; i++)
		{
			if (buttonGroup[selectedItems[i]])
			{	
				// Make sure it's an array
				retArr[i] = buttonGroup[selectedItems[i]].value;
			}
			else
			{
				// It's not an array (there's just one check box and it's selected)
				retArr[i] = buttonGroup.value;// return that value
			}
		}
	}
	return retArr;
}


/***********************************************************
OnFailureMessage()
This function returns the error message to the validation
routine that is appropriate to the error generated.
obj = handle to the field that is being validated.
lbl = field label
msg = string type for field
***********************************************************/
function OnFailureMessage( obj, lbl, msg){
	var errorMessage;
	var msgs = new Array();
	msgs["text"] = " is a required field. Please enter a value.";
	msgs["textarea"] = " is a required field. Please enter a value.";
	msgs["select-one"] = " is a required field. Please select an entry.";
	msgs["select-multiple"] = " is a required field. Please select an entry.";
	msgs["checkbox"] = " is a required field. Please select an entry.";
	msgs["file"] = " is a required upload. Please select a file.";
	msgs["fileType"] = " requires certain file types. Please select a valid file type.";
	msgs["fileLimit"] = " is a limited file upload. Please reduce number of attachment(s) first.";
	msgs["radio"] = " is a required field. Please select an entry.";
	msgs["number"] = " is a numeric field. Please enter a valid number.";
	msgs["date"] = " is a date field. Please enter a valid date.";
	msgs["email"] = " is an address field. Please enter a valid e-mail address.";
	if(msg[1]	|| msg[2]){ //upper/lower bound ranges have been specified
		
		if(msg[1]	&& msg[2]){//range
			term = ( msg[0] == "date" )? " ("+msg[3]+")" : "";
			errorMessage = lbl + msgs[msg[0]] + term + " between " + msg[1] + " and " + msg[2];
		} else if (msg[1]) {//lower bound
			term = ( msg[0] == "number" ) ? " greater than " : " (" + msg[3] + ") after ";
			errorMessage = lbl + msgs[msg[0]] + term + msg[1];
		} else {//upper bound
			term = ( msg[0] == "number" )? " less than " : " (" + msg[3] + ") before ";
			errorMessage= lbl + msgs[msg[0]] + term + msg[2];
		}
	} else {//no range given
		errorMessage = lbl + msgs[msg[0]];
	}
	//obj.focus();
	return errorMessage + "\n";
}


/***********************************************************
validateRequiredFields()
This function is passed an array of fields that are required
to be filled in and iterates through each ensuring they have
been correctly entered.
************************************************************/
function validateRequiredFieldsOneMessage( f, a ){
	var errorList ="";
	for (var i=0; i < a.length; i++){
		e = a[i][0];
		//checks input types: "text","select-one","select-multiple","textarea","checkbox","radio","file"
		switch (e.type) {
			case "text":	
				if ( trim(e.value) == '' ) errorList = errorList +  OnFailureMessage(e, a[i][1], ["text"]);
				if ( a[i][2] ) {
					switch ( a[i][2][0] ){
						case "number":
							if ( !validateNumber(e.value, a[i][2][1], a[i][2][2]) ) errorList = errorList + OnFailureMessage(e, a[i][1], ["number", a[i][2][1], a[i][2][2]]);
							break
						case "date":
							if ( !valiDate(e, a[i][2][1], a[i][2][2], a[i][2][3]) ) errorList = errorList +  OnFailureMessage(e, a[i][1], ["date", a[i][2][1], a[i][2][2], a[i][2][3]]);
							break
						case "email":
							if ( !validateEmail(e) ) errorList = errorList +  OnFailureMessage(e, a[i][1], ["email"]);
							break
						default:
							break
					} //end switch ( a[i][2][0] )
				} //end if ( a[i][2] )
				break
			case "file":
				//make sure AT LEAST one file gets attached
				if ( a[i][2][1] == 0 && trim(e.value) == "" ) errorList = errorList +  OnFailureMessage(e, a[i][1], ["file"]);
				if ( trim(e.value) != "") {
					//check type of file that is being uploaded
					if ( a[i][2][0] != null && validateFileType( e, a[i][2][0] ) == false ) errorList = errorList +  OnFailureMessage(e, a[i][1], ["fileType"]);
					//check that file limit has not been reached
					if ( a[i][2][2] != null && validateFileLimit( e, a[i][2][1], a[i][2][2] ) == false ) errorList = errorList +  OnFailureMessage(e, a[i][1], ["fileLimit"]);
				} //end if ( trim(e.value) != "") 
				break
			case "textarea":
				if ( trim(e.value) == "" ) errorList = errorList +  OnFailureMessage(e, a[i][1], ["textarea"]);
				break
			case "select-one":
				if ( e.selectedIndex == 0 ) errorList = errorList +  OnFailureMessage(e, a[i][1], ["select-one"]);
				break
			case "select-multiple":
				if (e.selectedIndex == -1) errorList = errorList +  OnFailureMessage(e, a[i][1], ["select-multiple"]);
				break
			default:
				//must be a checkbox or a radio group if none of above
 				if ( !e[0]) { //handle single item group first
					switch (e.type) {
						case "checkbox":
							if ( !e.checked )  errorList = errorList +  OnFailureMessage(e, a[i][1], ["checkbox"]);
							break
						case "radio":
							if ( !e.checked )  errorList = errorList +  OnFailureMessage(e, a[i][1], ["radio"]);
							break
						default:
							break
					} //end switch (e.type) 
				} else { //handle multi-item groups
					switch (e[0].type) {
						case "checkbox":
							if ( !isSomethingSelected( e ) )  errorList = errorList +  OnFailureMessage(e[0], a[i][1], ["checkbox"]);
							break
						case "radio":
							if ( !isSomethingSelected( e ) )  errorList = errorList +  OnFailureMessage(e[0], a[i][1], ["radio"]);
							break
						default:
							break
					} //end switch (e[0].type)
				} //end if ( !e[0])
				break
		} //end switch (e.type
	} //end for loop
	if(errorList != ""){
		alert (errorList);
		return false;
	}else{
		return true;
	} //end if(errorList != "")
}


/**********************************************************
Date Validation Section
**********************************************************/
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear;
var maxYear;
with (new Date()) minYear = getFullYear();
with (new Date()) maxYear = minYear + 50;

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
	{   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}


function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
	{   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


function DaysArray(n)
{
	for (var i = 1; i <= n; i++)
	{
		this[i] = 31;
		
		if (i==4 || i==6 || i==9 || i==11)
		{
			this[i] = 30;
		}
		if (i==2)
		{
			this[i] = 29;
		}
   } 
   return this
}


function isDate(dtStr, fldLabel)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	
	for (var i = 1; i <= 3; i++)
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (pos1==-1 || pos2==-1)
	{
		//alert(fldLabel + " format should be : mm/dd/yyyy")
		return false
	}
	
	if (strMonth.length<1 || month<1 || month>12)
	{
		//alert("Please enter a valid month for " + fldLabel)
		return false
	}
	
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		//alert("Please enter a valid day for " + fldLabel)
		return false
	}
	
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear + " for " + fldLabel)
		return false
	}
	
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		//alert("Please enter a valid date for " + fldLabel)
		return false
	}
	
	var today;
	with (new Date()) today = String(( getFullYear()*100 + getMonth()+1 )*100 + getDate());
	var St = dtStr;
	St = St.replace(/(\d+)\/(\d+)\/(\d+)/, "$3$1$2");
	
	return true
}


function isDate2(dtStr, fldLabel)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	
	for (var i = 1; i <= 3; i++)
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if (pos1==-1 || pos2==-1)
	{
		//alert(fldLabel + " format should be : mm/dd/yyyy")
		return false
	}
	
	if (strMonth.length<1 || month<1 || month>12)
	{
		//alert("Please enter a valid month for " + fldLabel)
		return false
	}
	
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		//alert("Please enter a valid day for " + fldLabel)
		return false
	}
	
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		//alert("Please enter a valid date for " + fldLabel)
		return false
	}
	
	var today;
	with (new Date()) today = String(( getFullYear()*100 + getMonth()+1 )*100 + getDate());
	var St = dtStr;
	St = St.replace(/(\d+)\/(\d+)\/(\d+)/, "$3$1$2");
	
	return true
}