﻿// JScript File

function newXMLDocument(rootTagName, namespaceURL)
{
    if (!rootTagName) rootTagName = "";
    if (!namespaceURL) namespaceURL = "";
    
    if(document.implementation && document.implementation.createDocument)
    {
        //W3C standards way of doing things...
        return document.implementation.createDocument(namespaceURL, rootTagName, null);            
    }
    else
    {
        //Microsoft/IE way of doing things...    
        var doc = new ActiveXObject("MSXML2.DOMDocument");
        
        if(rootTagName)
        {
            var prefix = "";
            var tagname = rootTagName;
            var p = rootTagName.indexOf(':');
            if(p != -1)
            {
                prefix = rootTagName.substring(0, p);
                tagname = rootTagName.substring(p+1);
            }
            //If we have a namspace, we must also have a namespace prefix
            //If we don't have a namespace, we discard any prefix
            if(namespaceURL)
            {
                if(!prefix) prefix = "a0"; //what firefox uses         
            }
            else
            {
                prefix = "";
            }
            
            //Create the root element (with optional namespace) as a string of text
            var text = "<" + (prefix?(prefix + ":"):"") + tagname + (namespaceURL?("xmlns:" + prefix + '="' + namespaceURL + '"'):"") + "/>";
            //and then parse that text into the empty document
            doc.loadXML(text);
        }
        return doc;       
    }
};

function XMLparse(text)
{
    if(typeof DOMParser != "undefined")
    {
        //Mozilla, FireFox, and related browsers
        return (new DOMParser()).parseFromString(text, "application/xml");    
    }
    else if (typeof ActiveXObject != "undefined")
    {
        //IE
        var doc = newXMLDocument();
        doc.loadXML(text);
        return doc;    
    }
    else
    {
        //Last resort techniques for browsers such as Safari
        var url = "data:text/xml;charset=utf-8," + encodeURIComponent(text);
        var request = new XMLHttpRequest();
        request.open("GET", url, false);
        request.send(null);
        return request.responseXML;            
    }
}

function escapeHTML (src) 
{
    return(src.replace(/&/g,'&amp;').replace(/>/g,'&gt;').replace(/</g,'&lt;').replace(/"/g,'&quot;'));                                                                     
};

function XMLmakeTable(xmldoc, schema, element)
{
    //something tells me that the appendChild method isn't doing EXACTLY what it is supposed to in IE...
    //so let's try some various other approaches...
    var div = document.createElement("div");    
    //Create the table element
    var table = document.createElement("table");
    //Create the header row of <th> elements in a <tr> in a <thead>
    var thead = document.createElement("thead");
    var header = document.createElement("tr");
    for(var i = 0; i < schema.columns.length; i++)
    {
        var c = schema.columns[i];
        var label = (typeof c == "string") ? c : c.label;
        var cell = document.createElement("th");
        cell.appendChild(document.createTextNode(label));
        header.appendChild(cell);
    }
    //put the header into the table
    thead.appendChild(header);
    table.appendChild(thead);
    //the remaining rows of the table go into a <tbody>
    var tbody = document.createElement("tbody");
    //and now for the elements which will contain data from the table rows
    var xmlrows = xmldoc.getElementsByTagName(schema.rowtag);
    //iterate through the collection, treating each as a new table row
    for(var r = 0; r < xmlrows.length; r++)
    {
        var xmlrow = xmlrows[r];
        var htmlrow = document.createElement("tr");
        //loop through the columns as specified by the current schema object
        for(var c = 0; c < schema.columns.length; c++)
        {
            var sc = schema.columns[c];
            var tagname = (typeof sc == "string") ? sc : sc.tagname;
            var celltext;
            if(tagname.charAt(0) == '@')
            {
                //if the tagname begins with an '@', treat it as an attribute name
                celltext = xmlrow.getAttribute(tagname.substring(1));            
            }
            else
            {
                //otherwise treat it as a child element
                var xmlcell = xmlrow.getElementsByTagName(tagname)[0];
                //and for this case we'll assume there will only be a single occurance.
                //and we'll also have to account for the fact here, that the node may or may not exist.
                //that IS how XML works after all...  Crackers...
                if(xmlcell)
                {
                    celltext = xmlcell.firstChild.data;
                }
                //the original line from O'Reiley's "Javascript - The Definitive Guide" on page 511 is listed as
                //var celltext = xmlcell.firstChild.data;
                //I didn't believe that the secondary declaration of celltext was necessary, so I removed it.
                //I guess I'll see just how well I am getting this soon enough...              
            }
            //create the HTML element for this cell
            var htmlcell = document.createElement("td");
            //put the text data into the HTML cell
            htmlcell.appendChild(document.createTextNode(celltext));
            //and of course add the cell to the row
            htmlrow.appendChild(htmlcell);
        }
        //add the HTML Row to the table body
        tbody.appendChild(htmlrow);
    }
    table.appendChild(tbody);
    //perform any other additional processing on the table object here (obviously, this is where it's getting fucking tedious)
    //The book says "Nore that in XML we must use setAttribute() instead." - Nice to know I guess...
    table.frame = "border";
    table.style.clear = "all";
    table.style.paddingBottom = "25px";
    //Now that we have created the HTML table add it to the specified element.
    if(typeof element == "string") element = document.getElementById(element);
    element.innerHTML = "";
    element.appendChild(table);
    element.style.height = "2000px";
    //element.s
}
