﻿// EEE: Enhanced Entity Editor
String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}



function EEE_GetHighLightedItemID(ClientID) {
    return document.getElementById(ClientID + '_HighLightContainer').value;
}

function EEE_GetHighlightedItem(ClientID) {
    var lID = document.getElementById(ClientID + '_HighLightContainer').value;
    if (lID.length > 0)
        return document.getElementById(lID);
    else
        return null;
}

function EEE_SetHighLightedItemID(ClientID, val) {
    document.getElementById(ClientID + '_HighLightContainer').value = val;
}

//hidden field that contains the string representation of the new items
function EEE_Hidden_Value(pControlId) {
    return document.getElementById(pControlId + '_EntryHolder');
}

//input field that is used to define a new item
function EEE_Input_NewItem(pControlId) {
    return document.getElementById(pControlId + '_InputNew');
}

function EEE_ItemHolder(pControlId) {
    return document.getElementById(pControlId + '_EE_List');
}

function EEE_DeleteButton(pControlId) {
    return document.getElementById(pControlId + '_DeleteButton');
}

function EEE_AddButton(pControlId) {
    return document.getElementById(pControlId + '_AddButton');
}

function EEE_DisposedItemHolder(pControlId) {
    return document.getElementById(pControlId + '_EE_Trash');
}

function EEE_ItemClicked(item, ControlID) {
    var currentID = GetHighLightedItemID(ControlID);

    if (currentID.length > 0 && document.getElementById(currentID) != null)
        document.getElementById(currentID).className = "deselecteditem";
    item.className = "selecteditem"
    EEE_SwitchToEdit(ControlID, item);
    SetHighLightedItemID(ControlID, item.getAttribute('id'));
}

//Remove the highlighted item from the list of selected items
function EEE_DeleteSelected(ControlID) {
    var item = EEE_GetHighlightedItem(ControlID);
    
    var value = item.getAttribute("value");
    var text = item.innerHTML;
    
    item.parentNode.removeChild(item);
    EEE_InitializeValue(ControlID);
    EEE_SwitchToInsert(ControlID);
}

//handle a new item insertion
function EEE_Input_NewItem_Clicked(ControlID) {


    var theField = EEE_Input_NewItem(ControlID);
    
    if (theField.value.trim().length == 0)
        return;
    var theList = EEE_ItemHolder(ControlID);    
    var theValue = theField.value;
    theField.value = '';
    var newItem
    if (EEE_SelectedItem[ControlID] == null) {
        newItem = document.createElement("LI");
        newItem.id = ControlID + "newItem_" + Date.parse(new Date());
        var newSubItem = document.createElement("DIV");
        newSubItem.className = "selectableItemHolder";
        newItem.appendChild(newSubItem);        
    }
    else {
        newItem = EEE_SelectedItem[ControlID];
    }
    if (newItem.childNodes.length > 0) {
        var valueItem = EEE_GetFirstNonTextChild(newItem);
        if( valueItem != null )
            valueItem.innerHTML = theValue;
    }
    newItem.className = "selecteditem unselectable";    
    newItem.onselectstart =  function(event) { return false; };
    newItem.onclick =  function(event) { EEE_ItemClicked(newItem, ControlID); };
    if (EEE_SelectedItem[ControlID] == null)
        EEE_insertAtSortedPosition(theList, newItem);
    EEE_SelectItem(ControlID, newItem);
    EEE_InitializeValue(ControlID);
    EEE_SwitchToInsert(ControlID);
}

function EEE_CancelEdit_Clicked(ControlId) {
    EEE_SwitchToInsert(ControlId);
}

function EEE_SelectItem(ControlID, item) {
    if (EEE_GetHighlightedItem(ControlID) != null )
        EEE_GetHighlightedItem(ControlID).className = "deselecteditem";
    item.className = "selecteditem";    
    EEE_SetHighLightedItemID(ControlID, item.id);    
}

function EEE_DeSelectAll(ControlID) {
     if (EEE_GetHighlightedItem(ControlID) != null )
        EEE_GetHighlightedItem(ControlID).className = "deselecteditem";
     EEE_SetHighLightedItemID(ControlID, '');
 }


//inserts theOption into the childrenCollection of theList on the alphabetically correct position so sorting is not needed.
function EEE_insertAtSortedPosition(theList, theOption) {
    var inserted = false;    
    var i = 0;
    for (i = 0; i < theList.childNodes.length; i++) {
        if (isTextNode(theList.childNodes[i]))
            continue;
        if (theOption.innerHTML.toLowerCase() < theList.childNodes[i].innerHTML.toLowerCase()) {
            theList.insertBefore(theOption, theList.childNodes[i]);
            inserted = true;
            break;
        }
    }
    if (!inserted) {
        theList.appendChild(theOption);
    }
}

//loop through the items in the list and concat the values, save the concatenation in the hidden valuefield.
function EEE_InitializeValue(ControlId) {
    var lHolder = document.getElementById(ControlId + '_EE_List');    
    var i = 0;
    var lValue = "-*-";
    for (var i = 0; i < lHolder.childNodes.length; i++) {
        var lNode = lHolder.childNodes[i];
        if (isTextNode(lNode) )
            continue;
        var firstChild = EEE_GetFirstNonTextChild(lNode)
        if ( firstChild != null) {
            lValue += firstChild.innerHTML.trim() + "-*-";            
        }
    }    
    EEE_Hidden_Value(ControlId).value = lValue;
}



//an array of arrays, will hold an array of values for every instance of the EEE control on the page
var EEE_SelectedItem = Array();

//configure the form to be in EditMode
function EEE_SwitchToEdit(ControlID, selectedItem) {
    var valueItem = EEE_GetFirstNonTextChild(selectedItem)
    
    EEE_Input_NewItem(ControlID).value = valueItem.innerHTML.trim();
    
    EEE_SelectedItem[ControlID] = selectedItem;
    EEE_ButtonVisible(true, '_DeleteButtonHolder', ControlID);
    EEE_ButtonVisible(true, '_CancelButtonHolder', ControlID);    
    EEE_AddButton(ControlID).innerHTML = "Wijzigen";
}

//configure the form to be in InsertMode
function EEE_SwitchToInsert(ControlID) {
    EEE_Input_NewItem(ControlID).value = "";
    EEE_SelectedItem[ControlID] = null;
    EEE_ButtonVisible(false, '_DeleteButtonHolder', ControlID);
    EEE_ButtonVisible(false, '_CancelButtonHolder', ControlID);    
    EEE_AddButton(ControlID).innerHTML = "Toevoegen";
    EEE_DeSelectAll(ControlID);
}

function EEE_ButtonVisible(pVisible, pButtonIdSuffix, pControlId) {
    var el = document.getElementById(pControlId + pButtonIdSuffix);
    el.style.display = (pVisible ? "block" : "none");
}

function EEE_GetFirstNonTextChild(item) {
    var i = 0;
    for (i = 0; i < item.childNodes.length; i++) {
        if( !isTextNode(item.childNodes[i]) )
            return item.childNodes[i]
    }

    return null;
        
    
}


