//==============================================================================
// Data dictionary AJAX functions
//==============================================================================

function initPageDD() {

    // Set up event handler for the table
    var tables = document.getElementsByTagName("table"); 
    for (var i=0; i<tables.length; i++) {
        var currentTable = tables[i];
        if (currentTable.className == "pubTable tableDefinitions") 
        {
            var images = currentTable.getElementsByTagName("div");
            for (var j=0; j<images.length; j++) {
                var currentImage = images[j];
                addEventHandler(currentImage, "click", processFields);
            }
        }
    }
}

//==============================================================================

function getTable(tableName) {

    // Create the request object
    var request = createRequest();
    if (request == null) {
        alert("Your browser does not support AJAX.");
        return;
    }

    // Set up the request object
    request.onreadystatechange = showTable;
    request.open("GET", "/lemma/php/getAjaxTable.php?tableName=" + tableName, 
        true);
    request.send(null);
}

//==============================================================================

function showTable() {

    if (request.readyState == 4) {
        if (request.status == 200) {

            // Display the record for the table
            var resultDiv = document.getElementById("result");
            resultDiv.innerHTML = request.responseText;

            // Set up event handler for the table
            var tableObj = resultDiv.firstChild; 
            var images = tableObj.getElementsByTagName("div");
            for (var i=0; i<images.length; i++) {
                var currentImage = images[i];
                addEventHandler(currentImage, "click", processFields);
            }
        }
    }
}

//==============================================================================

function processFields(e) {

    // Retrieve the object associated with this event
    var me = getActivatedObject(e);

    // Expand the flattened object 
    if (me.className == "flattened") {

        // Retrieve the table name
        var tableName = getParameterName(me, 1);

        // Retrieve the parent row and create a shallow copy of it
        var parentRow = getParentRow(me);
        var clonedRow = parentRow.cloneNode(false);

        // Create a TD element and tag it to be updated
        var tdElement = document.createElement("td");
        tdElement.className = "update";

        // Figure out how many columns this TD should span based on the 
        // number of TD elements in the row above (parentRow)
        var tdCount = parentRow.getElementsByTagName('td').length;
        tdElement.colSpan = tdCount;

        // Add this TD element to the new row
        clonedRow.appendChild(tdElement);

        // Add this clonedRow to the table after the activated row
        var parentTable = parentRow.parentNode;
        parentTable.insertBefore(clonedRow, parentRow.nextSibling);

        // Create the request object
        var request = createRequest();
        if (request == null) {
            alert("Your browser does not support AJAX.");
            return;
        }

        // Set up the request object
        request.onreadystatechange = showFields;
        request.open("GET", 
            "/lemma/php/getAjaxFields.php?tableName=" + tableName, true);
        request.send(null);
    }

    // Flatten the expanded object
    else {

        // Delete the row node which is the next sibling of this row
        var parentRow = getParentRow(me);
        var nextRow = parentRow.nextSibling;
        nextRow.parentNode.removeChild(nextRow);
    }

    // Toggle the associated image
    toggleImage(me);
}

//==============================================================================

function showFields() {

    if (request.readyState == 4) {
        if (request.status == 200) {

            // Find the TD element that has a class name of "update".  This is
            // where we need to insert our information.  
            var tdElements = document.getElementsByTagName("td");
            for (var i=0; i<tdElements.length; i++)
            {
                thisTD = tdElements[i];
                if (thisTD.className == "update") 
                {
                    thisTD.innerHTML = request.responseText;

                    // Set up event handler for the fields that have codes
                    var tableObj = thisTD.firstChild;
                    var images = tableObj.getElementsByTagName("div");
                    for (var i=0; i<images.length; i++) {
                        var currentImage = images[i];
                        addEventHandler(currentImage, "click", processCodes);
                    }

                    // Turn off the update tag
                    thisTD.className = "";

                    // Break out of this loop
                    break;
                }
            }
        }
    }
}

//==============================================================================

function processCodes(e) {

    // Retrieve the object associated with this event
    var me = getActivatedObject(e);

    // Expand the flattened object 
    if (me.className == "flattened") {

        // Retrieve the table name and field name 
        var tableName = getParameterName(me, 2);
        var fieldName = getParameterName(me, 1);

        // Retrieve the parent row and create a shallow copy of it
        var parentRow = getParentRow(me);
        var clonedRow = parentRow.cloneNode(false);

        // Add a TD element to the clonedRow
        var tdElement = document.createElement("td");
        tdElement.className = "update";
        tdElement.colSpan = 6;
        clonedRow.appendChild(tdElement);

        // Add this clonedRow to the table after the activated row
        var parentTable = parentRow.parentNode;
        parentTable.insertBefore(clonedRow, parentRow.nextSibling);

        // Create the request object
        var request = createRequest();
        if (request == null) {
            alert("Your browser does not support AJAX.");
            return;
        }

        // Set up the request object
        request.onreadystatechange = showCodes;
        qs  = "/lemma/php/getAjaxCodes.php?tableName=" + tableName;
        qs += "&fieldName=" + fieldName;
        request.open("GET", qs, true);
        request.send(null);
    }

    // Flatten the expanded object
    else {

        // Delete the row node which is the next sibling of this row
        var parentRow = getParentRow(me);
        var nextRow = parentRow.nextSibling;
        nextRow.parentNode.removeChild(nextRow);
    }

    // Toggle the associated image
    toggleImage(me);
}

//==============================================================================

function showCodes() {

    if (request.readyState == 4) {
        if (request.status == 200) {

            // Find the TD element that has a class name of "update".  This is
            // where we need to insert our information.  
            var tdElements = document.getElementsByTagName("td");
            for (var i=0; i<tdElements.length; i++)
            {
                thisTD = tdElements[i];
                if (thisTD.className == "update") 
                {
                    thisTD.innerHTML = request.responseText;

                    // Turn off the update tag
                    thisTD.className = "";

                    // Break out of this loop
                    break;
                }
            }
        }
    }
}

//==============================================================================

function getParameterName(me, level) {

    // Traverse the DOM to get to the parameter name 
    // Each level represents going up a level

    var currentNode = me;
    for (var i=0; i<level; i++) 
    {   
        if (i==0)
            currentNode = currentNode.parentNode;
        else {
            for (var j=0; j<5; j++) {
                currentNode = currentNode.parentNode;
            }
            currentNode = currentNode.previousSibling.firstChild;
        }
    }
    parameterName = currentNode.nextSibling.innerHTML;
    return(parameterName);
}

//==============================================================================

function getParentRow(me) {

    // Traverse the DOM to get to the parent row - up two levels
    var parentRow = me.parentNode.parentNode;

    return(parentRow);
}

//==============================================================================

function toggleImage(me) {
    if (me.className == "expanded") {
        me.className = "flattened";
    } else {
        me.className = "expanded";
    }
}

//==============================================================================
