function AddLinkedObject(objectToAddID, nextIndexFieldID, prefix)
{
    var NextIndexField = document.getElementById(nextIndexFieldID);
    var blankObject = document.getElementById("template."+objectToAddID);
    var RootName = objectToAddID.substr(objectToAddID, objectToAddID.length - "linkedobject".length);
    var insertBeforeRow = document.getElementById(RootName+"add.row");
    if (blankObject)
    {
        var Parent = getParent(insertBeforeRow);
        var newBlankObject = blankObject.cloneNode(true);
        replaceInAttributeValuesRecursive(newBlankObject, "template.", "");

        Parent.insertBefore(newBlankObject, insertBeforeRow);
        setSelectionsInClonedNode(blankObject, "template.");

        //-- change the index of the newly added blank object. This must be done after the selection fiddle is performed
        changeAddedObjectIndex(newBlankObject, NextIndexField.value, prefix);

    }

    var nextIndex = 1 ;
    nextIndex = nextIndex + Number(NextIndexField.value);
    NextIndexField.value = String(nextIndex);
}

function setSelectionsInClonedNode(originalNode, prefix)
{
    if (originalNode.tagName == 'SELECT')
    {
        var originalIdAttribute = originalNode.getAttribute("id")
        if (originalIdAttribute && startsWith(originalIdAttribute, prefix))
        {
            var newValue = originalIdAttribute.substr(prefix.length);
            var newNode = document.getElementById(newValue);
            if (newNode)
            {
                newNode.selectedIndex = originalNode.selectedIndex;
            }
        }
    }
    else
    {
    
        var children = getChildren(originalNode);
        if (children)
        {
            var numChildren = children.length;
            var childIdx;
            for (childIdx=0; childIdx < numChildren; childIdx++)
            {
                var originalChild = children[childIdx];
                
                setSelectionsInClonedNode(originalChild, prefix);
            }
        }
    }
}

function Test(String)
{
    alert(String);
}

function RemoveObject(eventElement)
{

    var RootName = eventElement.id.substr(eventElement.id, eventElement.id.length - "remove".length);
    
    var ActionElement = document.getElementById(RootName+"action");
    var ObjectToHide = document.getElementById(RootName+"linkedobject");
    ActionElement.value="remove";
    ObjectToHide.style.display="none";
}


function changeAddedObjectIndex(newObject, nextIndex, prefix)
{
    var regularExpression = /\[0\]$/;
    var replacement = "["+nextIndex+"]";
    var newPrefix = prefix.replace(regularExpression, replacement);
 
    ChangeAddedObjectButtons(newObject, prefix);   
    replaceInAttributeValuesRecursive(newObject, prefix, newPrefix);   
}

function replaceInAttributeValuesRecursive(obj, oldSubstring, newSubstring)
{
    if (!isTextNode(obj))
    {
        //-- fiddle the attributes on this object
        replaceInAttributeValues(obj, oldSubstring, newSubstring);
        
        //-- and recurse through its children
        var children = getChildren(obj);
        if (children)
        {
            var numChildren = children.length;
            var childIdx;
            for (childIdx=0; childIdx < numChildren; childIdx++)
            {
                var child = children[childIdx];
                replaceInAttributeValuesRecursive(child, oldSubstring, newSubstring);
            }
        }
    }
 }

function displayDataObjectEditCalendar(rootPath, format, obj)
{
    var buttonName = obj.name;
    var editBoxName = replace(buttonName, ".calendar", "");
    displayCalendar(rootPath, editBoxName, format, obj);
}

function replaceInAttributeValues(obj, originalSubstring, newSubstring)
{
    var numAttributes = obj.attributes.length;
    var attributeIdx;
    for (attributeIdx = 0; attributeIdx < numAttributes; attributeIdx++)
    {
        //-- the attribute value can include embedded references to fields, for instance in displayCalendar in onclick.
        var attribute = obj.attributes[attributeIdx];
        if (attribute && attribute.value)
        {
            var newValue = replace(attribute.value, originalSubstring, newSubstring);
            if (newValue != attribute.value)
            {
                if (attribute.name == "onclick")
                {
//                      Test(newValue);
//                      obj.onclick = function() { Test("hello") };
                }
                else
                {
                    attribute.value = newValue;
                }
            }
        }
    }
}


function ChangeAddedObjectButtons(newObject, newPrefix)
{
    var removeButtonDiv = document.getElementById(newPrefix+".remove.div");
    
    removeButtonDiv.style.display = "";
 }

function expandGroup(id)
{
    var elem=document.getElementById(id);
    if (elem!=null)
    {
		elem.style.display="inline";
    }
}

function collapseGroup(id)
{
    var elem=document.getElementById(id);
    if (elem!=null)
    {
		elem.style.display="none";
    }
}
