
function Ir_Carrito(ruta)
{
	top.document.location.href = URL()+ruta+".php";	
}

function valores_vacio()
{
	document.getElementById("envio_nombre").value = "";
	document.getElementById("envio_apellidos").value = "";
	document.getElementById("envio_direccion").value = "";
	document.getElementById("envio_localidad").value = "";
	document.getElementById("envio_ciudad").value = ""
	document.getElementById("envio_cp").value = "";
}

function Calcular()		//parar calcular los productos del carrito
{
	var registros_input = document.getElementsByTagName("input");
	var registros_select = document.getElementsByTagName("select");
	var total=0;
	var subtotal_producto=0;
	
	var precios_pro, unidades_pro, j, i;
	
	precios_pro = new Array();
	unidades_pro = new Array();
	
	j=0;
		for (i=0; i<registros_input.length; i++)
		{
			
			if (registros_input[i].id.substr(0,9) == "subPrecio")
			{
				precios_pro[j] = registros_input[i].id;
				j++;
			}
				
		}
	j=0;
		for (i=0; i<registros_select.length; i++)
		{
			if (registros_select[i].id.substr(0,11) == "subUnidades")
			{
				unidades_pro[j] = registros_select[i].id;
				j++;
			}
				
		}

		for (i=0; i<precios_pro.length; i++)
		{
				
				subtotal_producto=parseFloat(document.getElementById(precios_pro[i]).value)  * parseInt(document.getElementById(unidades_pro[i]).value);
				total += parseFloat(subtotal_producto);				
				document.getElementById("subTotal"+i).innerHTML = formato_frances(subtotal_producto)+" &euro;";
		}
		var gastos_envio = (parseFloat(total) <= 35) ? 3.90 : 0.00;
		document.getElementById("gastos_envio").value = formato_frances(gastos_envio);
		document.getElementById("Importe_Pedido").innerHTML = formato_frances(total);
		document.getElementById("Importe_Total").innerHTML = formato_frances(parseFloat(total) + parseFloat(document.getElementById("gastos_envio").value));
}


function Actualizar_Linea(unidades,producto,ruta)
{
	var ajax;
	ajax=objetoAjax();	//instanciamos el objetoAjax
	//usamos el medoto POST
	//archivo que realizar la operacion
	ajax.open("POST",ruta,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//mostrar resultados en esta capa
			document.getElementById("cesta-carrito").innerHTML = unescape(ajax.responseText);
			Calcular();
			Cerrar_Shadow();
			//MM_showHideLayers('procesando_datos','','hidden');
		}
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	//enviando los valores
	ajax.send("numUnidades="+ unidades +"&idProducto=" + producto);
}

function Comprueba_Campos(tipo) 	//funcion para realizar el pedido
{
	var Ok;
	var tipo = (tipo) ? tipo : "";
	if (document.getElementById("nombre").value=="" || document.getElementById("apellidos").value=="" || document.getElementById("direccion").value=="" || document.getElementById("localidad").value=="" || document.getElementById("provincia").value=="0" || document.getElementById("cp").value=="" || document.getElementById("email").value=="" || document.getElementById("telefono").value=="") 
	{
		MensajeShadow("Los campos con * son obligatorios. Gracias.");
	}
	else
	{
		if(document.getElementById("email").value != "") Ok = esMail(document.getElementById("email"));
	
		
		if(Ok && document.getElementById("telefono").value != "")
		{
			if(document.getElementById("telefono").value.length<9 || document.getElementById("telefono").value.length>12)
			{
				MensajeShadow(caracteres_utf8("El Tel\u00e9fono de contacto debe tener 9 d\u00edgitos si es nacional y 12 si en internacional."));
				document.getElementById("telefono").focus();
				return false;
			}
			else if(document.getElementById("telefono").value.length==9)
			{
				Ok=esFijo(document.getElementById("telefono"));
				if(!Ok) return false;
			}
			else if(document.getElementById("telefono").value.length==12)
			{
				Ok=esTelefonoInternacional(document.getElementById("telefono"));
				if(!Ok) return false;
			}
		}
		
		if(Ok)Ok=esCP(document.getElementById("cp"),document.getElementById("provincia").value,true);
		
		
		if(Ok == true && document.getElementById("datos_envio_distintos").checked == true)
		{
			if(tipo == "")
			{
				if(document.getElementById("envio_nombre").value=="" || document.getElementById("envio_apellidos").value=="" || document.getElementById("envio_direccion").value=="" || document.getElementById("envio_localidad").value=="" || document.getElementById("envio_provincia").value=="0" || document.getElementById("envio_cp").value=="") 
				{
					MensajeShadow("Los campos para enviar el pedido son obligatorios. Gracias.");
					return false;
				}
				else 
				{
					Ok=esCP(document.getElementById("envio_cp"),document.getElementById("envio_provincia").value,true);
				}
			}
			else
			{
				var cantidad = document.getElementsByName("rdDirEnvio").length;
				var num = 0; 
				for(var i = 0; i < cantidad; i++)
					if(document.form_pedido.rdDirEnvio[i].checked == true) num++;
					
				if(num == 0)
				{
					MensajeShadow("La direcci\u00f3n de env\u00edo es obligatoria. Gracias.");
					return false;
				}
			}
		}
		
		if(Ok == true)
			document.getElementById("form_pedido").submit();
	}
}