/*
  Print Footnotes - Copyright 2004 Jeff Howden
  jeff@jeffhowden.com - http://jeffhowden.com/
*/

var days = ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi'];
var months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'];

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 = 'Renvois :';
        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 = 'Imprimé de :';
    oDetailsDD = document.createElement('DD');
    oDetailsDD.innerText = location.href;
    oDetailsDL.appendChild(oDetailsDT);
    oDetailsDL.appendChild(oDetailsDD);
    oDetailsDT = document.createElement('DT');
    oDetailsDT.innerText = 'Le date :';
    oDetailsDD = document.createElement('DD');
    oDetailsDD.innerText = 'Le ' + days[dayofweek] + ' ' + day + ' ' + months[month] + ' ' + year;
    oDetailsDL.appendChild(oDetailsDT);
    oDetailsDL.appendChild(oDetailsDD);
    oDetailsDT = document.createElement('DT');
    oDetailsDT.innerText = 'Avertissement :';
    oDetailsDD = document.createElement('DD');
    oDetailsDD.innerText = 'La documentation rendue disponible par le Radios Rurales Internationales peut être copiée et adaptée sans permission expresse si : elle est distribuée gratuitement ou au prix coûtant; le Réseau et les sources originelles sont cités; et l\'édition de la documentation n\'altère pas son sens ou son message.';
    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);
}

