// Variable globales para el carrito de la compra

    var Pro;
	var S;
	var Importe;
	var Total = 0;
	var LineasPedido = 200;

function GuardaPro(producto)
{
	// verificación de si el producto ya existe en la cesta
	
	var YaExiste;
	var P;

	P = producto.split("#");

	for (var i=1;i<=Number(dameValor("sest0"));i++) {

		Pro = dameValor("sest"+i);
		S = Pro.split("#");

		if (S[1] == P[1]) {
			if (S[0] != -1) {
				if (S[4] == P[4]) {
					YaExiste = true
					break;
				}
			}
		}
	}

	if (YaExiste)
	{
		alert("Este producto ya ha sido añadido a la cesta.\nPara modificar la cantidad consulte su compra");
	}
	else
	{
		var PrTotal = Number(dameValor("sest0"));
		var NombreC = "sest";

		if (PrTotal == "") {
			PrTotal = 0;
		}
		PrTotal = Number(PrTotal) + 1;
		NombreC = NombreC + PrTotal;

		ponValor("sest0",PrTotal);
		ponValor(NombreC,producto);

		alert("Producto se ha añadido a la cesta");
	}
}
function ArrayPro(Tipo){

	CabeceraPedido();
	
	for (var i=1;i<=Number(dameValor("sest0"));i++) {

		Pro = dameValor("sest"+i);
		S = Pro.split("#");
		if (S[0] == -1) {
		    continue;
		}

		Importe = Number(S[0]) * Number(S[3]);
		Total = Number(Total) + Number(Importe);
		
		if (Tipo == 0) {
			document.write('<TR>');
			document.write('<TD align="left"><FONT FACE="VERDANA, ARIAL" size=1>'+S[1]+'</FONT></TD>');
			document.write('<TD align="left"><FONT FACE="VERDANA, ARIAL" size=1>'+S[2]+'</FONT></TD>');
			document.write('<TD align="center"><FONT FACE="VERDANA, ARIAL" size=1>'+S[4]+'</FONT></TD>');
			document.write('<TD align="right" valign="top"><FONT FACE="VERDANA, ARIAL" size=1>'+S[0]+'&nbsp;</FONT>');
			document.write('<a href="xcesta.htm" onClick="+Modificar('+i+',1)"><IMG SRC="graf/xmas.gif" WIDTH="17" HEIGHT="17" BORDER=0 ALT="Mas cantidad" align="bottom" ></A>&nbsp;');
			document.write('<a href="xcesta.htm" onClick="Modificar('+i+',0)"><IMG SRC="graf/xmenos.gif" WIDTH="17" HEIGHT="17" BORDER=0 ALT="Menos cantidad" align="bottom"></A></TD>');
			document.write('<TD align= "right"><FONT FACE="VERDANA, ARIAL" size=1>'+PonDecimales(S[3])+'</FONT></TD>');
			document.write('<TD align= "right"><FONT FACE="VERDANA, ARIAL" size=1>'+PonDecimales(Importe)+'</FONT></TD>');
			document.write('<TD><a href="xcesta.htm"  onClick="Borrar('+i+')"><IMG SRC="graf/xborral.gif" WIDTH="87" HEIGHT="17" BORDER=0 ALT="Borrar linea"></A></TD>');
			document.write('</TR>');

        }
		else
		{
			if (eval(S[0])>0)
			{
				document.write('<TR>');
				document.write('<TD align= "left"><FONT FACE="VERDANA, ARIAL" size=1>'+S[1]+'</FONT></TD>');
				document.write('<TD align= "left"><FONT FACE="VERDANA, ARIAL" size=1>'+S[2]+'</FONT></TD>');
				document.write('<TD align= "center"><FONT FACE="VERDANA, ARIAL" size=1>'+S[4]+'</FONT></TD>');
				document.write('<TD align= "right"><FONT FACE="VERDANA, ARIAL" size=1>'+S[0]+'</FONT></TD>');
				document.write('<TD align= "right"><FONT FACE="VERDANA, ARIAL" size=1>'+PonDecimales(S[3])+'</FONT></TD>');
				document.write('<TD align= "right"><FONT FACE="VERDANA, ARIAL" size=1>'+PonDecimales(Importe)+'</FONT></TD>');
				document.write('</TR>');
				LineasPedido = LineasPedido + 20;
			}

		}


	} // final for

	PiePedido();

	if (Tipo == 0) {
		BotonesPedido();
	}
	else {
		document.write('<BR><FONT face="Verdana, arial" color=#FF0000><B>Los pedidos tardan de 3 a 4 semanas</B></font><BR>');
	}

	document.write('</CENTER>');
	document.write('</DIV>');
} 
function CabeceraPedido()
{
	document.write('<DIV ID="CESTA" STYLE="position:absolute;top:60px;left:7px">');
	document.write('<CENTER>');
	document.write('<TABLE cellspacing=3 align="center" width=770>');
	document.write('<TR bgcolor ="#F1F0EA" align="center">');
	document.write('<TD><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">Referencia</FONT></TD>');
	document.write('<TD><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">Descripción</FONT></TD>');
	document.write('<TD><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">Talla</FONT></TD>');
	document.write('<TD><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">&nbsp;Cantidad&nbsp;</FONT></TD>');
	document.write('<TD><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">Precio</FONT></TD>');
	document.write('<TD><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">Importe</FONT></TD>');
	document.write('<TD bgcolor ="#FFFFFF">&nbsp;</TD>');
	document.write('</TR>');

}
function PiePedido()
{
	document.write('<TR>');
	document.write('<TD align= "right" bgcolor ="#F1F0EA" colspan=5><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">Suma de importes:&nbsp;</FONT></TD>');
	document.write('<TD align= "right" bgcolor ="#F1F0EA"><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">'+PonDecimales(Total)+'</FONT></TD>');
	document.write('<TD bgcolor ="#FFFFFF" colspan=2>&nbsp;</TD>');
	document.write('</TR>');

	document.write('<TR>');
	document.write('<TD align= "right" bgcolor ="#F1F0EA" colspan=5><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">Gastos de envio:&nbsp;</FONT></TD>');
	document.write('<TD align= "right" bgcolor ="#F1F0EA"><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#9F2186">'+PonDecimales(Gastos)+'</FONT></TD>');
	document.write('<TD bgcolor ="#FFFFFF">&nbsp;</TD>');
	document.write('<TD bgcolor ="#FFFFFF">&nbsp;</TD>');
	document.write('</TR>');

	document.write('<TR>');
	document.write('<TD align= "right" bgcolor ="#9F2186" colspan=5><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#F1F0EA"><B>Total pedido:&nbsp;</B></FONT></TD>');
	document.write('<TD align= "right"  bgcolor ="#9F2186"><FONT FACE="VERDANA, ARIAL" size=1 COLOR="#F1F0EA"><B>'+PonDecimales(eval(Total+Gastos))+'</B></FONT></TD>');
	document.write('<TD bgcolor ="#FFFFFF">&nbsp;</TD>');
	document.write('<TD bgcolor ="#FFFFFF">&nbsp;</TD>');
	document.write('</TR>');

	document.write('</TABLE>');

}
function BotonesPedido()
{
	document.write('<BR><BR><a href="javascript:history.back(1)"><IMG SRC="graf/xvolver.gif" WIDTH="87" HEIGHT="17" BORDER=0 ALT="Ir a la página anterior"></A>&nbsp;&nbsp;');
	document.write('<a href="javascript:BorrarTodo(0)"><IMG SRC="graf/xborrap.gif" WIDTH="87" HEIGHT="17" BORDER=0 ALT="Borrar TODO el pedido"></A>&nbsp;&nbsp;');
	document.write('<a href="javascript:window.print()"><IMG SRC="graf/ximprimir.gif" WIDTH="87" HEIGHT="17" BORDER=0 ALT="Imprimir el pedido"></A>&nbsp;&nbsp;');
	document.write('<a href="xayuda.htm"><IMG SRC="graf/xinforma.gif" WIDTH="87" HEIGHT="17" BORDER=0 ALT="Ver condiciones de venta y envio"></A>&nbsp;&nbsp;');
	document.write('<a href="javascript:VerificaLineasACero()"><IMG SRC="graf/xconfirma.gif" WIDTH="87" HEIGHT="17" BORDER=0 ALT="Confirmar la compra"></A>&nbsp;&nbsp;');

}
function ponValor(nombre,valor,dias) {
  if(typeof(dias) == "undefined") dias = 1;
  // una semana por defecto
  var fecha = new Date;
  fecha.setTime(fecha.getTime() + dias * 24 * 3600000);
  var caduca = "; expires=" + fecha.toGMTString();
  galleta = nombre + "=" + valor + caduca;
  document.cookie = galleta;
}

function borraValor(nombre) {
  ponValor(nombre,"",-1);
}

function dameValor(nombre) {
  var ini = document.cookie.indexOf(nombre);
  if(ini == -1) return ""; // no existe ese valor
  var sep = document.cookie.indexOf("=", ini);
  var fin = document.cookie.indexOf(";", ini);
  if(fin == -1) // el último no acaba en ;
    fin = document.cookie.length;
  return document.cookie.substring(sep+1, fin);
}

function MontaCarrito(Tipo) {

	var TotalLineas = dameValor("sest0");
	var TotalBorradas = dameValor("sestB");

	if (TotalLineas == "" || TotalLineas == TotalBorradas)
	{
		document.write('<DIV ID="CESTA" STYLE="position:absolute;top:80px;left:350px">');
		document.write('<FONT face="Verdana, arial" color=#FF0000><B>El carrito esta vacio</B></font>');
		document.write('<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:history.back(1)"><IMG SRC="graf/xvolver.gif" WIDTH="87" HEIGHT="17" BORDER=0 ALT="Ir a la página anterior"></A>&nbsp;&nbsp;');
		document.write('</DIV>');
		
	}
	else
	{
	    ArrayPro(Tipo)
    }
	
}
function PonDecimales(Numero) {

    var CNumero = Numero.toString();
	var Longitud = CNumero.length;
	var Decimales, Entero;
    
	Decimales = CNumero.substring(Longitud-2,Longitud);
	Entero    = CNumero.substring(0,Longitud-2);

	return Entero + "," + Decimales;
}

function Borrar(linea) {

	var Pro;
	var Pos;
	var BPro;

	Pro = dameValor("sest"+linea);
	Pos = Pro.indexOf("#",0);
    BPro = "-1"+Pro.substring(Pos,Pro.length);
    
	ponValor("sest"+linea,BPro);
	ContadorBorrar();

	location.reload;
	
}

function BorrarTodo() {

	if ( confirm("¿Seguro que quiere borrar todo el pedido?")) {

		for (var i=1;i<=Number(dameValor("sest0"));i++) {

			borraValor("sest"+i);
		
		}

		borraValor("sest0");
		borraValor("sestB");
		
		location.href = "xcesta.htm";

	}
}
function BorrarPed() {

	for (var i=1;i<=Number(dameValor("sest0"));i++) {

		borraValor("sest"+i);
	
	}

	borraValor("sest0");
	borraValor("sestB");
			
}
function PreguntaBorrar() {

	if ( confirm("¿Seguro que quiere borrar todo el pedido?")) {

		BorrarPed();
		location.href = "xnovedad.htm";
	}
}

function Modificar(linea, operacion) {

	var Pro;
	var Pos;
	var MPro;
	var S;
	var Suma = 0;
	
	Pro = dameValor("sest"+linea);
	S = Pro.split("#");

	Pos = Pro.indexOf("#",0);

	if (operacion == 1) {
		Suma = Number(S[0]) + 1;
		}
	else {
		if (Number(S[0]) != 0) {
			Suma = Number(S[0]) - 1;
			}
		}
	
    MPro = Suma+Pro.substring(Pos,Pro.length);
    
	ponValor("sest"+linea,MPro);

	location.reload;

}
function ContadorBorrar()
{
	var LBTotal = Number(dameValor("sestB"));

	if (LBTotal == "") {
		LBTotal = 0;
	}

	LBTotal = Number(LBTotal) + 1;
	ponValor("sestB",LBTotal);
	
}
function EscribeBR(Lineas)
{
	for (var i=1;i<=Number(Lineas);i++) {
		document.write('<BR>');
		
	}

}

function VerificaLineasACero()
{

	if (Total == 0) {
	    alert("Todas las lineas del pedido estan a 0");
	}
	else {
		location.href = "xconfirma.htm";
	}
}


function EnviaLineaPed() {
	var c = 0;
	var cadena;
	var limporte = 0;

	for (var i=1;i<=Number(dameValor("sest0"));i++) {
		Pro = dameValor("sest"+i);
		S = Pro.split("#");

		if (S[0] == -1) {
			continue;
		}

		c += 1;

		if (eval(S[0])>0){
				limporte = Number(S[0]) * Number(S[3]);
			    cadena = S[1] + " " +  S[2]  + " Cantidad: " +  S[0] + " Precio: "+PonDecimales(S[3])+ " Importe: " +PonDecimales(limporte) + " Talla: " +  S[4];
				document.write('<input type=hidden name='+c+' value='+'"'+cadena+'"'+'>');
		}
	}

	document.write('<input type=hidden name=Suma value='+'"'+PonDecimales(Total)+'"'+'>');
	document.write('<input type=hidden name=Gastos value='+'"'+PonDecimales(Gastos)+'"'+'>');
	document.write('<input type=hidden name=Total value='+'"'+PonDecimales(eval(Total+Gastos))+'"'+'>');
	document.write('<input type=hidden name=texto value="------------ D A T O S   P E R S O N A L E S  --------------------------------------------">');
}

function EnviarForm(){
	texto ='';
	error = 0;

	if (window.document.fconfirmar.nombre.value==''){
		error=1;
		texto= texto + 'Falta por completar el campo Nombre y apellidos' + '\n';
	}

	if (window.document.fconfirmar.direccion.value==''){
		if (error == 0){error=2};
		texto= texto + 'Falta por completar el campo Dirección ' + '\n';
	}

	if (window.document.fconfirmar.cpostal.value==''){
		if (error == 0){error=3};
		texto= texto + 'Falta por completar el campo Código Postal' + '\n';
	}

	if (window.document.fconfirmar.poblacion.value=='')
	{
		if (error == 0){error=4};
		texto= texto + 'Falta por completar el campo Población' + '\n';
	}

	if (window.document.fconfirmar.provincia.value=='')
	{
		if (error == 0){error=5};
		texto= texto + 'Falta por completar el campo Provincia' + '\n';
	}

	if (window.document.fconfirmar.pais.options[window.document.fconfirmar.pais.selectedIndex].text=='')
	{
		if (error == 0){error=6};
		texto= texto + 'Falta por completar el campo Pais' + '\n';
	}

	var atsign=document.fconfirmar.email.value;
	if ((atsign.indexOf('@') == -1) || (atsign == '') || (atsign.indexOf('.')== -1)){
		if (error == 0){error=7};
		texto= texto + 'Campo Correo electrónico Introduzca un valor valido' + '\n';
	}

	if (window.document.fconfirmar.informacion.value=='')
	{
		if (error == 0){error=8};
		texto= texto + 'Falta por completar el campo Desea  recibir información por E-mail SI/NO' + '\n';
	}

	if (error == 0)
	{
		window.document.fconfirmar.submit();
		BorrarPed();
	}
	else
	{
	alert(texto);
	ColocaCursor(error);
	}
}

function ColocaCursor(error){

	switch (error) { 
	case 8: 
		window.document.fconfirmar.informacion.focus()
		break
    case 7: 
		window.document.fconfirmar.correo.focus()
        break 
    case 6: 
		window.document.fconfirmar.pais.focus()
		break
    case 5: 
		window.document.fconfirmar.provincia.focus()
		break 
    case 4: 
		window.document.fconfirmar.poblacion.focus()
		 break 
    case 3: 
		window.document.fconfirmar.cpostal.focus()
		break 
    case 2: 
       window.document.fconfirmar.direccion.focus() 
	   break 
    case 1: 
       window.document.fconfirmar.nombre.focus() 
	} 
}
