var getObj = document.getElementById;

function switchActeur(iStr, iAct)
{
  var oStrContainer = getObj('strContainer_'    + iStr),
            oActive = getObj('activeContainer_' + iStr),
              oBack = getObj('backContainer_'   + iStr);
  
  if (iAct) {
    var oActContainer = getObj('actContainer_' + iAct);
    
    oStrContainer.style.display = 'none';
    oActive.innerHTML = oActContainer.innerHTML;
    
    oActive.style.display = oBack.style.display = '';
  
  } else {
    oActive.style.display = oBack.style.display = 'none';
    oStrContainer.style.display = '';
  }
}

function unFold(oSrc, aTitles)
{
  var oTarget = oSrc.parentNode.previousSibling;
  
  if (!aTitles) aTitles = ['Ouvrir la structure ...',
                           'Fermer la structure ...'];
  
  if (oTarget.style.display == 'none') {
    oTarget.style.display = '';
    oSrc.innerHTML = aTitles[1];
  
  } else {
    oTarget.style.display = 'none';
    oSrc.innerHTML = aTitles[0];
  }
}

function unFoldLinks(oSrc)
{
  oSrc.parentNode.style.display = 'none';
  oSrc.parentNode.nextSibling.style.display = '';
}

function compileCriterias(oForm)
{
  var aSelect = oForm.getElementsByTagName('select'),
      oCr = oForm.cr, aCriterias = new Array;
  
  for (var i = 0; i < aSelect.length; i++)
    if (aSelect[i].value) aCriterias.push(aSelect[i].value);
  
  oCr.value = aCriterias.join();
  return true;
}

var _lastOpenSubMenu = null;
function switchSubMenu(iId, bOpen)
{
  var oSubMenu = document.getElementById('subMenu_' + iId);
  
  if (!oSubMenu) return;
  if ( bOpen) {
    
    if (_lastOpenSubMenu)
        _lastOpenSubMenu.style.display = 'none';
    
    _lastOpenSubMenu = oSubMenu;
  }
  
  oSubMenu.style.display = bOpen ? '' : 'none';
}

/* Date select fields. ****************************************************** */

function _onChangeDate(oSrc)
{
  var sIdent = oSrc.id.slice(0, -2);
  
  var oD = getObj(sIdent + '_d'),
      oM = getObj(sIdent + '_m'),
      oY = getObj(sIdent + '_y'),
      oH = getObj(sIdent + '_h'),
      
      oDate = new Date;

  if (oSrc.value) {
    if (!oD.value) oD.value = oDate.getDate();
    if (!oM.value) oM.value = oDate.getMonth() + 1;
    if (!oY.value) oY.value = oDate.getFullYear();
  
  } else {
    oD.value = oM.value = oY.value = '';
  }
  
  if (!oD.value)  oH.value = '';
  else oH.value = oY.value + ('0' + oM.value).slice(-2) +
    ('0' + oD.value).slice(-2);
}

function createDateFields(sIdent, sValue)
{
  var sY = sValue ? sValue.slice(0, 4) : null,
      sM = sValue ? sValue.slice(4, 6) : null,
      sD = sValue ? sValue.slice(6)    : null,
      
      aMonths = ['Janvier', 'Fevrier', 'Mars', 'Avril', 'Mai', 'Juin',
                 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre',
                 'Decembre'];

  var sRet = '';
  
  sRet += '<input type="hidden" name="' +
    sIdent + '" value="'+ sValue +'" id="'+ sIdent +'_h">';
  sRet += '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';
  
  sRet += '<td style="padding-right: 2px"><select id="' +
    sIdent + '_d" onchange="_onChangeDate(this)" style="width: auto">' +
    '<option></option>';
  
  for (var i = 1; i <= 31; i++) sRet += '<option value="'+ i +'"' +
    (i == sD ? ' selected' : '') +'>'+ i +'</option>'; sRet += '</select></td>';
  
  sRet += '<td width="100%"><select id="' +
    sIdent + '_m" onchange="_onChangeDate(this)" style="width: 100%">' +
    '<option></option>';
  
  for (var i = 0; i < aMonths.length; i++) sRet += '<option value="'+ (i + 1) +
    '"'+ (i + 1 == sM ? ' selected' : '') +'>' + aMonths[i] +'</option>';
    sRet += '</select></td>';
  
  sRet += '<td style="padding-left: 2px"><select id="' +
    sIdent +'_y" onchange="_onChangeDate(this)" style="width: auto">' +
    '<option></option>';
  
  for (var i = 2003; i < 2008; i++) sRet += '<option value="'+ i +'"' +
    (i == sY ? ' selected' : '') +'>'+ i +'</option>'; sRet += '</select></td>';
  
  sRet += '</tr></table>';
  return sRet;
}

function bodyOnload()
{
  var oOpen = null;
  
  if (!arguments.callee.open ||
      ! (oOpen = getObj(arguments.callee.open))) return;
  
  oOpen.style.color = '#f94d0f';
  
  if (oOpen.tagName == 'A') oOpen = oOpen.parentNode.parentNode;
  else oOpen = oOpen.parentNode.nextSibling;
  
  /* Si le sous-menu est vide. */
  if (!oOpen) return;
  
  oOpen.id =
  oOpen.onmouseleave  = null;
  oOpen.style.display = '';
}

function printArticle()
{
  var oContainer = getObj('printContainer');
  
  if (!oContainer) return;
  var oWin = window.open('', 'print',
    'location=no, menubar=no, resizable=yes, status=no, toolbar=no, scrollbars=yes');
  
  oWin.document.open('text/html');
  oWin.document.write('<html><head>'+ document.body.previousSibling.innerHTML +
    '</head><body style="background: none; margin: 15px" ' +
    'onafterprint="window.close()">' + oContainer.innerHTML +'</body></html>');
  
  oWin.document.close();
  oWin.print();
}

function sendArticle()
{
  var   oEmail = null;
  if (!(oEmail = getObj('email'))) return;
  
  oEmail.style.top  = (document.body.offsetHeight - oEmail.offsetHeight)/2 + 'px';
  oEmail.style.left = (document.body.offsetWidth  - oEmail.offsetWidth)/2  + 'px';
  
  oEmail.style.visibility = 'visible';
  oEmail.firstChild.elements[0].focus();
}

function openEmail(oForm)
{
  if (oForm.email.value) {
    location = 'mailto:' + escape(oForm.email.value) + '?subject=' +
      escape("[@netville] Un article qui pourrait vous intéresser") + '&body=' +
      escape("L'article ŕ l'adresse suivante pourrait vous intéresser :\n" +
      location);
    
    oForm.annuler.onclick();
  }
  
  return false;
}

function openDossier(sURL)
{
  window.open(sURL, 'dossier', 'directories=no, location=no, menubar=yes, ' +
    'resizable=yes, scrollbars=yes, status=yes, toolbar=yes').focus();
}
