//******************************************************************************
// CLASE ELEMENTO DE COMPRA
//******************************************************************************

function ElementoCompra(referencia, producto, precio, cantidad){
// CONSTRUCTOR: Crea un elemento de compra
   this.referencia=referencia;
   this.producto=producto;
   this.precio=precio;
   this.cantidad=cantidad;
}

ElementoCompra.prototype.obtenerReferencia = function(){
   return this.referencia;
}

ElementoCompra.prototype.establecerReferencia = function(referencia){
   this.referencia=referencia;
}

ElementoCompra.prototype.obtenerProducto = function(){
   return this.producto;
}

ElementoCompra.prototype.establecerProducto = function(producto){
   this.producto=producto;
}

ElementoCompra.prototype.establecerRefencia = function(referencia){
   this.referencia=referencia;
}

ElementoCompra.prototype.obtenerPrecio = function(){
   return this.precio;
}

ElementoCompra.prototype.establecerPrecio = function(precio){
   this.precio=Number(precio);
}

ElementoCompra.prototype.obtenerCantidad = function(){
   return this.cantidad;
}

ElementoCompra.prototype.establecerCantidad = function(cantidad){
   this.cantidad=Number(cantidad);
}


//******************************************************************************
// CLASE CESTA DE LA COMPRA
//******************************************************************************

function CestaCompra(){
   this.elementos=new Object();
   this.numero_elementos=0;
   this.gastos_envio="6.00";
}

CestaCompra.prototype.anhadirElemento = function(referencia, producto, precio, cantidad){
   if(typeof(this.elementos[referencia])=="undefined"){
      this.elementos[referencia]=new ElementoCompra(referencia, producto, precio, cantidad);
      this.numero_elementos++;
   }else{
      this.elementos[referencia].establecerCantidad(cantidad);
   }
}

CestaCompra.prototype.eliminarElemento = function(referencia){
   if(typeof(this.elementos[referencia])!="undefined"){
      delete this.elementos[referencia];
      this.numero_elementos--;
   }
}

CestaCompra.prototype.fijarDecimales = function(numero,num_decimales){
// Pone un número con 'num_decimales' decimales. Ej: fijarDecimalles(2.379, 2) --> "2.38"
   var div=Math.pow(10,num_decimales);
   var ceros_requeridos;
   var localizacion_punto;

   numero=new String((Math.round(numero*div))/div);
   localizacion_punto=numero.lastIndexOf(".");
   if(localizacion_punto==-1){
      numero=numero+".";
      ceros_requeridos=num_decimales;
   }else{
      ceros_requeridos=num_decimales-(numero.length - localizacion_punto);
   }
   for(;ceros_requeridos>0;ceros_requeridos--){
      numero=numero+"0";
   }
   return numero;
}

CestaCompra.prototype.obtenerCestaHTML = function(es_actualizable){
   var cestaHTML="";
   var elemento;
   var subtotal_elemento=0;
   var total_compra=0;

   // Si se puede actualizar
   if(es_actualizable){ 
      // Crear formulario
      cestaHTML="<form method='post' action='' name='formularioCesta'>";
   }

   // Crear tabla
   cestaHTML+="<table width=\"550\" border=\"0\" align=\"center\"><tr>";

   // Cabecera de la tabla
   cestaHTML+="<td width=\"209\" bgcolor=\"#3399CC\"><div align=\"center\"><font size=\"3\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>PRODUCTO</font></b></div></td>";
   cestaHTML+="<td width=\"74\" bgcolor=\"#3399CC\"><div align=\"center\"><font size=\"3\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>PRECIO</font></b></div></td>";
   cestaHTML+="<td width=\"66\" bgcolor=\"#3399CC\"><div align=\"center\"><font size=\"3\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>UNID.</font></b></div></td>";
   cestaHTML+="<td width=\"96\" bgcolor=\"#3399CC\"><div align=\"center\"><font size=\"3\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>SUBTOTAL</font></b></div></td>";
   cestaHTML+="<td width=\"83\" bgcolor=\"#3399CC\"><div align=\"center\"><font size=\"3\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>BORRAR</font></b></div></td></tr>";

   // Bucle con los elementos de la compra
   for(elemento in this.elementos){

      // Ver producto
      cestaHTML+="<tr><td width=\"209\" bgcolor=\"#CCCCCC\"><div align=\"center\"><b><font face=\"Times New Roman, Times, serif\" color=\"#0000FF\" size=\"2\">";
      cestaHTML+=this.elementos[elemento].obtenerProducto();
      cestaHTML+="</font></b></div></td>";

      // Ver precio
      cestaHTML+="<td width=\"74\" bgcolor=\"#CCCCCC\"><div align=\"center\"><b><font face=\"Times New Roman, Times, serif\" color=\"#0000FF\" size=\"2\">";
      cestaHTML+=this.fijarDecimales(this.elementos[elemento].obtenerPrecio(),2) + " &euro;";
      cestaHTML+="</font></b></div></td>";

      // Ver unidades
      cestaHTML+="<td width=\"66\" bgcolor=\"#CCCCCC\"><div align=\"center\"> <b><font face=\"Times New Roman, Times, serif\" color=\"#0000FF\" size=\"2\">";
      if(es_actualizable){ // Si se puede modificar la cantidad
         cestaHTML+="<input name=\""+this.elementos[elemento].obtenerReferencia()+"\" type=\"text\" maxlength=\"3\" size=\"4\" value=\""+this.elementos[elemento].obtenerCantidad()+"\">";
      }else{
         cestaHTML+=this.elementos[elemento].obtenerCantidad();
      }
      cestaHTML+="</font></b></div></td>";

      // Ver subtotal
      subtotal_elemento = (this.elementos[elemento].obtenerCantidad()) * (this.elementos[elemento].obtenerPrecio());
      cestaHTML+="<td width=\"96\" bgcolor=\"#CCCCCC\"><div align=\"center\"><b><font face=\"Times New Roman, Times, serif\" color=\"#0000FF\" size=\"2\">";
      cestaHTML+= this.fijarDecimales(subtotal_elemento,2) +" &euro;";
      cestaHTML+="</font></b></div></td>";

      // Calcular total compra
      total_compra+=subtotal_elemento;

      // Ver eliminar producto
      cestaHTML+="<td width=\"83\" bgcolor=\"#CCCCCC\"><div align=\"center\"><b><font face=\"Times New Roman, Times, serif\" color=\"#0000FF\" size=\"2\">";
      cestaHTML+="<a href=\"javascript:eliminarProducto("+this.elementos[elemento].obtenerReferencia()+");\"><img src=\"imagenes/eliminar_producto.gif\" width=\"20\" height=\"20\" border=\"0\" alt=\"Eliminar producto\"></a>";
      cestaHTML+="</font></b></div></td></tr>";
   }

   // Ver el total de la compra
   cestaHTML+="<tr><td width=\"209\"><font size=\"3\" face=\"Times New Roman, Times, serif\"><b></b></font></td>";
   cestaHTML+="<td width=\"74\"><font size=\"3\" face=\"Times New Roman, Times, serif\"><b></b></font></td>";
   cestaHTML+="<td width=\"66\" bgcolor=\"#FFCC99\"><div align=\"center\"><font size=\"3\" face=\"Times New Roman, Times, serif\"><b><font color=\"#000000\">TOTAL</font></b></font></div></td>";
   cestaHTML+="<td width=\"96\" bgcolor=\"#FFCC99\"><div align=\"center\"><font size=\"3\" face=\"Times New Roman, Times, serif\"><b>";
   cestaHTML+= this.fijarDecimales(total_compra,2) +" &euro;";
   cestaHTML+="</b></font></div></td>";
   cestaHTML+="<td width=\"83\"><font size=\"3\" face=\"Times New Roman, Times, serif\"><b></b></font></td></tr>";

   // Fin de la tabla 
   cestaHTML+="</table>";

   // Ver botones
   if(es_actualizable){ // Si se puede modificar la cantidad
      // Fin formulario
      cestaHTML+="<br><br><table width=\"550\" border=\"0\" align=\"center\"><tr>";
      cestaHTML+="<td><div align=\"center\"><a href=\"javascript:comprar();\"><img src=\"imagenes/Boton_comprar_desactivado.gif\" width=\"111\" name=\"comprar\" onMouseOver=\"activar('comprar',1);\" onMouseOut=\"desactivar('comprar',1);\" height=\"21\" border=\"0\"></a></div></td>";
      cestaHTML+="<td><div align=\"center\"><a href=\"javascript:recalcular();\"><img src=\"imagenes/Boton_recalcular_desactivado.gif\" name=\"recalcular\" onMouseOver=\"activar('recalcular',2);\" onMouseOut=\"desactivar('recalcular',2);\" width=\"111\" height=\"21\" border=\"0\"></a></div></td>";
      cestaHTML+="<td><div align=\"center\"><a href=\"javascript:vaciar_cesta();\"><img src=\"imagenes/Boton_vaciar_cesta_desactivado.gif\" name=\"vaciar_cesta\" onMouseOver=\"activar('vaciar_cesta',3);\" onMouseOut=\"desactivar('vaciar_cesta',3);\" width=\"111\" height=\"21\" border=\"0\"></a></div></td>";
      cestaHTML+="<td><div align=\"center\"><a href=\"javascript:volver();\"><img src=\"imagenes/Boton_volver_desactivado.gif\" name=\"volver\" onMouseOver=\"activar('volver',4);\" onMouseOut=\"desactivar('volver',4);\" width=\"111\" height=\"21\" border=\"0\"></a></div></td>";
      cestaHTML+="</tr></table>";
   }else{
      cestaHTML+="<br><br><div align=\"center\">";
      cestaHTML+="<a href=\"javascript:volver();\"><img src=\"imagenes/Boton_volver_desactivado.gif\" name=\"volver\" onMouseOver=\"activar('volver',1);\" onMouseOut=\"desactivar('volver',1);\" width=\"111\" height=\"21\" border=\"0\"></a>";
      cestaHTML+="</div>";
   }

   return cestaHTML;
}

CestaCompra.prototype.obtenerCompraHTML = function(es_actualizable){
   var cestaHTML="";
   var elemento;
   var subtotal_elemento=0;
   var total_compra=0;

   // Si se puede actualizar
   if(es_actualizable){ 
      // Crear formulario
      cestaHTML="<form method='post' action='' name='formularioCesta'>";
   }

   // Crear tabla
   cestaHTML+="<table width=\"550\" border=\"0\" align=\"center\"><tr>";

   // Cabecera de la tabla
   cestaHTML+="<td width=\"226\" bgcolor=\"#3399CC\"><div align=\"center\"><font size=\"3\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>PRODUCTO</font></b></div></td>";
   cestaHTML+="<td width=\"85\" bgcolor=\"#3399CC\"><div align=\"center\"><font size=\"3\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>PRECIO</font></b></div></td>";
   cestaHTML+="<td width=\"111\" bgcolor=\"#3399CC\"><div align=\"center\"><font size=\"3\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>UNIDADES</font></b></div></td>";
   cestaHTML+="<td width=\"110\" bgcolor=\"#3399CC\"><div align=\"center\"><font size=\"3\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><b>SUBTOTAL</font></b></div></td></tr>";

   // Bucle con los elementos de la compra
   for(elemento in this.elementos){

      // Ver producto
      cestaHTML+="<tr><td width=\"226\" bgcolor=\"#CCCCCC\"><div align=\"center\"><b><font face=\"Times New Roman, Times, serif\" color=\"#0000FF\" size=\"2\">";
      cestaHTML+=this.elementos[elemento].obtenerProducto();
      cestaHTML+="</font></b></div></td>";

      // Ver precio
      cestaHTML+="<td width=\"85\" bgcolor=\"#CCCCCC\"><div align=\"center\"><b><font face=\"Times New Roman, Times, serif\" color=\"#0000FF\" size=\"2\">";
      cestaHTML+= this.fijarDecimales(this.elementos[elemento].obtenerPrecio(),2) + " &euro;";
      cestaHTML+="</font></b></div></td>";

      // Ver unidades
      cestaHTML+="<td width=\"111\" bgcolor=\"#CCCCCC\"><div align=\"center\"> <b><font face=\"Times New Roman, Times, serif\" color=\"#0000FF\" size=\"2\">";
      if(es_actualizable){ // Si se puede modificar la cantidad
         cestaHTML+="<input name=\""+this.elementos[elemento].obtenerReferencia()+"\" type=\"text\" maxlength=\"3\" size=\"4\" value=\""+this.elementos[elemento].obtenerCantidad()+"\">";
      }else{
         cestaHTML+=this.elementos[elemento].obtenerCantidad();
      }
      cestaHTML+="</font></b></div></td>";

      // Ver subtotal
      subtotal_elemento = (this.elementos[elemento].obtenerCantidad()) * (this.elementos[elemento].obtenerPrecio());
      cestaHTML+="<td width=\"110\" bgcolor=\"#CCCCCC\"><div align=\"center\"><b><font face=\"Times New Roman, Times, serif\" color=\"#0000FF\" size=\"2\">";
      cestaHTML+= this.fijarDecimales(subtotal_elemento,2) +" &euro;";
      cestaHTML+="</font></b></div></td></tr>";

      // Calcular total compra
      total_compra+=subtotal_elemento;
   }

   // Ver el total de la compra
   cestaHTML+="<tr><td width=\"226\"><font size=\"3\" face=\"Times New Roman, Times, serif\"><b></b></font></td>";
   cestaHTML+="<td width=\"85\"><font size=\"3\" face=\"Times New Roman, Times, serif\"><b></b></font></td>";
   cestaHTML+="<td width=\"111\" bgcolor=\"#FFCC99\"><div align=\"center\"><font size=\"3\" face=\"Times New Roman, Times, serif\"><b><font color=\"#000000\">TOTAL</font></b></font></div></td>";
   cestaHTML+="<td width=\"110\" bgcolor=\"#FFCC99\"><div align=\"center\"><font size=\"3\" face=\"Times New Roman, Times, serif\"><b>";
   cestaHTML+= this.fijarDecimales(total_compra,2) +" &euro;";
   cestaHTML+="</b></font></div></td></tr>";

   // Fin de la tabla 
   cestaHTML+="</table>";

   // Ver botones
   if(es_actualizable){ // Si se puede modificar la cantidad
      // Fin formulario
      cestaHTML+="<br><br><table width=\"550\" border=\"0\" align=\"center\"><tr>";
      cestaHTML+="<td><div align=\"center\"><a href=\"javascript:comprar();\"><img src=\"imagenes/Boton_comprar_desactivado.gif\" width=\"111\" name=\"comprar\" onMouseOver=\"activar('comprar',1);\" onMouseOut=\"desactivar('comprar',1);\" height=\"21\" border=\"0\"></a></div></td>";
      cestaHTML+="<td><div align=\"center\"><a href=\"javascript:recalcular();\"><img src=\"imagenes/Boton_recalcular_desactivado.gif\" name=\"recalcular\" onMouseOver=\"activar('recalcular',2);\" onMouseOut=\"desactivar('recalcular',2);\" width=\"111\" height=\"21\" border=\"0\"></a></div></td>";
      cestaHTML+="<td><div align=\"center\"><a href=\"javascript:vaciar_cesta();\"><img src=\"imagenes/Boton_vaciar_cesta_desactivado.gif\" name=\"vaciar_cesta\" onMouseOver=\"activar('vaciar_cesta',3);\" onMouseOut=\"desactivar('vaciar_cesta',3);\" width=\"111\" height=\"21\" border=\"0\"></a></div></td>";
      cestaHTML+="<td><div align=\"center\"><a href=\"javascript:volver();\"><img src=\"imagenes/Boton_volver_desactivado.gif\" name=\"volver\" onMouseOver=\"activar('volver',4);\" onMouseOut=\"desactivar('volver',4);\" width=\"111\" height=\"21\" border=\"0\"></a></div></td>";
      cestaHTML+="</tr></table>";
   }else{
      cestaHTML+="<br><br><table width=\"550\" border=\"0\" align=\"center\"><tr>";
      cestaHTML+="<td><div align=\"right\"><a href=\"javascript:comprar();\"><img src=\"imagenes/Boton_comprar_desactivado.gif\" width=\"111\" name=\"comprar\" onMouseOver=\"activar('comprar',1);\" onMouseOut=\"desactivar('comprar',1);\" height=\"21\" border=\"0\"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></td>";
      cestaHTML+="<td><div align=\"left\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:volver();\"><img src=\"imagenes/Boton_volver_desactivado.gif\" name=\"volver\" onMouseOver=\"activar('volver',4);\" onMouseOut=\"desactivar('volver',4);\" width=\"111\" height=\"21\" border=\"0\"></a></div></td>";      
      cestaHTML+="</tr></table>";
   }

   return cestaHTML;
}

CestaCompra.prototype.estaVacia = function(){
   if(this.numero_elementos<1){
      return true;
   }else{
      return false;
   }
}

CestaCompra.prototype.establecerCantidad = function(referencia,cantidad){
   if(cantidad<1){
      this.eliminarElementos(referencia);
   }else{
      this.elementos[referencia].establecerCantidad(cantidad);
   }
}

CestaCompra.prototype.actualizarCestaFormulario = function(formulario){
   var referencia;
   var num_campos_formulario=formulario.length;
   var campos_formulario=formulario.elements

   for(var indice=0;indice<num_campos_formulario;indice++){
      if(campos_formulario[indice].type=="text"){
         referencia=campos_formulario[indice].name;
         cantidad=parseInt(campos_formulario[indice].value);
         if((!isNaN(cantidad))&&(cantidad>0)){
            this.establecerCantidad(referencia,cantidad);
         }else{
            alert("La cantidad es incorrecta");
         }
      }
   }
}

CestaCompra.prototype.vaciarCesta = function(){
   var elemento;

   for(elemento in this.elementos){
      this.eliminarElemento(this.elementos[elemento].obtenerReferencia());
   }
}