/**
	This is a group of check boxes
	The value of the checkboxes is store as properties
	itsType: optional  true: single input field name
					   false (or omit): multiple input field names
*/
function CheckBoxesGroup(inFormName,itsNames,itsType)
{
	var self = this;
	var itsElements = document.forms[inFormName].elements;
	var itsProperties;
	var itsFilterOn;

	self.initialize = function(inProperties)
	{
		
		//extract the properties of the itsNames from input, and copy to itsProperty
		if (itsProperties == null)
			itsProperties = new Properties();

		Publisher(self);

		//this could be reimplemented if the default is no box checked. for now, check all boxes
		if (inProperties == null)
		{
			self.setAll(true);
			self.changeState();
			return;
		}

		for (var i = 0; i < itsElements.length; i++)
		{
			var theValue = itsElements[i];
			if (theValue != null)
				itsProperties.setProperty(itsNames[i],theValue);
		}

		self.setCheckBoxesWithProperties(itsProperties);
		self.changeState();
	};


	/**
	 * true if all checkboxes are checked.
	 */
	self.isAllSelected = function() {
		for (var i = 0; i < itsElements.length; i++)
		{
			if(!itsElements[i].checked) {
				return false;
			}
		}
		return true;
	};

	self.getSize = function() {
		return itsElements.length;
	};

	self.selectOneOnly = function(inID)
	{
		self.setAll(false);
		document.getElementById(inID).checked = true;
		self.changeState();
	};


	//parameter: true -- all checked when reset
	//			 false -- all clear when reset
	self.reset = function(inFlag)
	{
		self.setAll(inFlag);
		self.changeState();		
	};

	self.resetAll = function(inFlag)
	{
		self.setAll(inFlag);
		itsFilterOn = false;
		itsProperties.clear();
		self.notifySubscribers(itsProperties, itsFilterOn);
	}

	
	self.changeState = function()
	{
		//extract the current checkboxes state and update itsProperties
		itsProperties = new Properties();
		var theCounter = 0;
		for (var i = 0; i < itsElements.length; i++)
		{
			if (itsElements[i].checked)
			{
				itsProperties.addProperty(itsElements[i].name,itsElements[i].value);
				theCounter++;
			}
			else
			{
				if (!itsType)
					itsProperties.removeProperty(itsElements[i].name);
			}
		}

		if (theCounter == 0 || theCounter == itsElements.length)
			itsFilterOn = false;
		else
			itsFilterOn = true;

		//call subscribers here
		self.notifySubscribers(itsProperties,itsFilterOn);
	};


	//select all: inCheck = true
	//clear all: inCheck = false;
	self.setAll = function(inCheck)
	{
		for (var i = 0; i < itsElements.length; i++)
		{
			itsElements[i].checked = inCheck;
		}
	};

	self.setCheckBoxesWithProperties = function(inProperties)
	{
		for (var i = 0; i < itsElements.length; i++)
		{
			var hasSet = false;
			for (var j = 0; j < itsNames.length; j++)
			{
				if (itsElements[i].name == itsNames[j])
				{
					var theValue = inProperties.getProperty(itsNames[j]);
					if (theValue instanceof Array)
					{
						//check value match to set checkbox
						for (var k = 0; k < theValue.length; k++)
						{
							if (itsElements[i].value == theValue[k])
							{
								itsElements[i].checked = true;
								hasSet = true;
								break;
							}
						}
					}
					else
					{
						if (theValue == 1)
						{
							itsElements[i].checked = true;
							hasSet = true;
						}
						else if(theValue != 0)
						{
							if (itsElements[i].value == theValue)
							{
								itsElements[i].checked = true;
								hasSet = true;
							}
						}
					}
				}
			}

			if (!hasSet)
				itsElements[i].checked = false;
		}

		self.changeState();
	};


	self.getProperties = function()
	{
		return itsProperties;
	};


	self.getFilterOn = function()
	{
		return itsFilterOn;
	};
}


