/* Cookie encours :  encours=<id>:<px>!<id>:<px>;                          */
/*                              Bernard Mailhol, Nov 2006                  */
/*                              bernard/mailhol.com                        */
var hrm_extern = false;   /* permettre un appel depuis une page hors site  */
var hrm_author = '';      /* se limiter à un seul auteur                   */

function jardin_l ()
{
document.write('<center><table border=0 width=762 cellspacing=0 cellpadding=0>');
document.write('<TR>');
document.write('<TD WIDTH=201><IMG SRC="/DATA/jardin.gif" BORDER=0 HEIGHT=150 WIDTH=201></TD>');
document.write('<TD WIDTH=360><A HREF="http://www.lejardindeslivres.fr/index.html"><IMG SRC="/DATA/ejdl2.gif" BORDER=0 HEIGHT=100 WIDTH=372 ALT="Editions Le Jardin des Livres" title="Editions Le Jardin des Livres"></A><BR>');
}
function jardin_r ()
{
document.write('<td width="201"><img width="201" height="150" border="0" src="/DATA/jardin.gif"></td>');
document.write('</tr>');
document.write('</table></center>');
}

var hrm_nxspan = 100; /* 3 digits pour contourner un bug                   */
function gen_span( name )
{
   hrm_nxspan++;
   document.write('<span id="'+name+'/J-'+hrm_nxspan+'"></span>');
}

function get_cookie_encours ()
{
   cks = document.cookie; p = cks.indexOf("encours=");
   if (p != -1) /* isoler le cookie "encours"                              */
   {
      q = cks.indexOf(";", p); if (q == -1) q = cks.length;
      p = p + 8; ln = q - p/* après le =                                   */
      if (ln > 0)
      {
         ckst = cks.substr( p, ln);
         ck = ckst.split('!');
         for (j in ck)
         {
            ck2 = ck[j].split(':'); id = ck2[0];
            document.hrm_enc[id] = ck[j];
         }
      }
   }
}

function get_encours (id, ispdf)  /* retourne le nombre de livres commandés       */
{
   var en = document.hrm_enc[id];
   if ( en == undefined ) return 0; /* rien                                */
   var ck2 = en.split(':');
   return ck2[ispdf? 2 : 1]-0;
}

function add_encours( id, nbr, ispdf )
{
   var n = get_encours(id, ispdf) + nbr;  if (n < 0) n = 0;
   if (nbr == 0 & n == 0) n = 1;   /* demande de seule présence            */
   set_encours( id, n, ispdf);
}

function set_encours( id, n, ispdf )
{
   n = Math.max(n, 0); n = Math.min( n, 9);
   var n0; var n1;

   var en = document.hrm_enc[id];
   if ( en == undefined ) { n0 = 0; n1 = 0;}
   else
   {
     var ck2 = en.split(':');
     n0 = ck2[1]; n1 = ck2[2];
   }
   if (ispdf) { n1 = n;} else { n0 = n;}  /* choix d'affectation           */
   document.hrm_enc[id] = id + ':' + n0 + ':' + n1;
   document.cookie = 'encours=' + make_encours() + ';path=/;domain='+document.domain+';';
}

function make_encours()
{
   var txt = '';
   for (var idx in document.hrm_enc) txt = txt + '!' + document.hrm_enc[idx];
   return  txt.substring(1,999);
}

function reset_encours()
{
   document.hrm_enc = new Array();   /* document.hrm_enc.<id> = <id>:<px>  */
   document.cookie = 'encours=;path=/;domain='+document.domain+';';
   go_encours();
}

function reset_all()
{
   reset_encours();
   document.cookie = 'achat=' +  '' + ';secure';
}

/* Préparer les boutons achat et commande lors du chargement               */
function go_encours()
{
  /* Commuter sur le bon domaine (transmission des cookies)                */
  if (window.location.hostname == "lejardindeslivres.com") window.location.replace( window.location.protocol + "//www.lejardindeslivres.fr" + window.location.pathname);

  if (document.hrm_goenc == 0) { document.hrm_goenc = 1;}
   var idx =0; var id = 0; var ls = 0;  /* Locales                         */
   var lstd = document.getElementsByTagName( "div");
   var lsts = document.getElementsByTagName( "span");
   var sz_lst = (sz_div = lstd.length) + lsts.length;

   for (var i = 0; i < sz_lst; i++)  /* Initialiser chaque bouton d'achat  */
   {
       lst0 = (i<sz_div)? lstd[i] : lsts[i-sz_div];
       idx0 = lst0.id  /* Identification de la division                    */
       txid0 = idx0;
       var txct = lst0.textContent; if (txct == null) txct = lst0.innerText;
       if (txct == null) txct = '-------';
       if (txct.length == 0) txct = '------';
       var FromTxt = false;
       if ((idx0 == '') &( txct.substr(0,4) == 'HRM-'))
       {  /* il n'y a pas d'identification, mais un texte commençant par HRM */
          idx0 = txct.substr(4,20);    /* voir le texte                    */
          txid0 = idx0+'/HRM--'+(i+200);
          lst0.id = txid0 ;
          FromTxt = true;
       }
       if (idx0.substr(0,1) == 'B') /* Bouton achat ?                      */
       {
          idx = idx0.substr(2,20);
          p = idx.indexOf('/'); if (p == -1) p = idx.length;
          id = idx.substr(0,p);  /* Identification                         */
          var TitreExist = document.hrm_cat.hasOwnProperty(id);
          if (TitreExist)  /* Si ses 2 prix sont nulls, abandonner.        */
          {
              var px = document.hrm_cat[id][2]+document.hrm_cat[id][4];
              if (px == 0) TitreExist = false;
          }
          if (TitreExist)
          {
             ls = document.hrm_idx[id];
             if (ls == undefined)
             {
                document.hrm_idx[id] = Array();
             }
             document.hrm_idx[id].push(txid0);
             refresh_bta ( id );  /* Mettre à jour                         */
          }
          else if (FromTxt)
          {
             lst0.textContent = '';
             lst0.innerText   = '';
          }
       }
       if (idx0.substr(0,5) == 'GOCDE') /* Bouton commande ?               */
       {
          document.hrm_cde.push(txid0);  /* Nommer tous ces boutons        */
       }
   }
   refresh_cde();   /* Le bouton commande                                  */
}

function add_book( this_div, id, ispdf)     /* Passer si nécessaire de 0 à 1      */
{
   /* Mise à jour du cookie de description de la commande                  */
   add_encours( id, 0, ispdf); /* marquer une présence dans la liste       */
   refresh_bta ( id );
   refresh_cde();
}

function add_vol( id, nbr, ispdf)         /* ajouter ou retrancher sur quantité   */
{
   /* Mise à jour du cookie de description de la commande                  */
   add_encours( id, nbr, ispdf);
   refresh_bta ( id );
   refresh_cde();
}

function bt_nbvol( this_bt, id, ispdf)
{
   var nb = this_bt.value;  var nb0 = get_encours(id, ispdf);
   if ( nb != nb0)  /* nb est toujours numérique                           */
   {
      set_encours( id, nb, ispdf);
      refresh_bta ( id );
      refresh_cde();
   }
}

function refresh_bta ( id )   /* Bouton achat  BT--- ou BU--- ou BV---     */
{
   /* [0] titre; [1] auteur; [2] prix livre; [3] ISBN; [4] prix pdf;       */
   var livre = document.hrm_cat[id];
   var enc_liv = get_encours( id, false );
   var enc_pdf = get_encours( id, true );
   var enc = enc_liv + enc_pdf;
   var txt = ''; var cls = '';

   /* -------------------------------------------------------------------- */
   /* ---- Présentation bouton achat ------------------------------------- */
   /* -------------------------------------------------------------------- */
   var txtT = ''; var txtU = ''; var txtV = '';
   var rac = hrm_extern  ?  "http://www.lejardindeslivres.fr" : "";
   if ((livre[2] > 0)& (enc_liv == 0))
   {
    txtT += '<img src="'+rac+'/data/hrm-noliv.gif" onclick="add_book(this,\''+id+'\',false);" style="cursor:pointer;" title="Pour commander : '+livre[0]+'.">';
    txtU += '<img src="'+rac+'/data/hrm-noliv-s.gif" onclick="add_book(this,\''+id+'\',false);" style="cursor:pointer;" title="Pour commander : '+livre[0]+'.">';
    txtV += '<img src="'+rac+'/data/hrm-noliv-m.gif" onclick="add_book(this,\''+id+'\',false);" style="cursor:pointer;" title="Pour commander : '+livre[0]+'.">';
   }
   if ((livre[4] > 0)& (enc_pdf == 0))
   {
    txtT += '<img src="'+rac+'/data/hrm-nopdf.gif" onclick="add_book(this,\''+id+'\',true);" style="cursor:pointer;" title="Pour commander : '+livre[0]+'.">';
    txtU += '<img src="'+rac+'/data/hrm-nopdf-s.gif" onclick="add_book(this,\''+id+'\',true);" style="cursor:pointer;" title="Pour commander : '+livre[0]+'.">';
    txtV += '<img src="'+rac+'/data/hrm-nopdf-m.gif" onclick="add_book(this,\''+id+'\',true);" style="cursor:pointer;" title="Pour commander : '+livre[0]+'.">';
   }
   if (enc > 0)       /* Avec commande en cours, BT et BU BV               */
   {
      txtT += '<img src="'+rac+'/data/hrm-okcde.gif" onclick="document.location=\'#gocde\'" style="cursor:help;" title="Accès à votre panier">';
      txtU += '<img src="'+rac+'/data/hrm-okcde-s.gif" onclick="document.location=\'#gocde\'" style="cursor:help;" title="Accès à votre panier">';
      txtV += '<img src="'+rac+'/data/hrm-okcde-m.gif" onclick="document.location=\'#gocde\'" style="cursor:help;" title="Accès à votre panier">';
   }
   /* -------------------------------------------------------------------- */
   /* -------------------------------------------------------------------- */

   cls = (enc == 0) ? 'hrm-bt0' : 'hrm-bt1';
   var ls = document.hrm_idx[id];
   if (ls != undefined)
   {
      for (var j = 0; j < ls.length; j++)  /* Tous les boutons de cet ouvrage */
      {
         var idx = ls[j];
         var bt = document.getElementById( idx );
         var txx = txtT;
         if (idx.substr(1,1) == "U") txx = txtU;
         if (idx.substr(1,1) == "V") txx = txtV;
         bt.innerHTML = txx;
         bt.className = cls;
      }
   }
}

function refresh_cde()                    /* couton commande               */
{
   /* -------------------------------------------------------------------- */
   /* ---- Présentation bouton commande ---------------------------------- */
   /* -------------------------------------------------------------------- */
   var txt = '<b>Ma commande</b> :<ul>'; var go_cde = 0;
   var id = ''; var nbr = 0; var pxt = 0;
   var detail = ''
   var rac = hrm_extern  ?  "http://www.lejardindeslivres.fr" : "";
   for(id in document.hrm_enc)
   {
       var ck2 = document.hrm_enc[id].split(':');
       var livre = document.hrm_cat[id];  var pxl = 0;
       for (var ipdf = 0; ipdf < 2; ipdf++)
       {
          var ispdf = (ipdf == 1);
          var enc = ck2[ipdf+1] - 0;
          var pxu = ipdf ? livre[4] : livre[2];
          var typ = ispdf ? ' pdf' : ' volume(s)';
          if (enc > 0)
          {
             var in_enc = '<select onchange="bt_nbvol(this,\''+id+'\','+ispdf+');">'
             for (var j = 0; j < 10; j++){ var chk = ''; if (j == enc) chk = 'selected';
             in_enc = in_enc + '<option value="'+j+'" '+chk+' >'+j+'</option>';}
             in_enc = in_enc + '</select>';
             var px = enc * pxu;  nbr = nbr + enc; pxt = pxt + px;  pxl+= px;
             txt = txt + '<br><img src="'+rac+'/data/hrm-pl.gif" onclick="add_vol(\''+id+'\', 1,'+ispdf+');" style="cursor:pointer;" title="Augmenter d\'un exemplaire"> ';
             txt = txt + '<img src="'+rac+'/data/hrm-mn.gif" onclick="add_vol( \''+id+'\',-1,'+ispdf+');" style="cursor:pointer;" title="Diminuer d\'un exemplaire"> ';
             txt = txt +  livre[0] + ' ( ' + in_enc + typ + ', soit ' + px.toFixed(2) + '&nbsp;&euro;)';
             go_cde = 1;
          }
       }
       detail += '{'+ck2[0]+'}'+ck2[1]+'}'+livre[2]+'}'+(pxl.toFixed(2))+'}'+livre[0]+'}'+ck2[2]+'}'+livre[4]+'}';
   }
   txt = txt + '</ul>'
   txt = txt + '<br>Soit ' + nbr + ' volume(s) pour ' + pxt.toFixed(2) + '&nbsp;&euro;. &nbsp; ';
// txt = txt + "<button onclick=\"reset_encours();\" style=\"cursor:pointer;\">Recommencer</button>";
// txt = txt + "<br><em>Vous pouvez modifier les quantit&eacute;s indiqu&eacute;es,";
// txt = txt + "<br>ou annuler un volume en plaçant sa quantit&eacute; à 0 </em>";
   var auth = hrm_author == '' ? '' :",aut:\'"+hrm_author+"\'";
   var prm = hrm_extern ? ( "{enc:\'"+make_encours()+"\'"+auth+'}') : '{ok:1}'

   /* Il faudrait passer ces infos en POST                                 */
   if (document.hrm_goenc == 1)
    { txt = txt + "<button onclick=\'call_hermes(\"#cmd\");\' style=\"cursor:pointer;\">&Eacute;tape suivante</button>"; }

   if (document.hrm_goenc == 2)
    {
       achat[chx_lst] = detail.substr(1,9999); /* det : { cod } nb } px } lib } */
       calculate_port( liv_zpo, pxt, nbr );
    }

   /* -------------------------------------------------------------------- */
   /* -------------------------------------------------------------------- */

   if (!go_cde)
   {
      if (document.hrm_goenc == 2) {txt = "";}
      else txt = '<center>Vous n\'avez encore choisi aucun livre; pour commander, cliquez sur <a href="javascript:call_hermes(\'\');"><img src="'+rac+'/data/hrm-nocde-s.gif" border=0></a> sous chaque livre.<br>Votre commande vous est alors pr&eacute;sent&eacute;e, ici-m&ecirc;me.</center>';

   }

   cls = 'hrm-cde';

   /* De multiples boutons commande                                        */
   var ls = document.hrm_cde;
   for (var j = 0; j < ls.length; j++)
   {
      var idx = ls[j];
      var txt1 = '<a name="gocde">&nbsp;</a>'
      if (j > 0){ txt1 = '<a name="gocde'+j+'">&nbsp;</a>'; }
      var bt = document.getElementById( idx );
      bt.className = cls;
      bt.innerHTML = txt1 + txt;
   }
   document.hrm_nbr = nbr;  /* Pour passage en achat                       */
   document.hrm_pxt = pxt;
}


function bma_eb1 (chn)
{
   chn = chn.replace(  /^(\s*)/, "");
   chn = chn.replace(  /(\s*)$/, "");
   return chn.replace( /([ ]+)/g, " ");
}

function call_hermes(etq)
{
   var prm = new Array();
   if (hrm_author != '') prm['aut'] = hrm_author;
   if (hrm_extern)
   {
      prm['enc'] = make_encours();
      reset_encours();
   }
   to = "https://www.lejardindeslivres.fr/hermes/hermes.php"+etq;
   postwith(to, prm);
}

function postwith (to,p) {
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  document.body.removeChild(myForm) ;
}

/* ----------------------------------------------------------------------- */
/* Affichage d'une marque de saisie                                        */
/* un champ de saisie dispose de ces conditions                            */
/*             onfocus="beg_focus(this);" onblur="end_focus(this);"        */
/* ----------------------------------------------------------------------- */

function beg_focus(el_inp) {el_inp.nextSibling.innerHTML = "<img src=\"/hermes/focus-ok.gif\">"; }

function end_focus(el_inp) { el_inp.nextSibling.innerHTML = ''; }

function ers_focus(el_span) { em_span.innerHTML = ''; }

/* ----------------------------------------------------------------------- */
/* ----------------------------------------------------------------------- */

/* Initialisation de la programmation de la page                           */
   document.hrm_enc = new Array();   /* document.hrm_enc.<id> = <id>:<px>  */
   document.hrm_idx = new Array();
   document.hrm_cde = new Array();
   document.hrm_goenc = 0;

   get_cookie_encours();

