// JavaScript Document

if (tienda){}else{var tienda = new Array();}
if (cont){}else{var cont=0;}
function ver_valor(valor){
	var number = parseInt(valor);
	if(isNaN(number)==true){
		val=0;
		alert("Valor no numerico");
		return val;
	}else if(number<=0){
		val=0;
		alert("Valor tiene que ser superior 0");
		return val;
	}
}

function refrescar_carrito(){
	location.href = 'productos.asp';
}
function ad_ces(cant){
	alert(cant);
}

function ad_cesta(id, cant, ref){
	//alert(cant);
	var ind, pos1;
	pos1=1;
	//alert("document.getElementById('cant_'+<%=propios.Fields.Item("+id+").Value%>+').value="+cant);
	//eval("document.getElementById('cant_<%=propios.Fields.Item("+id+").Value%>').value="+cant);
	eval("document.productos.cant_"+ id +".value="+cant);
	if(ver_valor(cant)!=0){
		if (tienda.length!=0){
			
			for(ind=0; ind<tienda.length; ind++){
					//alert("buscando");
					
					if (tienda[ind][0] == id){
						//alert("reg duplicado");
						
						tienda[ind][1]=parseInt(cant);//+parseInt(tienda[ind][1]);
						//alert("reg ad valor");
						pos1=0;
						alert("Producto aņadido a la cesta");
						break;
					}
			}
			if(pos1==1){
				cont=tienda.length++;
				//cont++;
				tienda[cont]=new Array(parseInt(id), parseInt(cant), ref);
				
				alert("Producto aņadido a la cesta");
				pos1=1;
				//alert("ad reg");
			}
		}else{
			tienda[cont]=new Array(parseInt(id), parseInt(cant), ref);
			alert("Producto aņadido a la cesta");
			//alert("new reg");
		}
		var cant_total=0;
			for(ind=0;ind<tienda.length;ind++){
						eval("document.forms.productos.cant_"+tienda[ind][0]+".value='"+tienda[ind][1]+"'");
						cant_total=parseInt(cant_total)+parseInt(tienda[ind][1]);	
			} 
		document.forms.productos.num_total.value=cant_total;
		//document.forms.productos.num_total.value=tienda.length;
		//refrescar_carrito();
		
	}
	
}

function pasarACookie(esto){
	var pepe = new Array();
	//alert("pasarAcookie var esto: " + esto);
	pepe=esto;
	var valores="";
	for(a=0;a<pepe.length;a++){
		for(b=0;b<pepe[a].length;b++){
			if(b>0){valores+=",";}
			valores+=pepe[a][b];
		}
		valores+="|";
	}
	//alert("res cookie: " + valores);
	expir = new Date();
	tiempo = expir.getTime()+(24*3600*1000*10);
	//tiempo = expir.getTime()+(0*0*0*0);
	expir.setTime(tiempo);
	//valores = valores + "; expires="+expir.toGMTString()+"; path=/";
	valores = "cesta_"+document.forms.productos.login.value+"=" + valores + "; expires="+expir.toGMTString()+"; path=/";
	//alert("despues res cookie: " + valores);
	//alert("funcio1: "+valores);
	//valores = valores + "; path=/";
	document.cookie=valores;
	
	//alert("funcio2: "+document.cookie);
	//alert("grabado");
	//alert("pass ck:"+document.cookie);
}
function Get_Cookie( name ) {	
	//alert("entra get_cookie");
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function pasarAArray(){
	
	//alert("pasarAArray: " + document.cookie);
	//Get_Cookie('cesta');
		var texto;
		var cookie_nombre = 'cesta_'+document.forms.productos.login.value;
		texto=Get_Cookie(cookie_nombre);
		
		var pepe = new Array();
		
		var cosa;
		//alert("texto pasarAArray: "+texto);
		pepe=texto.split("|");
		
		for(var a=0;a<pepe.length;a++){
			
			cosa=pepe[a];	
			
			pepe[a]=cosa.split(",");
		
		}
		//alert("resultado pepe array: " + pepe);
		return pepe;
	//alert("pass array:"+document.cookie);
}

function verCesta(urlCesta)
{
	pasarACookie(tienda);
	location.href = urlCesta;
}

function borrar_prod(id){
	var aux = new Array();
	for(ind=0;ind<tienda.length-1;ind++){
		if (ind!=id){
			//alert(tienda[ind]);
			aux[cont++]=tienda[ind];
		}
	}
	//alert("borrar_prod: "+id);
	pasarACookie(aux);
	location.href = 'cesta.asp';
}

function preparar_pedido(){
	var pedido="";
	for(c=0;c<tienda.length-1;c++){
		pedido=pedido+tienda[c]+"|";
	}
	//alert(pedido);
	location.href = 'pedido.asp?ped='+pedido+'';
}

function Delete_Cookie( name, path, domain ) {
	//alert("entra");
	var val;
	val="";
	//document.cookie= name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	document.cookie=val;
	//alert(document.cookie);
}


function borrar_pedido_1(login1){
	//alert(document.forms.productos.login.value);
	//var cookie_name = 'cesta_' + document.forms.productos.login1.value;
	var cookie_name = 'cesta_' + login1;
	var result = get_cookie(cookie_name);
	document.cookie = cookie_name + '=; expires=' + new Date().toGMTString() + '; path=/';
	return result;	 
}
function borrar_pedido( login1, path) {
	var name= 'cesta_'+login1;
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
		 
		 
function borrar_pedido_2(){
	//Delete_Cookie( '', '/', '') ;
	var valores1;
	valores1="";
	//document.cookie= valores1;	
	//var valores2;
	//var tiempo2;
	//valores2="";
	//expir2 = new Date();
	//tiempo2 = expir.getTime()+(24*3600*1000*10);
	//tiempo2 = expir2.getTime()+(0*0*0*0);
	//tiempo2 = "Thu,01-Jan-70 00:00:01 GMT";
	
	// document.cookie=name+"="

//+((path==null)?"":";path="+path)

//+((domain==null)?"":";domain="+domain)+";

//expires=Thu,01-Jan-70 00:00:01 GMT"; 

	//expir2.setTime(tiempo2);
//	valores2 = valores2 + " expires="+expir2.toGMTString()+"; path=/";
	//valores2 = "expires="+tiempo2+"; path=/";
	//alert(valores2);
	//document.cookie= valores2;
	document.cookie= valores1;
}

