/*
  Print Footnotes - Copyright 2004 Jeff Howden
  jeff@jeffhowden.com - http://jeffhowden.com/
*/

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var now = new Date();
var dayofweek = now.getDay();
var day = now.getDate();
var month = now.getMonth();
var year = now.getYear();
if(year < 1000)
  year = year + 1900;

function hasParentAttributeValue(oElement, sAttribute, sValue)
{
  var returnValue = false;
  while(oElement.tagName.toUpperCase() != 'BODY')
  {
    if(oElement[sAttribute] == sValue)
    {
      returnValue = true;
      break;
    }
    oElement = oElement.parentNode;
  }
  return returnValue;
}

function setBeforePrint()
{
  var oFootnote = document.getElementById('footnote');
  if(oFootnote)
  {
    var oMain = document.getElementById('content');
    var oMainLinks = oMain.getElementsByTagName('A');
    if(oMain && oMainLinks.length)
    {
      var aLinks = new Array();
          aLinks[0] = '';
      
      for(var i = 0; i < oMainLinks.length; i++)
      {
        sHref = oMainLinks[i].href;
        sName = oMainLinks[i].name;
        sClass = oMainLinks[i].className;
        sText = oMainLinks[i].innerText;
        sHTML = oMainLinks[i].innerHTML;
        
        sHref = sHref.replace('mailto:', '');
        
        if(sHref && hasParentAttributeValue(oMainLinks[i], 'style.display', 'none'))
          sHref = null;
        if(sHref && hasParentAttributeValue(oMainLinks[i], 'className', 'noprint'))
          sHref = null;
        if(sHref && sHref.toLowerCase().indexOf('javascript:') == 0)
          sHref = null;
        if(sHref && sHref.toLowerCase().indexOf('ymsgr:') == 0)
          sHref = null;
        if(sHref && sHref.toLowerCase().indexOf('aim:') == 0)
          sHref = null;
        if(sHref && sHref == sText)
          sHref = null;
        if(sHref && sHref.replace('mailto:', '') == sText)
          sHref = null;
    
  //      if(sHref && sHref.indexOf('http:\/\/' + location.host) == 0)
  //        sHref = sHref.replace('http:\/\/' + location.host, '');
  //      if(sHref && sHref.indexOf('https:\/\/' + location.host) == 0)
  //        sHref = sHref.replace('https:\/\/' + location.host, '');
    
        if(sHref && sHref.indexOf(location.pathname) == 0)
        {
          sHref = sHref.replace(location.pathname, '');
          if(sHref.indexOf('?') == 0 || sHref.indexOf('#') == 0)
            sHref = null;
          else
            sHref = location.pathname + sHref;
        }
  
        if(sHref)
        {
          iLinks = aLinks.length;
          for(var j = aLinks.length; j > 0; j--)
          {
            if(aLinks[j] == sHref)
            {
              iLinks = j;
              break;
            }
          }
          aLinks[iLinks] = sHref;
          oMainLinks[i].oldInnerHTML = oMainLinks[i].innerHTML;
          oMainLinks[i].innerHTML += ' [' + iLinks + ']';
        }
      }
      if(aLinks.length > 0)
      {
        oFootnoteH6 = document.createElement('H6');
        oFootnoteH6.innerHTML = 'Endnotes:';
        oFootnote.appendChild(oFootnoteH6);
        oFootnoteOL = document.createElement('OL');
        for(i = 1; i < aLinks.length; i++)
        {
          oFootnoteLI = document.createElement('LI');
          oFootnoteLI.innerText = aLinks[i];
          oFootnoteOL.appendChild(oFootnoteLI);
        }
        oFootnote.appendChild(oFootnoteOL);
        oMain.appendChild(oFootnote);
      }
    }
    oDetailsDL = document.createElement('DL');
    oDetailsDT = document.createElement('DT');
    oDetailsDT.innerText = 'Printed From:';
    oDetailsDD = document.createElement('DD');
    oDetailsDD.innerText = location.href;
    oDetailsDL.appendChild(oDetailsDT);
    oDetailsDL.appendChild(oDetailsDD);
    oDetailsDT = document.createElement('DT');
    oDetailsDT.innerText = 'Date:';
    oDetailsDD = document.createElement('DD');
    oDetailsDD.innerText = days[dayofweek] + ', ' + months[month] + ' ' + day + ', ' + year;
    oDetailsDL.appendChild(oDetailsDT);
    oDetailsDL.appendChild(oDetailsDD);
    oDetailsDT = document.createElement('DT');
    oDetailsDT.innerText = 'Disclaimer:';
    oDetailsDD = document.createElement('DD');
    oDetailsDD.innerText = '© The Motorhome Group';
    oDetailsDL.appendChild(oDetailsDT);
    oDetailsDL.appendChild(oDetailsDD);
    oFootnote.appendChild(oDetailsDL);
  }
}

function setAfterPrint()
{
  var oFootnote = document.getElementById('footnote');
  var oMain = document.getElementById('content');
  var oMainLinks = oMain.getElementsByTagName('A');
  if(oMain && oMainLinks.length)
  {
    for(var i = 0; i < oMainLinks.length; i++)
      if(oMainLinks[i].oldInnerHTML)
        oMainLinks[i].innerHTML = oMainLinks[i].oldInnerHTML;
  }
  if(oFootnote)
    oFootnote.innerText = '';
}

if(window.attachEvent)
{
  window.attachEvent('onbeforeprint', setBeforePrint);
  window.attachEvent('onafterprint', setAfterPrint);
}
