/* this function checks all required fields on a form
   for each required field, create a hidden form field
   with the same name as the required field,
   but with '_required' appended to the name
   
   parameters:
   theform - the form being submitted  */
function checkRequiredFields(theform)
{
	var missinginfo = "";
	// go through all form elements
	for (i = 0; i < theform.length; i++)
	{
		// all required fields should have a corresponding hidden field with _required appended to the field name
		if (theform.elements[i].name.indexOf("_required") != -1)
		{
			// get the corresponding field (without _required)
			endindex = theform.elements[i].name.indexOf("_required");
			fieldname = theform.elements[i].name.substring(0,endindex);
			requiredfield = eval("theform." + fieldname);
			// if type is undefined and field has a length, assume this is a radio button
			if (((!requiredfield.type) && (requiredfield.length)) || (requiredfield.type == "radio"))
			{
				radiochecked = "No";
				for (j = 0; j < requiredfield.length; j++)
				{
					if (requiredfield[j].checked == true)
					{
						radiochecked = "Yes";
					}
				}
				if (radiochecked == "No")
				{
					errormessage = theform.elements[i].value;
					missinginfo += "\n  - " + errormessage;
				}
			}
			// validate select, text, password, and textarea types
			if ((requiredfield.type == "checkbox" && requiredfield.checked == false) || 
			(requiredfield.type == "select-one" && requiredfield.options[requiredfield.selectedIndex].value == "") || 
			(requiredfield.type == "select-multiple" && (requiredfield.selectedIndex == -1 || requiredfield.options[requiredfield.selectedIndex].value == "")) || 
			((requiredfield.type == "text" || requiredfield.type == "textarea" || requiredfield.type == "file" || requiredfield.type == "password") && requiredfield.value == ""))
			{
				// get error message from corresponding hidden field
				errormessage = theform.elements[i].value;
				missinginfo += "\n  - " + errormessage;
			}
		}
	}
	// if anything is missing, display alert and do not submit form
	if (missinginfo != "")
	{
		missinginfo = "Sorry, there was a problem with your form:\n" + missinginfo;
		alert(missinginfo);
		return false;
	}
	else return true;
}


