﻿    //Controls in the entityselector have a prefix (usually 'ControlID' or sin their ID 
    //because they can exist more than once on a single page    
    var EntitySelector_InitHooks = Array();
    
    //IMPORTANT: utilize the OnloadSpawner to initialize every entityselector instance that is known to exist on the page
    onloadAdd(EntitySelector_DoInitialValues);
    
    function GetControl(ControlID , name) {    
        var lTrueID = ControlID + '_' + name        
        return document.getElementById(ControlID + '_' + name);    
    }
    
    function GetStoredValue(ControlID) {        
        return GetControl(ControlID, "ValueHolder").value;
    }
    
    function SetStoredValue(ControlID, pValue) {
        GetControl(ControlID, "ValueHolder").value = pValue;
    }
    
    function GetLeftSelectorControl(ControlID) {
        return GetControl(ControlID, "LeftSelector")
    }
    
    function GetRightSelectorControl(ControlID) {
        return GetControl(ControlID, "RightSelector")
    }
    
    function SelectedSelectItem(pSelectBox) {
        return pSelectBox.options[pSelectBox.selectedIndex];
    }

    function GetHighLightedItemID(ClientID) {
        return document.getElementById(ClientID + '_HighLightContainer').value;
    }

    function GetHighlightedItem(ClientID) {
        var lID = document.getElementById(ClientID + '_HighLightContainer').value;
        if (lID.length > 0)
            return document.getElementById(lID);
        else
            return null;
    }

    function SetHighLightedItemID(ClientID, val) {
        document.getElementById(ClientID + '_HighLightContainer').value = val;
    }

    function ItemDoubleClicked(item, ControlID, theID) {
        var right = GetRightSelectorControl(ControlID);
        var left = GetLeftSelectorControl(ControlID);
        var toBeSelected = item.parentNode == left;
        var targetList = toBeSelected ? right : left;        
        insertAtSortedPosition(targetList, item);                
        if( toBeSelected ) { 
            theID = StoredValueFor(theID);
            var val = GetStoredValue(ControlID);    
            if( ! val.indexOf(theID) > -1 )
                SetStoredValue(ControlID, GetStoredValue(ControlID) + theID);
        }
        else {
            theID = StoredValueFor(theID);
            var val = GetStoredValue(ControlID);            
            SetStoredValue(ControlID, val.replace(theID, ''));
        }
    }

    //inserts an option into an unsortedlist (UL) at the alphabetically correct position so that there's no need to sort
    function insertAtSortedPosition(theList, theOption) {
        var inserted = false;        
        var i = 0;
        for (i = 0; i < theList.childNodes.length; i++) {
            //check the nodes, for firefox compatability, the nodes can NEVER be textnodes
            if (isTextNode(theList.childNodes[i]) || isTextNode(theOption) )
                continue;
            if (theOption.innerHTML.toLowerCase() < theList.childNodes[i].innerHTML.toLowerCase()) {
                theList.insertBefore(theOption, theList.childNodes[i]);
                inserted = true;
                break;
            }
        }
        if (!inserted) {
            theList.appendChild(theOption);
        }
    }

    function ItemClicked(item, ControlID) {
        var currentID = GetHighLightedItemID(ControlID);
        
        if (currentID.length > 0) 
            document.getElementById(currentID).className = "deselecteditem";
        item.className = "selecteditem"
        SetHighLightedItemID(ControlID, item.getAttribute('id'));
    }        
    
    function StoredValueFor(pId) {
        return ";" + pId + ";";
    }   
    
    function AlertStoredValue(pControlID) {
        alert(GetStoredValue(pControlID));
    }

    //VERY IMPORTANT
    //for each instance of the entityselector that exists in the InitHooks array, initialize it's stored state
    function EntitySelector_DoInitialValues() {
        var i = 0;
        for( i=0; i < EntitySelector_InitHooks.length; i++ ) {
            MoveInitialValues(EntitySelector_InitHooks[i]);
        }
    }

    //loop through the items in the left list, compare the ID's with the ID's that exist in the Value
    //if the ID exists in the Value then append the item to the right list.
    function MoveInitialValues(pControlID) {
        var lRightSelector = GetRightSelectorControl(pControlID); 
        var lLeftSelector = GetLeftSelectorControl(pControlID);
        var lStoredValue = GetStoredValue(pControlID);
        var i = 0;               
        for (i = lLeftSelector.childNodes.length - 1; i >= 0; i--) {
            if( isTextNode(lLeftSelector.childNodes[i]) )
                continue;
            var lValue = StoredValueFor(lLeftSelector.childNodes[i].getAttribute('value'))
            if (lStoredValue.indexOf(lValue) > -1 && lValue.length > 0) {
                insertAtSortedPosition(lRightSelector, lLeftSelector.childNodes[i]);                    
            }
        }
    }

    function selectHighlighted(controlID) {
        MoveHighlighted(controlID, GetLeftSelectorControl(controlID));
    }

    function deselectHighlighted(controlID) {
        MoveHighlighted(controlID, GetRightSelectorControl(controlID));
    }

    //fromList dictates the parentnode of the selected control
    function MoveHighlighted(controlID, fromList) {
        var lCtrl = GetHighlightedItem(controlID)        
        if( lCtrl != null && lCtrl.parentNode == fromList) 
            lCtrl.ondblclick();       
    }

    function selectAll(pControlID) {
        var lRightSelector = GetRightSelectorControl(pControlID);
        var lLeftSelector = GetLeftSelectorControl(pControlID);
        while (lLeftSelector.childNodes.length > 0) {            
            insertAtSortedPosition(lRightSelector, lLeftSelector.childNodes[0]);            
        }
    }

    function deselectAll(pControlID) {
        var lRightSelector = GetRightSelectorControl(pControlID);
        var lLeftSelector = GetLeftSelectorControl(pControlID);
        while (lRightSelector.childNodes.length > 0) {
            insertAtSortedPosition(lLeftSelector, lRightSelector.childNodes[0]);
        }
    }

