/*-----------------------------------------------------------------------------
 * Provides specific helper functions for Query Module Pages.
 *
 * Modifications:
 * Date        By          Description
 * ----------  ----------  -------------------------------------------------
 * 05/03/2007  Garth       Initial version split from query/module html.xslt, 
 *                         site specific.xslt, and old selections.js files.
 *---------------------------------------------------------------------------*/


// function to check to see if cross1 and cross2 are set the same.  If
// so popup an alert notifying the user that they are both the same.
function isGroupByDimensionsSame()
{
	var rowGroupByDimensionValue = (new NamedInputElement("_RowGroupByDimensionName")).getValue();
	var colGroupByDimensionValue = (new NamedInputElement("_ColumnGroupByDimensionName")).getValue();
	if((rowGroupByDimensionValue != null) && (rowGroupByDimensionValue == colGroupByDimensionValue))
	{
		alert
		(
			"--Duplicate Row/Column Grouping Selections Notice--\n" + 
			"\nBoth the row and column grouping selections" +
			"\nare set to the same value.  This will work but" +
			"\nprobably will not produce the desired results."+
			"\nIt is recommended that you change one of the"+
			"\nselections before submitting the query." 
		);
		return(true);
	}		
	return(false);
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// clear all sections selectinos if not part of a selected selection set.  This
// function loops through all major "selections" sets (first question of the 
// step) and calls the clear non selected for that root selection.
function clearAllNonSelectedSelections()
{
	var selections;
	var count = 1;
	do
	{
		selections = document.getElementById(this.selectionsIDPrefix + count);
		if(selections != null) clearNonSelectedSelections(selections, true);
		count++;
	} while(selections != null);

} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// recursive method that starts with the current selections (question) container
// and drills down the tree (via child nodes) to clear all answers that are not
// a part of a selected parent container.   The parameter "selectedFlag" is a 
// boolean that controls whether the current parent element/object being looped 
// through is selected.  If not selected then this is the flag that says clear
// all the siblings.  The only type of selector objects that this works for is
// radio and checkbox type selectors since these are the user interface 
// mechansims used to activate sub levels.
function clearNonSelectedSelections(selections, selectedFlag)
{
	if((selections != null) && (selections.childNodes != null)) 
	{
		// Loop for all selection elements within the selections container.
		for(var i=0; i<selections.childNodes.length; i++)
			if((selections.childNodes[i].id != null) && (selections.childNodes[i].id.indexOf(this.selectionIDPrefix) != -1))
			{
				var selector = document.getElementById( selections.childNodes[i].id.replace(this.selectionIDPrefix, SELECTOR_ID_PREFIX) );
				if(selector != null)
				{
					if(isCheckable(selector))	// radio or checkbox
					{
						// if container not selected then clear the checked value!!!
						if(selectedFlag == false) selector.checked = false;

						// now setup the recursive call for any sub selections...
						var subSelectionsID = selections.childNodes[i].id.replace(this.selectionIDPrefix, this.selectionsIDPrefix);
						var subSelections   = document.getElementById(subSelectionsID);

						// if other questions then walk tree and clear.
						// else end leaf node, add to query strings if selected.
						if(subSelections != null)
						{
							clearNonSelectedSelections(subSelections, selector.checked);
// this works in cleaning up the actual imput items posted.  however, it messes
// up the ability to do a browser back and see what was selected / make mods and 
// resubmit!!!  Put in and commented out 3-27-04 because there's not way around this 
// to work!!!
//selectedFlag.checked = false;							
						}
					}
					
					// else if this group is not a selections candidate AND the
					// current selection element is not part of a selected 
					// selection then clear the values...
					else if(selectedFlag == false)
					{
						if((selector.type == "select-multiple") || (selector.type == "select-one"))
							selector.selectedIndex = -1;
						else if(selector.type == "text")
							selector.value = "";
					}
					// had some code in here to handle leaf node selection lists.
					// commented out for some reason??? removed 5/26/04.

				} //----- ends if a valid input object...
			} //----- ends if a answer div container...
	}
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~


// called by the submit query button.  Need to clear all the non
// selected answers so that those parameters are NOT submitted to 
// the CGI app.
function submitQuery()
{
	if(isGroupByDimensionsSame()) return;	// can't return null or false...
	selections.clearAllNonSelectedSelections();

	// if here, then submit the page's form contents... 
	document.form.submit();
} //~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End of Function ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/*=============================== End of File ================================*/


