var xmlHttp;

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function bloquear(flag)
{
	// "true" bloquea la pantalla
	// "false" desbloquea la pantalla
}

function llamadaAlServidor(identificador,nuevaCantidad)
{
//	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		
		// Actualizar Cantidad
		var objeto = document.getElementById("cantidad-" + identificador);
		objeto.value = nuevaCantidad;

		// Actualizar Monto
		objeto = document.getElementById("total-" + identificador);
		precio = document.getElementById("precio-" + identificador).innerHTML;
		precio = precio * 1;
		total = precio * nuevaCantidad;
		objeto.innerHTML = total;
}

function acutalizarMontos(identificador, nuevaCantidad)
{
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url = "/index.php?action=actualizarCanasta&modul=cesta";
	url = url + "&id=" + identificador;
	url = url + "&nuevaCant=" + nuevaCantidad;

	xmlHttp.onreadystatechange = llamadaAlServidor(identificador,nuevaCantidad);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function incremento(identificador, tipo)
{
	//bloquear(true);
	var item = "cantidad-" + identificador;
	var objeto = document.getElementById(item);
	var nuevaCantidad = document.getElementById(item).value;
	
	nuevaCantidad = (nuevaCantidad * 1);	
	if(tipo == '+')
		nuevaCantidad = nuevaCantidad + 1;
	else
		nuevaCantidad = nuevaCantidad - 1;	
	
	acutalizarMontos(identificador, nuevaCantidad);
	//bloquear(false);
}

function cargarCesta()
{
	stock = document.getElementById("stock").value;
	idioma = document.getElementById("idioma").value;	
	id_articulo = document.getElementById("id_articulo").value;
	nuevoEnCesta = document.getElementById("nuevoEnCesta_"+id_articulo).value;	
	mensaje = 0;

	if(nuevoEnCesta == 0)// No nuevo
	{				
		nuevaCant = (document.getElementById("cantMiniCesta_"+id_articulo).innerHTML * 1) + 1;			
		if(nuevaCant > stock)
			mensaje = 0;
	}

	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	var url = "/index.php?action=agregarProductoCesta&modul=cesta";
	url = url + "&id_articulo=" + id_articulo;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	actualizarMiniCesta();

	nuevaCant = (document.getElementById("cantMiniCesta_"+id_articulo).innerHTML * 1);			
	if(nuevaCant == stock)
		document.getElementById("detalle_compra_stock").src = '../../../images/rojo.gif';	

}
function separarmiles(nums)
{
var longitud = nums.length;
if(longitud<=6)
	return nums;
var comma = nums.indexOf(',');

longMiles=comma;
if(longMiles<3)
	miles=0;
else
	miles = Math.floor(longMiles / 3);
milesAux="";
for(i=0;i<=miles;i++)
	{
	if(i!=miles)
		milesAux= "." + nums.substring((longMiles-3),longMiles)+ milesAux;	
	else
		milesAux=nums.substring((longMiles-3),longMiles) + milesAux;
	longMiles=longMiles-3;
	}
milesAux=milesAux + nums.substring(comma,comma+3);
		
return milesAux;
}



function actualizarMiniCesta()
{
	//nombre_modelo_act = document.getElementById("nombre_modelo_act").innerHTML;
	id_articulo = document.getElementById("detalle_compra_nombre").innerHTML;
	color = document.getElementById("detalle_compra_color").innerHTML;
	medidas = document.getElementById("detalle_compra_medidas").innerHTML;
	precio = (document.getElementById("detalle_compra_precio").innerHTML);	 
	nuevoEnCesta = document.getElementById("nuevoEnCesta_"+id_articulo).value;
	precio_numerico=precio.replace(/\./g,"");
	//precio_numerico=parseFloat(precio_numerico.replace(/\,/g,"."));
	precio_numerico=precio_numerico.replace(/\,/g,".");
	//precio_numerico = precio_numerico.split(",");
	//precio_numerico=((precio_numerico_aux[0])+((precio_numerico_aux[1])/100));
	if(nuevoEnCesta == 0)// No nuevo  
	{
		nuevaCant = (document.getElementById("cantMiniCesta_"+id_articulo).innerHTML * 1) + 1;
		document.getElementById("cantMiniCesta_"+id_articulo).innerHTML = nuevaCant;
		valor1 =document.getElementById("totalProdMiniCesta_"+id_articulo).innerHTML; 
		valor1=valor1.replace(/\./g,"");
		valor1=valor1.replace(/\,/g,".");
		nuevoPrecio=eval(parseFloat(valor1)+ parseFloat(precio_numerico));
		nuevoPrecio=String(nuevoPrecio).replace(/\./g,",");
		nuevoPrecio = '' + nuevoPrecio;
		pos =  nuevoPrecio.indexOf(',');
		if(pos<0)
			nuevoPrecio = nuevoPrecio+',00';
		else
		{
			if( (pos+3) > nuevoPrecio.length ) 
				nuevoPrecio = nuevoPrecio + '0';			
		}
		nuevoPrecio=separarmiles(nuevoPrecio);	
		document.getElementById("totalProdMiniCesta_"+id_articulo).innerHTML = nuevoPrecio;
	}
	else // Nuevo
	{
		precioTxt = '' + precio;
		pos =  precioTxt.indexOf(',');
		if(pos<0)
			precioTxt = precioTxt + ',00';
		else
		{
			if( (pos+3) > precioTxt.length ) 
				precioTxt = precioTxt + '0';			
		}
		nuevoItem = document.getElementById("nuevoItemCesta").innerHTML + "<table border='0' cellpadding='0' cellspacing='0'><tr><td class='col1'><span id='cantMiniCesta_"+id_articulo+"'>1</span></td><td class='col2'><span id='descripMiniCesta_"+id_articulo+"'>"+ document.getElementById("nombre_modelo_act").innerHTML + " " + color + " " + medidas +"</span></td><td class='col3'>&euro; <span id='totalProdMiniCesta_"+id_articulo+"'>" + precioTxt + "</span>.-</td></tr></table>";
		document.getElementById("nuevoItemCesta").innerHTML = nuevoItem;
		document.getElementById("miniCesta").style.display = "block";
		document.getElementById("nuevoEnCesta_"+id_articulo).value = 0;
	}
	
	valor1 = (document.getElementById("totalMiniCesta").innerHTML);
	if(valor1=="")
		valor1="0";
	valor1=valor1.replace(/\./g,"");
		valor1=valor1.replace(/\,/g,".");
		nuevoTotal=eval(parseFloat(valor1)+ parseFloat(precio_numerico));
		nuevoTotal=String(nuevoTotal).replace(/\./g,",");
	
	nuevoTotal = '' + nuevoTotal;
	
	pos =  nuevoTotal.indexOf(',');
	if(pos<0)
		nuevoTotal = nuevoTotal+',00';
	else
	{
		if( (pos+3) > nuevoTotal.length ) 
			nuevoTotal = nuevoTotal + '0';			
	}
	nuevoTotal=separarmiles(nuevoTotal);
	document.getElementById("totalMiniCesta").innerHTML = nuevoTotal;	
}



function reCargarCesta(id_articulo)
{
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	var url = "/index.php?action=agregarProductoCesta&modul=cesta";
	url = url + "&id_articulo=" + id_articulo;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	actualizarCestaYMiniCestaMas(id_articulo);
}

function actualizarCestaYMiniCestaMas(id_articulo)
{
	//Actualizacion Cesta
	precio = (document.getElementById("precioProdCesta_"+id_articulo).innerHTML * 1);	 
	
	nuevaCant = (document.getElementById("cantCesta_"+id_articulo).value * 1) + 1;
	document.getElementById("cantCesta_"+id_articulo).value = nuevaCant;
	
	nuevoPrecio = (document.getElementById("totalProdCesta_"+id_articulo).innerHTML * 1) + precio;
	document.getElementById("totalProdCesta_"+id_articulo).innerHTML = nuevoPrecio;
	
	nuevoTotal = (document.getElementById("totalCesta").innerHTML * 1) + precio;
	document.getElementById("totalCesta").innerHTML = nuevoTotal;
	
	//Actualizacion MiniCesta		
	document.getElementById("cantMiniCesta_"+id_articulo).innerHTML = nuevaCant;
	document.getElementById("totalProdMiniCesta_"+id_articulo).innerHTML = nuevoPrecio;
	document.getElementById("totalMiniCesta").innerHTML = nuevoTotal;	
}

function desCargarCesta(id_producto)
{
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
/*	var url = "/index.php?action=decrementarUnProductoCesta&modul=cesta";
	url = url + "&id_producto=" + id_producto;*/

	window.location='/index.php?action=decrementarUnProductoCesta&modul=cesta&id_producto='+id_producto;

	/*xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	actualizarCestaYMiniCestaMenos(id_articulo);*/
}

function actualizarCestaYMiniCestaMenos(id_articulo)
{
	//Actualizacion Cesta
	precio = (document.getElementById("precioProdCesta_"+id_articulo).innerHTML * 1);	 
	
	nuevaCant = (document.getElementById("cantCesta_"+id_articulo).value * 1) - 1;
	document.getElementById("cantCesta_"+id_articulo).value = nuevaCant;
	
	nuevoPrecio = (document.getElementById("totalProdCesta_"+id_articulo).innerHTML * 1) - precio;
	document.getElementById("totalProdCesta_"+id_articulo).innerHTML = nuevoPrecio;
	
	nuevoTotal = (document.getElementById("totalCesta").innerHTML * 1) - precio;
	document.getElementById("totalCesta").innerHTML = nuevoTotal;
	
	//Actualizacion MiniCesta		
/*	document.getElementById("cantMiniCesta_"+id_articulo).innerHTML = nuevaCant;
	document.getElementById("totalProdMiniCesta_"+id_articulo).innerHTML = nuevoPrecio;
	document.getElementById("totalMiniCesta").innerHTML = nuevoTotal;	*/
}


