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

var nomdivdesti = "";


function actualitza_div (tip, nom, url) {
   nomdivdesti = nom;
   ajaxRequest(tip, url);
}


function ajaxResponseHandler (responseText) {
   src = ajax_http.responseText
   document.getElementById (nomdivdesti).innerHTML = src;
   ini = src.indexOf ('<script type="text/javascript">');
   if ( ini > -1 ) {
      ini = ini + 31;
      end = ajax_http.responseText.indexOf ('</script>', ini);
      javascript = ajax_http.responseText.slice (ini, end);
      eval (javascript);
   }
}


function cist_void (lang, fami, mssg) {
   if ( confirm (mssg) ) {
      updt_cist ('cistmain', lang, fami, '-1', '', '', '', '');
   }
}


function obre_bigi (lang, img, alt, wid, hei) {
   if (wid == '') wid = 200;
   if (hei == '') hei = 200;
   lef = Math.round ((screen.availWidth  - wid) / 2);
   ttp = Math.round ((screen.availHeight - hei) / 2);
   window.open ('/appl/botiga/bigi.php?lang='+lang+'&img='+img+'&alt='+escape(alt), 'bigi', 'left='+lef+',top='+ttp+',width='+wid+',height='+hei+',resizable=yes,scrollbars=no,toolbar=no,locationbar=no,statusbar=no');
}


function obre_cata (port, lang, cata, mssg) {
   
   if ( document.getElementById ('cist').modi.value == '1' ) {
      document.getElementById ('cist').save.focus ();
      if ( confirm (mssg) ) {
         gosh = true;
      } else {
         gosh = false;
      }
   } else {
      gosh = true;
   }
   if ( gosh ) {
      if ( port ) {
         document.getElementById ('cist').oper.value = '';
         document.getElementById ('cist').action = cata;
         document.getElementById ('cist').submit ();
      } else {
         document.getElementById ('cist').action = cata;
         document.getElementById ('cist').submit ();
      }
   }

}


function obre_cist (lang, fami, insr, form, quan, vari) {
   varv = new Array ();
   vars = vari.split (':');
   for (i = 0; i < vars.length; i++) {
      if ( vars[i] == '1' ) {
         varx = eval ('document.getElementById (form).vari_'+insr+'_'+i);
         varv[i] = varx.value;
      } else {
         varv[i] = '';
      }
   }
   vari = varv.join (':');
   updt_cist ('cistmain', lang, fami, '', insr, '', quan, vari);
}  


/*
function obre_conf (lang, fami, cist, tpre, cmin, hmin) {
   if ( parseFloat (tpre) < parseFloat (cmin) ) {
      alert (hmin);
   } else {
      document.getElementById ('cist').action = '/'+lang+'/'+cist+'/3/'+fami;
      document.getElementById ('cist').submit ();
   }
}
*/


function obre_prnt (lang) {

   wid = 700;
   hei = 500;
   lef = Math.round ((screen.availWidth  - wid) / 2);
   ttp = Math.round ((screen.availHeight - hei) / 2);

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

}


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

   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 (win6) {
         case 'blank':
            window.open ('/appl/botiga/paga.php?lang=$lang&paga='+document.getElementById ('conf').paga.value, 'paga', 'toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,width=800,height=600,top=0,left=0,resizable=yes');
            paga.focus ();
            break;
         case 'flot':
            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=600,height=400,top=100,left=100,resizable=no');
            break;
         default:
            document.getElementById ('conf').action = '/appl/botiga/paga.php';
            document.getElementById ('conf').submit();
      }
   } else {
      switch (win6) {
         case 'blank':
            window.open ('paga.php?lang=$lang&paga='+document.getElementById ('conf').paga.value, 'paga', 'toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,width=800,height=600,top=0,left=0,resizable=yes');
            break;
         case 'flot':
            paga = window.open ('paga.php?lang=$lang&paga='+document.getElementById ('conf').paga.value, 'paga', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=600,height=400,top=100,left=100,resizable=no');
            break;
         default:
            document.getElementById ('conf').action='paga.php';
            document.getElementById ('conf').submit();
      }
   }

}


function obre_pswd (lang, win66) {
   wid = 500; 
   hei = 300;
   lef = Math.round ((screen.availWidth  - wid) / 2);
   ttp = Math.round ((screen.availHeight - hei) / 2);
   window.open ('/appl/botiga/pswd.php?lang='+lang, 'pswd', 'left='+lef+',top='+ttp+',width='+wid+',height='+hei+',resizable=yes,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
}


function obre_prod (port, lang, cata, prod, codi, win1) {
   if ( port ) {
      switch (win1) {
         case 'blank':
            window.open ('/'+lang+'/'+prod+'/1/'+codi);
            break;
         case 'flot':
            wid = 600;
            hei = 500;
            lef = Math.round ((screen.availWidth  - wid) / 2);
            ttp = Math.round ((screen.availHeight - hei) / 2);
            window.open ('/'+lang+'/'+prod+'/1/'+codi, 'prod', 'left='+lef+',top='+ttp+',width='+wid+',height='+hei+',resizable=yes,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
            break;
         default:
            document.getElementById ('cata').oper.value = '1';
            document.getElementById ('cata').codi.value = codi;
            document.getElementById ('cata').action = '/'+lang+'/'+prod+'/1/'+codi;
            document.getElementById ('cata').submit();
      }
   } else {
      switch (win1) {
         case 'blank':
            window.open ('dpro.php?lang='+lang+'&codi='+codi);
            break;
         case 'flot':
            wid = 600;
            hei = 500;
            lef = Math.round ((screen.avalWidth - wid) / 2);
            ttp = Math.round ((screen.avalWidth - hei) / 2);
            window.open ('dpro.php?lang='+lang+'&codi='+codi, 'prod', 'left='+lef+',top='+ttp+',width='+wid+',height='+hei+',resizable=yes,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
            break;
         default:
            document.getElementById ('cata').codi.value = codi;
            document.getElementById ('cata').action = 'dpro.php';
            document.getElementById ('cata').submit();
      }
   }
}


function obre_regi (port, lang, fami, regi, win5) {
   
   if ( port ) {
      switch (win5) {
         case 'blank':
            window.open ('/'+lang+'/'+regi+'/5/'+fami, 'regi', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=800,height=600,top=0,left=0,resizable=no');
            break; 
         case 'flot':
            wid = 700;
            hei = 500;
            lef = Math.round ((screen.availWidth  - wid) / 2);
            ttp = Math.round ((screen.availHeight - hei) / 2);
            window.open ('/'+lang+'/'+regi+'/5/'+fami, 'regi', 'left='+lef+',top='+ttp+',width='+wid+',height='+hei+',resizable=yes,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
            break;
         default:
            window.location = '/'+lang+'/'+regi+'/5/'+fami;
      }
   } else {
      switch (win5) {
         case 'blank':
            window.open ('regi.php?lang='+lang, 'regi', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=800,height=600,top=0,left=0,resizable=no');
            break;
         case 'flot':
            wid = 700;
            hei = 500;
            lef = Math.round ((screen.availWidth  - wid) / 2);
            ttp = Math.round ((screen.availHeight - hei) / 2);
            window.open ('regi.php?lang='+lang, 'regi', 'left='+lef+',top='+ttp+',width='+wid+',height='+hei+',resizable=yes,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
            break;
         default:
            window.location = 'regi.php?lang='+lang;
      }
   }

}


function obre_text (port, lang, fami, text, tobj, wino) {
   if ( port ) {
      switch (wino) {
         case 'blank':
            window.open ('/'+lang+'/'+tobj+'/9/'+fami+'/'+text);
            break;
         case 'flot':
            wid = 600;
            hei = 400;
            lef = Math.round ((screen.availWidth  - wid) / 2);
            ttp = Math.round ((screen.availHeight - hei) / 2);
            window.open ('/'+lang+'/'+tobj+'/9/'+fami+'/'+text, 'text', 'left='+lef+',top='+ttp+',width='+wid+',height='+hei+',resizable=no,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
            break;
         default:
            window.location = '/'+lang+'/'+tobj+'/9/'+fami+'/'+text;
      }
   } else {
      switch (wino) {
         case 'blank':
            window.open ('/appl/botiga/text.php?lang='+lang+'&codi='+text+'&head=1');
            break;
         case 'flot':
            wid = 600;
            hei = 400;
            lef = Math.round ((screen.availWidth  - wid) / 2);
            ttp = Math.round ((screen.availHeight - hei) / 2);
            window.open ('/appl/botiga/text.php?lang='+lang+'&codi='+text+'&head=1', 'text', 'left='+lef+',top='+ttp+',width='+wid+',height='+hei+',resizable=no,scrollbars=yes,toolbar=no,locationbar=no,statusbar=no');
            break;
         default:
            window.location = '/appl/botiga/text.php?lang='+lang+'&codi='+text+'&head=1';
      }
   }
}

function regi_clos (port, lang, back, win5, canc) {
   if ( canc == '1' ) {
      if ( win5 == 'blank' || win5 == 'flot' ) {
         window.close ();
      } else {
         window.location = back;
      }
   } else {
      if ( win5 == 'blank' || win5 == 'flot' ) {
         window.opener.location = back;
         window.close ();
      } else {
         window.location = back;
      }
   }
}


function updt_cist (name, lang, fami, dele, insr, modi, quan, vari) {
   actualitza_div ('GET', name, '/appl/botiga/cist.php?lang='+lang+'&fami='+fami+'&dele='+dele+'&insr='+insr+'&modi='+modi+'&quan='+quan+'&vari='+vari);
}


function usid_subm (blnk) {

   if ( document.getElementById ('usid').usid_uid.value == '' ||
        document.getElementById ('usid').usid_pwd.value == '' ) {
      alert (blnk);
      if ( 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 setcook (ses, uid, init, expi, noc2, noc3) {
   if ( (document.cookie.indexOf ("BTG_SES") == -1) ||
        (document.cookie.indexOf ("BTG_UID") == -1) ) {
      if ( document.cookie.indexOf ("BTG_CTL") >- 1 ) {
         alert(noc3);
      }
      vdat = new Date ();
      vdat.setTime (vdat.getTime () + expi);
      document.cookie = "BTG_SES=" + ses +"; path=/; expires=" + vdat.toGMTString ();
      if ( document.cookie.indexOf ("BTG_UID") == -1 ) {
         document.cookie = "BTG_UID=" + uid + "; path=/; expires=" + vdat.toGMTString ();
      }
      if ( (document.cookie.indexOf ("BTG_SES") == -1) ||
           (document.cookie.indexOf ("BTG_UID") == -1) ) {
         alert(noc2);
         if (init) document.location = init;
      } else {
         document.cookie = "BTG_CTL=1; path=/;";
      }
   } else {
      /* Per si de cas havien tancat el navegador */
      if ( document.cookie.indexOf ("BTG_CTL") == -1 ) {
         document.cookie = "BTG_CTL=1; path=/;";
      }
      /* Refresquem la caducitat de les galetes */
      vdat = new Date ();
      vdat.setTime (vdat.getTime () + expi);
      document.cookie = "BTG_SES=" + ses + "; path=/; expires=" + vdat.toGMTString ();
      document.cookie = "BTG_UID=" + uid + "; path=/; expires=" + vdat.toGMTString ();
      /* document.cookie = "BTG_KEY=" + key + "; path=; expires=" + vdat.toGMTString (); */
      
   }
}

function testcook (init, expi, noc1, noc2, noc3) {
   if ( document.cookie.length <= 0 ) {
      /* El navegador no tÃ© cap galeta; possiblement les tÃ© desactivades */
      alert (noc1);
      if (init) document.location = init;
      return false;
   } else {
      s = document.cookie.indexOf ("BTG_SES");
      u = document.cookie.indexOf ("BTG_UID");
      if ( s == -1 || u == -1 ) { 
         /* El navegador tÃ© les galetes activades perÃ² no tÃ© les galetes */
         /* de la botiga: aixÃ² vol dir que ha arribat aquÃ­ sense passar per */
         /* cist.php, o bÃ© no ha acceptat les galetes quan se li han demanat, */
         /* o bÃ© que li han caducat */
         if ( document.cookie.indexOf ("BTG_CTL") > -1 ) {
            /* Galetes caducades */
            vdat = new Date ();
            vdat.setTime (vdat.getTime () - expi);
            document.cookie = "BTG_CTL=1; path=/; expires=" + vdat.toGMTString ();
            alert(noc3);
         } else {
            /* AccÃ©s indegut */
            alert(noc2);
         }
         if (init) document.location = init;
         return false;
      } else {
         /* Refresquem la caducitat de les galetes */
         si = s + 8;
         se = document.cookie.indexOf (";", si);
         if ( se == -1 ) {
            ses = document.cookie.substr (si);
         } else {
            ses = document.cookie.substr (si, se);
         }
         ui = u + 8;
         ue = document.cookie.indexOf (";", ui);
         if ( ue == -1 ) {
            uid = document.cookie.substr (ui);
         } else {
            uid = document.cookie.substr (ui, ue);
         }
         vdat = new Date ();
         vdat.setTime (vdat.getTime () + expi);
         document.cookie = "BTG_SES=" + ses + "; path=/; expires=" + vdat.toGMTString ();
         document.cookie = "BTG_UID=" + uid + "; path=/; expires=" + vdat.toGMTString ();
         /* document.cookie = "BTG_KEY=" + key + "; path=; expires=" + vdat.toGMTString (); */
         return true;
      }
   }
}

/*****************************************************************************/
/* J. Badiella A. jbadiella@drac.com                                         */
/* DRAC telemàtic  http://www.drac.com                                       */
/* Terrassa, Abril 2008                                                      */
/*****************************************************************************/

