///////////////////////////////////////////////////////////////////////////////////////////////////
// MultiInstanceControl Class
// 	Infrastructure for uniform manipulation of controls, whether they are single elements or arrays
//
// Author
//	Lucio A Aguirre Z.
//
// Change History
//	9/12/00		LAAZ	Created
//


// constructor
function MultiInstanceControl(aCtl)
{
	this.m_ctl = aCtl;
	
	// by default, treat m_ctl as an array of controls
	this.m_multiInstanceCtl = this.m_ctl ;
	
	// if m_ctl is a single control: get the array of controls to which it belongs (if any)
	if ( typeof(this.m_ctl) != "undefined" && typeof(this.m_ctl.name) != "undefined" )
		this.m_multiInstanceCtl = this.m_ctl.form[this.m_ctl.name] ;
		
	this.m_instanceCount = this.getInstanceCount();
}


// # of instances of m_ctl in its form, where m_ctl may be a single element or an array of elements
MultiInstanceControl.prototype.getInstanceCount = function()
{
	if ( typeof(this.m_ctl) == "undefined" ) return 0;
	if ( typeof(this.m_ctl.name) == "string" )
		if ( this.m_multiInstanceCtl == this.m_ctl )
			return 1;
		else
			return this.m_multiInstanceCtl.length;
			
	return this.m_multiInstanceCtl.length;
}


// returns the idx-th element of m_ctl, where m_ctl may be a single element or an array of elements
// Useful to provide uniform reference to single/multi instance controls.
MultiInstanceControl.prototype.getInstanceAt = function(idx)
{
	return ( ( this.m_instanceCount <= 1 ) ? this.m_ctl : this.m_multiInstanceCtl[idx] )			
}


// is there any checkbox checked?
MultiInstanceControl.prototype.anyChecked = function()
{
	for (var i = 0; i < this.m_instanceCount; i++)
		if ( this.getInstanceAt(i).checked ) return true;

	return false;
}


// get the selected radio value, list of checked check boxes.
// for other types, always gets value (except select)
MultiInstanceControl.prototype.getValue = function()
{
	var value = "";
	for (var i = 0; i < this.m_instanceCount; i++)
		if ( "checkbox,radio".search(this.getInstanceAt(i).type) != -1 )
		{
			if ( this.getInstanceAt(i).checked ) 
				value += this.getInstanceAt(i).value + ",";
		}
		else
				value += this.getInstanceAt(i).value + ",";
	return value.substr(0, value.length-1);		// strips last comma
}


// Set the value of this multiInstanceControl (text, hidden, ...)
// If a parallelMultiInstanceCheckbox is passed only sets the instances correspondant
// to the checked ones in parallelMultiInstanceCheckbox.
MultiInstanceControl.prototype.setValue = function(value, parallelMultiInstanceCheckbox)
{
	if ( typeof(parallelMultiInstanceControl) == "undefined" )
		for (var i = 0; i < this.m_instanceCount; i++)
			this.getInstanceAt(i).value = value;
	else
		for (var i = 0; i < this.m_instanceCount; i++)
			if ( parallelMultiInstanceCheckbox.getInstanceAt(i).checked )
				this.getInstanceAt(i).value = value;
}


// Returns a list that is a subset of parallelValues (list/array) correspondant to the
// checked values in this (radio/checkBox).
MultiInstanceControl.prototype.getParallelValues = function(parallelValues)	// list or array
{
	if ( typeof(parallelValues) == "string" )
		var parallelArray = parallelValues.split(",");
	else
		var parallelArray = parallelValues;
		
	parallelValues ="";
	for (var i = 0; i < this.m_instanceCount; i++)
		if ( this.getInstanceAt(i).checked )
			parallelValues += parallelArray[i] + ",";
	return parallelValues.substr(0, parallelValues.length-1);
}


// checkes the radio/checkbox control which value is VALUE
// if reset, uncheck other values
MultiInstanceControl.prototype.checkByValue = function(value, reset) 
{
	if ( typeof(reset) == "undefined" ) reset = false;
	
	var currentInstance;
	for (var i = 0; i < this.m_instanceCount; i++)
	{
		currentInstance = this.getInstanceAt(i);
		if (currentInstance.type == "checkbox" || currentInstance.type == "radio")
			if ( currentInstance.value == value )
				currentInstance.checked = true;
			else if ( reset )
				currentInstance.checked = false;
	}
}

// Checkes/uncheckes all instances of a MultiInstanceControl
// The test currentInstance.type == "checkbox" is because sometimes a MultiInstanceControl
// can have diferent types of instances (eg checkbox and hiddens for tabular format sake)
MultiInstanceControl.prototype.checkAll = function(bChecked) 
{
	var currentInstance;
	for (var i = 0; i < this.m_instanceCount; i++)
	{
		currentInstance = this.getInstanceAt(i);
		if (currentInstance.type == "checkbox" )
			currentInstance.checked = bChecked;
	}
}


// uncheckes all instances of a MultiInstanceControl
MultiInstanceControl.prototype.uncheckAll = function() 
{
	this.checkAll(false);
}

