//----------------------------------//
//File Name:		checkMandatories.js
//Author:		Ewan Mcghee
//Version:		1
//Date Last Edited:	31/01/03

//This file uses the the program's coding convention of attaching the prefix 'mndtry' to all form fields for which it is mandatory to supply a value to check that values have indeed been supplied for all mandatory fields.

//Where the value has to come from at least 1 option in a radio button, or checkbox group, the code uses 2 flags to trap whether the previous element in the iteration through the form elements belonged to such a mandatory group, and, if it did, to check whether a selection was made by the user.

//N.B. THE CODE WILL ONLY WORK FOR A RADIO BUTTON/CHECKBOX GROUP, WHERE THAT GROUP DOES NOT CONTAIN THE LAST ELEMENT ON THE FORM

//The code will work for all form elements, but only for a single form

//-----------------------------------//

previousGroupElementName = ""; //used to check whether the previous group element name is the same as the new one, in an iteration

valueSuppliedForGroup = false; //used to check whether a value has been supplied for a mandatory group

previousWasMandatoryGroupElement = false;
var alphabet = new Array ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z");

function mandatoriesError()
{
	alert("You haven't completed all mandatory fields. Please complete all fields marked with an asterisk.");
}

function checkTextArea(tArea)
{
	var contentIsValid = false;

	var theArea = eval(tArea);

	for(j = 0; j < alphabet.length; j++)
	{
		if(theArea.value.indexOf(alphabet[j]) != -1 || theArea.value.indexOf(alphabet[j].toUpperCase()) != -1)
		{
			contentIsValid = true;
			break;
		}
	}
	//if the field is empty, warn the user and quit the submit
	if(!contentIsValid)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function checkMandatories()
{

	previousGroupElementName = "";

	valueSuppliedForGroup = false;

	previousWasMandatoryGroupElement = false;

	theForm = document.forms[0];

	for(i=0; i < theForm.elements.length; i++)
	{
		elementName = theForm.elements[i].name;

		if(elementName.indexOf("mndtry") == 0)
		{

			//if the field is part of a radio group or checkbox group
			if(theForm.elements[i].type == "radio" || theForm.elements[i].type == "checkbox")
			{

				previousWasMandatoryGroupElement = true;

				//if we're in the same group as the last iteration (or this is the 1st element of the 1st group 2 b checked)
				if(previousGroupElementName == elementName || previousGroupElementName == "")
				{
					previousGroupElementName = elementName;
					if(valueSuppliedForGroup == true) //we've already got 1 value for the group, so we don't need to check for the rest
					{
						continue;
					}
					else
					{
						if(document.forms(0).elements[i].checked)
						{
							valueSuppliedForGroup = true;
							continue;
						}
					}
				}
				else
				{// it's a new group

					previousGroupElementName = elementName;
					if(valueSuppliedForGroup == false)
					{
						previousGroupElementName = "";
						mandatoriesError();
						return false
					}
					else
					{
						//re-set
						valueSuppliedForGroup = false;
						if(document.forms(0).elements[i].checked)
						{
							valueSuppliedForGroup = true;
							continue;
						}
					}
				}

			}
			else
			{
				contentIsValid = false;
				if(theForm.elements[i].type != "button" && theForm.elements[i].type != "submit" && theForm.elements[i].type != "reset")
				{
					//if(theForm.elements[i].type == "textarea")
					//{
						//for(j = 0; j < alphabet.length; j++)
						//{
							//if(theForm.elements[i].value.indexOf(alphabet[j]) != -1 || theForm.elements[i].value.indexOf(alphabet[j].toUpperCase()) != -1)
							//{
							//	contentIsValid = true;
							//	break;
							//}
						//}
						//if the field is empty, warn the user and quit the submit
						//if(!contentIsValid)
						//{
							//theForm.elements[i].focus();
							//mandatoriesError();
							//return false;
						//}
					//}
					//if the field is empty, warn the user and quit the submit

					if(theForm.elements[i].value == "")
					{
						theForm.elements[i].focus();
						mandatoriesError();
						return false;
					}
				}
			}//end if radio/checkbox group or not
		}//end if mandatory field

		//(for each) new form element, was the previous type a mandatory group
		if(previousGroupElementName != elementName && previousWasMandatoryGroupElement)
		{
			//re-set
			previousWasMandatoryGroupElement = false;

			if(valueSuppliedForGroup == false) //was a value supplied for that mandatory group
			{
				previousGroupElementName = "";
				mandatoriesError();
				return false
			}
		}
	} // end loop thru all form elements
	return true;

} //end function
