﻿/**
* Handler when the control tells us that it thinks it should be closed.
* We want to deny all of it's requests unless we call this method manually (see following method)
*/
var cwMSOverrideDDL = false;
function cwMultiSelectDDLPropagation(item, eventArgs)
{
	// Stops Propagation
	cwUpdateMultiSelectDDLLabel(item);

	var temp = cwMSOverrideDDL;
	cwMSOverrideDDL = false;

	// new prom. way (instead of returning false)
	eventArgs.set_cancel(!temp);
   
	return temp;
}

/**
* Handler to propogate control and close the drop-down portion of a multi-select drop down
*/
function cwMultiSelectDDLClosing(item, eventArgs) 
{
    // Stops Propagation
    cwUpdateMultiSelectDDLLabel(item);

    var temp = cwMSOverrideDDL;
    cwMSOverrideDDL = false;

    // new prom. way (instead of returning false)
    eventArgs.set_cancel(temp);
    
    return temp;
}

/**
* Manually closes the drop-down portion of a multi-select drop down
*/
function cwCloseMultiSelectDDL(combobox)
{
	cwMSOverrideDDL = true;
	combobox.hideDropDown();
	cwMSOverrideDDL = false;
	combobox.set_text(combobox.get_text() + " ");
}

/**
* Changes the text displayed in a multi-select drop down box
*/
function cwUpdateMultiSelectDDLLabel(combobox)
{
	var temp = "";

	// Since we're using item templates which have checkboxes, the RAD control knows nothing about the selection of them
	// So what we REALLY need to do is look at the DOM nodes of each of the row items, find it's checkbox, and see if it
	// is actually selected. If you try to look at combobox.Items.Selected for each item, it will always be 0 no matter what you select.
	var selectedCount = 0;
	var lastSelectedItemText = "";

	if (combobox.get_items)
	{
		var Items = combobox.get_items();

		for (var i = 0; i < Items.get_count(); i++)
		{
			//cwDebugObjectInNewWindow(combobox._element); fvAlert( "index is " + Items.getItem(i).get_index() + " [" + combobox + "]" );
			var obj = document.getElementById(combobox._element.id + "_i" + Items.getItem(i).get_index() + "_" + Items.getItem(i).get_text());
			if (obj)
			{
				if (obj.checked)
				{
					selectedCount++;
					lastSelectedItemText = Items.getItem(i).get_text();
				}
			}
		}

		if (selectedCount == 0)
		{
		    // show 'no items selected'
		    combobox.set_text(getDefaultText());
		}
		else
		{
			// show 'multiple items selected'
			combobox.set_text("(" + selectedCount + ") " + getSelectedText());
		}
	}
}