/**
 * Functions and classes for e-Grip CMS front end
 * @version $Id: ajax_common.js,v 1.1.2.2.4.1 2006/08/02 13:53:02 kruithof Exp $
 */


/**
 * Gets the nodeValue for a child node with a given name
 * @access public
 * @param DOMNode node the parent node
 * @param string subNodeName the child node's name
 * @return string the subnode's value if existing, otherwise false
 */
function getSubNodeValue(node, subNodeName)
{
    var value = false;
    if ((node.nodeType == 1) && (node.hasChildNodes())) {
        for (var i = 0; i < node.childNodes.length; i++) {
            childNode = node.childNodes.item(i);
            if ((childNode.nodeType == 1) && (childNode.tagName == subNodeName)) {
                value = getNodeContent(childNode);
            }
        }
    }
    return value;
}

function getNodeContent(node)
{
    var content = '';

    if (node.hasChildNodes()) { // node contains CDATA
        for (var i = 0; i < node.childNodes.length; i++) {
            content += getNodeContent(node.childNodes[i]);
        }
    } else {
        content = node.nodeValue;
    }
    return content;
}

function setNodeText(node, value)
{
    var content = '';

    if (node.hasChildNodes() && (node.childNodes.length == 1) && (node.firstChild.nodeType == 3)) { // node has one subnode, which is a textnode
        setNodeText(node.firstChild, value);
    } else if (node.nodeType == 3) {
        node.nodeValue = value;
    }
}

function is_array(array)
{
    return (array instanceof Array);
}

function in_array(needle, haystack)
{
    if ((needle != '') && (haystack.length > 0)) {
        for (var i = 0; i < haystack.length; i++) {
            if (haystack[i] == needle) return true;
        }
    }
    return false;
}

function setClassName(node, classname)
{
    if (node.setAttribute('class', classname)) {
        return true;
    } else if (node.setAttribute('className', classname)) {
        return true;
    }
    return false;
}

function addTextNode(node, text)
{
    var txt = document.createTextNode(text);
    node.appendChild(txt);

    return txt;
}

function removeChildren(node)
{
    while (node.firstChild) node.removeChild(node.firstChild);
}
