// NAME
//      $RCSfile: ajax_support.js,v $
// DESCRIPTION
//      [given below in javadoc format]
// DELTA
//      $Revision: 1.2 $
// CREATED
//      $Date: 2009/12/02 16:36:46 $
// TO DO
//


// global flag
var isIE = false;

// global request and XML document objects
var req;

// function to call inside ajaxResponse, when the answer is correct
var ajaxCallback;

function buildContentForm(form)
{
    var value;
    var content = "";
    var elements = form.elements;
    for (var i = 0; i < elements.length; i++)
    {
        var elem = elements[i];
        var name = elem.name;
        var type = elem.type;
        if (name)
        {
            var value = elem.value;
            if (type == 'radio' || type == 'checkbox')
            {
                if (elem.checked)
                {
                    content = buildContent(content, name, value);
                }
                else
                {
                    content = buildContent(content, name, "");
                }
            }
            else
            {
                content = buildContent(content, name, value);
            }
        }
    }
    return content;
}


function buildContent(content, name, value)
{
    if (content.length > 0)
    {
        content += "&";
    }
    content += name + "=" + encodeURIComponent(value);
    return content;
}


// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url, content, namefunction) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) 
    {
        req = new XMLHttpRequest();
    } 
    // branch for IE/Windows ActiveX version
    else if (window.ActiveXObject) 
    {
        isIE = true;
        req = new ActiveXObject("Msxml2.XMLHTTP");
        if (!req) 
        {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if (req) 
    {
        /*
        alert('loadXMLDoc():\n'
          + 'url=' + url + '\n'
          + 'content=' + content + '\n'
          + 'namefunction=' + namefunction + '\n'
        );
        */

        // req.onreadystatechange = namefunction;
        ajaxCallback = namefunction;
        req.onreadystatechange = ajaxResponse;
        req.open("POST", url, true);
        req.setRequestHeader('Content-Type', 
            'application/x-www-form-urlencoded');
        req.send(content);
    }
}


function printElements(title, elements) 
{
    alert("elements: " + title + ", no: " + elements.length);
    for (var i = 0; i < elements.length; i++)
    {
        var element = elements[i];
        printElement(element);
    }
}


function printElement(element) 
{
    var childNodes = element.childNodes;
    var len = childNodes.length;

    alert("printElement: " + element.nodeName + ", children: " + len);

    var node;
    for (var i = 0; i < len; i++)
    {
        node = childNodes[i];
        if (node.nodeType == 1)
        {
            // 1 = ELEMENT_NODE
            printElement(node);
        }
        else
        {
            // TEXT_NODE = 3
            alert("node: " + node 
                + ", name=" + node.nodeName 
                + ", value=" + node.nodeValue
                + ", type=" + node.nodeType
                + "\nparent=" + element.nodeName
                + ", i=" + i
                );
        }
    }
}


// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) 
{
    var tag;
    if (prefix && isIE) 
    {
        // IE/Windows way of handling namespaces
        tag = prefix + ":" + local;
    } 
    else 
    {
        // the namespace versions of this method
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided
        // there aren't conflicts with non-namespace element
        // names
        tag = local;
    }
    var elements = parentElem.getElementsByTagName(tag);
    var result = elements[index];

    var returnValue = null;
    if (result) 
    {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes
        var node;
        if (result.childNodes.length > 1) 
        {
            node = result.childNodes[1];
        } 
        else 
        {
            node = result.firstChild;
        }
        returnValue = node.nodeValue;
    } 
    return returnValue;
}


// This replaces the content of 'ajax_replace' by the response we
// get back
function ajaxResponse()
{
    // only if req shows "loaded"
    if (req.readyState == 4)
    {
        // only if "OK"
        if (req.status == 200)
        {
            ajaxCallback.call(this, req);
        }
        else
        {
            alert("There was a problem retrieving the (XML) data:\n"
                + req.statusText);
        }
    }
}


