/*****************************************************************************/
/* Llibreria JavaScript Botigues DRAC                                        */
/*****************************************************************************/
/*

=head1 NOM

B<Botigues DRAC - Llibreria JavaScript>

=head1 SINOPSI

   <script type="text/javascript" src="js/funcions.js"></script>

=head1 DESCRIPCIÓ

Llibreria estàndard de funcions JavaScript de les botigues. Aquesta llibreria es copia al directori C<js> de la botiga amb el nom C<funcions.js>. Tots els fonts web de la botiga fan I<include> d'aquesta llibreria o bé de la seva equivalent amb Ajax (L<fun1.js(n)>).



=cut
*/
/*****************************************************************************/
/*

=head1 FUNCIONS DE LA LLIBRERIA

=head2 FUNCIONS COMUNES A TOTES LES BOTIGUES (AJAX i NO AJAX)

Això és, funcions que són B<iguals> tant en aquesta llibreria com en la llibreria equivalent de botigues Ajax (L<fun1.js(n)>).

=cut
*/
/*****************************************************************************/
/*

=head3 obre_bigi (port, lang, imag, text, widt, heig)

=over

=item DESCRIPCIÓ

Obre una finestra flotant d'amplada C<widt> i alcada C<heig>, centrada a la pantalla, per mostrar una imatge ampliada (habitualment, les imatges dels productes).

=item PARÀMETRES

   port: nom del portal
   lang: idioma
   imag: url de la imatge
   text: text que s'utilitza com a text alternatiu de la imatge i com a titol de la finestra
   widt: amplada de la finestra (px)
   heig: alcada de la finestra  (px)

=back

=cut
*/

function obre_bigi (port, lang, imag, text, widt, heig) {

   if (widt == '') widt = 200;
   if (heig == '') heig = 200;

   left = Math.round ((screen.availWidth  - widt) / 2);
   ttop = Math.round ((screen.availHeight - heig) / 2);

   if ( port ) {
      iurl = '/appl/botiga/bigi.php';
   } else {
      iurl = 'bigi.php';
   }

   bigi = window.open (iurl+'?lang='+lang+'&imag='+imag+'&text='+escape(text), 'bigi', 'left='+left+',top='+ttop+',width='+widt+',height='+heig+',resizable=yes,scrollbars=no,toolbar=no,locationbar=no,statusbar=no');

   bigi.focus ();

}

/*

=head3 obre_conf (port, lang, fami, cist)

=over

=item DESCRIPCIÓ

Obre el formulari de confirmacio de la comanda (L<conf(n)>).

=item PARÀMETRES

   port: nom del portal
   lang: idioma
   fami: codi de la familia que s'estava visualitzant al cataleg
   cist: objecte del portal que pinta la cistella

=back

=cut
*/

function obre_conf (port, lang, fami, cist) {

   if ( ! port ) {
      document.location = 'conf.php?lang='+lang+'&fami='+fami;
   } else {
      document.location = '/'+lang+'/'+cist+'/3/'+fami;
   }

}

/*

=head3 obre_paga (port, lang, win7, fpag, gift, ucom, upro, upai, nofp, nogi, noco, widt, heig)

=over

=item DESCRIPCIÓ

Obre el formulari de pagament (L<pag0(n)> / L<pag1(n)> / L<pag2(n)>)

=item PARÀMETRES

   port: nom del portal
   lang: idioma
   win7: mode d'obertura de la finestra ($BTG_WIN7)
   fpag: codi de la forma de pagament (1-5)
   gift: valor la variable global $BTG_GIFT
   ucom: valor la variable global $BTG_UCOM
   upro: valor la variable global $BTG_UPRO
   upai: valor la variable global $BTG_UPAI
   nofp: text d'avís quan l'usuari no ha triat la forma de pagament
   nogi: text d'avís quan l'usuari ha emplenat només parcialment els camps de tramesa de regal
   noco: text d'avís quan l'usuari no ha marcat l'acceptació de les condicions de compra
   widt: amplada de la finestra (px) quan s'obre en finestra flotant
   heig: alcada de la finestra  (px) quan s'obre en finestra flotant

=back

=cut
*/

function obre_paga (port, lang, win7, fpag, gift, ucom, upro, upai, nofp, nogi, noco, widt, heig) {

   if ( fpag > 1 ) {
      if ( document.getElementById ('conf').paga.value == '' ) {
         alert (nofp);
         document.getElementById ('conf').conf_pag[0].focus();
         return false;
      }
   }
   if ( ! document.getElementById ('conf').cond.checked ) {
      alert (noco);
      document.getElementById ('conf').cond.focus();
      return false;
   }
   if ( gift ) {
      err = 0;
      ple = 0;
      if ( document.getElementById ('conf').conf_gift_noms.value == '' ) {
         if ( ple == 1 ) {
            err = 1;
         }
      } else {
         ple = 1;
      }
      if ( document.getElementById ('conf').conf_gift_addr.value == '' ) {
         if ( ple == 1 ) {
            err = 1;
         }
      } else {
         if ( ple == 0 ) {
            err = 1;
         }
      }
      if ( document.getElementById ('conf').conf_gift_pobl.value == '' ) {
         if ( ple == 1 ) {
            err = 1;
         }
      } else {
         if ( ple == 0 ) {
            err = 1;
         }
      }
      if ( document.getElementById ('conf').conf_gift_zipc.value == '' ) {
         if ( ple == 1 ) {
            err = 1;
         }
      } else {
         if ( ple == 0 ) {
            err = 1;
         }
      }
      if ( ucom ) {
         if ( document.getElementById ('conf').conf_gift_coma.value == '' ) {
            if ( ple == 1 ) {
               err = 1;
            }
         } else {
            if ( ple == 0 ) {
               err = 1;
            }
         }
      }
      if ( upro ) {
         if ( document.getElementById ('conf').conf_gift_prov.value == '' ) {
            if ( ple == 1 ) {
               err = 1;
            }
         } else {
            if ( ple == 0 ) {
               err = 1;
            }
         }
      }
      if ( upai ) {
         if ( document.getElementById ('conf').conf_gift_pais.value == '' ) {
            if ( ple == 1 ) {
               err = 1;
            }
         } else {
            if ( ple == 0 ) {
               err = 1;
            }
         }
      }
      if ( document.getElementById ('conf').conf_gift_text.value != '' ) {
         if ( ple == 0 ) {
            err = 1;
         }
      }
      if ( err == 1 ) {
         alert(nogi);
         return false;
      }
   }

   if ( port ) {

      switch (win7) {

         case 'blank':

            window.open ('/appl/botiga/paga.php?lang=$lang&paga='+document.getElementById ('conf').paga.value, 'paga');
            paga.focus ();

            break;

         case 'flot':

            if (widt == '') widt = 550;
            if (heig == '') heig = 500;
   
            left = Math.round ((screen.availWidth  - widt) / 2);
            ttop = Math.round ((screen.availHeight - heig) / 2);

            window.open ('/appl/botiga/paga.php?lang=$lang&paga='+document.getElementById ('conf').paga.value, 'paga', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width='+widt+',height='+heig+',top='+ttop+',left='+left+',resizable=yes');

            break;

         default:

            document.getElementById ('conf').action = '/appl/botiga/paga.php';
            document.getElementById ('conf').submit();

      }

   } else {

      switch (win7) {

         case 'blank':

            window.open ('paga.php?lang=$lang&paga='+document.getElementById ('conf').paga.value, 'paga');

            break;

         case 'flot':

            if (widt == '') widt = 550;
            if (heig == '') heig = 500;
   
            left = Math.round ((screen.availWidth  - widt) / 2);
            ttop = Math.round ((screen.availHeight - heig) / 2);

            window.open ('paga.php?lang=$lang&paga='+document.getElementById ('conf').paga.value, 'paga', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width='+widt+',height='+heig+',top='+ttop+',left='+left+',resizable=yes');

            break;

         default:

            document.getElementById ('conf').action='paga.php';
            document.getElementById ('conf').submit();

      }

   }

}

/*

=head3 obre_prod (port, lang, fami, codi, win1, widt, heig)

=over

=item DESCRIPCIÓ

Obre el formulari de la fitxa del producte

=item PARÀMETRES

   port: nom del portal
   lang: codi de l'idioma
   fami: codi de la familia
   codi: codi del producte
   win1: mode d'obertura de la finestra ($BTG_WIN1)
   widt: amplada de la finestra (px) quan s'obre en finestra flotant
   heig: alcada de la finestra  (px) quan s'obre en finestra flotant

=back

=cut
*/

function obre_prod (port, lang, fami, codi, win1, widt, heig) {

   if ( port ) {
      purl = '/appl/botiga/dpro.php';
   } else {
      purl = 'dpro.php';
   }

   switch (win1) {

      case "blank":

         wpro = window.open (purl+'?lang='+lang+'&codi='+codi+'&fami='+fami, 'prod');
         wpro.focus();

         break;

      case "flot":

         if (widt == '') widt = 550;
         if (heig == '') heig = 500;

         left = Math.round ((screen.availWidth  - widt) / 2);
         ttop = Math.round ((screen.availHeight - heig) / 2);

         wpro = window.open (purl+'?lang='+lang+'&codi='+codi+'&fami='+fami, 'prod', 'left='+left+',top='+ttop+',width='+widt+',height='+heig+',resizable=yes,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');

         wpro.focus();

         break;

   }

}

/*

=head3 obre_prnt (lang, widt, heig)

=over

=item DESCRIPCIÓ

Obre una finestra flotant amb la comanda, la imprimeix i la torna a tancar.

=item PARÀMETRES

   lang: codi de l'idioma
   widt: amplada de la finestra (px)
   heig: alcada de la finestra  (px)

=back

=cut
*/

function obre_prnt (lang, widt, heig) {

   if (widt == '') widt = 550;
   if (heig == '') heig = 500;

   left = Math.round ((screen.availWidth  - widt) / 2);
   ttop = Math.round ((screen.availHeight - heig) / 2);

   window.open ('/appl/botiga/resu.php?lang='+lang+'&prnt=1', 'prnt', 'left='+left+',top='+ttop+',width='+widt+',height='+heig+',resizable=yes,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');

}

/*

=head3 obre_pswd (port, lang, fami, usid, win6, widt, heig)

=over

=item DESCRIPCIÓ

   Obre el formulari de recupració de contrasenya (L<usid.php>).

=item PARÀMETRES

   port: nom del portal
   lang: codi de l'idioma
   fami: codi de la familia
   usid: valor de la variable global $BTG_BACK_USID
   win6: mode d'obertura de la finestra ($BTG_WIN66)
   widt: amplada de la finestra (px) quan s'obre en finestra flotant
   heig: alcada de la finestra  (px) quan s'obre en finestra flotant

=back

=cut
*/

function obre_pswd (port, lang, fami, usid, win6, widt, heig) {

   if ( port ) {
      purl = '/appl/botiga/usid.php';
   } else {
      purl = 'usid.php';
   }

   switch (win6) {

      case "blank":

         pswd = window.open (purl+'?lang='+lang+'&fami='+fami+'&codi=3', 'pswd');
         pswd.focus();

         break;

      case "flot":

         if (widt == '') widt = 400;
         if (heig == '') heig = 300;

         left = Math.round ((screen.availWidth  - widt) / 2);
         ttop = Math.round ((screen.availHeight - heig) / 2);

         pswd = window.open (purl+'?lang='+lang+'&fami='+fami+'&codi=3', 'pswd', 'left='+left+',top='+ttop+',width='+widt+',height='+heig+',resizable=yes,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
         pswd.focus();

         break;

      default:

         if ( ! port ) {
            document.getElementById ('nopa').href = 'usid.php?lang='+lang+'&codi=3';
         } else {
            document.getElementById ('nopa').href = '/'+lang+'/'+usid+'/6/'+fami+'/3';
         }

   }

}

/*

=head3 obre_regi (port, lang, fami, vure, regi, win5, widt, heig)

=over

=item DESCRIPCIÓ

   Obre el formulari de registre de dades d'usuari (L<regi(n)>).

=item PARÀMETRES

   port: nom del portal
   lang: codi de l'idioma
   fami: codi de la familia
   vure: '1' per forçar l'usuari a registrar-se, buit altrament
   regi: valor de la variable global $BTG_BACK_REGI
   win5: mode d'obertura de la finestra ($BTG_WIN5)
   widt: amplada de la finestra (px) quan s'obre en finestra flotant
   heig: alcada de la finestra  (px) quan s'obre en finestra flotant

=back

=cut
*/

function obre_regi (port, lang, fami, vure, regi, win5, widt, heig) {

   if ( port ) {
      purl = '/appl/botiga/regi.php';
   } else {
      purl = 'regi.php';
   }

   switch (win5) {

      case "blank":

         regw = window.open (purl+'?lang='+lang+'&fami='+fami+'&codi='+vure, 'regi');
         regw.focus();

         break;

      case "flot":

         if (widt == '') widt = 550;
         if (heig == '') heig = 500;

         left = Math.round ((screen.availWidth  - widt) / 2);
         ttop = Math.round ((screen.availHeight - heig) / 2);

         regi = window.open (purl+'?lang='+lang+'&fami='+fami+'&codi='+vure, 'regi', 'left='+left+',top='+ttop+',width='+widt+',height='+heig+',resizable=yes,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
         regi.focus();

         break;

      default:

         if ( ! port ) {
            document.getElementById ('vure').href = 'regi.php?lang='+lang+'&fami='+fami+'&codi='+vure;
            document.getElementById ('usid').action = 'regi.php?lang='+lang+'&fami='+fami+'&codi='+vure;
         } else {
            if ( vure ) {
               document.getElementById ('vure').href = '/'+lang+'/'+regi+'/5/'+fami+'/1';
               document.getElementById ('usid').action = '/'+lang+'/'+regi+'/5/'+fami+'/1';
            } else {
               document.getElementById ('vure').href = '/'+lang+'/'+regi+'/5/'+fami;
               document.getElementById ('usid').action = '/'+lang+'/'+regi+'/5/'+fami;
            }
         }
         document.getElementById ('usid').submit ();

   }

}

/*

=head3 obre_text (port, lang, text, widt, heig)

=over

=item DESCRIPCIÓ

Obre el formulari de presentació de textos estàndard (L<text(n)>).

=item PARÀMETRES

   port: nom del portal
   lang: codi de l'idioma
   text: codi del text (1-5)
   widt: amplada de la finestra (px) quan s'obre en finestra flotant
   heig: alcada de la finestra  (px) quan s'obre en finestra flotant

=back

=cut
*/

function obre_text (port, lang, text, widt, heig) {

   if (widt == '') widt = 550;
   if (heig == '') heig = 500;

   left = Math.round ((screen.availWidth  - widt) / 2);
   ttop = Math.round ((screen.availHeight - heig) / 2);

   if ( port ) {
      window.open ('/appl/botiga/dtxt.php?lang='+lang+'&codi='+text+'&head=1', 'text', 'left='+left+',top='+ttop+',width='+widt+',height='+heig+',resizable=no,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
   } else {
      window.open ('dtxt.php?lang='+lang+'&codi='+text+'&head=1', 'text', 'left='+left+',top='+ttop+',width='+widt+',height='+heig+',resizable=no,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
   }

}

/*

=head3  usid_subm (blnk)

=over

=item DESCRIPCIÓ

Comprova que l'usuari ha introduït un usuari i una contrasenya. En aquest cas, fa el I<submit> del formulari per registrar-lo; en cas contrari dóna un missatge d'avís.

=item PARÀMETRES

   blnk: missatge d'avís quan l'usuari no ha emplenat els dos camps d'usuari i contrasenya

=back

=cut
*/

function usid_subm (blnk) {

   if ( (/^\s*$/.test (document.getElementById ('usid').usid_uid.value)) ||
        (/^\s*$/.test (document.getElementById ('usid').usid_pwd.value)) ) {
      alert (blnk);
      if ( /^\s*$/.test (document.getElementById ('usid').usid_uid.value) ) {
         document.getElementById ('usid').usid_uid.focus ();
      } else {
         document.getElementById ('usid').usid_pwd.focus ();
      }
   } else {
      document.getElementById ('usid').submit ();
   }

}

/*****************************************************************************/
/*

=head2 FUNCIONS ESPECÍFIQUES DE LES BOTIGUES NO AJAX

Això és, tres funcions que existeixen també a la llibreria de botigues Ajax (L<fun1.js(n)>), amb els mateixos paràmetres, però amb funcionalitat lleugerament diferent: C<obre_cist ()>, C<updt_cist()> i C<updt_prod ()>.

=cut
*/
/*****************************************************************************/

/*

=head3 obre_cist (port, lang, fami, prod, cist, form, win1, vars)

=over

=item DESCRIPCIÓ

Obre el formulari de la cistella de compra (L<cist(n)>).

=item PARÀMETRES

   port: nom del portal
   lang: idioma
   fami: codi de la familia que s'esta visualitzant al cataleg
   prod: codi del producte que s'afegeix a la cistella
   cist: objecte del portal que pinta la cistella
   form: nom del formulari des del qual s'accedeix a la cistella
   win1: mode d'obertura de la finestra ($BTG_WIN1)
   vars: valors per defecte de les variacions de producte

=back

=cut
*/

function obre_cist (port, lang, fami, prod, cist, form, win1, vars) {

   if ( form == 'cata' ) {

      document.getElementById ('cata').oper.value = '2'; 
      document.getElementById ('cata').fami.value = fami;
      document.getElementById ('cata').insr.value = prod;
      document.getElementById ('cata').vars.value = vars;
      if ( ! port ) {
         document.getElementById ('cata').action = 'cist.php';
      } else {
         document.getElementById ('cata').action = '/'+lang+'/'+cist+'/2/'+fami;
      }
      document.getElementById ('cata').submit ();

   } else {

      varv = new Array ();
      varp = vars.split (':');
   
      for (v = 0; v < varp.length; v++) {

         eval ("varr = document.getElementById ('prod').vari_"+v);
   
         switch (varr.type) {
            case "select-one":
               varv.push (varr.value);
               break;
            case "checkbox":
               if ( varr.checked ) {
                  varv.push ('0');
               } else {
                  varv.push ('');
               }
               break;
            case "hidden":
            case "text":
               varv.push (varr.value);
               break;
            default:
               c = '';
               for (r = 0; r < varr.length; r++) {
                  if ( varr[r].checked ) {
                     c = r;
                     break;
                  }
               }
              varv.push (c);
         }

      }

      vars = varv.join (':');

      if ( win1 != 'self' ) {

         window.opener.document.getElementById ('cata').oper.value = '2';
         window.opener.document.getElementById ('cata').insr.value = prod;
         window.opener.document.getElementById ('cata').vars.value = vars;
         if ( ! port ) {
            window.opener.document.getElementById ('cata').action = 'cist.php';
         } else {
            window.opener.document.getElementById ('cata').action = '/'+lang+'/'+cist+'/2/'+fami;
         }
         window.opener.document.getElementById ('cata').submit ();
         window.close ();

      } else {

         document.getElementById ('prod').oper.value = '2';
         document.getElementById ('prod').insr.value = prod;
         document.getElementById ('prod').vars.value = vars;
         if ( ! port ) {
            document.getElementById ('prod').action = 'cist.php';
         } else {
            document.getElementById ('prod').action = '/'+lang+'/'+cist+'/2/'+fami+'/'+prod;
         }
         document.getElementById ('prod').submit ();

      }

   }

}

/*

=head3 updt_cist (name, lang, fami, dele, insr, modi, quan, vars)

=over

=item DESCRIPCIÓ

Recarrega el formulari de la cistella de compra (L<cist(n)>) quan l'usuari hi fa modificacions.

=item PARÀMETRES

   name: objecte del portal que pinta la cistella
   lang: idioma
   fami: codi de la familia que s'esta visualitzant al cataleg
   dele: codi d'ordre del producte que es vol suprimir
   insr: codi del producte que es vol afegir
   modi: codi d'ordre del producte que es vol modificar
   quan: quantitat del producte afegit/modificat
   vars: codi de la variació que s'està modificant (si és el cas)

=back

=cut
*/

function updt_cist (name, lang, fami, dele, insr, modi, quan, vars) {

   document.getElementById ('cist').dele.value = dele;
   document.getElementById ('cist').modi.value = modi;
   document.getElementById ('cist').vars.value = vars;
   document.getElementById ('cist').quan.value = quan;
   document.getElementById ('cist').submit ();

}

/*

=head3 updt_prod (win1, nvar, puni, quni)

=over

=item DESCRIPCIÓ

Refresca el preu i el pes a la fitxa del producte (L<prod(n)>) d'acord amb les variacions triades

=item PARÀMETRES

   win1: mode d'obertura de la finestra ($BTG_WIN1)
   nvar: nombre de variacions del producte
   puni: literal de les unitats de preu
   quni: literal de les unitats de pes

   Tots els parametres hi ssóon nomes per compatibilitat amb la funcio de les botigues ajax, no cal instanciar-los

=back

=cut
*/

function updt_prod (win1, nvar, puni, quni) {

   document.getElementById('prod').submit();

}


function view_prod (prod, e, x) {
   if ( /_\d+$/.test (prod) ) {
      ipro = prod.replace (/_\d+$/, '');
      for (p = 1; p <= e; p++) {
         document.getElementById (ipro+'_'+p).style.display = 'none';
      }
      if ( x == 1 ) {
         xtop = document.body.scrollTop || document.documentElement.scrollTop
         ttop = xtop + 50;
         document.getElementById (prod).style.top = ttop +  'px';
      }
   } else {
      for (p = 0; p < e; p++) {
         document.getElementById ('prod'+p).style.display = 'none';
      }
      if ( x == 1 ) {
         xtop = document.body.scrollTop || document.documentElement.scrollTop
         ttop = xtop + 50;
         document.getElementById (prod).style.top = ttop +  'px';
      }
   }
   document.getElementById (prod).style.display = 'block';
}


function switch_img (e, n) {
   for (i = 1; i <= n; i++) {
      document.getElementById ('dpro_' + i).style.display = 'none';
   }
   document.getElementById ('dpro_' + e).style.display = 'block';
}

/*****************************************************************************/
/*

=head1 VEGEU TAMBÉ

L<Botiga(n)>, L<bigi(n)>, L<botiga(n)>, L<cerc(n)>, L<cist(n)>, L<conf(n)>, L<dpro(n)>, L<dtxt(n)>, L<fun1.js(n)>, L<pag0(n)>, L<pag1(n)>, L<pag2(n)>, L<regi(n)>, L<resu(n)>, L<usid(n)>, L<warn(n)>

=head1 AUTOR

J. Badiella A. L<jbadiella@drac.com>

DRAC telemàtic L<http://www.drac.com>

Terrassa, Maig 2009  

=cut
*/
/*****************************************************************************/

