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

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

   /* Obre una finestra flotant d'amplada 'wid' i alcada 'hei', centrada a   */
   /* la pantalla, per mostrar la imatge amplida del producte.               */
   /*                                                                        */
   /*   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 dinestra (px)                                    */
   /*   heig: alcada de la finestra  (px)                                    */

   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 ();

}


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

   /* Obre el formulari de confirmacio de la comanda                         */
   /*                                                                        */
   /*    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                      */

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

}


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();

      }

   }

}


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

   /* Obre la fitxa del producte en finestra nova o flotant                  */ 
   /*                                                                        */ 
   /* port: nom del portal                                                   */ 
   /* lang: codi de l'idioma                                                 */ 
   /* fami: codi de la familia                                               */ 
   /* codi: codi del producte                                                */ 
   /* win1: valor la variable global $BTG_WIN1                               */ 
   /* widt: amplada de la finestra (px)                                      */ 
   /* heig: alcada de la finestra  (px)                                      */ 

   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;

   }

}


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');

}


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

   /* Obre el formulri de contrasenya                                        */ 
   /*                                                                        */ 
   /* port: nom del portal                                                   */ 
   /* lang: codi de l'idioma                                                 */ 
   /* fami: codi de la familia                                               */ 
   /* usid: valor de la variable global $BTG_BACK_USID                       */ 
   /* win6: valor la variable global $BTG_WIN66                              */ 
   /* widt: amplada de la finestra (px)                                      */ 
   /* heig: alcada de la finestra  (px)                                      */ 

   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';
         }

   }

}


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

   /* Obre el registre d'usuari                                              */ 
   /*                                                                        */ 
   /* 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: valor la variable global $BTG_WIN5                               */ 
   /* widt: amplada de la finestra (px)                                      */ 
   /* heig: alcada de la finestra  (px)                                      */ 

   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 ();

   }

}


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');
   }

}


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 ();
   }

}

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

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

   /* Obre el formulari de la cistella de compra                             */
   /*                                                                        */
   /*    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: codi d'obertura de la fitxa de producte (BTG_WIN1)            */
   /*    vars: valors per defecte de les variacions de producte              */

   if ( form == 'cata' ) {

      document.getElementById ('cata').oper.value = '2'; 
      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 ();

      }

   }

}


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 ();

}


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

   /* Refresca el preu i el pes a la fitxa del producte d'acord amb les      */
   /* variacions triades                                                     */
   /*                                                                        */
   /*    win1: codi d'obertura de la fitxa de producte (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 son nomes per compatibilitat amb la funcio de   */
   /* les botigues ajax, no cal instanciar-los                               */

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

}

/*****************************************************************************/
/* J. Badiella A. jbadiella@drac.com                                         */
/* DRAC telematic  http://www.drac.com                                       */
/* Terrassa, Octubre 2008                                                   */
/*****************************************************************************/
