//
// The page processing functionality within the Hedgehog architecture ensures
// that the content for each active element (using the attribute 
// class="active") within the page is requested, asynchronously.
//

// PagePopulator
// First find all elements with the class "active"
// Then for each element, send XMLhttp request with the id and the name of the current screen and next screen*
// *this used to be the next URL, but we are moving away from URLs and towards 'screen names'.
// Insert reply into element (when it arrives, asynchronously).

function PagePopulator(screen)
{
    // Find all nodes with the class attribute set to "active"
    var activeNodes = getElementsByAttribute("class", "active");
    
    // then, for each active node
    //   populate it
    for (i=0; i<activeNodes.length; i++)
    {
        //debug
        // alert( "about to call PopulateElement with [" + activeNodes[i].id + "," + screen + "]" );
        PopulateElement(activeNodes[i].id,screen);
    }
}

function PopulateElement(ElementID,screen)
{
    // debug
    // alert("PopulateElement(" + ElementID + ")");
    
    var URLarray = document.URL.split('/');
    var URL = URLarray[URLarray.length-1];
    var XMLHttpRequestObject = false;
    
    // Cater for Microsoft/non-Microsoft differences
    
    if (window.XMLHttpRequest)
    {
        XMLHttpRequestObject = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (XMLHttpRequestObject)
    {
        var obj = document.getElementById(ElementID);
        var Generator;
        var dataToSend;

        // At this point in time, all active elements generate the same kind of request, APART FROM the text elements,
        // which simply requests a text file (which, we happen to know, contains tagged data)
        
        if (Left(ElementID, 4) == "text")
        {
            // alert("text element : " + ElementID);
            
            // This is a text active element - either a fixed text file or a variable (screen dependent) text file.
            // The suffix .txt is added at the end.
            //
            // If fixed then the filename is included at the end of the element name, as in:
            //
            // textFixed(filename) -> textFixed(filename).txt
            //
            // If variable then the filename within this screen is included at the end of the element name as in:
            //
            // textVariable(filename) -> textVariable(screen)(filename).txt
            
            if (Left(ElementID, 9) == "textFixed")
            {
                // alert("Fixed");
                
                // This is a fixed text which has the name of the file embedded into the active element name
                
                obj.innerHTML = "Loading...";
                
                // textFixed is a website wide text/html file named textFixedX.txt where X is the file id
                
                Generator = "./content/" + ElementID + ".txt";
            }
            else if (Left(ElementID, 12) == "textVariable")
            {
                var elementString = String(ElementID);
                var elementLen = elementString.length;
                var textID = elementString.substring(12, elementLen) ;
                
                obj.innerHTML = "";
                
                // textVariable is a screen dependent text/html file named textvariable(screen)(X).txt where X is the file id
                // alert("Variable, String = " + elementString + ", length = " + elementLen + ", textID = " + textID);
                
                Generator = "./content/" + "textVariable" + screen + textID + ".txt";
            }

            dataToSend = null;

            // alert("set up XMLHttpRequest with [" + Generator + "] and [" + dataToSend + "]" );

            XMLHttpRequestObject.open("GET", Generator);            
        }
        else // not a text active element - call the script
        {
            // tell the server which active element we are, and which screen we are requesting.
            // The information is used by various active elements.
            
            Generator = "architecture/UpdateElements.php";
            dataToSend = "id=" + escape(ElementID) + "&screen=" + escape(screen);

            // was XMLHttpRequestObject.open("GET", Generator); but to avoid caching problems, use POST instead (Head Rush Ajax; 301)
            XMLHttpRequestObject.open("POST", Generator, true);
        }

        // === CALLBACK FUNCTION ===
        
        XMLHttpRequestObject.onreadystatechange = function()
        {
            if (XMLHttpRequestObject.readyState == 4)
            {
                if (XMLHttpRequestObject.status == 200)
                {
                    var fileType = XMLHttpRequestObject.getResponseHeader("Content-Type");
                    var text = true;
                    
                    if ( fileType == "text/xml")
                    {
                        var text = XMLHttpRequestObject.responseText;
                        var xmlDoc = XMLHttpRequestObject.responseXML;
                        var functionNameNodes = xmlDoc.getElementsByTagName("code");
                        var dataNodes = xmlDoc.getElementsByTagName("data");
                        var functionName = functionNameNodes[0].firstChild.nodeValue;
                        var functionParameter = dataNodes[0];
                        var evaluationString = functionName + "(obj,functionParameter);";
            
                        eval( evaluationString );
                    }
                    else if (fileType == "text/plain" ||
                             fileType == "text/html")
                    {
                        var text = XMLHttpRequestObject.responseText;
                        
                        clearText( obj );
                        obj.innerHTML = text;
                    }
                    else
                    {
                        text = false;
                    }
                    // if (text) alert("Type = " + fileType + ", contents = " + text);
                }
                else if (XMLHttpRequestObject.status == 404)
                {
                    var FNF_text = "<h1>Page Under Construction</h1>" +
                                    "<p>Please note this website is currently under construction " +
                                    "and some pages are yet to be populated.  This page will be " +
                                    "populated with content very shortly.</p>";
                    obj.innerHTML = FNF_text;
                }
            }
        }

        XMLHttpRequestObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        XMLHttpRequestObject.send( dataToSend );
    }
}