var Gfocus=false;
var Glastfocus=false;
var Goldmenu = false;
var Gobjcourant = false;
var Gcentrebody = false;

var resolut800 = screen.height == 800 ? 1 : 0;

function findPosX(obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  } else if (obj.x) curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  } else if (obj.y) curtop += obj.y;
  return curtop;
}

function menu(id, etat, fix)
{
  if (! Goldmenu) {
    Goldmenu = document.getElementById('accueil');
  }
  if (fix) {
    Goldmenu.src="/"+(resolut800 ? '800' : '1024')+"/images/"+Goldmenu.id+"-off-trans.png";
  }

 
  var newobj = document.getElementById(id);
  if (etat != 'off' || Goldmenu != newobj) { 
    newobj.src="/"+(resolut800 ? '800' : '1024')+"/images/"+id+"-"+etat+"-trans.png";
  }

  if (fix) {
    var ab = document.getElementById('abeille');
    ab.style.top=parseInt(newobj.style.top)-(resolut800 ? 17 : 22);
    Goldmenu = newobj; 

    var bo = document.getElementById('body');
    bo.className=id+(Gcentrebody ? ' centredabody' : '');

    var ifr = document.getElementById('main');
     ifr.src=id+'-main.'+(id != 'sccommande' ? 'html' : 'pl');
/*    ifr.src=id+'-main.html'; */
  } 
}

function switchimg(id, etat)
{
  var newobj = document.getElementById(id);
  if (etat != 'off' || Goldmenu != newobj) { 
    newobj.src="/"+(resolut800 ? '800' : '1024')+"/images/"+id+"-"+etat+"-trans.png";
  }
}

function switchtd(e, etat)
{
  if (!e) e =window.event;
  var obj = e.srcElement || e.target || e.fromElement;
  var preumscol = obj.parentNode.firstChild;
  preumscol.className=obj.className=etat;
}

function switchalltd(e, etat)
{
  if (!e) e =window.event;
  var obj = e.srcElement || e.target || e.fromElement;
  var tr = false;
  for (tr= obj.parentNode; tr && tr.name!='tr'; tr=tr.parentNode) ;
  var i;
  tr.className=etat;
 /* for (i; i < tr.childNodes.length; i++) {
    var td = tr.childNodes[i];
    td.className=etat;
  } */
}

function maintoucheEnter(e)
{
  if (!e) e =window.event;
  var key;
  if( typeof( e.keyCode ) == 'number'  ) {
    key = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    key = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    key = e.charCode;
  }
  if (key == 13) { location.href=document.getElementById('redirect').href; }
}

function switchalltd_on(e)
{
  if (!e) e =window.event;
  switchalltd(e, 'cmdon');
}
function switchalltd_off(e)
{
  if (!e) e =window.event;
  switchalltd(e, 'cmd');
}


function format_prix(va)
{
  var vl;
  if (va.match(/\./)==null) {
    vl =va+',00';
  } else {
    vl = va.replace(/\./, ',');
    while (vl.match(/\d\d$/)==null) {
      vl+='0';
    }
    if (vl.match(/^(\d*),(\d\d)\d*$/)!=null) {
      vl=RegExp.$1+','+RegExp.$2;
    }
  }

  return vl;
}

function changenbr(e)
{
  if (!e) e =window.event;
  var obj = e.srcElement || e.target || e.fromElement;

  var px = document.getElementById('px');
  var nbr = document.getElementById('nbr');
  var total = document.getElementById('total');
  
  var tmpn = obj.value;
 
  var tmpp = px.innerHTML.replace(/,/, '.'); 
  var tmpt = parseFloat(tmpp) * 100 * tmpn;
  tmpt/=100;
  nbr.innerHTML=tmpn;
  total.innerHTML=format_prix(''+tmpt);

}
function changeprix(e)
{
  if (!e) e =window.event;
  var obj = e.srcElement || e.target || e.fromElement;

  var dat = obj.value.split('|');
  var px = document.getElementById('px');
  var nbr = document.getElementById('nbr');
  var total = document.getElementById('total');
  
  var tmpp = dat[1];
  
  var tmpt = parseInt(nbr.innerHTML) * tmpp;

  px.innerHTML=format_prix(''+tmpp);
  total.innerHTML=format_prix(''+tmpt);
}

function perfthis(id, typepot, act)
{
  var nbr = document.getElementById('nbr');

  add_intoGcmd(id+'|'+typepot+'|'+nbr.innerHTML)
  cClick();
  if (act=='close') {
    parent.window.menu('commande', 'on', true);
  }
}

function addthis(id, typepot) { perfthis(id, typepot, 'add'); }
function closethis(id, typepot) { perfthis(id, typepot, 'close'); }

function focus_this(input) {
  if (input && Gfocus != input) {
    if (Gfocus) {
      Gfocus.className = '';

      if (Gfocus.type=='select-one') {
        var lbl=Gfocus.parentNode.childNodes[2];
        var inp=Gfocus.parentNode.childNodes[0]; if (inp) inp.style.display='none';
        var inp2=Gfocus.parentNode.childNodes[1];
        if (lbl) {
          inp2.style.display='none';
          lbl.style.display='';
        } else {
          inp2.style.display='';
        }
      }
    }
    Gfocus = input;
    input.className='over'; 
    try { input.focus(); input.select(); } catch (er) { }

    return 1;
  }
  return 0;
}

function switchinput(e) {
  if (!e) e =window.event;
  var obj = e.srcElement || e.target || e.fromElement || e;
  var i;

  var input = false;
  if (obj.type=="text") {
    input = obj;
  } else {
    for (i=0; i<obj.parentNode.childNodes.length; i++) {
      input = obj.parentNode.childNodes[i];
      if (input.nodeType != input.ELEMENT_NODE) continue;
    }
  }
 
  focus_this(input); 
}

function navig(e)
{
  if (!e) e =window.event;
  var key;
  var maj = false;
  if( typeof( e.keyCode ) == 'number'  ) {
    key = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    key = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    key = e.charCode;
  }
  maj = e.shiftKey;

  if (key != 13) return;
  var obj = e.srcElement || e.target || e.fromElement;

  if (false==obj || obj.type!='text') {
    switchinput(Gnavig[0]);
    return;
  }
  var i;
  var next = 0;
  for (i=0; i < Gnavig.length; i++) {
    if (Gnavig[i].name == obj.name) {
      if (maj) {
        if (i>0) i--;
        switchinput(Gnavig[i]);
        return;
      }
      next = 1;
    } else if (next == 1) {
      switchinput(Gnavig[i]);
      return;
    }
  }
 
  switchinput(Gnavig[0]);
}

function maj_quantite(e)
{
  if (!e) e =window.event;
  var obj = e.srcElement || e.target || e.fromElement || e;
  var lbl = obj.parentNode.childNodes[1];
  lbl.innerHTML=obj.value;
}

function populate_select_p(Aobj, gtarif, tpot) {
  var type=gtarif.type;
  var listpot = eval('Gtpots.'+type);
  var prixmiel=gtarif.prix;

  while (Aobj.options.length) Aobj.options[0] = null;
  var i;
  var vp = 0;
  for (i=0; i < listpot.length; i++) {
    if (prixmiel[i] != '-1') {
      Aobj.options[vp++] = new Option(listpot[i], i+1);
    }
  }
  Aobj.selectedIndex = parseInt(tpot)-1;
}

function populate_select_q(Aobj, Anbr)
{
  while (Aobj.options.length) Aobj.options[0] = null;
  var i;
  for (i=0; i < 49; i++) {
    Aobj.options[i] = new Option(i+1, i+1);
  }
  Aobj.selectedIndex = Anbr-1;
}

function dynamic_change_produit(e)
{
  if (!e) e =window.event;
  var obj = e.srcElement || e.target || e.fromElement;
  
  var trinp = document.getElementById('tradd');
  var ltpot = trinp.childNodes[1].childNodes[1];
  
  var tref = eval('Gtarif.p'+obj.value);
  populate_select_p(ltpot, tref, 1+ltpot.selectedIndex);
}

function change_visibility(tr, etat)
{
  var i;
  var td;
  if (!tr) return;
  for (i=0; i<tr.childNodes.length; i++) {
    td=tr.childNodes[i];
    td.style.visibility=etat;
  }
}

function quantite_over(e)
{
  if (!e) e =window.event;
  var obj = e.srcElement || e.target || e.fromElement;
  var tr = false;
  for (tr= obj.parentNode; tr && tr.name!='tr'; tr=tr.parentNode) ;

  /* on commence par restaurer la visibilite de l'ancienne ligne ayant le focus */
  change_visibility(Gobjcourant, 'visible');
  Gobjcourant = tr;
  change_visibility(Gobjcourant, 'hidden');
  /* puis on cache la courante */
  var px = findPosX(tr);
  var py = findPosY(tr);
  var trinp = document.getElementById('tradd');
  trinp.style.top = py-8;
  trinp.style.display='';

  /* nombre de pot */
  var nbr = tr.childNodes[0].childNodes[0].innerHTML;
  var code = tr.childNodes[1].childNodes[0].value;
  var tpot = tr.childNodes[1].childNodes[1].value;

  /* object nombre */
  var inp = trinp.childNodes[0].childNodes[0];
  populate_select_q(inp, parseInt(nbr));

  var lbl = trinp.childNodes[1].childNodes[0];
  lbl.onchange=null;
  populate_select_m(lbl, code);

  var tref = eval('Gtarif.p'+code);
  if (!tref) tref = eval('Gtarif.p'+Gfirstref);
//  var utype = tref ? tref.type : 'Miel';
  var ltpot = trinp.childNodes[1].childNodes[1];

  lbl.onchange=dynamic_change_produit;
 
  populate_select_p(ltpot, tref, tpot);
//  populate_select_p(ltpot, utype, tpot);

}

function variete_over(e)
{

/*
  if (!e) e =window.event;
  var obj = e.srcElement || e.target || e.fromElement;
  var lbl = obj.childNodes[2];
  var inp = obj.childNodes[0];
  if (lbl.style.display!='none') {
    if (focus_this(inp)) {
      Gsel=false;
      var inp2 = obj.childNodes[1];

      lbl.style.display='none';
      inp.style.display='';
      inp2.style.display='';
    } 
  } */
}

function write_quantite(nbr) {
  return (nbr ? '<font class="cmd">'+nbr+'</font>' : '<font class="bold">. . . . . . . .</font>');
}

function write_codeproduit(code, tpot)
{
  return '<input type=hidden value="'+code+'"><input type=hidden value="'+tpot+'">';

}
function write_produit(nbr, lbl, type, tpot) {
  return (nbr ? '<font class="cmd">'+lbl+' <i>'+get_type(type, tpot)+'</i></font>' : '<font class="bold">. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .</font>');
}

function add_cmd_ligne(tbl, brutdata)
{
  var code, tpot, nbr, lbl, tref, pu, montant;
  if (brutdata) {
    var data = new Array();
    data = brutdata.split('|');
    code=data[0]; tpot=data[1]; nbr=data[2];
    tref = eval('Gtarif.p'+code);
    lbl = unescape(tref.label);
    pu = tref.prix[parseInt(tpot)-1];
    montant = parseFloat(pu) * nbr;

    Gtotal += montant;
  } else {
    code=tpot=nbr=lbl=false;
  }

  var tb = tbl.tBodies[0];
  var tr = document.createElement('TR'); tr.className="cmd"; tr.name='tr';
  /* quantite */
  var td_q = document.createElement('TD'); td_q.className="cmd";
  td_q.innerHTML=write_quantite(nbr);
  td_q.onmouseover=switchalltd_on; td_q.onmouseout=switchalltd_off
  td_q.onclick=quantite_over;
  /* variete */
  var td_v = document.createElement('TD'); td_v.className="cmd";
  td_v.innerHTML = write_codeproduit(code, tpot);
  td_q.title = td_v.title = nbr ? "Cliquez ici pour modifier cette ligne" : "Cliquez ici pour ajouter un produit";
  /* prix unitaire */
  var td_p = document.createElement('TD'); td_p.className="cmdn";
  td_p.innerHTML=(nbr ? '<font class="cmd">'+format_prix(''+pu)+' &euro;</font>' : '<font class="bold">. . . . . . .</font>');
  /* montant */
  var td_m = document.createElement('TD'); td_m.className="cmdn";
  td_m.innerHTML=(nbr ? '<font class="cmd">'+format_prix(''+montant)+' &euro;</font>' : '<font class="bold">. . . . . . .</font>');

  tr.appendChild(td_q);
  tr.appendChild(td_v);
  tr.appendChild(td_p);
  tr.appendChild(td_m);

  tb.appendChild(tr);


  td_v.innerHTML+= write_produit(nbr, lbl, tref ? tref.type : false, tpot);
/* (nbr ? '<font class="cmd">'+lbl+' <i>'+get_type(tref.type, tpot)+'</i></font>' : '<font class="bold">. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .</font>'); */

  td_v.onmouseover=switchalltd_on; td_v.onmouseout=switchalltd_off
  td_v.onclick=quantite_over;
}

function nouvelle_cmd()
{
  try {
    parent.window.frames.ifracommande.Gcmd = new Array();
    cClick();
    parent.window.frames.menu('boutique', 'on', true);
  } catch (e) { }
}

function check_a_champ(code, label)
{
  var obj = eval('document.commande.'+code);

  if (obj && '' == obj.value) {
    var texterr=code!='tel' ? 'Vous devez renseigner le champ "<b>'+label+'</b>" !' :
                              'Pour que nous puissions vous contacter en cas de problèmes merci de renseigner le champ "<b>Téléphone ou Adresse email<b>".';
    overlib('<table class=tfixed><tr><td>'+texterr+'</td></tr><tr><td class=rt><a href=\'#\' onclick=\'cClick()\'>Retour</a></td></tr></table>', CAPTION, '<img src=images/warn.png class=bimg>Erreur', STICKY, EXCLUSIVE, WIDTH, 275);
    obj.focus();
    return false;
  }

  return true;
}
function check_champs()
{
  return (check_a_champ('nom', "Nom") &&
          check_a_champ('prenom', 'Pr&eacute;nom') &&
          check_a_champ('adresse', 'Adresse') &&
          check_a_champ('cpostal', 'Code Postal') &&
          check_a_champ('ville', 'Ville') &&
          check_a_champ('tel', 'T&eacute;l&eacute;phone'));
}

function transfert_data(action)
{
  var fvers = parent.window.document.pdf;
  var fde = document.commande;
  var tdate = new Date;
  fvers.action='/commande.pl?'+tdate.getTime();

  fvers.nom.value = fde.nom.value;
  fvers.prenom.value = fde.prenom.value;
  fvers.adresse.value = fde.adresse.value;
  fvers.ville.value = fde.ville.value;
  fvers.codepostal.value = fde.cpostal.value;
  fvers.tel.value = fde.tel.value;
  fvers.faction.value = action;
  fvers.port.value = document.getElementById('port').innerHTML.replace(/\s.*$/, " e");
  fvers.total.value = document.getElementById('apayer').innerHTML.replace(/\s.*$/, " e");

  var tbl = document.getElementById('tablecmd');
  var tb = tbl.tBodies[0];
  var i;
  fvers.quantite.value = fvers.variete.value = fvers.pu.value = fvers.montant.value = '';
/* 0 - entete 1 - ligne d'ajout dynamique */
  for (i=0; i < tb.childNodes.length; i++) {
    var uneligne = tb.childNodes[i];
    try {
      var quantite = uneligne.childNodes[0].childNodes[0].innerHTML;
      if (!(parseInt(quantite) > 0)) continue;
    } catch (er) {
      continue;
    }
    fvers.quantite.value+=quantite+'|';
    var variete = uneligne.childNodes[1].childNodes[2].innerHTML;
    fvers.variete.value+=variete+'|';
    var pu = uneligne.childNodes[2].childNodes[0].innerHTML.replace(/\s.*$/, " e");
    fvers.pu.value+=pu+'|';
    var montant = uneligne.childNodes[3].childNodes[0].innerHTML.replace(/\s.*$/, " e");
    fvers.montant.value+=montant+'|';
  }
  if (action == 'print') {
    fvers.target="_blank";
  } else {
    fvers.target='';
  }

  return fvers;
}

function perf_print()
{
  if (!check_champs()) return;
  var formu=transfert_data('print');
  formu.submit();
}

function perf_save()
{
  if (!check_champs()) return;
  var formu=transfert_data('save');
  formu.submit();
}
function ifrapopup(url)
{
  var ifra= document.getElementById('popup');
  ifra.src=url;
}
function ifrashow(x, y, width, height)
{
  var ifra= document.getElementById('popup');
  ifra.style.top=y;
  ifra.style.left=x;
  ifra.style.width=width;
  ifra.style.height=height;
  ifra.style.display='';
}
function FullSize()
{
 var bo = document.getElementById('body');
 var pa = document.getElementById('page');
  if (screen.height<=600) { /* resolution 800x600 ou - */
    if (bo.clientHeight<540) { /* on affiche la barre de scroll vertical */
      try { bo.style.overflow="-moz-scrollbars-vertical"; } catch (er) { }
      bo.style.overflowY='scroll'; 
    } /*else { * test centre 
      bo.className+=" centrebody";
      Gcentrebody = true; 
    }*/
  } else if (screen.height==768) { /* resolution 1024 * 768 */
    if (bo.clientHeight<705) { /* on affiche la barre de scroll vertical */
      try { bo.style.overflow="-moz-scrollbars-vertical"; } catch (er) { }
      bo.style.overflowY='scroll'; 
    }
  } else { /* test centre */
    bo.className+=" centredabody";
//    bo.className+=" centrebody";
    pa.className="centrebody"; 
    Gcentrebody = true; 
  }
 /*   bo.style.marginTop=0;
    bo.style.top=-20;*/

}

function remove_fromGcmd(lpos)
{
  parent.window.frames.ifracommande.Gcmd.splice(lpos, 1);
}

function add_intoGcmd(fid) {
  parent.window.frames.ifracommande.Gcmd.push(fid);
}

function replace_intoGcmd(lpos, fid)
{
  parent.window.frames.ifracommande.Gcmd[lpos]=fid;
}

function search_intoGcmd(fid)
{
  var r = -1;
  var i;
  var Lcmd = parent.window.frames.ifracommande.Gcmd;
  for (i=0; i < Lcmd.length; i++) {
    if (Lcmd[i] == fid) {
      return i;
    }
  }

  return r;
}

function extract_quantite(html)
{
  if (html.match(/([0-9]+)/)==null) {
    return -1;
  } else {
    return RegExp.$1;
  }
}

function maj_total()
{
  var i;
  var Lcmd = parent.window.frames.ifracommande.Gcmd;
  Gtotal = 0.0;
  for (i=0; i < Lcmd.length; i++) {
    var adata = Lcmd[i].split('|');
    var som = Number(eval('Gtarif.p'+adata[0]+'.prix['+(adata[1]-1)+']') * adata[2]).toFixed(2);
    Gtotal+=Number(parseFloat(som).toFixed(2))
  }
}

function Valider(valider)
{
  var tradd = document.getElementById('tradd');
  
  /* quantite */ 
  var objnbr = Gobjcourant.childNodes[0];
  /* variete */
  var objprod = Gobjcourant.childNodes[1]; 
  var oldcode = objprod.childNodes[0].value;
  var oldtype = objprod.childNodes[1].value;
  /* prix unitaire */
  var pu = Gobjcourant.childNodes[2];
  /* montant */
  var montant = Gobjcourant.childNodes[3];

  var selnbr = tradd.childNodes[0].childNodes[0];
  var selcode = tradd.childNodes[1].childNodes[0];
  var seltpot = tradd.childNodes[1].childNodes[1];

  var selnbrv = valider ? selnbr.value : false;

  var tref = eval('Gtarif.p'+selcode.value);
  if (valider) {
    var valpu = tref.prix[parseInt(seltpot.value)-1];
    pu.innerHTML='<font class="cmd">'+format_prix(''+valpu)+' &euro;</font>';
    var valmontant = valpu * selnbr.value;
    montant.innerHTML='<font class="cmd">'+format_prix(''+valmontant)+' &euro;</font>';
    objprod.innerHTML = write_codeproduit(selcode.value, seltpot.value)+
                        write_produit(selnbrv, unescape(tref.label), tref.type, seltpot.value);

    objnbr.title=objprod.title="Cliquez ici pour modifier cette ligne";
    var pos = search_intoGcmd(oldcode+'|'+oldtype+'|'+extract_quantite(objnbr.innerHTML));
    if (pos>-1) { replace_intoGcmd(pos, selcode.value+'|'+seltpot.value+'|'+selnbr.value); }
    else { add_intoGcmd(selcode.value+'|'+seltpot.value+'|'+selnbr.value); }
  } else {
    pu.innerHTML='<font class="bold">. . . . . . .</font>';
    montant.innerHTML='<font class="bold">. . . . . . .</font>';

    /* on cherche le premier produit correspondant dans Gcmd, et on l'efface */
    var pos = search_intoGcmd(oldcode+'|'+oldtype+'|'+extract_quantite(objnbr.innerHTML));
    if (pos>-1) { remove_fromGcmd(pos); }

    objprod.innerHTML = write_codeproduit('', '')+
                        write_produit(false, '', '', '');

    objnbr.title=objprod.title="Cliquez ici pour ajouter un produit";
  }

  objnbr.innerHTML = write_quantite(selnbrv);

  maj_total();
  maj_frais_port();

  tradd.style.display='none'; nd();
  change_visibility(Gobjcourant, 'visible');
  Gobjcourant=false;
}

function bootstrap()
{
  focus();
  var bs = document.getElementById('body');
  if (screen.height<=600) {
    /* mode 800 x 600 */
    var url='800/index.html';
    if (bs.clientHeight<540) {
      bs.style.backgroundcolor='#b6975c';
      document.getElementById('bootstrap').style.display='';
      document.getElementById('redirect').href=url;
    } else {
      top.location.href=url;
    }
  } else {
    /* mode 1024 x 768 et + */
    var url='1024/index.html';
    if (bs.clientHeight<705) {
      bs.style.backgroundcolor='#b6975c';
      document.getElementById('bootstrap').style.display='';
      document.getElementById('redirect').href=url;
    } else {
      top.location.href=url;
    }

  }
}
/* vim:et:ts=8:sts=2:sw=2 */

