/*********************************************** Javascript para la página web de la ************************************************/
/************************************************ Fundación Carmen Pardo Valcarce *************************************************/
/*********************************************** Diseñador: Raúl González Villanueva ***********************************************/
/***************************************************** raul.gonzalez@gmail.com ****************************************************/

/* Función que envía un formulario para poder visualizar una noticia en una ventana */
function abrenoticia(idnoticia){
	cadena="";
	cadena+='<form action="../fcpv/vernoticia.php" method="post" name="noticia" target="principal">';
	cadena+='<input type="text" name="idnoticia" value="'+idnoticia+'">';
	cadena+='</form>';
	document.getElementById('capaoculta').innerHTML = cadena;
	document.noticia.submit();

}
function PopUp_center(){
	var v1=arguments,v2=v1[2].split(","),v3=(v1.length>3)?v1[3]:false,v4=(v1.length>4)?parseInt(v1[4]):0,v5=(v1.length>5)?parseInt(v1[5]):0,v6,v7=0,v8,v9,v10,v11,v12,v13,v14,v15,v16;v11=new Array("width,left,"+v4,"height,top,"+v5);for (i=0;i<v11.length;i++){v12=v11[i].split(",");l_iTarget=parseInt(v12[2]);if (l_iTarget>1||v1[2].indexOf("%")>-1){v13=eval("screen."+v12[0]);for (v6=0;v6<v2.length;v6++){v10=v2[v6].split("=");if (v10[0]==v12[0]){v14=parseInt(v10[1]);if (v10[1].indexOf("%")>-1){v14=(v14/100)*v13;v2[v6]=v12[0]+"="+v14;}}if (v10[0]==v12[1]){v16=parseInt(v10[1]);v15=v6;}}if (l_iTarget==2){v7=(v13-v14)/2;v15=v2.length;}else if (l_iTarget==3){v7=v13-v14-v16;}v2[v15]=v12[1]+"="+v7;}}v8=v2.join(",");v9=window.open(v1[0],v1[1],v8);if (v3){v9.focus();}document.MM_returnValue=false;return v9;
}
	
var ventanaPopup = {
	centrar:function(ruta,ventana){
		PopUp_center(ruta,ventana,'width=510,height=825,scrollbars=no',1,2,2);
	},
	mapa:function(ruta,ventana){
		PopUp_center(ruta,ventana,'width=900,height=676,scrollbars=no',1,2,2);
	}
}

function cambiar(ini,num,total,name){
	//eval("document."+name+".src = foto");
	eval("document.images['"+name+"'].src = imagen"+num+".src");
	total = parseInt(ini,10) + parseInt(total,10) - 1;
	for (i=ini;i<=total;i++){
		//alert (ini+', '+num+', '+total+', '+i);
		if (i == num){
			document.getElementById("capa"+i).style.background = "#339999";
			//document.getElementById("enlace").href=foto;
			document.getElementById("texto"+i).style.color = "#FFFFFF";
		}
		else{
			document.getElementById("capa"+i).style.background = "#65c8ca";
			document.getElementById("texto"+i).style.color = "#FFFFFF";
		}
	}
}

var menu = {
	despliega:function(menu,total){
		//total es el numero de opciones de menu
		for (i=1;i<=total;i++){
			if (menu == i){
				oculta = document.getElementById("linea_horiz_"+i).style.display;
				if (oculta == "none" || oculta == ""){
					document.getElementById("linea_horiz_"+i).style.display = "block";
					document.getElementById("menu_secundario_"+i).style.display = "block";
				}
				else{
					document.getElementById("linea_horiz_"+i).style.display = "none";
					document.getElementById("menu_secundario_"+i).style.display = "none";
				}
			}
			else{
				document.getElementById("linea_horiz_"+i).style.display = "none";
				document.getElementById("menu_secundario_"+i).style.display = "none";
			}
		}
	}
}

function navegador(id,seccion,pagina){
	valor = true;
	if (pagina == 'eliminar.php'){
		valor = confirm("Atención vas a eliminar al paciente. ¿Deseas eliminar al paciente?");
	}
	if (pagina == 'eliminararchivo.php'){
		valor = confirm("Atención vas a eliminar el archivo. ¿Deseas eliminarlo?");
	}
	if (valor){
		cadena="";
		cadena+='<form action="../'+seccion+'/'+pagina+'" method="post" name="datos">';
		cadena+='<input type="text" name="id" value="'+id+'">';
		cadena+='</form>';
		document.getElementById('capaoculta').innerHTML = cadena;
		document.datos.submit();
	}
}

function borra(producto){
		document.getElementById(producto).value = "";
}

function comprueba(producto){
	caracter = document.getElementById(producto).value;
	if (caracter == ""){
		document.getElementById(producto).value = "0";
	}
}

function redondeo2decimales(numero){
	var original=parseFloat(numero);
	var result=Math.round(original*100)/100 ;
	return result;
}

function calcular(iprem){
	iprem = eval(iprem);
	if (document.calculadora.num_trab.value != '' && document.calculadora.num_trab_dis.value != ''){
		num_trab = eval(document.calculadora.num_trab.value);
		num_trab_dis = eval(document.calculadora.num_trab_dis.value);
		//Comprobamos que es un número entero
		if (/^(\+|\-)?\d+$/.test(num_trab) == false || /^(\+|\-)?\d+$/.test(num_trab_dis) == false){
			alert("Tienes que poner un número entero para el número de trabajadores y el número de trabajadores discapacitados");
		}
		else{
			if (num_trab < 50){
				alert("Tu empresa está exenta de esta obligación ya que no supera los 50 trabajadores");
				document.calculadora.donacion.value = '0.00';
				document.calculadora.contraprestacion.value = '0.00';
				document.calculadora.total_dona.value = '0.00';
				document.calculadora.total_contra.value = '0.00';
			}
			else{
				donacion = redondeo2decimales(1.5 * iprem * (num_trab * 0.02 - num_trab_dis));
				contraprestacion = redondeo2decimales(3 * iprem * (num_trab * 0.02 - num_trab_dis));
				document.calculadora.donacion.value = donacion;
				document.calculadora.contraprestacion.value = contraprestacion;
				var resultado = 0;
				var sumando = 0;
				for (var i=1;i<=6;i++){
					sumando += eval ("document.calculadora.producto"+i+".value");
					if (sumando != ''){
						sumando = parseFloat(sumando);
						resultado += sumando;
					}
					sumando = 0;
				}
				if (resultado != 0){
					document.calculadora.subtotales.value = resultado;
				}
				document.calculadora.total_dona.value = redondeo2decimales((contraprestacion - resultado) / 2);
				document.calculadora.total_contra.value = redondeo2decimales(contraprestacion - resultado);
			}
		}
	}
	else{
		if (document.calculadora.num_trab.value == ''){
			alert("Tienes que poner un valor para el número de trabajadores");
		}
		else{
			alert("Tienes que poner un valor para el número de trabajadores discapacitados");
		}
	}
}

var popUp = {
	capa:null,
	pos:[0,0],
	posAntigua:[null,null],
	load:function(elem){
		this.capa=$(elem);
		popUp.crearCapa();
		popUp.mostrar()
		if($(".popup img.button_cerrar").size()!=0) {
			$(".popup img.button_cerrar").click(function(){
				popUp.ocultar();
			});
		}
	},
	crearCapa:function(){
		var anchura = $("body").width();
		var altura = $("body").height();
		if($("#wrappop1").size()==0){
			$("#page").after("<div id='wrappop1'></div>")
		}
		if (altura==0){
			var margenes = parseInt($("#cabecera").css("marginTop"))+parseInt($("#cabecera").css("marginBottom"))+parseInt($("#contenedorprincipal").css("marginBottom"))+parseInt($("#pie").css("marginBottom"));
			altura=$("#cabecera").height()+$("#contenedorprincipal").height()+$("#pie").height()+margenes;
		}
		$("#wrappop1").height(altura)
		popUp.pos = popUp.calculaPos(anchura,altura);
	},
	calculaPos:function(ancho,alto){
		var posicion=[0,0];
		posicion[0]=parseInt((ancho/2)-($(popUp.capa).width()/2));
		posicion[1]=20;
		//posicion[1]=parseInt((alto/2)-($(popUp.capa).height()/2))-50;
		return posicion;
	},
	mostrar:function(){
		$(popUp.capa).css("left",popUp.pos[0]+'px');
		$(popUp.capa).css("top",popUp.pos[1]+'px');
		$("#wrappop1").css("display","block");
		$(popUp.capa).show("slow");
		$(popUp.capa).find(".cab_popup").bind("mouseover",function(){
			$(this).css("cursor","move"); //Cambio la imagen del cursor para que  se sepa que se puede mover el popup
			$(this).bind("mousedown",function(e){
				posAntigua = [e.pageX,e.pageY];
				$(document).bind("mousemove",function(e){
					popUp.desplaza(e);
				});
			})
		})
		$(popUp.capa).find(".cab_popup").bind("mouseup",function(){
			$(document).unbind("mousemove");
			$(popUp.capa).find(".cab_popup").css("cursor","default");
		})
	},
	ocultar:function(){
		$(popUp.capa).hide("slow");
		$("#wrappop1").css("display","none");
		$("#video").css("visibility","visible");
	},
	desplaza:function(e){
		if(popUp.posAntigua[0] == null){
			popUp.posAntigua = [e.pageX,e.pageY]
		}
		var posPopup = [parseInt($(popUp.capa).css("left")),parseInt($(popUp.capa).css("top"))];
		var posX = (posPopup[0]+((e.pageX)-popUp.posAntigua[0]));
		var posY = (posPopup[1]+((e.pageY)-popUp.posAntigua[1]));
		$(popUp.capa).css("left",posX+"px");
		$(popUp.capa).css("top",posY+"px");
		popUp.posAntigua = [e.pageX,e.pageY];
	}
}

function validarfechas(fecha) {
		if (fecha == '') {
			return(fecha)
		}
		else {
			fecha = fecha.split('/');
			if (fecha.length == 1){
				alert('Tienes que introducir bien la fecha');
				return false;
			}
			else{
				if ((fecha[0].length != 2) || (fecha[1].length != 2) || (fecha[2].length != 4)){
				  alert('Tienes que introducir bien la fecha');
					return false;
				}
				else{				
					return true;
				}
			}
		}
	}

function enviar() {
		if (!document.vale_descuento.sexo[0].checked && !document.vale_descuento.sexo[1].checked){
			alert('Tienes que especificar tu sexo');
			return(false);
		}
		if (document.vale_descuento.nombre.value == ''){
	  	alert("Tienes que introducir tu nombre");
			document.vale_descuento.nombre.focus();
			return(false);
		}
		if (document.vale_descuento.apellido1.value == ''){
			alert("Tienes que introducir tu primer apellido");
			document.vale_descuento.apellido1.focus();
			return(false);
		}
		if (document.vale_descuento.telf_movil.value == ''){
			alert("Tienes que introducir el teléfono movil");
			document.vale_descuento.telf_movil.focus();
			return(false);
		}
		else{
			if (document.vale_descuento.telf_movil.value.length < 9){
				alert("El teléfono movil debe tener 9 cifras");
				document.vale_descuento.telf_movil.focus();
				return(false);
			}
			if(/^\d{9}$/.test(document.vale_descuento.telf_movil.value)==false) {
				alert('El teléfono movil tiene que tener solo cifras');
				return(false);
			}
		}
		if (document.vale_descuento.conocio.value == '-1'){
			alert("Tienes que introducir el campo ¿Como nos conoció?");
			return(false);
		}
		if (document.vale_descuento.contacto[0].checked){
			if (!document.vale_descuento.comunicacion[0].checked && !document.vale_descuento.comunicacion[1].checked){
				alert("Tienes que introducir el campo ¿Cómo prefiere recibir nuestras comunicaciones?");
				return(false);
			}
			else{
				if (document.vale_descuento.comunicacion[0].checked){
					if (document.vale_descuento.email.value == ''){
						alert("Para recibir información via Email \n tienes que facilitarnos tu correo electrónico");
						document.vale_descuento.email.focus();
						return(false);
					}
					else{
						if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.vale_descuento.email.value)){
							alert("La dirección de email " + document.vale_descuento.email.value + " es incorrecta.");
							document.vale_descuento.email.focus();
							return (false)
						}
					}
				}
				if (document.vale_descuento.comunicacion[1].checked){
					if (document.vale_descuento.direccion.value == ''){
						alert("Para recibir información via Correo ordinario \n tienes que facilitarnos tu dirección");
						document.vale_descuento.direccion.focus();
						return(false);
					}
					if (document.vale_descuento.poblacion.value == ''){
						alert("Para recibir información via Correo ordinario \n tienes que facilitarnos tu población");
						document.vale_descuento.poblacion.focus();
						return(false);
					}
					if (document.vale_descuento.provincia.value == '-1'){
						alert("Para recibir información via Correo ordinario \n tienes que facilitarnos tu provincia");
						document.vale_descuento.provincia.focus();
						return(false);
					}
					if (document.vale_descuento.cp.value == ''){
						alert("Para recibir información via Correo ordinario \n tienes que facilitarnos tu código postal");
						document.vale_descuento.cp.focus();
						return(false);
					}
				}
			}			
		}
		
		var bien = validarfechas(document.vale_descuento.nacimiento.value)
		if ((bien)||(document.vale_descuento.nacimiento.value=='')){
			document.vale_descuento.submit();
		}
		else {
			document.vale_descuento.nacimiento.focus();
			return false;
		}
	}
var apar = 0.0;
var aparIE = 0;
var i = 0;
var idtime = 0;
function aparecer(){
	apar += 0.1;
	aparIE += 10;
	document.getElementById("capa_noticia_"+i).style.opacity = apar;
	//document.getElementById("capa_noticia_"+i).style.filter = "alpha(opacity="+aparIE+")";
	//alert("opacidad de la capa "+i+": "+document.getElementById("capa_noticia_"+i).style.opacity);
	//alert("apar de aparecer: "+apar);
	if (apar < 1.1){
		//alert("Entra");
		idtime = setTimeout("aparecer()", 100);
	}
	else{
		//alert("Empieza desaparecer");
		idtime = setTimeout("desaparecer()", 5000);
	}
}

function desaparecer(){
	apar -= 0.1;
	aparIE -= 10;
	document.getElementById("capa_noticia_"+i).style.opacity = apar;
	//document.getElementById("capa_noticia_"+i).style.filter = "alpha(opacity="+aparIE+")";
	if (apar >= 0.0){
		idtime = setTimeout("desaparecer()", 100);
	}
	else{
		document.getElementById("capa_noticia_"+i).style.opacity = "0.0";
		//document.getElementById("capa_noticia_"+i).style.filter = "alpha(opacity=0)";
		document.getElementById("capa_noticia_"+i).style.display = "none";
		i++;
		if (i == 5) {
			i = 0;
		}
		idtime = setTimeout("escrolea()", 1000);
	}
}


function escrolea( ){
	document.getElementById("capa_noticia_"+i).style.opacity = "0.0";
	//document.getElementById("capa_noticia_"+i).style.filter = "alpha(opacity=0)";
	document.getElementById("capa_noticia_"+i).style.display = "block";
	//alert("Empieza aparecer");
	aparecer();
}

function pausar(){
	clearTimeout(idtime);
	apar = 1.2;
	aparIE = 100;
	document.getElementById("capa_noticia_"+i).style.opacity = apar;
	//document.getElementById("capa_noticia_"+i).style.filter = "alpha(opacity="+aparIE+")";
	//alert("opacidad de la capa "+i+": "+document.getElementById("capa_noticia_"+i).style.opacity);
	//alert("apar de aparecer: "+apar);
}

function activar(){
	apar = 1.2;
	aparIE = 100;
	document.getElementById("capa_noticia_"+i).style.opacity = apar;
	//document.getElementById("capa_noticia_"+i).style.filter = "alpha(opacity="+aparIE+")";
	idtime = setTimeout("desaparecer()", 100);
}

/*
function escrolea(){
	for (i=0;i<5;i++){
		document.getElementById("capa_noticia_"+i).style.opacity = "0.0";
		document.getElementById("capa_noticia_"+i).style.display = "block";
		alert("capa_noticia_"+i);
		for(j=1;j<10;j++){
			setTimeout("aparecer('0.0',"+i+")",1000);
			alert("Entra");
		}
		//setTimeout(,40);
		for(j=1;j<11;j++){
			setTimeout("desaparecer('1.0',"+i+")",1000);
		}
		document.getElementById("capa_noticia_"+i).style.display = "none";
	}
}
*/

function enviar_jornadas() {
	if (document.form_jornadas.nombre.value == ''){
	alert("Tienes que introducir tu nombre");
		document.form_jornadas.nombre.focus();
		return(false);
	}
	if (document.form_jornadas.apellido1.value == ''){
		alert("Tienes que introducir tu primer apellido");
		document.form_jornadas.apellido1.focus();
		return(false);
	}
	if (document.form_jornadas.apellido2.value == ''){
		alert("Tienes que introducir tu segundo apellido");
		document.form_jornadas.apellido2.focus();
		return(false);
	}
	if (document.form_jornadas.email.value == ''){
		alert("Tienes que introducir el correo electrónico");
		document.form_jornadas.email.focus();
		return(false);
	}
	else{
		if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.form_jornadas.email.value)){
			alert("La dirección de email " + document.form_jornadas.email.value + " es incorrecta.");
			document.form_jornadas.email.focus();
			return (false)
		}
	}
	if (document.form_jornadas.movil.value == ''){
		alert("Tienes que introducir el teléfono movil");
		document.form_jornadas.movil.focus();
		return(false);
	}
	else{
		if (document.form_jornadas.movil.value.length < 9){
			alert("El teléfono movil debe tener 9 cifras");
			document.form_jornadas.movil.focus();
			return(false);
		}
		if(/^\d{9}$/.test(document.form_jornadas.movil.value)==false) {
			alert('El teléfono movil tiene que tener solo cifras');
			document.form_jornadas.movil.focus();
			return(false);
		}
	}
	document.form_jornadas.submit();
}
	
