/**
 * @fileoverview This file contains functions to adapt the generic functions used by the application creators to the sitemanager.
 * @author Michael Kamphausen, www.michael-kamphausen.de
 * @version 1.0
 */

/**
 * Creates a new tag node "tagname" of namespace "namespace" with the attributes and values in "attributes" 
 * @param {string} tagname tagname of the new node
 * @param {array} attributes attributes and values of the new node as two-dimensional array, a table with two columns: the first one for the attribute names as string, 
                     the second one for the corresponding values as string; here is an example for the creation of the array:
                     new Array(new Array("type", "image"), new Array("src", "image1.png"))
 * @param {string} text if not null, the text is appended as text node to the new node
 * @return the new node
 * @type Object
 */
function createTagNode(tagname, attributes, text) {
  // create the new tag node
  var node = document.createElement(tagname);
  // if there are attributes, add them
  if (attributes) {
    for (var i = 0; i < attributes.length; i++) {
      // make sure, that there are three columns for one attribute
      if (attributes[i].length >= 2) {
        node.setAttribute(attributes[i][0], attributes[i][1]);
      }
    }
  }
  // if text exists, append it as text node to the new node
  if (text) {
    node.appendChild(document.createTextNode(text));
  }
  // return the new node
  return node;
}

/**
 * Creates a new tag node "tagname" of namespace "namespace" with the attributes and values in "attributes" and appends it as child to the node "parent"
 * @param {Object} parent parent node of the new node
 * @param {string} tagname tagname of the new node
 * @param {array} attributes attributes and values of the new node as two-dimensional array, a table with two columns: the first one for the attribute names as string, 
                     the second one for the corresponding values as string; here is an example for the creation of the array:
                     new Array(new Array("type", "image"), new Array("src", "image1.png"))
 * @return the new node, if parent exists, otherwise null
 * @type Object
 */
function appendTagNode(parent, tagname, attributes, text) {
  // if parent does not exist, exit method and return null
  if (parent) {
    // create the new tag node with attributes
    var node = createTagNode(tagname, attributes, text);
    // append the new node to the parent node
    if (node) {
      parent.appendChild(node);
    }
    // return the new node
    return node;
  }
  return null;
}