// JavaScript Document// Accepts an arbitrary number of arguments, all input field references whose values are required in the form...
// Can be called from an object itself: onBlur="requiredField(this)"
// or from a different object (i.e. button): onClick="requiredField(txtProtNum, txtCntrNum, txtSubjId)"
//
//Updated by Tim Haak and Simona Popescu to handle required radio buttons.
function requiredField()
{
	var myRegExp = /\S/;
	var maxLen = arguments.length;
	var missingFields = 0;
	var firstError = -1;

	for(var x = 0; x < maxLen; x++)
	{
		//Added by Tim Haak and Simona Popescu to handle required radio buttons
		//If radio group has no value and is not disabled, check for error
		if(arguments[x].type == null && arguments[x][0].type == "radio" && !arguments[x][0].disabled)
		{

			var rdbLen = arguments[x].length;
			var missingFound = 0;
			
			for(var j = 0; j < rdbLen; j++)
			{
				if(!arguments[x][j].checked)
				{
					missingFound++;
				}
			}
			
			if(missingFound == rdbLen)
			//The number missing is equal to the length of the radio button group array
			{
				missingFields++;
				//Set to true since the missing field is a radio button or checkbox
				if(firstError == -1)
				{
					firstError = x;
				}
			}	
		}
		else
		{
			// if the field is blank or contains only white space and is not disabled, mark it as an error
			if((arguments[x].value == null || arguments[x].value.length == 0 || !myRegExp.test(arguments[x].value)) && !arguments[x].disabled)
			{
				missingFields++;
				if(firstError == -1)
				{
					firstError = x;
				}
			}
		}
	}
	// if at least one required field is blank and checkError returns true, throw error
	if(missingFields > 0)
	{
		alert('Please complete all required fields before proceeding');
		if(arguments[firstError].type == null && arguments[firstError][0].type == "radio")
		//If the missing field is a radio button use the first radio button in that group to give focus to
		{
			arguments[firstError][0].focus();
			arguments[firstError][0].select();
		}
		else
		{
			arguments[firstError].focus();
			// if the field is not a select box, also select the value
			if(arguments[firstError].type != "select-one" && arguments[firstError].type != "select-multiple")
			{
				arguments[firstError].select();
			}
		}
		// return false so that submit buttons will not submit
		return false;
	}
	// otherwise, all required fields have been entered
	else
	{
		return true;
	}
}	
/****************************************************************************************/
function sendMail(objForm)
{
	if(requiredField(objForm.txtFirstName, objForm.txtLastName, objForm.txtEmail, objForm.txtQuestionMessage))
	{
		objForm.submit();
	}
}