function checkForm(f)
{
	var errorText = "";
	var checkedGroups = "";
	for (var i=0; i<f.elements.length; i++)
	{
		var element = f.elements[i];
		switch (element.type)
		{
			case "text":
			case "textarea":
			case "password":
				if (!element.attributes.readonly)
				{
					if ((element.value.replace(/\s/g, "") == ""))
					{
						switch (element.parentNode.className)
						{
							case "required":
							case "error":
								errorText += element.name +",";
								element.parentNode.className = "error";
								break;														
						}
					}
					else
					{
						if (element.parentNode.className != "optional")
						{element.parentNode.className = "required"}
					}
				}
				break;
			case "checkbox":
				if (!element.checked)
				{
					errorText += element.name + ",";
					element.parentNode.className = "error";
				}
				break;
			case "radio":
				var group = f.elements[element.name];
				if (checkedGroups.indexOf("[" + element.name + "]") > -1)
				{continue;}
				else
				{checkedGroups += "[" + element.name + "]";}
				var groupok = false;
				for (var j=0; j<group.length; j++)
				{
					if (group[j].checked)
					{groupok = true;}
				}
				if (!groupok)
				{errorText += element.name + ",";}
				break;
			case "select-one":
			case "select-multiple":
				var selectok = false;
				for (var j=0; j<element.options.length;j++)
				{
					var item = element.options[j];
					if (item.selected && item.value != "")
					{selectok = true;}
				}
				if (!selectok)
				{
					switch (element.parentNode.className)
					{
						case "required":
						case "error":
							errorText += element.name +",";
							element.parentNode.className = "error";
							break;														
					}
				}
				else
				{
					if (element.parentNode.className != "optional")
					{element.parentNode.className = "required"}
				}
				break;
		}
	}

	if (errorText == "")
	{
		// submit form
		document.forms[f.name].submit();
		return true;
	}
	else
	{
		var nodeAlert = $("divAlert");
		if (nodeAlert == null)
		{
			var newDiv = document.createElement("div");
				newDiv.setAttribute("id", "divAlert");
			var newText = document.createTextNode("There is a problem or two with your form.  This is most likely because required information was left out or entered incorrectly.  The fields that need your attention have been highlighted.");
			  
			$("buttons").appendChild(newDiv);
			newDiv.appendChild(newText);
		}
		return false;
	}
}

function textCounter(tfInput, tfCount, nMax) 
{
	// if the length of the input string exceeds
	// the maximum allowable size...
	if (tfInput.value.length > nMax)
	{
		// don't allow any more input
		tfInput.value = tfInput.value.substring(0, nMax);
	}
	// otherwise...
	else
	{
		// set the display field to remaining number
		tfCount.value = nMax - tfInput.value.length;
	}
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

function showNewProgramUI(nodeSelect)
{
	var nodeProgName = $("progName");
	
	// If the user's program isn't listed, show the first step
	// in registering a new program
	if ((nodeSelect.options[nodeSelect.selectedIndex].value == "0") && (nodeProgName == null))
	{
		var newDiv = document.createElement("div");
			newDiv.setAttribute("class", "required");
			
		var newLabel = document.createElement("label");
		var newText = document.createTextNode("your program name");
		
		var newInput = document.createElement("input");
			newInput.setAttribute("type", "text");
			newInput.setAttribute("name", "progName");
			newInput.setAttribute("id", "progName");
			newInput.setAttribute("class", "inputText");
			newInput.setAttribute("size", "10");
			newInput.setAttribute("maxLength", "100");
			newInput.setAttribute("value", "");
		
		nodeSelect.parentNode.parentNode.appendChild(newDiv);
		newDiv.appendChild(newLabel);
		newLabel.appendChild(newText);
		newDiv.appendChild(newInput);
	}
	// Otherwise, if the user selects a program, make sure the
	// form updates accordingly
	else
	{
		// if the user selected the "unlisted program" previously,
		// remove the displayed element from the node
		if (nodeProgName != null)
		{nodeProgName.parentNode.parentNode.removeChild(nodeProgName.parentNode);}
	}
}

function restrictSelectOptions(nodeSelect)
{	
	// For all select elements in the supplied 
	// fieldset, remove any options with values
	// equaling that for the current item
	var fs = nodeSelect.parentNode.parentNode;
	var arrSelects = fs.getElementsByTagName("select");
	var selectedVal = nodeSelect.options[nodeSelect.selectedIndex].value;
	// loop through the related select elements
	for (var i=0; i<arrSelects.length; i++)
	{
		var element = arrSelects[i];
		if (element.getAttribute("id") != nodeSelect.getAttribute("id"))
		{
			for (var j=1; j<element.options.length;j++)
			{
				if (element.options[j].selected && element.options[j].value == selectedVal)
				{element.selectedIndex = 0;}
			}		
		}			
	}
}