var WinPosX,WinPosY;

function authenticate(){
        var tmpkeyword=document.forms['searchkeyword'].elements['K'].value;
        if(tmpkeyword!=''){
                self.location.href='result.php?<?=SID?>&K='+tmpkeyword;

        }
}

function searchkeyword(){
        var tmpkeyword=document.forms['searchkeyword'].elements['K'].value;
        if(tmpkeyword!=''){
                self.location.href='result.php?<?=SID?>&K='+tmpkeyword;

        }
}

function searchmarque(){
        var tmpindex=document.forms['searchmarque'].elements['M'].selectedIndex;
        var tmpkeyword = document.forms['searchmarque'].elements['M'].options[tmpindex].value;
        if(tmpindex!=0){
                self.location.href='result.php?<?=SID?>&M='+tmpkeyword;

        }
}

function searchfamily(myfamily){
        alert('recherche par famille');
}

function search(type) {
  if (type==1)
       self.document.forms['SEARCH'].submit();
  else if (type==2) {
       // Pas de recherche de comptabilité si toutes les infos ne sont indiquées
       if ( self.document.forms['COMPAT'].marqueImprimante.selectedIndex==0 ||
            self.document.forms['COMPAT'].familleImprimante.selectedIndex==0 ||
            self.document.forms['COMPAT'].Imprimante.selectedIndex==0 ) return;
       self.document.forms['COMPAT'].submit();
  }
  else if (type==3)
       self.document.forms['RECHART'].submit();
}

function PopOpen(myname, mywidth, myheight){
        centerWindow(mywidth, myheight);
        window.open('',myname,'width='+mywidth+',height='+myheight+',screenX='+WinPosX+',screenY='+WinPosY+',left='+WinPosX+',top='+WinPosY+',location=no,toolbar=no,directories=no,status=no,scrollbars=no,menubar=no');
}

function PopOpenV2(myname, mywidth, myheight){
        centerWindow(mywidth, myheight);
        window.open('',myname,'width='+mywidth+',height='+myheight+',screenX='+WinPosX+',screenY='+WinPosY+',left='+WinPosX+',top='+WinPosY+',location=no,toolbar=no,directories=no,status=no,scrollbars=yes,menubar=no');
}

function PopOpenV3(src, myname, mywidth, myheight){
        centerWindow(mywidth, myheight);
        window.open(src,myname,'width='+mywidth+',height='+myheight+',screenX='+WinPosX+',screenY='+WinPosY+',left='+WinPosX+',top='+WinPosY+',location=no,toolbar=no,directories=no,status=no,scrollbars=no,menubar=no');
}

function centerWindow(WinWidth,WinHeight)
{
        var xMax;
        var yMax;
        xMax = screen.width;
        yMax = screen.height;
            WinPosX = (xMax - WinWidth) / 2;
        WinPosY = (yMax - WinHeight) / 2;
}

var roundnumber = 0;
var TableChar = new Array("-","*","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");

function rndcalculator(){
        roundnumber  = Math.floor(Math.random()*TableChar.length);
}

function GetRndCode(num){
        CODE = "";i=0;
        while(i<num){
                rndcalculator();
                CODE = CODE + TableChar[roundnumber];
                i++;
        }
        return(CODE);
}

//  Détection du navigateur
Nom = navigator.appName;
Version = navigator.appVersion.substr(0,1);
//alert(Nom+'/'+Version);
ns5 = (Nom == 'Netscape' && Version > '4' ) ? 1:0;
ns4 = (document.layers) ? 1:0;
ns3 = (Nom == 'Netscape' && Version < '4' ) ? 1:0;
ie4 = (Nom == 'Microsoft Internet Explorer' && Version >= '4' ) ? 1:0;
ie3 = (Nom == 'Microsoft Internet Explorer' && Version < '4' ) ? 1:0;
//alert(ns5+' '+ns4+' '+ns3+' '+ie4+' '+ie3)
function afficDate() {
        var tmois = new Array ("janvier", "février", "mars", "avril", "mai", "juin", "juillet",
                               "août", "septembre", "octobre", "novembre", "décembre")
        var kdate=new Date();
        var annee=kdate.getYear()
        if (annee < 1000) annee+=1900;
        var day=kdate.getDay();
        var mois=tmois[kdate.getMonth()];
        //if (mois<10) mois="0"+mois;
         var jour=kdate.getDate()
        //if (jour<10) jour="0"+jour;
        document.write(jour+" "+mois+" "+annee)
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

ie4 = (document.all)? true:false

function showit() {
if (ie4) {newshome.style.visibility = "visible"}
setTimeout("hideit()",2000)
}

function hideit() {
if (ie4) {newshome.style.visibility = "hidden"}
setTimeout("showit()",300)
}
function buttEnter(nomdoc) {
         document.forms[nomdoc].submit();
         return true;
}

function Crypt(s) {r='';for(i=0;i<s.length;i++){n=s.charCodeAt(i); if (n>=8364) {n = 128;} r += String.fromCharCode( n - 3 ); }return r;}

function afficEmailjs(namemail,sitemail,objetmail,libmail) {
 var a, s, n;

 a ="pdlowr=";
 m='&#64;';d=unescape(m);
 var aro = namemail + d + sitemail + '?subject=' + objetmail;
 document.write('<a h' + 'ref="' + Crypt(a) + aro + '">' + libmail + '</a>');
}

function showpub()
        {
        //if(document.getElementById && ScanCookie("supersticiel")==0)
        if(document.getElementById)
                {
                document.getElementById("pub").style.width = larg;
                document.getElementById("pub").style.height = haut;
                document.getElementById("pub").innerHTML = html_haut+html_mil+html_bas;
                //setTimeout("closepub()",10000);
                //CreationCookie("supersticiel",false);
                }
        }

function closepub()
        {
        if(document.getElementById)
                {
                document.getElementById("pub").style.width = 1;
                document.getElementById("pub").style.height = 1;
                document.getElementById("pub").innerHTML = '';
                }
        }

function ScanCookie(variable)
        {
        cook = document.cookie;
        variable += "=";
        place = cook.indexOf(variable,0);
        if (place <= -1)
                return("0");
        else
                {
                end = cook.indexOf(";",place)
                if (end <= -1)
                        return(unescape(cook.substring(place+variable.length,cook.length)));
                else
                        return(unescape(cook.substring(place+variable.length,end)));
                }
        }

function CreationCookie(nom,valeur,permanent)
        {
        if(permanent)
                {
                dateExp = new Date(2020,11,11);
                dateExp = dateExp.toGMTString();
                ifpermanent = '; expires=' + dateExp + ';';
                }
        else
                ifpermanent = '';
        document.cookie = nom + '=' + escape(valeur) + ifpermanent;
        }

function listeImprimante(step, f) {

  var l1    = f.elements["marqueImprimante"];
  var l2    = f.elements["familleImprimante"];
  var l3    = f.elements["Imprimante"];
  var index1 = l1.selectedIndex;   // alert(l1.options[index1].value);
  var index2 = l2.selectedIndex;  //  alert(l2.options[index2].value);
  if(index1 < 1) {
     l2.options.length = 1;
     l3.options.length = 1;
  }
  else {
      if(index2 < 1 || step == 1)   l3.options.length = 1;

      var xhr_object = null;

      if(window.XMLHttpRequest) // Firefox
         xhr_object = new XMLHttpRequest();
      else if(window.ActiveXObject) // Internet Explorer
         xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
      else { // XMLHttpRequest non supporté par le navigateur
         alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
         return;
      }

      xhr_object.open("POST", "c.rechercheprinter.php", true);

      xhr_object.onreadystatechange = function() {
             if(xhr_object.readyState == 4)
                 eval(xhr_object.responseText);
               //  alert(xhr_object.responseText);
          }

      xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      if (step == 1)
           var data = "M="+escape(l1.options[index1].value)+"&form="+f.name+"&select=familleImprimante";
      else
           var data = "M="+escape(l1.options[index1].value)+"&F="+escape(l2.options[index2].value)+"&form="+f.name+"&select=Imprimante";

      xhr_object.send(data);
   }
 //  alert(data);
}

var resultat;
function addPanier(form, larg, haut) {

 var post_prix = document.forms[form].prix.value;
 var post_ref  = document.forms[form].ref.value;
 var post_idpromo = document.forms[form].idpromo.value;
 var post_idref = document.forms[form].idref.value;
 var post_qte = document.forms[form].qte.value;
 var post_NbRefPlus = document.forms[form].NbRefPlus.value;
  //alert(post_ref);
 var ajxFile = "ajoutpanier.php";
 var affichageEnShadowbox = 1;
  $.post(ajxFile, { prix: post_prix, ref: post_ref, idpromo: post_idpromo, idref: post_idref, qte: post_qte, NbRefPlus: post_NbRefPlus },
              function(data){ resultat = data;
                         //   alert(larg+'/'+haut);
                         //   ouvre_fen();
                              if (affichageEnShadowbox) {
                                     // affichage Shadowbox
                                     Shadowbox.open({
                                                content:   resultat,
                                                player :   'iframe',
                                                height:     haut,
                                                width:      larg,
                                                options: {onClose: function () { /*window.location.reload();*/ }}
                                      });
                                      //resultat.indexOf("$nb=", position);
                                      //part1 = resultat.substr(position, resultat.length)
                                     // alert(resultat)
                                      arrparam = resultat.split('&');
                                      var part_num=0;
                                      var nbart=0;
                                      var montant=0;
                                      while (part_num < arrparam.length)
                                      {
                                          pos = arrparam[part_num].indexOf("nb=", 0);
                                          if (pos!=-1) { nbart = arrparam[part_num].substr(3,arrparam[part_num].length);
                                                         part_num+=1;
                                                         continue;
                                                       }
                                          pos = arrparam[part_num].indexOf("mont=", 0);
                                          if (pos!=-1)  montant = arrparam[part_num].substr(5,arrparam[part_num].length);
                                          part_num+=1;
                                      }
                                      if (nbart && montant) {
                                          if (nbart==1)  textS = ''; else textS = 's';
                                          document.getElementById('panierRecap').innerHTML = nbart+' Article'+textS+' ............  '+montant.replace(".",",")+' &euro;<br/><a href="panier.htm">&gt; D&eacute;tails</a>';
                                          document.getElementById('panierPicto').innerHTML = '<a href="panier.htm"><img src="img/panier.png" alt="panier" title="panier" class="panier"/></a>';
                                      }
                                      // rechargement du panier si ajout article en complément
                                     // if (form.indexOf('artpanier',0)==0)  window.location.reload();
                              }
                              else { // affichage plein écran
                                    document.write(resultat);
                              }
                            }
        );
}
