﻿// JScript File
var TestudoXmlTransformerVersion = 1;
function hasDocumentElement(strOneId)
{
    var theSoughtAfterElement = document.getElementById(strOneId);
    var bRc = false;
    try 
    {
       var strTest = theSoughtAfterElement.innerHTML;
       jsDebugTrace ('The ' + strOneId + ' element was succesfully found in the browser document.');
       bRc = true;
    }
    catch (exBadDom) 
    {
    }
    return bRc;
}
function safeGetDocumentElement(strOneId)
{
    var theSoughtAfterElement = document.getElementById(strOneId);
    try 
    {
       var strTest = theSoughtAfterElement.innerHTML;
       jsDebugTrace ('The ' + strOneId + ' element was succesfully obtained from the current browser document.');
    }
    catch (exBadDom) 
    {
       alert('XML transormation error: unable to find a node callled ' + strOneId + ' in the browser document.');
    }
    return theSoughtAfterElement;
}
function safeGetChild(theNodeList, nCurrent)
{
   var oneKid = theNodeList.item(nCurrent);
   try
   {
        var strTest = oneKid.nodeName;
   }
   catch (badObject)
   {
        alert('The XML node list does not contain an element #' + nCurrent + '.');
   }
   return oneKid;
}
function safeGetFirstChildNodeValue(child) // as string
{
  var strValue = ""
   try
   {
        strValue = child.firstChild.xml;
   }
   catch (badObject)
   {
        //alert('The XML node ' + strNodeNameInResults + ', object #' + nThisNode + ' has no child nodes. It must contain at least one child node.' );                
   }
   return strValue;
}
function xmlToPrototype(xmlDoc,strNodeNameInResults,strDivContainingPrototypeInBrowserDoc,strResultsOutputPrefix)
{
  try
  {
//alert(xmlDoc.xml);        
        var theNodes = xmlDoc.getElementsByTagName(strNodeNameInResults);
        var nNodes = theNodes.length;
        jsDebugTrace ('inside the xformer, there are ' + nNodes + ' XML nodes to parse named ' + strNodeNameInResults );
        
        var nThisNode = 0;
        var strOneId;
        while (nThisNode < nNodes) 
        {
            var theNode = theNodes[nThisNode];
            jsDebugTrace('Processing ' + strNodeNameInResults + ' node #' + nThisNode);
            strOneId = strResultsOutputPrefix + nThisNode;
            var theResultsDiv = safeGetDocumentElement(strOneId);
            nThisNode++;
            
            var kids = theNode.childNodes;
            var thePrototypeDiv = safeGetDocumentElement(strDivContainingPrototypeInBrowserDoc);
            var strHtmlOut = thePrototypeDiv.innerHTML;
            var nLength = kids.length;
            jsDebugTrace('The object has ' + nLength + ' properties');
            var nCurrent = 0;
            var oneKid;
            var strValue;
            var strName ;
            while (nCurrent < nLength)
            {
               oneKid = safeGetChild(kids,nCurrent);
               strValue = safeGetFirstChildNodeValue(oneKid);
               strName = strDivContainingPrototypeInBrowserDoc + oneKid.nodeName;
               while (strHtmlOut.indexOf(strName) > -1)
               {
                  jsDebugTrace('Substituting in ' + strValue + '.');
                  strHtmlOut = strHtmlOut.replace(strName, strValue);
               }
               nCurrent++;
            }
            theResultsDiv.innerHTML = strHtmlOut;
        }
        jsDebugTrace('Finished processing server returned data, now cleaning up possible leftovers from last trip...');
        var bFinshed = false;
        while (!bFinshed)
        {
          strOneId = strResultsOutputPrefix + nThisNode;
          if (! hasDocumentElement(strOneId))
          {
              bFinshed = true;
          }
          else
          {
              theResultsDiv = safeGetDocumentElement(strOneId);
              theResultsDiv.innerHTML = '';
          }
          nThisNode++;
        }
        jsDebugTrace('Done cleanup...')
  }
  catch (e)
  {
      alert("Error:" + e.description);
  }
}

function testDebugger()
{
  try
  {
    if (m_nTestudoDebugJsVersion != 3) 
       alert('The debugging subsystem is version mismatched. The external JS is at version ' + 
            m_nTestudoDebugJsVersion + ' while this embedded code expects V3')
  }
  catch (ex)
  {
    alert('The debugging subsystem version cannot be determined: the debugger is not is not loaded.')
  }
}
testDebugger();
