﻿// JScript File for ListBoxSelectLong user control

// selects the items from multi-select box
function SetLbslSelected(controlPrefix, emptyText, validatorControlID, separator)
{
    var lstSel = $get(controlPrefix + 'lstSel');
    if (lstSel == null) return;
    
    var lblSelDesc = $get(controlPrefix + 'lblSelDesc');
    var hSelIds = $get(controlPrefix + 'hSelIds');

    var hHlIds = $get(controlPrefix + 'hHlIds');
    var hHlClass = $get(controlPrefix + 'hHlClass');

	lblSelDesc.innerHTML = emptyText;
	hSelIds.value = '';
	
	var firstTime = true;
	
	// Update the Description and hidden Id field...
	for (i=0; i < lstSel.options.length; i++)
	{
		if (lstSel.options[i].selected)
		{
		    if (firstTime)
		        lblSelDesc.innerHTML = '';
		    else
		    {
			    lblSelDesc.innerHTML += ', ';
			    hSelIds.value += separator;
		    }

            // highlight?
	        var isHighlight = false;
	        
		    if (hHlIds.value != '')
		    {
		        var hIds = hHlIds.value.split(separator);
		        
	        	for (j=0; j < hIds.length; j++)
	        	{
	        	    if (lstSel.options[i].value == hIds[j]) 
	        	    {
	        	        isHighlight = true;
	        	        break;
	        	    }
	        	}
		    }
            
            if (isHighlight)
			    lblSelDesc.innerHTML += '<span class="' + hHlClass.value + '">' + lstSel.options[i].text + '</span>';
			else
			    lblSelDesc.innerHTML += lstSel.options[i].text;
			    			
			hSelIds.value += lstSel.options[i].value;
			
			firstTime = false;
		}
	}
	
	if (validatorControlID != '')
	    $get(validatorControlID).innerHTML = '';
}

// Clears selection
function SetLbslClear(lstSelId)
{
    var lstSel = $get(lstSelId);
    
	for (var i=0; i<lstSel.options.length; i++)
		lstSel.options[i].selected = false;
}

// Cancels selection
function SetLbslCancel(lstSelId, hSelIdsId, separator)
{
    var lstSel = $get(lstSelId);
    if (lstSel == null) return;
    
    var ids = $get(hSelIdsId).value.split(separator);

    // Reset what items are selected.
	for (i=0; i < lstSel.options.length; i++)
	{
	    // Default each item to false.
		lstSel.options[i].selected = false;
	    
	    // Do we have it our hidden list of selected?
	    for (j=0; j < ids.length; j++)
	    {
            if (lstSel.options[i].value == ids[j]) 
            {
                lstSel.options[i].selected = true;
                break;
            }
	    }
	}
}