function GetAbsolutePath(RelPath) {
  // Create the absolute path
  var absPath = location.href;
  absPath += RelPath;
  var parts = absPath.replace("/\/\.\//g", "/").split("/");
  {for(var i=4; i<parts.length; i++){
      if (parts[i] == "..") {
        {for (var j=i+1; j<parts.length; j++){
          parts[j-2] = parts[j];
        }}
        parts.length -= 2;
        i -= 2;
      }
  }}
  absPath = parts.join("/");
  return absPath;
}
function GetRootPath() {
  // Create the absolute path
  var absPath = location.href;
  var parts = absPath.replace("/\/\.\//g", "/").split("/");
  if(parts.length>=4) {
    if(parts[3].indexOf('~',0)==0) {
      parts.length=4;
    } else {
      parts.length=3;
    }
  }
  absPath = parts.join("/")+'/';
  return absPath;
}

function MakeHTTPRequest(handler,url,Level) {
  var httpRequest;
  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
    if (httpRequest.overrideMimeType) {
      httpRequest.overrideMimeType('text/xml');
      // See note below about this line
    }
  } else if (window.ActiveXObject) { // IE
    try {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert('Caught Exception: ' + e.description);
      }
    }
  }
  if (!httpRequest) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  httpRequest.onreadystatechange = function() {handler(httpRequest,Level); };
  httpRequest.open('GET', url, true);
  httpRequest.send(null);
}


function BuildChildrenMenu(AnchorList){
  var DynamicChildrenMenu = document.getElementById('DynamicChildrenMenu');
  {for (var i=0;i<AnchorList.length;i++){
    var label=AnchorList[i].childNodes[0].nodeValue;
    if(AnchorList[i].getAttribute('href')!='') {
      var href=location.href + AnchorList[i].getAttribute('href');
      var li=document.createElement('li');
      //var h2=document.createElement('h2');
      var a=document.createElement('a');
      a.setAttribute('href',href);
      var linkText=document.createTextNode(label);
      a.appendChild(linkText);
      li.appendChild(a);
      if(DynamicChildrenMenu) {
        DynamicChildrenMenu.appendChild(li);
      }
    }
  }}
}

function BuildSiblingMenuAndSiblingFooter(SiblingAnchorList){
  var DynamicMenu = document.getElementById('DynamicSiblingMenu');        //var DynamicMenu = document.getElementById('DynamicChildMenu');
  //clear DynamicMenu div
  //while(DynamicMenu.hasChildNodes()){
  //  DynamicMenu.removeChild(DynamicMenu.childNodes[0]);
  //}
  // Proceed in the reverse order because we are using insertbefore!
  var NumSiblings=SiblingAnchorList.length;
  var MySiblingIndex=-1;
  {for (var i=NumSiblings-1;i>=0;i--){
    var label=SiblingAnchorList[i].childNodes[0].nodeValue;
    var href='../' + SiblingAnchorList[i].getAttribute('href');
    var li=document.createElement('li');
    var h2=document.createElement('h2');
    var a=document.createElement('a');
    a.appendChild(document.createTextNode(label));
    a.title=label;
    a.href=href;
    var abspath=GetAbsolutePath(href);
    var LinkToThisPage;
    if(location.href==abspath) {
      MySiblingIndex=i;
      LinkToThisPage=true;
      var sp=document.createElement('span');
      sp.appendChild(document.createTextNode(label));
      h2.appendChild(sp);
    } else {
      LinkToThisPage=false;
      h2.appendChild(a);
    }
    li.appendChild(h2);
    if(DynamicMenu) {
      if(label!='Home') {
        if(DynamicMenu.hasChildNodes()) {
          DynamicMenu.insertBefore(li,DynamicMenu.childNodes[0]);
        } else {
          DynamicMenu.appendChild(li);
        }
      }
    }
  }}
  var divfooter= document.getElementById('footer');
  if(NumSiblings>1 && MySiblingIndex!=-1) {
    //var divfooter=document.createElement('div');
    //divfooter.id='footer';
    var p1=document.createElement('p');
      var center=document.createElement('center');
        var table=document.createElement('table');
        var tr=table.insertRow(0);
          // Add the three navigation buttons:
          {for(var ibtn=0;ibtn<3;ibtn++) {
            var td=tr.insertCell(ibtn);
            //
            var btnid;
            var btnLabel;
            var DoEnable=false;
            var href;
            if(ibtn==0){
              btnid='prevbtn';
              btnLabel='Previous';
              DoEnable=(MySiblingIndex>0);
              if(DoEnable) {
                var SiblingLabel=SiblingAnchorList[MySiblingIndex-1].childNodes[0].nodeValue;
                if(SiblingLabel=='Home'){
                  DoEnable=false;
                }
                href='../' + SiblingAnchorList[MySiblingIndex-1].getAttribute('href');
                btnLabel=SiblingAnchorList[MySiblingIndex-1].childNodes[0].nodeValue;
              }
            } else if(ibtn==1) {
              btnid='upbtn';
              btnLabel='Up';
              {
                var atop=document.createElement('a');
                if(location.href==GetRootPath()) {
                  // Already at top.
                  DoEnable=false;
                } else {
                  DoEnable=true;
                }
              }
              if(DoEnable) {
                href='../';
              }
            } else if(ibtn==2) {
              btnid='nextbtn';
              btnLabel='Next';
              DoEnable=(MySiblingIndex<(NumSiblings-1));
              if(DoEnable) {
                href='../' + SiblingAnchorList[MySiblingIndex+1].getAttribute('href');
                btnLabel=SiblingAnchorList[MySiblingIndex+1].childNodes[0].nodeValue;
              }
            }
            if(!DoEnable) {
              // Disabled Prev button
              td.className='disabled';
              var div=document.createElement('div');
              div.id=btnid;
                var sp=document.createElement('span');
                sp.appendChild(document.createTextNode(btnLabel));
                div.appendChild(sp);
              td.appendChild(div);
            } else {
              // Enabled Prev button
              var a=document.createElement('a');
              a.id=btnid;
              a.href=href;
              a.title=btnLabel;
                var sp=document.createElement('span');
                sp.appendChild(document.createTextNode(btnLabel));
                a.appendChild(sp);
              td.appendChild(a);
            }
          }}
        center.appendChild(table);
      p1.appendChild(center);
    //divfooter.appendChild(p1);
    if(divfooter.hasChildNodes()) {
      divfooter.insertBefore(p1,divfooter.childNodes[0]);
    } else {
      divfooter.appendChild(p1);
    }
  }
  CreatedSiblingMenu=true;
}
function AddCopyrightFooter(){
  var divfooter= document.getElementById('footer');
  var p2=document.createElement('p');
  p2.id='legal';
  {
    var a=document.createElement('a');
    a.href='/Policies/CopyrightPolicy.html';
    a.appendChild(document.createTextNode('Copyright \251 2007 Advanced Numerical Solutions LLC. '));
    p2.appendChild(a);
  }
  p2.appendChild(document.createTextNode(' | '));
  {
    var a=document.createElement('a');
    a.href='http://www.freecsstemplates.org/';
    a.appendChild(document.createTextNode('CSS Credit'));
    p2.appendChild(a);
  }
  divfooter.appendChild(p2);
  var p3=document.createElement('p');
  p3.id='legal';
  {
    var a=document.createElement('a');
    a.href='/Policies/PrivacyPolicy.html';
    a.appendChild(document.createTextNode('Privacy'));
    p3.appendChild(a);
  }
  p3.appendChild(document.createTextNode('|'));
  {
    var a=document.createElement('a');
    a.href='/Policies/CreditCardPolicy.html';
    a.appendChild(document.createTextNode('Credit Cards'));
    p3.appendChild(a);
  }
  p3.appendChild(document.createTextNode('|'));
  {
    var a=document.createElement('a');
    a.href='/Policies/ShippingPolicy.html';
    a.appendChild(document.createTextNode('Shipping'));
    p3.appendChild(a);
  }
  p3.appendChild(document.createTextNode('|'));
  {
    var a=document.createElement('a');
    a.href='/Policies/RefundsPolicy.html';
    a.appendChild(document.createTextNode('Returns'));
    p3.appendChild(a);
  }
  divfooter.appendChild(p3);
}
function BuildTopLevelMenu(TopLevelAnchorList){
  var DynamicMenu = document.getElementById('menu');
  var ul=document.createElement('ul');
  {for(var i=0;i<TopLevelAnchorList.length;i++){
    var li=document.createElement('li');
    if(i==0) {
      li.className='first';
    }
    var label=TopLevelAnchorList[i].childNodes[0].nodeValue;
    var href=GetRootPath() + TopLevelAnchorList[i].getAttribute('href');
    var a=document.createElement('a');
    a.appendChild(document.createTextNode(label));
    a.href=href;
    a.title=label;
    li.appendChild(a);
    ul.appendChild(li);
  }}
  DynamicMenu.appendChild(ul);
}
function RetrieveTOC(httpRequest,Level) {
  if (httpRequest.readyState == 4) {
    if (httpRequest.status == 200) {
      var xmldoc = httpRequest.responseXML;
      var AnchorList=xmldoc.getElementsByTagName('a');
      if(Level==0) {
        BuildChildrenMenu(AnchorList);
      } else if(Level==1) {
        BuildTopLevelMenu(AnchorList);
      } else if(Level==2) { 
        BuildSiblingMenuAndSiblingFooter(AnchorList);
      }
    } else {
      //alert('There was a problem with the request.');
    }
  }
}
function OnBodyLoad(){
  {for(var Level=0;Level<3;Level++){
    var TOCUrl='';
    if(Level==0) {
      TOCUrl='toc.xml';
    } else if(Level==1) {
      TOCUrl=GetRootPath()+'toc.xml';
    } else if(Level==2) {
      // Check if the page is at the root of the web site:
      var absrootpath=GetRootPath();
      if(location.href==absrootpath) {
        // Already at root
        continue;
      }
      TOCUrl='../toc.xml';
    }
    MakeHTTPRequest(RetrieveTOC,TOCUrl,Level);
  }}
  AddCopyrightFooter();
}



