/*****************************************************************************
 *
 * getArrayControlById - retrieves an array of controls for a name
 *
 *****************************************************************************/
	function getArrayControlById(form,groupid)
	{
	// Iterate over all elements looking for ones that start with the groupid
		groupid += '_';
		var strlen = groupid.length;
		var ctls = new Array();
		if (document.all)
		{
			for (var i in document.all)
			{
				if (!i) continue;
				try {
					var ctl = document.all[i];
					if (!ctl) continue;
					var i_type = typeof i;
					var sub = (i_type == 'string') ? i.substring(0,strlen) : '';
					if (typeof ctl.type == 'undefined' || sub != groupid)
						continue;
					ctls[ctls.length] = ctl;
				} catch(e) {}
			}
		}
		if (ctls.length < 0 && document.layers)
		{
			for (var i in document.layers)
			{
				if (!i) continue;
				try {
					var ctl = document.layers[i];
					if (!ctl) continue;
					var i_type = typeof i;
					var sub = (i_type == 'string') ? i.substring(0,strlen) : '';
					if (typeof ctl.type == 'undefined' || sub != groupid)
						continue;
					ctls[ctls.length] = ctl;
				} catch(e) {}
			}
		}
		if (ctls.length < 0)
		{
			for (var i in document)
			{
				if (!i) continue;
				try {
					var ctl = document[i];
					if (!ctl) continue;
					var i_type = typeof i;
					var sub = (i_type == 'string') ? i.substring(0,strlen) : '';
					if (typeof ctl.type == 'undefined' || sub != groupid)
						continue;
					ctls[ctls.length] = ctl;
				} catch(e) {}
			}
		}
		return ctls;
	}

/*****************************************************************************
 *
 * getSelectedControlById - retrieves the selected control for a name
 *
 *****************************************************************************/
	function getSelectedControlById(form,groupid)
	{
	// Iterate over all elements looking for ones that start with the groupid
		groupid += '_';
		var strlen = groupid.length;
		if (document.all)
		{
			for (var i in document.all)
			{
				if (!i) continue;
				try {
					var ctl = document.all[i];
					if (!ctl) continue;
					var i_type = typeof i;
					var i_type = typeof i;
					var sub = (i_type == 'string') ? i.substring(0,strlen) : '';
					if (typeof ctl.type == 'undefined' || sub != groupid)
						continue;
					switch (ctl.type.toLowerCase())
					{
					case 'radio':
					case 'checkbox':
						if (ctl.checked)
							return ctl;
					default:
						return ctl;
					}
				} catch(e) {}
			}
		}
		if (document.layers)
		{
			for (var i in document.layers)
			{
				if (!i) continue;
				try {
					var ctl = document.layers[i];
					if (!ctl) continue;
					var i_type = typeof i;
					var sub = (i_type == 'string') ? i.substring(0,strlen) : '';
					if (typeof ctl.type == 'undefined' || sub != groupid)
						continue;
					switch (ctl.type.toLowerCase())
					{
					case 'radio':
					case 'checkbox':
						if (ctl.checked)
							return ctl;
					default:
						return ctl;
					}
				} catch (e) {}
			}
		}
		for (var i in document)
		{
			if (!i) continue;
			try {
				var ctl = document[i];
				if (!ctl) continue;
				var i_type = typeof i;
				var sub = (i_type == 'string') ? i.substring(0,strlen) : '';
				if (typeof ctl.type == 'undefined' || sub != groupid)
					continue;
				switch (ctl.type.toLowerCase())
				{
				case 'radio':
				case 'checkbox':
					if (ctl.checked)
						return ctl;
				default:
					return ctl;
				}
			} catch (e) {}
		}
		return null;
	}

/*****************************************************************************
 *
 * getControl - retreives the actual control on the form. If the control is a
 *              radio button, it returns the radio button that was checked
 *
 *****************************************************************************/
	function getControl(form,n)
	{
		var name = n+'[]';
		var control = null;


	// Try to look the control up directly
		var ctl = control = (form[name]) ? form[name] : form[n];
		var element_type = typeof form.elements;
		var ctl_type = typeof ctl;
		if (ctl != null && typeof ctl != 'undefined' && ctl.name && ctl.name != n && ctl.name != name)
		{
			ctl_type = 'undefined';
			ctl = null;
			control = null;

		// Find it manually
			for (var i in form.elements)
			{
				var c = form.elements[i];
				if (c && (typeof c == 'object' && c.name))
				{
					if (c.name == n || c.name == name)
					{
						ctl = control = c;
						break;
					}
				}
			}
			if (ctl == null)
			{
				for (var i in form)
				{
					var c = form[i];
					if (c && (typeof c == 'object' && c.name))
					{
						if (c.name == n || c.name == name)
						{
							ctl = control = c;
							break;
						}
					}
				}
			}
			if (ctl == null)
			{
				for (var i in document.all)
				{
					var c = document.all[i];
					if (c && (typeof c == 'object' && c.name))
					{
						if (c.name == n || c.name == name)
						{
							ctl = control = c;
							break;
						}
					}
				}
			}
		}
		if ((ctl == null || ctl_type == 'undefined') && element_type != 'undefined')
		{
		// Otherwise, use the conventional elements member
			var elements = form.elements;
			ctls = new Array();
			var length_type = typeof elements.length;
			if (length_type != 'undefined' && length_type != 'object')
			{
				var len = elements.length;
				for (var x=0; x<len; x++)
				{
					var ctl = elements[x];
					if (ctl.name == name || ctl.name == n)
					{
						ctls[ctls.length] = ctl;
						if (ctl.type == 'radio' && ctl.checked)
							return ctl;
					}
				}
				control = (ctls.length == 1) ? ctls[0] : ctls;
			}
		}
		else if (ctl && typeof ctl.length != 'undefined' && ctl.length != 'object' && typeof ctl.type == 'undefined')
		{
			var type = ctl[0].type.toLowerCase();
			if (type == 'radio' || type == 'checkbox')
			{
				var len = ctl.length;
				for (var x=0; x<len; x++)
				{
					if (ctl[x].checked)
						control = ctl[x];
				}
			}
		}
		
		if (control == 'undefined' || typeof control == 'undefined')
			control = null;
		return control;
	}

/*****************************************************************************
 *
 * getArrayControl - retreives an array of controls on the form.
 *
 *****************************************************************************/
	function getArrayControl(form,n)
	{
		var name = n+'[]';
		var ctls = (form[name]) ? form[name] : form[n];
		var ctls_type = typeof ctls.length;
		if ((!ctls || ctls_type == 'undefined') && form.elements != 'undefined')
		{
		// Otherwise, use the conventional elements member
			ctls = new Array();
			var len = form.elements.length;
			for (var x=0; x<len; x++)
			{
				var ctl = form.elements[x];
				if (ctl.name == name || ctl.name == n)
				{
					ctls[ctls.length] = ctl;
				}
			}
		}
		return ctls;
	}


/*****************************************************************************
 *
 * getValueControl - retrieves the control with the matching value
 *
 *****************************************************************************/
	function getValueControl(form,n,v)
	{
		var name = n+'[]';
		var control = null;

		var ctl = (form[name]) ? form[name] : form[n];
		if (ctl)
		{
			if (typeof ctl.length != 'undefined')
			{
				var len = ctl.length;
				for (var x=0; x<len; x++)
				{
					var c = ctl[x];
					if (c.value == v)
						control = c;
				}
			}
		}
		else if (typeof form.elements != 'undefined')
		{
			var len = form.elements.length;
			for (var x=0; x<len; x++)
			{
				ctl = form.elements[x];
				if ((ctl.name == name || ctl.name == n) && ctl.value == v)
					control = ctl;
			}
		}
		if (control == 'undefined' || typeof control == 'undefined')
			control = null;
		return control;
	}

/*****************************************************************************
 *
 * getIndexControl - retrieves the i'th control in an array of controls
 *
 *****************************************************************************/
	function getIndexControl(form,n,i)
	{
		var name = n+'[]';
		var control = null;

		var ctl = control = (form[name]) ? form[name] : form[n];
		if ((ctl == null || typeof ctl == 'undefined') && typeof form.elements != 'undefined')
		{
			var len = form.elements.length;
			var c = 0;
			for (var x=0; x<len; x++)
			{
				var ctl = form.elements[x];
				if (ctl.name == name || ctl.name == n)
				{
					if (c == i)
						control = ctl;
					c++;
				}
			}

		}
		else if (ctl && typeof ctl.length != 'undefined' && typeof ctl.type == 'undefined')
		{
			if (i > ctl.length)
				i = ctl.length-1;
			control = ctl[i];
		}
		if (control == 'undefined' || typeof control == 'undefined')
			control = null;
		return control;
	}

/*****************************************************************************
 *
 * getControlValue - retrieves an array of values for the control
 *
 *****************************************************************************/
	function getControlValue(form,n)
	{
		var val = null;
		var values = new Array();
		var name = n+'[]';

		if (typeof form.elements != 'undefined' && typeof form.elements.length != 'object')
		{
			var len = form.elements.length;
			for (var x=0; x<len; x++)
			{
				var ctl = form.elements[x];
				if (ctl.name == name || ctl.name == n)
				{
					if ((ctl.type == 'radio' || ctl.type == 'checkbox') && !ctl.checked)
					{
						continue;
					}
					else if (ctl.type == 'select')
					{
						var index = ctl.selected;
						val = ctl.options[index].value;
						values[values.length] = val;
					}
					else
					{
						val = ctl.value;
						values[values.length] = val;
					}
				}
			}
		}
		else
		{
			var ctl = (form[name]) ? form[name] : form[n];
			if (ctl)
			{
				var type = null;
				if (typeof ctl.nodeName != 'undefined')
					type = ctl.nodeName.toLowerCase();
				if (typeof ctl.type != 'undefined')
					type = ctl.type.toLowerCase();
				if (type == 'select')
				{
					var index = ctl.selectedIndex;
					val = ctl.options[index].value;
					values[values.length] = val;
				}
				else
				{
					if (typeof ctl.length != 'undefined')
					{
						var len = ctl.length;
						for (var i=0;i<len;i++)
						{
							var type = (typeof ctl[i].nodeName != 'undefined') ? ctl[i].nodeName.toLowerCase() : ctl[i].type.toLowerCase();
							if ((type == 'radio' || type == 'checkbox') && !ctl[i].checked)
							{
								continue;
							}
							else if (type == 'select')
							{
								var index = ctl[i].selected;
								val = ctl[i].options[index].value;
								values[values.length] = val;
							}
							else
							{
								val = ctl[i].value;
								values[values.length] = val;
							}
						}
					}
					else
					{
						val = ctl[i].value;
						values[values.length] = val;
					}
				}
			}
		}
		return values;
	}
