﻿/* Comun */
function ArchivoCambiarEstado(url, estado)
{
	var fin = url.lastIndexOf("]");
	if (fin < 1)
		return url;
	var inicio = url.lastIndexOf("[", fin - 1);
	if (inicio < 0)
		return url;
	
	url = url.substr(0, inicio + 1) + estado + url.substr(fin);
	
	return url;
}
function EnvioTipoClass(id, nombre)
{
	this.id = id;
	this.nombre = nombre;
}
function IrA(link)
{
	window.location.href = link;
}
function PaisClass(id, nombre)
{
	this.id = id;
	this.nombre = nombre;
}
function PaisSeleccionar(selectId, valor)
{
	if (valor == '')
		return;
	for (var i = 0; i < document.getElementById(selectId).options.length; i++)
	{
		if (document.getElementById(selectId).options[i].text == valor)
			document.getElementById(selectId).options[i].selected = true;
	}
}
function Rollout(imagen)
{
	var obj = document.getElementById(imagen);
	var url = ArchivoCambiarEstado(unescape(obj.src), "Off");
	
	obj.src = url;	
}
function Rollover(imagen)
{
	var obj = document.getElementById(imagen);
	var url = ArchivoCambiarEstado(unescape(obj.src), "On");
	
	obj.src = url;	
}
		
/* Federico :: Biografia y Bibliografia */		
function CambiarEstado(Id, Item)
{
	var Objeto= document.getElementById(Item);
	
	if(Item != Id)
	{		
		if (navigator.userAgent.indexOf( "MSIE " ) > 0)
		{
			if(Objeto.style.color == '#f1b7a0')
				Objeto.style.color = '#840004';
			else
				Objeto.style.color = '#f1b7a0';					
		}
		else
		{
			if (Objeto.style.color == 'rgb(241, 183, 160)')
				Objeto.style.color = 'rgb(132, 0, 0)';	
			else
				Objeto.style.color = 'rgb(241, 183, 160)';
		}
	}
}

/* Home :: noticias */
function HomeNoticiaClase(id, texto, fecha)
{
	this.Id = id;
	this.Texto = texto;
	this.Fecha = fecha;
}
function HomeNoticiaAsignar(indice, titleAnterior, altAnterior, titleSiguiente, altSiguiente, over)
{
	var html = "";
	//noticia anterior
	if (indice > 0)
		//html += "<a href=\"javascript:HomeNoticiaAsignar(" + parseInt(indice - 1) + ",'" + titleAnterior + "','" + altAnterior + "','" + titleSiguiente + "','" + altSiguiente + "','true');\" title=\"" + titleAnterior + "\"><img alt=\"" + altAnterior + "\" src=\"_img/NoticiaAnterior[On].jpg\" /></a>";
		html += "<a href=\"javascript:HomeNoticiaAsignar(" + parseInt(indice - 1) + ",'" + titleAnterior + "','" + altAnterior + "','" + titleSiguiente + "','" + altSiguiente + "','true');\" title=\"" + titleAnterior + "\"> </a>";
	else
		html += "&nbsp;";
	document.getElementById("HomeNoticiaAnterior").innerHTML = html;
	
	
	//texto
	document.getElementById("HomeNoticiasDescripcion").innerHTML = HomeNoticias[indice].Texto;
	
	//cantidades
	html = "<span>" + parseInt(indice + 1) + "</span> / <span>" + HomeNoticias.length + "</span>";
	document.getElementById("HomeNoticiasCantidades").innerHTML = html;
	
	//noticia siguiente
	if (indice < HomeNoticias.length - 1)
		//html = "<a href=\"javascript:HomeNoticiaAsignar(" + parseInt(indice + 1) + ",'" + titleAnterior + "','" + altAnterior + "','" + titleSiguiente + "','" + altSiguiente + "','true');\" title=\"" + titleSiguiente + "\"><img alt=\"" + altSiguiente + "\" src=\"_img/NoticiaSiguiente[On].jpg\" /></a>";
		html = "<a href=\"javascript:HomeNoticiaAsignar(" + parseInt(indice + 1) + ",'" + titleAnterior + "','" + altAnterior + "','" + titleSiguiente + "','" + altSiguiente + "','true');\" title=\"" + titleSiguiente + "\"></a>";
	else
		html = "&nbsp;";	
	document.getElementById("HomeNoticiaSiguiente").innerHTML = html;
}

/* La Fundacion :: Formularios de Amigos y Suscriptores */
function CapaMostrar(id)
{
	document.getElementById(id).style.display = "block";
}
function CapaOcultar(id)
{
	document.getElementById(id).style.display = "none";
}
function SuscripcionDatosObtener(tipoSuscripcion, tipoDatos)
{
	var zona = "";
	var duracion = "";
	var precio = "";

	if (tipoSuscripcion == "1")
	{
		zona ="España";
		duracion = "1";
		precio = "12";
	}
	if (tipoSuscripcion == "2")
	{
		zona ="España";
		duracion = "2";
		precio = "20";
	}
	if (tipoSuscripcion == "3")
	{
		zona ="Europa";
		duracion = "1";
		precio = "15";
	}			
	if (tipoSuscripcion == "4")
	{
		zona ="Europa";
		duracion = "2";
		precio = "24";
	}
	if (tipoSuscripcion == "5")
	{
		zona ="Resto de países";
		duracion = "1";
		precio = "20";
	}
	if (tipoSuscripcion == "6")
	{
		zona ="Resto de países";
		duracion = "2";
		precio = "29";
	}	
	if (tipoDatos == "zona")
		return zona;
	if (tipoDatos == "duracion")
		return duracion;
	if (tipoDatos == "precio")
		return precio;
}			
function VisibilidadCambiar(id)
{
	document.getElementById(id).style.display = (document.getElementById(id).style.display == "none" ? "block" : "none");
}

/* Tienda :: Carrito operaciones (añadir, eliminar, tramitar, vaciar, continuar comprando, actualizar) */
function BotonDescargarOrdenVisibilidad(id, visible)
{
	if (visible)
		document.getElementById(id).style.display = "block";
	else
		document.getElementById(id).style.display = "none";
}
function BotonDescargarOrdenVisibilidadCambiar(id, idBoton)
{
	var obj = document.getElementById(id);
	var objBoton = document.getElementById(idBoton);				
	objBoton.style.display = "none";
	if (obj.value == 'DB') // Domiciliacion bancaria
		objBoton.style.display = "block";
}
function CarritoProcesar(idioma, operacion, id, valor, tipo)
{
	switch (operacion)
	{
		case "add":
			document.location.href = "/Tienda/CarritoProcesar.aspx?Operacion=add&Prod_Id=" + id + "&Unidades=" + valor + "&Tipo=" + tipo;
			break;
			
		case "removeAt":
			document.location.href = "/Tienda/CarritoProcesar.aspx?Operacion=removeAt&Index=" + id;
			break;
		
		case "save": 		
			document.location.href = "/Tienda/CarritoProcesar.aspx?Operacion=save";
			break;
		
		case "preview": 
			document.location.href = "/Tienda/PedidoFormulario.aspx";
			break;
			
		case "continue":
			document.location.href = "/Tienda/Categoria.aspx?Cat_Tipo=LI";
			break;
		
		/*
		//Con el rediseño de la tienda, se ha pedido quitar la opcion de "VACIAR CARRITO, accesible desde la opcion CARRITO del menu de TIENDA
		case "empty":
			document.location.href = "/Tienda/CarritoProcesar.aspx?Operacion=empty";
			break;
		*/
		
		case "update":
			document.location.href = "/Tienda/CarritoProcesar.aspx?Operacion=update";
			
		case "updateAt":
			//Actualizamos el carrito para que tenga efecto el cambio de unidades
			var error;
			var errores = "";

			error = HnlValidateNumber(valor, true, 1);
			if (error)
				errores += (idioma == 'Es' ? '- El campo es obligatorio.\n- El número debe ser entero.' : '- This field is compulsory.\n- Whole numbers only.') + "\n";
			
			if (errores)
			{
				errores = (idioma == 'Es' ? "No se han podido actualizar las unidades debido a alguno de los siguientes errores:\n" : "Could not send the form due to the following errors") + errores;
				alert(errores);
			}
			else
				document.location.href = "/Tienda/CarritoProcesar.aspx?Operacion=updateAt&Index=" + id + "&Unidades=" + valor + "&Tipo=" + tipo;
			break;
	}
}
function CarritoUnidadesValidar(e, id, idioma, operacion, indice, valor, tipo) 
{
	var keynum;
	var keychar;
	var numcheck;
	
	keynum = window.event ? e.keyCode : e.which;
	
	//ENTER => actualizamos el carrito
	if (keynum == 13)
		CarritoProcesar(idioma, operacion, indice, valor, tipo)
		
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/; //verificamos que el caracter sea un digito
	
	if (numcheck.test(keychar) && window.document.getElementById(id).value.length == 0 && parseFloat(keychar) == 0) //evitamos que pongan un "0" al principio
		return false;

	return numcheck.test(keychar);
}
function DatosFacturacionCambiar()
{
	if (document.getElementById('DatosFacturacionDiferentes').checked)
		DatosFacturacionClonar();
	else
	{
		document.getElementById("Ped_FacturacionEmpresa").value = "";
		document.getElementById("Ped_FacturacionNombre").value = "";
		document.getElementById("Ped_FacturacionApellidos").value = "";
		document.getElementById("Ped_FacturacionNifcif").value = "";
		document.getElementById("Ped_FacturacionDireccion").value = "";
		document.getElementById("Ped_FacturacionLocalidad").value = "";
		document.getElementById("Ped_FacturacionProvincia").value = "";
		document.getElementById("Ped_FacturacionCp").value = "";
		document.getElementById("Ped_FacturacionPais").value = "";
		document.getElementById("Ped_FacturacionTelefono").value = "";
		document.getElementById("Ped_FacturacionOtroTelefono").value = "";
	}
}
function DatosFacturacionClonar()
{
	//Copia los contenidos de datos personales a los de datos de facturacion
	document.getElementById("Ped_FacturacionEmpresa").value = document.getElementById("Ped_EnvioEmpresa").value;
	document.getElementById("Ped_FacturacionNombre").value = document.getElementById("Ped_EnvioNombre").value;
	document.getElementById("Ped_FacturacionApellidos").value = document.getElementById("Ped_EnvioApellidos").value;
	document.getElementById("Ped_FacturacionNifcif").value = document.getElementById("Ped_EnvioNifcif").value;
	document.getElementById("Ped_FacturacionDireccion").value = document.getElementById("Ped_EnvioDireccion").value;
	document.getElementById("Ped_FacturacionLocalidad").value = document.getElementById("Ped_EnvioLocalidad").value;
	document.getElementById("Ped_FacturacionProvincia").value = document.getElementById("Ped_EnvioProvincia").value;
	document.getElementById("Ped_FacturacionCp").value = document.getElementById("Ped_EnvioCp").value;
	document.getElementById("Ped_FacturacionPais").value = document.getElementById("Ped_EnvioPais").value;
	document.getElementById("Ped_FacturacionTelefono").value = document.getElementById("Ped_EnvioTelefono").value;
	document.getElementById("Ped_FacturacionOtroTelefono").value = document.getElementById("Ped_EnvioOtroTelefono").value;
}
function GastosEnvioActualizar(idPais, idTipoEnvio, idGastosEnvio, idSubtotal, idTotal, incluir)
{
	if (incluir == 'False')
		return;
		
	var objPais = document.getElementById(idPais);
	var objTipo = document.getElementById(idTipoEnvio);
	var objGastosEnvio = document.getElementById(idGastosEnvio);
	var objSubtotal = document.getElementById(idSubtotal);
	var objTotal = document.getElementById(idTotal);
	
	var gastos = "0,00";
	var tipo = objTipo.value;
	var zona = "";
	var subtotal = new Number(objSubtotal.innerHTML.replace(',', '.'));
	
	switch (objPais.value.toUpperCase())
	{
		case "AMÉRICA CENTRAL":
		case "AMÉRICA DEL NORTE":
		case "AMÉRICA DEL SUR":
		case "ASIA":
		case "ESTADOS UNIDOS":
		case "OCEANÍA":
			zona = "AM";
			break;
		case "ÁFRICA":
			zona = "AF";
			break;
		default:
			if (objPais.value.toUpperCase() == "ESPAÑA")
				zona = "ES";	//España
			else
				zona = "EU";	//Europa
			break;
	}
	
	gastos = GastosObtener(tipo, zona);

	objGastosEnvio.innerHTML = gastos;
	gastos = new Number(gastos.replace(',', '.'));
	var total = new Number(subtotal) + new Number(gastos);
	if (objTotal != null)
		objTotal.innerHTML = total.toFixed(2).replace('.', ',');
}
function GastosEnvioLimpiar(id)
{
	var obj = document.getElementById(id);
	if (obj != null)
		obj.innerHTML = "0,00";
}
function GastosObtener(tipo, zona)
{
	var gastos = "0,00";
	for (var i = 0; i < GastosEnvioArray.length; i++)
	{
		if (GastosEnvioArray[i][0] == tipo && GastosEnvioArray[i][1] == zona)				
			gastos = GastosEnvioArray[i][2];
	}
	return gastos;
}
function NumeroTransferenciaBancariaVisibilidad(id, visible)
{
	if (visible)
		document.getElementById(id).style.display = "block";
	else
		document.getElementById(id).style.display = "none";
}
function NumeroTransferenciaBancariaVisibilidadCambiar(id, idNumero)
{
	var obj = document.getElementById(id);
	var objNumero = document.getElementById(idNumero);				

	objNumero.style.display = "none";
	if (obj.value == 'TR') // Transferencia bancaria
		objNumero.style.display = "block";
	
}
function objSelectLimpiar(objSel)
{
	for (var m = objSel.options.length - 1; m >= 0; m--)
		objSel.options[m] = null;
}
function TiposEnvioActualizar(idPais, idTipoEnvio, idGastosEnvio, idioma)
{
	var objPais = document.getElementById(idPais);
	var objTipo = document.getElementById(idTipoEnvio);
	var arrayTipos = (idioma == 'Es' ? EnviosTiposEs : EnviosTiposEn);

	if (objPais.value == "")
		return;
		
	objSelectLimpiar(objTipo);
	
	var oOption = document.createElement("OPTION");
		oOption.text = "";
		oOption.value = "";
		objTipo.options.add(oOption);
						
	if (objPais.value == "España")
	{
		for (var i = 0; i < arrayTipos.length; i++)
		{
			if (arrayTipos[i].id != 'CC') //Carta Certificada
			{
				var oOption = document.createElement("OPTION");
				oOption.text = arrayTipos[i].nombre;
				oOption.value = arrayTipos[i].id;
				objTipo.options.add(oOption);
			}
		}
	}
	else
	{
		for (var i = 0; i < arrayTipos.length; i++)
		{
			if (arrayTipos[i].id != 'PA') //Paquete Azul
			{
				var oOption = document.createElement("OPTION");
				oOption.text = arrayTipos[i].nombre;
				oOption.value = arrayTipos[i].id;
				objTipo.options.add(oOption);
			}
		}
	}
	GastosEnvioLimpiar(idGastosEnvio);
}

/* Inicializacion */
var EnviosTiposEn = new Array(	new EnvioTipoClass("PA", "Paquete azul"),
								new EnvioTipoClass("CC", "Carta certificada"),
								new EnvioTipoClass("PE", "Postal Express"),
								new EnvioTipoClass("SS", "Free shipping"));

var EnviosTiposEs = new Array(	new EnvioTipoClass("PA", "Paquete azul"),
								new EnvioTipoClass("CC", "Carta certificada"),
								new EnvioTipoClass("PE", "Postal Exprés"),
								new EnvioTipoClass("SS", "Sin envío"));

var Paises = new Array(	new PaisClass("Alemania","Alemania"),
						new PaisClass("Austria", "Austria"),
						new PaisClass("Bélgica", "Bélgica"),
						new PaisClass("Chipre", "Chipre"),
						new PaisClass("Dinamarca", "Dinamarca"),
						new PaisClass("Eslovaquia", "Eslovaquia"),
						new PaisClass("Eslovenia", "Eslovenia"),
						new PaisClass("España", "España"),
						new PaisClass("Estonia", "Estonia"),
						new PaisClass("Finlandia", "Finlandia"),
						new PaisClass("Francia", "Francia"),
						new PaisClass("Grecia", "Grecia"),
						new PaisClass("Holanda", "Holanda"),
						new PaisClass("Hungría", "Hungría"),
						new PaisClass("Irlanda", "Irlanda"),
						new PaisClass("Italia", "Italia"),
						new PaisClass("Jamaica", "Jamaica"),
						new PaisClass("Letonia", "Letonia"),
						new PaisClass("Lituania", "Lituania"),
						new PaisClass("Luxemburgo", "Luxemburgo"),
						new PaisClass("Malta", "Malta"),
						new PaisClass("Polonia", "Polonia"),
						new PaisClass("Portugal", "Portugal"),
						new PaisClass("Reino Unido", "Reino Unido"),
						new PaisClass("República Checa", "República Checa"),
						new PaisClass("Suecia", "Suecia"),
						new PaisClass("", "----------------------------------"),
						new PaisClass("Otros países de Europa", "Otros países de Europa"),
						new PaisClass("", "----------------------------------"),
						new PaisClass("África", "África"),
						new PaisClass("América Central", "América Central"),
						new PaisClass("América del Norte", "América del Norte"),
						new PaisClass("América del Sur", "América del Sur"),
						new PaisClass("Asia", "Asia"),
						new PaisClass("Estados Unidos", "Estados Unidos"),
						new PaisClass("Oceanía", "Oceanía"));
