<?
session_start();

include_once ("db_connect.php");
include_once ("pwdcheck.php");

$socio_activo=false;
	
if (is_user($nsoc))
{
	if (is_passwd($psoc,$nsoc))
	{
		$t_act=time();	
		SetCookie("nsoc",$nsoc,$t_act+3600);
		SetCookie("psoc",$psoc,$t_act+3600);
		
		$socio_activo=true;
	}
}

if ($envi!="")
{
	$envdes=desencripta($envi);
	$envact=true;	
}
else
{
	$envact=false;
}

$ope=desencripta($op);
$ide=desencripta($id);
$val=desencripta($vl);

$database_name="qaq398";
$noproductos=true; //indica que no hay productos en el carrito

switch($ope) //La operación, añadir,quitar o vaciar
{
	case 1: //Añade un elemento al carrito
		if (!session_is_registered('data'))
		{
			session_register('data');
			$data[$ide]=1; //establece el número de artículos en 1	
		}
		else
		{
			++$data[$ide];
		}
		
		header ("location:carrito.php?pid=$PHPSESSID");

		exit();
		
	case 2: //Quita un elemento del carrito
		if (session_is_registered('data')) //si la variable está registrada se pueden descontar elemento, sino NO
		{
			if ($data[$ide]<=1) //si solo hay un elemento se desregistra la variable
			{
				unset($data[$ide]);
			}
			else
			{
				--$data[$ide];
			}
		}

		header ("location:carrito.php?pid=$PHPSESSID");

		exit();


	case 3: //Establece el número de unidades de un elemento
		if (!session_is_registered('data'))
		{
			if ($val!=0)
			{
				session_register('data');
				$data[$ide]=$val; //establece el número de artículos en 1	
			}
		}
		else
		{
			if ($val!=0)
				$data[$ide]=$val;
			else
				unset($data[$ide]);
		}

		header ("location:carrito.php?pid=$PHPSESSID");

		exit();

	
	case 4: //Elimina el número de unidades de un elemento
		if (session_is_registered('data'))
		{
			$data[$ide]=0;
			unset($data[$ide]);
		}

		header ("location:carrito.php?pid=$PHPSESSID");

		exit();


	case 5: //Vacia el carrito
		if (session_is_registered('data'))
		{
			session_unregister('data');
			unset($data);
		}

		header ("location:carrito.php?pid=$PHPSESSID");

		exit();

}


Function numero_elementos()
{
	global $data;

	$res=0;	
	
	if (session_is_registered('data') && isset($data))
	{
		reset($data);
		while( list( $key, $cantidad ) = each($data) ) 
		{
			++$res;
		}
	}

	return ($res);	
}

Function show()
{
	global $envact;
	global $envdes;
	global $data;
	global $noproductos;
	global $PHPSESSID;
	global $database_name;
	global $socio_activo;
	global $nsoc;
	global $totalglob;
	global $gastosglob;

	if (session_is_registered('data') && isset($data) && numero_elementos()>0)
	{
		$noproductos=false;
		
		connect();
		
	   	echo "<table width=\"95%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\" bordercolor=\"#FFFF99\">\n";
		echo " <tr>\n";
		echo "  <td bgcolor=\"#FFCC66\" align=\"left\" valign=\"middle\" height=\"20\" width=\"40%\">\n";
		echo "   <font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Producto</b></font></td>\n";
		echo "  <td bgcolor=\"#FFCC66\" align=\"center\" valign=\"middle\" height=\"20\" width=\"10%\">\n";
		echo "   <font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\"><b>Precio (unidad)</b></font></td>\n";
		echo "  <td bgcolor=\"#FFCC66\" align=\"center\" valign=\"middle\" height=\"20\" width=\"10%\">\n";
		echo "   <font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\"><b>Precio socio (unidad)</b></font></td>\n";
		echo "  <td bgcolor=\"#FFCC66\" align=\"center\" valign=\"middle\" height=\"20\" width=\"10%\">\n";
		echo "   <font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\"><b>Unidades</b></font></td>\n";
		echo "  <td bgcolor=\"#FFCC66\" align=\"center\" valign=\"middle\" height=\"20\" width=\"10%\">\n";
		echo "   <font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\"><b>Oferta</b></font></td>\n";
		echo "  <td bgcolor=\"#FFCC66\" align=\"center\" valign=\"middle\" height=\"20\" width=\"10%\">\n";
		echo "   <font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\"><b>Subtotal</b></font></td>\n";
		echo "  <td bgcolor=\"#FFCC66\" align=\"center\" valign=\"middle\" width=\"10%\">\n";
		echo "   &nbsp;";
		echo "  </td>\n";
		echo " </tr>\n";

		$total=0;
		$fila=0;
		$linea=0;
		
		reset($data);
		while( list( $key, $cantidad ) = each($data) ) 
		{
			$cad_sql="SELECT autor,titulo,precio,precio_socio,oferta FROM discos WHERE ref=$key";
			
			$resultid=mysql_db_query($database_name,$cad_sql);
			
			if ($resultid)
			{
				$damefila=mysql_fetch_array($resultid);

				if ($damefila && !tiene_ekis($damefila["oferta"]))
				{
					++$fila;
					
					if (($fila % 2)==0)
					{
						echo " <tr bgcolor=\"#FFCC66\"> \n";
					}
					else
					{
						echo " <tr bgcolor=\"#FF972F\"> \n";
					}

					echo "  <td width=\"10%\" valign=\"middle\" align=\"left\">";
					echo "   <font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">";
					echo "$damefila[0] <br>";
					if ($damefila[1]!="") echo "($damefila[1])";
					echo "</font></td>\n"; //pone el autor y título del cd
					echo "  <td width=\"10%\" align=\"center\" valign=\"middle\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">";

					if ($damefila[2]>0) //precio del artículo
					{
						$precio=$damefila[2];
						echo "$precio&euro;";
					}
					else
					{
						$precio=0;
						echo "Consultar precio";
					}
					
					echo "</font></td>\n";
					echo "  <td width=\"10%\" align=\"center\" valign=\"middle\">";

					if ($fila%2==0)
						echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#FF0000\"><b>";
					else
						echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#000000\"><b>";

					if ($damefila[3]>0) //precio de socio del artículo
					{
						$precio_socio=$damefila[3];
						echo "$precio_socio&euro;";
					}
					else
					{
						$precio_socio=0;
						echo "&nbsp;";
					}

					echo "</b></font></td>\n";
					echo "  <td width=\"10%\" align=\"center\" valign=\"middle\">\n"; 
		                        echo "   <select name=\"menu\" onChange=\"MM_jumpMenu('parent',this,0)\" class=\"peque\">";
		
					if ($cantidad>=100)
						$tope=2 * $cantidad;
					else
						$tope=100;
					
					for($i=1;$i<=$tope;++$i)
					{
						echo "<option value=\"carrito.php?pid=$PHPSESSID&op=" . encripta(3) . "&id=" . encripta($key) . "&vl=" . encripta($i) . "\""; 
						if ($i==$cantidad) echo " selected";
						echo " >$i</option>\n";
					}
		
					echo "   </select>\n";
					echo "  </td>\n";
					echo "  <td width=\"10%\" align=\"center\" valign=\"middle\">";
					echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#000000\">";
					echo $damefila["oferta"];
					echo "</font></td>\n";
					echo "  <td width=\"10%\" align=\"center\" valign=\"middle\">";


					if (tiene_tanto_por_ciento($damefila["oferta"]))
					{
						$preciooferta=round((100 - obtener_numero($damefila["oferta"])) * $precio) /100;
					}
					else
					{
						$preciooferta=$precio;
					}
										
					if ($socio_activo)
					{
						if ($precio_socio!=0 || $precio_socio>$precio_oferta)
						{
							if ($fila%2==0)
								echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#FF0000\"><b>";
							else
								echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#000000\"><b>";


							$precio_final=$precio_socio;
							
							if ($precio_final>$preciooferta) $precio_final=$preciooferta;

							$subtotal= $precio_final * $cantidad;
							echo "$subtotal&euro;</b></font></td>\n";
						}
						else
						{
						
							echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">";
							$subtotal = $preciooferta * $cantidad;
							echo "$subtotal&euro;</font></td>\n";
						}
					}
					else
					{
						echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">";
						$subtotal = $preciooferta * $cantidad;
						echo "$subtotal&euro;</font></td>\n";			
					}
					
					$total = $total + $subtotal;
					echo "  <td>";
					echo "<a href=\"carrito.php?pid=$PHPSESSID&op=" . encripta(3) . "&id=" . encripta($key) . "&vl=" . encripta(0) . "\"><img src=\"elimina.gif\" border=\"0\"></a>";
					echo "</td>\n";
					echo " </tr>\n";
				}
				else if ($damefila) //tiene el campo "oferta" con algun numero, luego se incluye en la lista negra
				{
					$arti_ofertas[$linea]=$damefila["autor"] . ";" . $damefila["titulo"] . ";" . $damefila["precio"] . ";" . $damefila["precio_socio"] . ";" . $damefila["oferta"] . ";" .  $key . ";" . $cantidad;
					++$linea;
				}
			}
		}

		if ($arti_ofertas)
		{	
			//ordenar el array de ofertas (ordenación por selección)
			if (!$socio_activo)
			{
				for ($j=0;$j<$linea;++$j)
				{
					$ofertainicial=explode(";",$arti_ofertas[$j]);
					
					
					for ($k=$j+1;$k<$linea;++$k)
					{
						$ofertafinal=explode (";",$arti_ofertas[$k]);
						
						if ($ofertafinal[2]>$ofertainicial[2])
						{
							$ofertaintermedia=$arti_ofertas[$j];
							$arti_ofertas[$j]=$arti_ofertas[$k];
							$arti_ofertas[$k]=$ofertaintermedia;
						}
					}
				}
			}
			else
			{
				for ($j=0;$j<$linea;++$j)
				{
					$ofertainicial=explode(";",$arti_ofertas[$j]);
					
					for ($k=$j+1;$k<$linea;++$k)
					{
						$ofertafinal=explode (";",$arti_ofertas[$k]);
						
						$precio_sociofinal=$ofertafinal[3];
						$precio_socioinicial=$ofertainicial[3];
						
						$precio_normalfinal=$ofertafinal[2];
						$precio_normalinicial=$ofertainicial[2];
						
						if ($precio_sociofinal==0) //comparo el precio normal final con...
						{
							if ($precio_socioinicial==0) //...con el precio normal inicial
							{
								if ($precio_normalfinal>$precio_normalinicial)
								{
									$ofertaintermedia=$arti_ofertas[$j];
									$arti_ofertas[$j]=$arti_ofertas[$k];
									$arti_ofertas[$k]=$ofertaintermedia;
									$ofertainicial=explode (";",$arti_ofertas[$k]);
								}
							}
							else //...con el precio de socio inicial
							{
								if ($precio_normalfinal>$precio_socioinicial)
								{
									$ofertaintermedia=$arti_ofertas[$j];
									$arti_ofertas[$j]=$arti_ofertas[$k];
									$arti_ofertas[$k]=$ofertaintermedia;
									$ofertainicial=explode (";",$arti_ofertas[$k]);
								}
							}
						}
						else //comparo el precio de socio final...
						{
							if ($precio_socioinicial==0) //...con el precio normal inicial
							{
								if ($precio_sociofinal>$precio_normalinicial)
								{
									$ofertaintermedia=$arti_ofertas[$j];
									$arti_ofertas[$j]=$arti_ofertas[$k];
									$arti_ofertas[$k]=$ofertaintermedia;
									$ofertainicial=explode (";",$arti_ofertas[$k]);
								}
							}
							else //...con el precio de socio inicial
							{
								if ($precio_sociofinal>$precio_socioinicial)
								{
									$ofertaintermedia=$arti_ofertas[$j];
									$arti_ofertas[$j]=$arti_ofertas[$k];
									$arti_ofertas[$k]=$ofertaintermedia;
									$ofertainicial=explode (";",$arti_ofertas[$k]);
								}
							}
						}
					}
				}
			}
						
			$total_articulos=0;
			
			//array ordenado de mayor a menor precio de artículo de los artículos en 3x2
			for ($j=0;$j<$linea;++$j)
			{
				$caracter_articulos=explode(";",$arti_ofertas[$j]);
				
				$total_articulos += $caracter_articulos[6];			
			}
			
			$articulos_regalados= floor($total_articulos/3);
			
			$articulos_restantes=$total_articulos;
			
			$articulos_fuera_de_oferta=$total_articulos - ($articulos_regalados *3);
			
			$articulos_normales= $total_articulos - $articulos_fuera_de_oferta - $articulos_regalados;
			
			$j=0;
			
			$parcial_articulos_normales=0;
			$parcial_articulos_fuera=0;
			$paricla_articulos_regalo=0;
			
			while ($articulos_restantes>0)
			{
				$damefila=explode(";", $arti_ofertas[$j]);

				$cantidad=$damefila[6];
				
				$key=$damefila[5];
				
				$damefila["oferta"]=$damefila[4];
				
				++$fila;
				
				++$j;
				
				if (($fila % 2)==0)
				{
					echo " <tr bgcolor=\"#FFCC66\"> \n";
				}
				else
				{
					echo " <tr bgcolor=\"#FF972F\"> \n";
				}
	
				echo "  <td width=\"10%\" valign=\"middle\" align=\"left\">";
				echo "   <font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">";
				echo "$damefila[0] <br>";
				if ($damefila[1]!="") echo "($damefila[1])";
				echo "</font></td>\n"; //pone el autor y título del cd
				echo "  <td width=\"10%\" align=\"center\" valign=\"middle\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">";
	
				if ($damefila[2]>0) //precio del artículo
				{
					$precio=$damefila[2];
					echo "$precio&euro;";
				}
				else
				{
					$precio=0;
					echo "Consultar precio";
				}
				
				echo "</font></td>\n";
				echo "  <td width=\"10%\" align=\"center\" valign=\"middle\">";
	
				if ($fila%2==0)
					echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#FF0000\"><b>";
				else
					echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#000000\"><b>";
	
				if ($damefila[3]>0) //precio de socio del artículo
				{
					$precio_socio=$damefila[3];
					echo "$precio_socio&euro;";
				}
				else
				{
					$precio_socio=0;
					echo "&nbsp;";
				}
	
				echo "</b></font></td>\n";
				echo "  <td width=\"10%\" align=\"center\" valign=\"middle\">\n"; 
	                        echo "   <select name=\"menu\" onChange=\"MM_jumpMenu('parent',this,0)\" class=\"peque\">";
	
				if ($cantidad>=100)
					$tope=2 * $cantidad;
				else
					$tope=100;
				
				for($i=1;$i<=$tope;++$i)
				{
					echo "<option value=\"carrito.php?pid=$PHPSESSID&op=" . encripta(3) . "&id=" . encripta($key) . "&vl=" . encripta($i) . "\""; 
					if ($i==$cantidad) echo " selected";
					echo " >$i</option>\n";
				}
	
				echo "   </select>\n";
				echo "  </td>\n";
				echo "  <td width=\"10%\" align=\"center\" valign=\"middle\">";
				echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\" color=\"#000000\">";
				echo $damefila["oferta"];
				echo "</font></td>\n";
				echo "  <td width=\"10%\" align=\"center\" valign=\"middle\">";
	
				echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\">";


				if ($socio_activo && $precio_socio>0)
					$preciofueradeoferta=$precio_socio;
				else
					$preciofueradeoferta=$precio;


				$articulosfuera=0;
				$articulosnormal=0;
				$articulosregal=0;
				
				if ($parcial_articulos_normales<$articulos_normales)
				{
					if ($cantidad<($articulos_normales-$parcial_articulos_normales))
						$articulosnormal=$cantidad;
					else
						$articulosnormal=$articulos_normales-$parcial_articulos_normales;

					$parcial_articulos_normales += $articulosnormal;
				}
				
				if ($parcial_articulos_fuera<$articulos_fuera_de_oferta)
				{
					if (($cantidad-$articulosnormal)<($articulos_fuera_de_oferta-$parcial_articulos_fuera))
						$articulosfuera=$cantidad - $articulosnormal;
					else
						$articulosfuera=$articulos_fuera_de_oferta-$parcial_articulos_fuera;
					
					$parcial_articulos_fuera += $articulosfuera;
				}
				
				if ($parcial_articulos_regalo<$articulos_regalados)
				{
					if (($cantidad-$articulosnormal-$articulosfuera)<($articulos_regalados-$parcial_articulos_regalo))
						$articulosregal=$cantidad-$articulosnormal-$articulosfuera;
					else
						$articulosregal=$articulos_regalados-$parcial_articulos_regalo;
						
					$parcial_articulos_regalo += $articulosregal;
				}
				
				$subtotal = ($precio * $articulosnormal) + ($preciofueradeoferta * $articulosfuera);
				$articulos_restantes -= $cantidad;
				
				echo "$subtotal&euro;</font></td>\n";			
				
				$total = $total + $subtotal;
				echo "  <td>";
				echo "<a href=\"carrito.php?pid=$PHPSESSID&op=" . encripta(3) . "&id=" . encripta($key) . "&vl=" . encripta(0) . "\"><img src=\"elimina.gif\" border=\"0\"></a>";
				echo "</td>\n";
				echo " </tr>\n";
			}
		}

		if ($total>0)
		{
			++$fila;

			echo "<tr>\n";
			echo " <td width=\"40%\">&nbsp;</td>\n";
			echo " <td width=\"10%\">&nbsp;</td>\n";
			
			if (($fila % 2)==0)
			{
				echo " <td colspan=\"4\" bgcolor=\"#FFCC66\" width=\"25%\" align=\"center\" valign=\"middle\">";			
			}
			else
			{
				echo " <td colspan=\"4\" bgcolor=\"#FF972F\" width=\"25%\" align=\"center\" valign=\"middle\">";
			}

			$gastosenv=-1;

			echo "<a href=\"gastosenvio.php\" onclick=\"openChromeslessWindow('gastosenvio.php','Gastos',330,245,null,null,'Gastos de env&iacute;o', '#000000', '#000000', '#F8F3A5', '#FEC460' ,true,'Verdana, Arial, Helvetica, sans-serif', '1','#000000'); return false\" onMouseOut=\"MM_swapImgRestore()\" onMouseOver=\"MM_swapImage('gastosenv','','gastosover.gif',1)\"><img name=\"gastosenv\" src=\"gastosout.gif\" width=\"124\" height=\"19\" border=\"0\"></a>";
			
	                echo "</td><td width=\"10%\">&nbsp;</td></tr>\n";
			
			if ($envact)
			{
				++$fila;
	
				echo "<tr>\n";
				echo " <td width=\"40%\">&nbsp;</td>\n";
				echo " <td width=\"10%\">&nbsp;</td>\n";
				
				if (($fila % 2)==0)
				{
					echo " <td colspan=\"4\" bgcolor=\"#FFCC66\" width=\"25%\" align=\"center\" valign=\"middle\">";			
				}
				else
				{
					echo " <td colspan=\"4\" bgcolor=\"#FF972F\" width=\"25%\" align=\"center\" valign=\"middle\">";
				}
	
				
				if ($socio_activo) //es socio
				{
					if (($total>=30.05) && (strcasecmp($envdes,"entregadom")==0)) //compra superior a 30,05 y entrega a domicilio 
					{
						echo "  <font color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><b>";
						echo "Gastos de envío: </b>";
						echo "0&euro;<br></font>";
						$gastosenv=0;
					}
					else if (($total<30.05) && (strcasecmp($envdes,"entregadom"))==0) //compra inferior a 30,05 y entrega a domicilio 
					{
						echo "  <font color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><b>";
						echo "Gastos de envío: </b>";
						echo "3&euro;<br></font>";
						$gastosenv=3;
					}
					else if (strcasecmp($envdes,"entreganor")==0) //entrega normal
					{
						echo "  <font color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><b>";
						echo "Gastos de envío: </b>";
						echo "3&euro;<br></font>";
						$gastosenv=3;
					}
					else if (strcasecmp($envdes,"entregaurg")==0) //entrega urgente
					{
						echo "  <font color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><b>";
						echo "Gastos de envío: </b>";
						echo "6&euro;<br></font>";
						$gastosenv=6;
					}
				}
				else
				{
					if (strcasecmp($envdes,"entregadom")==0) //compra superior a 30,05 y entrega a domicilio 
					{
						echo "  <font color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"1\"><b>";
						echo "La entrega a domicilio s&oacute;lo es aplicable para socios</b>";
						echo "</font>";
					}
					else if (strcasecmp($envdes,"entreganor")==0) //entrega normal
					{
						echo "  <font color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><b>";
						echo "Gastos de envío: </b>";
						echo "5&euro;<br></font>";
						$gastosenv=5;
					}
					else if (strcasecmp($envdes,"entregaurg")==0) //entrega urgente
					{
						echo "  <font color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><b>";
						echo "Gastos de envío: </b>";
						echo "9&euro;<br></font>";
						$gastosenv=9;
					}
				}
		                echo "</td><td width=\"10%\">&nbsp;</td></tr>\n";
			}
			
			++$fila;
			
			echo "<tr>\n";
			echo " <td width=\"10%\">&nbsp;</td>\n";
			echo " <td width=\"10%\">&nbsp;</td>\n";
			
			if (($fila % 2)==0)
			{
				echo " <td colspan=\"4\" bgcolor=\"#FFCC66\" width=\"10%\" align=\"center\" valign=\"middle\">";			
			}
			else
			{
				echo " <td colspan=\"4\" bgcolor=\"#FF972F\" width=\"10%\" align=\"center\" valign=\"middle\">";
			}

			echo "  <font color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><b>";

			if ($gastosenv>=0) //gastos de envío conocidos
			{
				echo "Total: " . ($total + $gastosenv) . "&euro; </b></font></td>";
		                echo "</tr>\n";
		        }
		        else
		        {
				echo "Total sin gastos de envío: " . $total . "&euro; </b></font></td>";
				echo " <td width=\"10%\">&nbsp;</td>\n";				
		                echo "</tr>\n";
		        }
		}
		
		echo "<tr>\n";
		echo "<td colspan=\"6\">&nbsp;</td>\n";
		echo "</tr>\n";
		echo "</table>\n";
	}
	else //no hay elementos en el carrito
	{
		echo "<font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\"><b>";
		echo "No hay productos en la cesta de compra.</b></font>\n";
	}
	
	$totalglob=$total;
//	$gastosenvglob=$gastosenv;
}

Function encripta($cad)
{
	$coct["0"]="00";	//Matriz de conversión hexadecimal-octal
	$coct["1"]="01";
	$coct["2"]="02";
	$coct["3"]="03";
	$coct["4"]="04";
	$coct["5"]="05";
	$coct["6"]="06";
	$coct["7"]="07";
	$coct["8"]="10";
	$coct["9"]="11";
	$coct["A"]="12";
	$coct["B"]="13";
	$coct["C"]="14";
	$coct["D"]="15";
	$coct["E"]="16";
	$coct["F"]="17";

	$corresp[0]="F";
	$corresp[1]="C";
	$corresp[2]="G";
	$corresp[3]="D";
	$corresp[4]="A";
	$corresp[5]="E";
	$corresp[6]="B";
	$corresp[7]="H";

	$prim_res=bin2hex($cad);
		
	$lpres=strlen($prim_res);

	$res="";

	for ($i=0;$i<$lpres;++$i) //Convierte a octal
	{
		$res .= $coct[strtoupper($prim_res[$i])];			
	}

	$lpres=strlen($res);

	$final_res="";

	for ($i=0;$i<$lpres;++$i) //Convierte a carácter
	{
		$final_res .= $corresp[$res[$i]];			
	}

	return $final_res;
}

Function desencripta($cad)
{
	$coct["00"]="0";	//Matriz de conversión octal-hexadecimal
	$coct["01"]="1";
	$coct["02"]="2";
	$coct["03"]="3";
	$coct["04"]="4";
	$coct["05"]="5";
	$coct["06"]="6";
	$coct["07"]="7";
	$coct["10"]="8";
	$coct["11"]="9";
	$coct["12"]="A";
	$coct["13"]="B";
	$coct["14"]="C";
	$coct["15"]="D";
	$coct["16"]="E";
	$coct["17"]="F";

	$corresp["F"]="0";
	$corresp["C"]="1";
	$corresp["G"]="2";
	$corresp["D"]="3";
	$corresp["A"]="4";
	$corresp["E"]="5";
	$corresp["B"]="6";
	$corresp["H"]="7";

	$lcad=strlen($cad);

	if ($lcad % 2 ==0) //la cadena puede ser una representación hexadecimal de alfo
	{
		$res="";

		$error=false;

		for ($i=0;$i<$lcad && !$error;++$i) //primera conversión
		{
			if (($cad[$i]>="A" && $cad[$i]<="H") ||
				($cad[$i]>="a" && $cad[$i]<="h"))
			{
				$res .= $corresp[strtoupper($cad[$i])];
			}
			else
			{
				$error=True;
			}
		}

		if (!$error) //realiza la segunda conversión (pares octales a hexadecimal)
		{
			$res2="";

			for ($i=0;$i<$lcad;$i=$i+2)
			{
				$temp=$res[$i] . $res[$i+1];

				$res2 .= $coct[$temp];
			}
		}

		//Obtiene el resultado sin intercambio de caracteres.
		//Ahora hay que convertir los pares en hexadecimal a cadena

		if (!$error)
		{	
			$lres2=strlen($res2);

			if ($lres2%2==0)
			{
				$final_res="";

				for ($i=0;$i<$lres2;$i=$i+2)
				{
					$temp=$res2[$i] . $res2[$i+1];
					eval("\$final_res .= \"\\x$temp\";");				
				}
				return $final_res;
			}
			else
				return "";
		}
		else
			return "";
	}
	else
		return "";
}

Function tiene_ekis($cad)
{
	$lcad=strlen($cad);
	
	$tiene=false;
	
	for ($i=0;$i<$lcad && !$tiene;++$i)
	{
		$tiene= ($cad[$i]=="x" || $cad[$i]=="X");
	}
	
	return $tiene;
}

Function tiene_tanto_por_ciento($cad)
{
	$lcad=strlen($cad);
	
	$tiene=false;
	
	for ($i=0;$i<$lcad && !$tiene;++$i)
	{
		$tiene= ($cad[$i]=="%");
	}
	
	return $tiene;
}

Function obtener_numero($cad)
{
	$lcad=strlen($cad);
	
	$res="";
	
	for ($i=0;$i<$lcad;++$i)
	{
		if (ctype_digit($cad[$i])) $res .= $cad[$i];
	}
	
	return ($res);
}

//<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
?>
<HTML>
<HEAD>
<style><!--BODY{scrollbar-face-color:#ffc05f;scroll-arrow-color:#F0F0A0;scrollbar-track-color:#FFD387;scrollbar-shadow-color:#ffc05f;scrollbar-highlight-color:`#´ffffff;scrollbar-3dlight-color:`#´000000;scrollbar-darkshadow-Color:`#´000000;}.peque {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9px; font-style: normal}
--></style>
<TITLE>Discos Sevilla Este - Cesta de la compra</TITLE>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<script language="JavaScript">
<!--

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

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) { //v4.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);
  if(!x && document.getElementById) x=document.getElementById(n); 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];}
}

function openChromeslessWindow(openUrl, winName, wWidth, wHeight, wPosx, wPosy, wTIT,
												windowBORDERCOLOR, windowBORDERCOLORsel, windowTITBGCOLOR, windowTITBGCOLORsel,
													bCenter, sFontFamily, sFontSize, sFontColor){
	
	openchromeless(openUrl,winName, wWidth, wHeight, wPosx, wPosy, wTIT, wTIT ,
							windowBORDERCOLOR, windowBORDERCOLORsel, windowTITBGCOLOR, windowTITBGCOLORsel,
								bCenter, sFontFamily, sFontSize, sFontColor);
}
//-->
</script>
<script id="chromeless" src="pz_chromeless_2.1.js"></script>
</HEAD>
<BODY onload="window.name='padre';" background="" bgColor=#F8F3A5 leftMargin=0 scroll=auto
link=#993333 alink=#993333 vlink=#993333 text=#000000 topMargin=0 marginwidth="0" marginheight="0" onLoad="MM_preloadImages('botonera_b1_over.gif','botonera_b2_over.gif','botonera_b3_over.gif','botonera_b4_over.gif','botonera_b5_over.gif','botonera_b6_over.gif','botonera_b7_over.gif','seguir_b1_over.gif','anular_b1_over.gif','tramitar_b1_over.gif','gastosover.gif')">
<TABLE border=0 cellPadding=0 cellSpacing=0 height="100%" width="100%">
  <TBODY> 
  <TR>
    <TD vAlign=top>
      <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td height="100%" align="center" valign="top"> 
            <table width="100%" border="0" cellspacing="0" cellpadding="0" height="100%">
              <tr> 
                <td colspan="2" align="center" height="26"> 
                  <table height=18 cellspacing=0 cellpadding=0 width="100%" border=0 bgcolor="#F8F3A5">
                    <tbody> 
                    <tr> 
                      <td valign=top align=left height="26" width="75"> 
                        <table cellspacing=0 cellpadding=0 width="100%" border=0 height="23">
                          <tbody> 
                          <tr> 
                            <td valign=top align=left width=56 height="21"><img src="logodisc.gif" width="56" height="21"></td>
                            <td valign=middle align=center width=20 height="14"><img src="discogiro2.gif" width="16" height="16"></td>
                          </tr>
                          </tbody> 
                        </table>
                      </td>
                      <td valign=top align=left height="26" width="106"><img src="logosevillaeste.gif" width="106" height="39"></td>
                      <td valign=center align=center width=841 height="26"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width="300" height="37">
                          <param name=movie value="construccion.swf">
                          <param name=quality value=high>
                          <param name="wmode" value="transparent">
                          <embed src="construccion.swf" quality=high pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="300" height="37" wmode="transparent">
                          </embed> 
                        </object></td>
                    </tr>
                    </tbody> 
                  </table>
                </td>
              </tr>
              <tr> 
                <td colspan="2" bgcolor=#f8f3a5 align="center" height="40"> 
                  <form name="buscar" method="post" action="buscador.php">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td align="center">
                          <table width="100%" height="50" bgcolor=#fec460 border="0" cellspacing="0" cellpadding="0">
                            <tr> 
                              <td width="304"><font size="1">&nbsp; </font> <font size="1">
                                <table width="304" border="0" cellspacing="0" cellpadding="0">
                                  <tr> 
                                    <td nowrap width="75"><b><font 
                  face="Verdana, Arial, Helvetica, sans-serif" size=1> <font color="#FFCC66">--</font>Buscar 
                                      por</font></b></td>
                                    <td width="5"><b><font 
                  face="Verdana, Arial, Helvetica, sans-serif" size=1> 
                                      <input type="hidden" name="order" value="1">
                                      <input type="hidden" name="npag" value="1">
                                      <input type="hidden" name="nres" value="10">
                                      <input type="hidden" name="tipoord" value="0">
                                      <select name="tiposel">
                                        <option value="0" selected>Solista/Grupo</option>
                                        <option value="1">T&iacute;tulo del &aacute;lbum</option>
                                        <option value="2">T&iacute;tulo de canci&oacute;n</option>
                                        <option value="3">G&eacute;nero</option>
                                        <option value="4">Discogr&aacute;fica</option>
                                        <option value="5">A&ntilde;o de edici&oacute;n</option>
                                        <option value="6">Todos</option>
                                      </select>
                                      </font></b></td>
                                    <td width="5"><b><font 
                  face="Verdana, Arial, Helvetica, sans-serif" size=1> 
                                      <input name=buscarpor>
                                      </font></b></td>
                                    <td width="5"> 
                                      <input type="submit" name="Ir" value="Ir">
                                    </td>
                                    <td>&nbsp;</td>
                                  </tr>
                                </table>
                                </font></td>
                              <td align="right" valign="middle">&nbsp;</td>
                            </tr>
                          </table>
                        </td>
                      </tr>
                      <tr>
                        <td align="center">
                          <table border=0 cellpadding=0 cellspacing=0 width=600>
                            <tbody> 
                            <tr> 
                              <td width="27"><img src="botonera_left.gif" width="27" height="27"></td>
                              <td width="78"><a href="index.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image6','','botonera_b1_over.gif',1)"><img name="Image6" border="0" src="botonera_b1.gif" width="78" height="27"></a></td>
                              <td width="78"><a href="localizacion.html" onMouseOut="MM_swapImgRestore()"  onMouseOver="MM_swapImage('Image7','','botonera_b2_over.gif',1)"><img name="Image7" border="0" src="botonera_b2.gif" width="78" height="27"></a></td>
                              <td width="78"><a href="ecarrito.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image8','','botonera_b3_over.gif',1)"><img name="Image8" border="0" src="botonera_b3.gif" width="78" height="27"></a></td>
                              <td width="78"><a href="registro.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image9','','botonera_b4_over.gif',1)"><img name="Image9" border="0" src="botonera_b4.gif" width="78" height="27"></a></td>
                              <td width="78"><a href="sugerencias.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image10','','botonera_b5_over.gif',1)" onClick="openChromeslessWindow('sugerencias.html','Sugerencias',320,300,null,null,'Sugerencias', '#000000', '#000000', '#F8F3A5', '#FEC460' ,true,'Verdana, Arial, Helvetica, sans-serif', '1','#000000'); return false"><img name="Image10" border="0" src="botonera_b5.gif" width="78" height="27"></a></td>
                              <td width="78"><a href="socios.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image11','','botonera_b6_over.gif',1)" onClick="openChromeslessWindow('socios.html','Socios',320,300,null,null,'Socios', '#000000', '#000000', '#F8F3A5', '#FEC460' ,true,'Verdana, Arial, Helvetica, sans-serif', '1','#000000'); return false"><img name="Image11" border="0" src="botonera_b6.gif" width="78" height="27"></a></td>
                              <td width="78"><a href="ofertas.html" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image12','','botonera_b7_over.gif',1)"><img name="Image12" border="0" src="botonera_b7.gif" width="78" height="27"></a></td>
                              <td width="27"><img src="botonera_right.gif" width="27" height="27"></td>
                            </tr>
                            </tbody> 
                          </table>
                        </td>
                      </tr>
                    </table>
                  </form>
                </td>
              </tr>
              <tr> 
                <td colspan="2" bgcolor="#f8f3a5" align="center" valign="top"><? show(); ?> 
                  <table width="100%" border="0" cellspacing="0" cellpadding="0" height="25">
                    <tr>
<?

if (!$noproductos) //hay productos
{
	if ($socio_activo)
	{
?>
                      <td valign="middle" align="center"><a href="datos.php?<? echo "pid=" . $PHPSESSID . "&tp=" . encripta (encripta($totalglob)); ?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('tramitar','','tramitar_b1_over.gif',1)"><img name="tramitar" border="0" src="tramitar_b1.gif" width="112" height="19"></a></td>
<?
	}
	else
	{
?>
                      <td valign="middle" align="center"><a href="datos.php?<? echo "pid=" . $PHPSESSID . "&tp=" . encripta (encripta($totalglob)); ?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('tramitar','','tramitar_b1_over.gif',1)"><img name="tramitar" border="0" src="tramitar_b1.gif" width="112" height="19"></a></td>
<?
	}
?>
                      <td align="center" valign="middle"><a href="#" onclick="javascript:history.go(-1)" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('seguir','','seguir_b1_over.gif',1)"><img name="seguir" border="0" src="seguir_b1.gif" width="124" height="19"></a></td>
                      <td align="center" valign="middle"><a href="<? echo "carrito.php?pid=$PHPSESSID&op=" . encripta(5); ?>" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('anular','','anular_b1_over.gif',1)"><img name="anular" border="0" src="anular_b1.gif" width="100" height="19"></a></td>
<?
}
else
{
?>
                      <td valign="middle" align="center">&nbsp;</td>
                      <td align="center" valign="middle"><a href="#" onclick="javascript:history.go(-1)" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('seguir','','seguir_b1_over.gif',1)"><img name="seguir" border="0" src="seguir_b1.gif" width="124" height="19"></a></td>
                      <td align="center" valign="middle">&nbsp;</td>
<?
}
?>
                    </tr>
                  </table>
                </td>
              </tr>
              <tr>
                <td colspan="2" bgcolor=#f8f3a5 align="center" height="27"><font face="Verdana, Arial, Helvetica, sans-serif" color="#993333" size="2">&copy;</font><font face="Verdana, Arial, Helvetica, sans-serif" color="#993333"> 
                  <font size="1"><a href="http://www.proyecto3.com">www</a></font><a href="http://www.proyecto3.com">.<font size="2">Proyecto3</font>.<font size="1">com</font> 
                  <font size="2">2002</font></a></font></td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </TD>
  </TR></TBODY></TABLE>
</BODY></HTML>
