﻿//----------------------------------
//returns an object if supplied or
//gets the object if an id is given
//----------------------------------
function GetObject(o)
{
    if (typeof(o) == 'object')
    {
        return o;
    }
    else
    {
        return document.getElementById(o);
    }
}
//----------------------------------
//shows an object 
//----------------------------------
function Show(o)
{
    var obj = GetObject(o);
    obj.style.display = 'block';
}
//----------------------------------
//hides an object 
//----------------------------------
function Hide(o)
{
    var obj = GetObject(o);
    obj.style.display = 'none';
}
//----------------------------------
//hides an object only if the object is found
//----------------------------------
function TryHide(o)
{
    var obj = GetObject(o);
    if (obj!=null)
        Hide(obj);
}
//----------------------------------
//shows/hides the specified object/id
//----------------------------------
function ConfigureVisibility(o, isShow)
{
    if(isShow)
        Show(o);
    else
        Hide(o);
}
//----------------------------------
//hides all of the child elements
//within an object
//----------------------------------
function HideAll(o)
{
    var obj = GetObject(o);
    for (var index=0;index<obj.childNodes.length;index++)
	{
	    try {Hide(obj.childNodes[index].id);} catch(e) {}
	}
}
//----------------------------------
//deactivates all the child elements 
//within an object by removing the
//class name
//----------------------------------
function DeactivateAll(o)
{
    var obj = GetObject(o);
    for (var index=0;index<obj.childNodes.length;index++)
	{
	    obj.childNodes[index].className = '';
	}
}
//----------------------------------
//checks if object has objects collection
//----------------------------------
function IsOptionsValid(obj)
{
    if(obj != null && obj.options != null)
    {
        return true;
    }
    return false;
}
//----------------------------------
//removes all options from select
//----------------------------------
function ClearOptions(from)
{
    if(!IsOptionsValid(from))
    {return;}
    
    for(var index=(from.options.length-1);index>=0;index--)
    {from.options[index] = null;}
    from.selectedIndex = -1;
}
//----------------------------------
//sorts the select box by string
//----------------------------------
function SortSelect(obj)
{
    var o = new Array();
    if(!IsOptionsValid(obj))
        {return;}
    
    for(var index=0;index<obj.options.length;index++)
    {
        o[o.length] = new Option
                        (obj.options[index].text, obj.options[index].value, 
                           obj.options[index].defaultSelected, obj.options[index].selected);
    }

    if(o.length==0)
        {return;}
    
    o = o.sort(
                function(a,b)
                {
                    if((a.text+"") < (b.text+""))
                        {return -1;}
                        
                    if((a.text+"") >(b.text+""))
                        {return 1;}
                return 0;
                }
              );
    
    for(var index=0;index<o.length;index++)
    {
        obj.options[index] = new Option(o[index].text, o[index].value, o[index].defaultSelected, o[index].selected);
    }
}
//----------------------------------
//moves selected items from source to target
//----------------------------------
function MoveSelectedItems(sourceListId, targetListId, isSortTarget)
{
    var sourceList = document.getElementById(sourceListId);
    var targetList = document.getElementById(targetListId);
    
    if (sourceList == null || targetList == null)
        return;
        
    var sourceOptions = sourceList.options;
    var targetOptions = targetList.options;
    
    var isItemsMoved = false;
    var index = 0;
    while (index < sourceOptions.length)
    {
        var curOption = sourceOptions[index];
        if (curOption.selected)
        {
            var newOption = new Option(curOption.text, curOption.value, false, false);
            targetOptions.add(newOption);
            //removes item from original collection
            sourceList.remove(index);
            isItemsMoved = true;
        }
        else
        {
            index++;
        }
    }//end for

    //sorts the target list if at least one item has moved
    if (isItemsMoved && isSortTarget)
        SortSelect(targetList);
}//end function
//----------------------------------
//sets the class of an element
//----------------------------------
function SetClass(o, className)
{
    var obj = GetObject(o);
    obj.className = className;
}
//------------------------------------------------------------------------
//----------------------------------
//gets the specified drop down control selected value
//----------------------------------  
function GetDropDownSelectedValue(o)
{
    var cmb = GetObject(o);
    if(cmb==null)
        return;
        
    var selectedIndex = cmb.selectedIndex;
    if(selectedIndex < 0)
        return '';
        
    var selectedItem = cmb.options[selectedIndex];
    var selectedValue = selectedItem.value;
    
    return selectedValue;
}