function createNode(nodeName)
{
    return document.createElement(nodeName);
}

function createImage(src, width, height, alt, title, border, id, className,
    verticalAlign, cursor, onclick)
{
	var image = document.createElement("img");

	image.src = src;
	image.width = width;
	image.height = height;
    image.alt = alt;
    image.title = title;
	image.border = border;
    image.id = id;
    image.className = className;
    image.onclick = onclick;
    image.style.verticalAlign = verticalAlign;
    image.style.cursor = cursor;

	return image;
}


function createLink(linkURL, linkTarget, linkClass, linkObject)
{
	var link = document.createElement("a");
	link.href = linkURL;
	link.target = linkTarget;
	if (linkClass != "")
		link.className = linkClass;
	link.appendChild(linkObject);

	return link;
}


function createAnchor(anchorName, anchorObject)
{
	var linkObject = document.createElement("a");
	linkObject.name = anchorName;
	linkObject.id = anchorName;
	linkObject.appendChild(anchorObject);

	return linkObject;
}


function createText(text)
{
	return document.createTextNode(text);
}


function createInputField(fieldName, fieldID, fieldValue, fieldSize, maxLength,
    className)
{
	var field = document.createElement("input");

	field.type = "text";
	field.name = fieldName;
	field.id = fieldID != null ? fieldID : fieldName;
	field.value = fieldValue;
	field.size = fieldSize;
    field.className = className;

    if (maxLength != null)
    {
        field.maxLength = maxLength;
    }

	return field;
}


function createFileUploadField(fieldName, fieldValue, fieldSize)
{
	var field = document.createElement("input");

	field.type = "file";
	field.name = fieldName;
	field.id = fieldName;
	field.value = fieldValue;
	field.size = fieldSize;

	return field;
}


function createCheckBox(fieldName, fieldValue, checked)
{
	var field = document.createElement("input");

	field.type = "checkbox";
	field.name = fieldName;
	field.id = fieldName;
	field.value = fieldValue;
	field.checked = checked;

	return field;
}


function createSelectionField(fieldName, fieldSize, options)
{
	var field = document.createElement("select");

	field.name = fieldName;
	field.id = fieldName;
	field.size = fieldSize;

	for (var nextOptionID in options)
	{
		var nextOption = options[nextOptionID];

		var option = createSelectionOption(
            nextOption[0], nextOption[1], nextOption[2]);

        // add the created option to the cell
        field.add(option);
	}

	return field;
}


function createSelectionOption(optionValue, optionText, optionSelected)
{
    var option = document.createElement("option");

    option.value = optionValue;
    option.text = optionText;
    option.selected = optionSelected;

    return option;
}


function createHiddenField(fieldName, fieldValue)
{
	var hiddenField = document.createElement("input");

	hiddenField.type = "hidden";
	hiddenField.name = fieldName;
	hiddenField.id = fieldName;
	hiddenField.value = fieldValue;

	return hiddenField;
}


/**
 * Internal function to get a child node in the given parent node context.
 *
 * @param   parentNode  The context to search in.
 * @param   childNodeId The Id of the requested element.
 * @param   recursive   If the search should be recursive searching the child
 *                      nodes of child nodes.
 * @return  The child node if found, otherwise <code>null</code>.
 */

function getChildNodeById(parentNode, childNodeId, recursive)
{
    // nothing to do if the parent node is not there
    if (parentNode == null)
    {
        return null;
    }

    // the default search behavior is recursive
    if (recursive == null)
    {
        recursive = true;
    }

    // start with retrieving all child nodes of the specified parent node
    var childNodes = parentNode.childNodes;

    if (childNodes == null)
    {
        return null;
    }

    // check each child node if it is the requested node
    for (var i = 0; i < childNodes.length; i++)
    {
        // the next child node from the list
        var nextChildNode = childNodes[i];

        // compare the id of the child node from the list with the requested id
        if (nextChildNode.id == childNodeId)
        {
            // it is the requested node, so the search is over
            return nextChildNode;
        }

        // it it is not the requested but recursive search is turned on
        if (recursive)
        {
            // check the child nodes of the child node if there is a node
            // with the requested id
            var recursiveResult =
                getChildNodeById(nextChildNode, childNodeId, true);

            // check if the requested child node was found recursively
            if (recursiveResult != null)
            {
                // the node was found, so the search is over
                return recursiveResult;
            }
        }
    }

    // no child node was found with the requested id
    return null;
}


/**
 * Internal function to get a child node in the given parent node context.
 *
 * @param   parentNode  The context to search in.
 * @param   childNodeId The Id of the requested element.
 * @param   recursive   If the search should be recursive searching the child
 *                      nodes of child nodes.
 * @return  The child node if found, otherwise <code>null</code>.
 */

function searchChildNodeById(parentNode, childNodeId, recursive)
{
    // nothing to do if the parent node is not there
    if (parentNode == null)
    {
        return null;
    }

    // the default search behavior is recursive
    if (recursive == null)
    {
        recursive = true;
    }

    // start with retrieving all child nodes of the specified parent node
    var childNodes = parentNode.childNodes;

    if (childNodes == null)
    {
        return null;
    }

    // check each child node if it is the requested node
    for (var i = 0; i < childNodes.length; i++)
    {
        // the next child node from the list
        var nextChildNode = childNodes[i];

        if (nextChildNode.id != null)
        {
            // compare the id of the child node from the list with the requested
            if (String(nextChildNode.id).search(childNodeId) != -1)
            {
                // it is the requested node, so the search is over
                return nextChildNode;
            }
        }

        // it it is not the requested but recursive search is turned on
        if (recursive)
        {
            // check the child nodes of the child node if there is a node
            // with the requested id
            var recursiveResult =
                searchChildNodeById(nextChildNode, childNodeId, true);

            // check if the requested child node was found recursively
            if (recursiveResult != null)
            {
                // the node was found, so the search is over
                return recursiveResult;
            }
        }
    }

    // no child node was found with the requested id
    return null;
}


function searchChildNodesById(parentNode, childNodeId, recursive)
{
    var childNodesList = new Array();

    // nothing to do if the parent node is not there
    if (parentNode == null)
    {
        return childNodesList;
    }

    // the default search behavior is recursive
    if (recursive == null)
    {
        recursive = true;
    }

    // start with retrieving all child nodes of the specified parent node
    var childNodes = parentNode.childNodes;

    if (childNodes == null)
    {
        return childNodesList;
    }

    // check each child node if it is the requested node
    for (var i = 0; i < childNodes.length; i++)
    {
        // the next child node from the list
        var nextChildNode = childNodes[i];

        if (nextChildNode.id != null)
        {
            // compare the id of the child node from the list with the requested
            if (String(nextChildNode.id).search(childNodeId) != -1)
            {
                childNodesList.push(nextChildNode);
            }
        }

        // it it is not the requested but recursive search is turned on
        if (recursive)
        {
            // check the child nodes of the child node if there is a node
            // with the requested id
            var recursiveResult =
                searchChildNodesById(nextChildNode, childNodeId, true);

            // check if the requested child node was found recursively
            if (recursiveResult != null)
            {
                for (var j = 0; j < recursiveResult.length; j++)
                {
                    childNodesList.push(recursiveResult[j]);
                }
            }
        }
    }

    // no child node was found with the requested id
    return childNodesList;
}


function getChildNodeByName(parentNode, childNodeName, recursive)
{
    // nothing to do if the parent node is not there
    if (parentNode == null)
    {
        return null;
    }

    // the default search behavior is recursive
    if (recursive == null)
    {
        recursive = true;
    }

    // start with retrieving all child nodes of the specified parent node
    var childNodes = parentNode.childNodes;

    // check each child node if it is the requested node
    for (var i = 0; i < childNodes.length; i++)
    {
        // the next child node from the list
        var nextChildNode = childNodes[i];

        // compare the name of the child node from the list with the requested
        if (String(nextChildNode.name) == childNodeName)
        {
            // it is the requested node, so the search is over
            return nextChildNode;
        }

        // it it is not the requested but recursive search is turned on
        if (recursive)
        {
            // check the child nodes of the child node if there is a node
            // with the requested name
            var recursiveResult =
                getChildNodeByName(nextChildNode, childNodeName, true);

            // check if the requested child node was found recursively
            if (recursiveResult != null)
            {
                // the node was found, so the search is over
                return recursiveResult;
            }
        }
    }

    // no child node was found with the requested name
    return null;
}


function getChildNodeByTypeAndName(
    parentNode, nodeTypeName, nodeName, recursive)
{
    // nothing to do if the parent node is not there
    if (parentNode == null)
    {
        return null;
    }

    // the default search behavior is recursive
    if (recursive == null)
    {
        recursive = true;
    }

    // start with retrieving all child nodes of the specified parent node
    var childNodes = parentNode.childNodes;

    // check each child node if it is the requested node
    for (var i = 0; i < childNodes.length; i++)
    {
        // the next child node from the list
        var nextChildNode = childNodes[i];

        var nextChildNodeNodeTypeName =
            String(nextChildNode.nodeName).toLowerCase();
        var nextChildNodeName = String(nextChildNode.name);

        // compare the type and name of the child node from the list with the
        // requested type and name
        if (nextChildNodeNodeTypeName == nodeTypeName.toLowerCase() &&
            nextChildNodeName == nodeName)
        {
            // it is the requested node, so the search is over
            return nextChildNode;
        }

        // it it is not the requested but recursive search is turned on
        if (recursive)
        {
            // check the child nodes of the child node if there is a node
            var recursiveResult = getChildNodeByTypeAndName(
                nextChildNode, nodeTypeName, nodeName, true);

            // check if the requested child node was found recursively
            if (recursiveResult != null)
            {
                // the node was found, so the search is over
                return recursiveResult;
            }
        }
    }

    // no child node was found
    return null;
}


function getChildNodeByType(parentNode, nodeTypeName, recursive)
{
    // nothing to do if the parent node is not there
    if (parentNode == null)
    {
        return null;
    }

    // the default search behavior is recursive
    if (recursive == null)
    {
        recursive = true;
    }

    // start with retrieving all child nodes of the specified parent node
    var childNodes = parentNode.childNodes;

    // check each child node if it is the requested node
    for (var i = 0; i < childNodes.length; i++)
    {
        // the next child node from the list
        var nextChildNode = childNodes[i];

        var nextChildNodeNodeTypeName =
            String(nextChildNode.nodeName).toLowerCase();

        // compare the type of the child node from the list with the requested
        if (nextChildNodeNodeTypeName == nodeTypeName.toLowerCase())
        {
            // it is the requested node, so the search is over
            return nextChildNode;
        }

        // it it is not the requested but recursive search is turned on
        if (recursive)
        {
            // check the child nodes of the child node if there is a node
            var recursiveResult = getChildNodeByType(
                nextChildNode, nodeTypeName, true);

            // check if the requested child node was found recursively
            if (recursiveResult != null)
            {
                // the node was found, so the search is over
                return recursiveResult;
            }
        }
    }

    // no child node was found
    return null;
}


function getAllChildNodesByType(parentNode, nodeTypeName, recursive)
{
    // nothing to do if the parent node is not there
    if (parentNode == null)
    {
        return null;
    }

    // the default search behavior is recursive
    if (recursive == null)
    {
        recursive = true;
    }

    // the list to collect all child nodes
    var foundChildNodes = new Array();

    // start with retrieving all child nodes of the specified parent node
    var childNodes = parentNode.childNodes;

    // check each child node if it is a requested node
    for (var i = 0; i < childNodes.length; i++)
    {
        // the next child node from the list
        var nextChildNode = childNodes[i];

        var nextChildNodeNodeName =
            String(nextChildNode.nodeName).toLowerCase();

        // compare the type of the child node from the list with the requested
        if (nextChildNodeNodeName == nodeTypeName.toLowerCase())
        {
            // it is the requested node, add it
            foundChildNodes.push(nextChildNode);
        }

        // if recursive search is turned on
        if (recursive)
        {
            // check the child nodes of the child node if there is a node
            var recursiveResult = getAllChildNodesByType(
                nextChildNode, nodeTypeName, true);

            // add all recursivly found child nodes
            for (var j = 0; j < recursiveResult.length; j++)
            {
                foundChildNodes.push(recursiveResult[j]);
            }
        }
    }

    return foundChildNodes;
}


function removeChildNode(parentNode, childNodeId, recursive)
{
    // nothing to do if the parent node is not there
    if (parentNode == null)
    {
        return null;
    }

    // the default search behavior is recursive
    if (recursive == null)
    {
        recursive = true;
    }

    // start with retrieving all child nodes of the specified parent node
    var childNodes = parentNode.childNodes;

    // check each child node if it is the requested node
    for (var i = 0; i < childNodes.length; i++)
    {
        // the next child node from the list
        var nextChildNode = childNodes[i];

        // compare the id of the child node from the list with the requested id
        if (nextChildNode.id == childNodeId)
        {
            // it is the requested node, so remove it
            parentNode.removeChild(nextChildNode);

            // signal success
            return true;
        }

        // it it is not the requested but recursive search is turned on
        if (recursive)
        {
            // check the child nodes of the child node if there is a node
            // with the requested id
            var recursiveResult =
                removeChildNode(nextChildNode, childNodeId, true);

            // check if the requested child node was found recursively
            if (recursiveResult)
            {
                // the node was found, so the search is over
                return recursiveResult;
            }
        }
    }

    // signal failure to remove the child node
    return false;
}


function removeAllChildNodes(parentNode)
{
    if (parentNode == null)
    {
        return false;
    }

    // start with retrieving all child nodes of the specified parent node
    var childNodes = parentNode.childNodes;

    if (childNodes == null)
    {
        return false;
    }

    // remove all child nodes, start with the last one
    for (var i = childNodes.length - 1; i >= 0; i--)
    {
        parentNode.removeChild(childNodes[i]);
    }

    return true;
}


function copyAllChildNodes(sourceNode, targetNode, replace, remove)
{
    if (sourceNode != null && targetNode != null)
    {
        if (replace)
        {
            if (! removeAllChildNodes(targetNode))
            {
                return false;
            }
        }

        var sourceChildNodes = sourceNode.childNodes;

        if (sourceChildNodes != null)
        {
            for (var i = 0; i < sourceChildNodes.length; i++)
            {
                targetNode.appendChild(sourceChildNodes[0]);
            }
        }

        if (remove)
        {
            if (! removeAllChildNodes(sourceNode))
            {
                return false;
            }
        }

        return true;
    }

    return false;
}


function getParentNodeById(childNode, parentNodeId, includeSelf)
{
    if (childNode != null)
    {
        var parentNode = includeSelf ? childNode : childNode.parentNode;

        if (parentNode != null)
        {
            if (parentNode.id != null)
            {
                var parentNodeNodeId = String(parentNode.id);

                if (parentNodeNodeId.search(parentNodeId) != -1)
                {
                    return parentNode;
                }
                else
                {
                    var result = getParentNodeById(
                        parentNode, parentNodeId, false);

                    if (result != null)
                    {
                        return result;
                    }
                }
            }
        }
    }

    return null;
}


function getNodeText(node)
{
    var text = null;

    // start with retrieving all child nodes of the specified parent node
    var childNodes = node.childNodes;

    // check each child node if it is the requested node
    for (var i = 0; i < childNodes.length; i++)
    {
        var nextChildNode = childNodes[i];

        if (nextChildNode.nodeName.toLowerCase() == "#text")
        {
            var nodeValue = nextChildNode.nodeValue;

            text = text == null ? nodeValue : (text + " " + nodeValue);
        }
    }

    return text;
}


function replaceImage(imageNode, src)
{
    imageNode.src = src;
}
