/*
 *+------------------------------------------------------------------------+
 *| Licensed Materials - Property of IBM
 *| BI and PM: prmt
 *| (C) Copyright IBM Corp. 2002, 2011
 *|
 *| US Government Users Restricted Rights - Use, duplication or
 *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *|
 *+------------------------------------------------------------------------+
*/


/*
	CMultipleIntervalPicker.js

	This script is used to provide interactivity for the
	multiple select selectInterval prompt component.  This component is
	a combination of the interval picker and a list box.
*/

//Constructor to create a checkboxlist component
//	oIntervalPicker: the interval picker control
//	oLstChoices: the name of the select form control
//	oSubmit: the form control to submit selections to the server
//  bRequired: is this a required field true/false
//  sSubmitType: submit this as a standard form, or as XML
//  oErrorFeedback: object used to provide validation feedback
//  oSizer: image object used to control the minimum size of the list
//  oInsertButton: the insert button
//  oRemoveButton: the remove button

function CMultipleIntervalPicker(oIntervalPicker, oLstChoices, oSubmit, bRequired, sSubmitType, oErrorFeedback, oSizer, oInsertButton, oRemoveButton, sCVId)
{
	this.setCVId(sCVId);
	this.m_oIntervalPicker = oIntervalPicker;
	this.m_oLstChoices = oLstChoices;
	this.m_oSubmit = oSubmit;

	this.m_bRequired = bRequired;
	this.m_bValid = false;
	this.m_sSubmitType = sSubmitType;
	this.m_oErrorFeedback = oErrorFeedback;

	if (oInsertButton)
	{
		this.m_oInsertButton = oInsertButton;
	}

	if (oRemoveButton)
	{
		this.m_oRemoveButton = oRemoveButton;
	}

	this.checkData();
	//resize the list if necessary
	if(oSizer)
	{
		this.m_oSizer = oSizer;
		this.resizeList();
	}
	//check the states of the insert and remove buttons
	this.checkInsertRemove();
}

CMultipleIntervalPicker.prototype = new CPromptControl();

//add an item to the list without any checking by the control
//this method provides an efficient way to add items, but
//the update() method should be called to clean up the control
//when finished adding
function CMultipleIntervalPicker_addNoUpdate(sDisplayValue, sInsertText, sel)
{
	sDisplayValue = sDecodeU003( sDisplayValue );
	sInsertText = sDecodeU003( sInsertText );
	if (sel == true) {
		this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sDisplayValue, sInsertText, true, true);
	}
	else {
		this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sDisplayValue, sInsertText, false, false);
	}
}

//clean up the control
function CMultipleIntervalPicker_update()
{
	this.checkData();
	this.resizeList();
	this.resizeList();
	//check the states of the insert and remove buttons
	this.checkInsertRemove();
}

//select all items
function CMultipleIntervalPicker_selectAll()
{
	for (var i=0; i < this.m_oLstChoices.options.length; i++)
	{
		this.m_oLstChoices.options[i].selected =  true;
	}
	this.checkData();
	//check the states of the insert and remove buttons
	this.checkInsertRemove();
}

//remove selection from all items
function CMultipleIntervalPicker_deSelectAll()
{
	for (var i=0; i < this.m_oLstChoices.options.length; i++)
	{
		this.m_oLstChoices.options[i].selected =  false;
	}
	this.checkData();
	//check the states of the insert and remove buttons
	this.checkInsertRemove();
}

//insert values from the picker control
function CMultipleIntervalPicker_insert()
{
	var sInsertInterval = this.m_oIntervalPicker.sGetInterval();
	var sInsertFormatInterval = this.m_oIntervalPicker.sGetFormatInterval();
	this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sInsertFormatInterval, sInsertInterval, false, false);
	this.checkData();
	this.resizeList();
	this.resizeList();
}

//remove selections from the list of choices
function CMultipleIntervalPicker_remove()
{
	this.removeSelectedChoices();
}

//perform any special processing for the server.
//this function will wrap all list items in XML.
//This is required for cases where the unselected items need
//to be submitted too.
function CMultipleIntervalPicker_preProcess()
{
	var i=0;
	if (this.m_sSubmitType == K_PRMT_sXML)
	{
		var sURLValues = K_PRMT_sEMPTY;
		if (this.m_oLstChoices.options.length > 0)
		{
			for(i = 0; i < this.m_oLstChoices.options.length; i ++)
			{
				sURLValues += '<selectOption';
				sURLValues += ' displayValue="' + sXmlEncode(this.m_oLstChoices.options[i].text) +'"';
				sURLValues += ' useValue="' + sXmlEncode(this.m_oLstChoices.options[i].value) + '"';
				if (this.m_oLstChoices.options[i].selected == true)
				{
					sURLValues += ' selected="true" />';
				}
				else
				{
					sURLValues += ' selected="false" />';
				}
			}
		}
		addSelectChoices(this.m_oSubmit, sURLValues);
	}
	else
	{
		//select all the choices so they can be submitted with the form
		for(i = 0; i < this.m_oLstChoices.options.length; i ++)
		{
			this.m_oLstChoices.options[i].selected = true;
		}
	}
}


//return true unless this is a required prompt
//and there are no values
function CMultipleIntervalPicker_checkData()
{
	if ((this.m_bRequired == true) && (this.m_oLstChoices.options.length === 0))
	{
		this.m_bValid = false;
		this.checkFail();
		return false;
	}
	else
	{
		this.m_bValid = true;
		this.checkPass();
		return true;
	}
}

//make sure that choices are always visible and set a minimum size
//resize the list to fit the data if required
//if there are no items, prevent the list from
//shrinking too small
//allow the list to expand to fit the biggest item
function CMultipleIntervalPicker_resizeList()
{
	if (this.m_oSizer)
	{
		this.m_oLstChoices.style.width='auto';
		if (this.m_oSizer.width < 200)
		{
			this.m_oLstChoices.style.width='200px';
		}
	}
}

//enable and disable the insert and remove buttons
//based on what the user has selected
function CMultipleIntervalPicker_checkInsertRemove()
{
	if (this.m_oInsertButton)
	{
		if ((this.m_oIntervalPicker.getValid() == true) && this.m_oIntervalPicker.hasValue())
		{
			this.m_oInsertButton.disabled = false;
		}
		else
		{
			this.m_oInsertButton.disabled = true;
			this.m_oInsertButton.className = "clsInsertRemoveButton";
		}
	}

	if (this.m_oRemoveButton)
	{
		if (this.m_oLstChoices.selectedIndex == -1)
		{
			this.m_oRemoveButton.disabled = true;
			this.m_oRemoveButton.className = "clsInsertRemoveButton";
		}
		else
		{
			this.m_oRemoveButton.disabled = false;
		}
	}
}

CMultipleIntervalPicker.prototype.addNoUpdate = CMultipleIntervalPicker_addNoUpdate;
CMultipleIntervalPicker.prototype.update = CMultipleIntervalPicker_update;
CMultipleIntervalPicker.prototype.selectAll = CMultipleIntervalPicker_selectAll;
CMultipleIntervalPicker.prototype.deSelectAll = CMultipleIntervalPicker_deSelectAll;
CMultipleIntervalPicker.prototype.insert = CMultipleIntervalPicker_insert;
CMultipleIntervalPicker.prototype.remove = CMultipleIntervalPicker_remove;
CMultipleIntervalPicker.prototype.preProcess = CMultipleIntervalPicker_preProcess;
CMultipleIntervalPicker.prototype.checkData = CMultipleIntervalPicker_checkData;
CMultipleIntervalPicker.prototype.resizeList = CMultipleIntervalPicker_resizeList;
CMultipleIntervalPicker.prototype.checkInsertRemove = CMultipleIntervalPicker_checkInsertRemove;