// JavaScript Document
function valida_frm_inscripcion(){
	var flag;
	flag = true;

	//valido el nombre del usuario
    if (document.frmInscripcion.Nombre.value.length==0){
		document.getElementById("mNombre").className = "mensajeError";
		document.getElementById("Nombre").style.backgroundColor = "#FFCC00";
		document.getElementById("mNombre").innerHTML = "Debe introducir el nombre";
		document.getElementById("mNombre").style.display = "block";
		document.frmInscripcion.Nombre.focus(); 
		flag=false;
    }else{
		document.getElementById("Nombre").style.backgroundColor = "#ffffff";
		document.getElementById("mNombre").style.display = "none";
	}

	//valido los apellidos del usuario
    if (document.frmInscripcion.Apellidos.value.length==0){
		document.getElementById("mApellidos").className = "mensajeError";
		document.getElementById("Apellidos").style.backgroundColor = "#FFCC00";
		document.getElementById("mApellidos").innerHTML = "Debe introducir los apellidos";
		document.getElementById("mApellidos").style.display = "block";
		document.frmInscripcion.Apellidos.focus(); 
		flag=false;
    }else{
		document.getElementById("Apellidos").style.backgroundColor = "#ffffff";
		document.getElementById("mApellidos").style.display = "none";
	}

	//valido el NIF del usuario
		//antes quito los espacios en blanco si los hay...
		document.frmInscripcion.NIF.value = QuitarEspacios(document.frmInscripcion.NIF.value);
    if (document.frmInscripcion.NIF.value.length==0){
		document.getElementById("mNIF").className = "mensajeError";
		document.getElementById("NIF").style.backgroundColor = "#FFCC00";
		document.getElementById("mNIF").innerHTML = "Debe introducir el DNI/NIF";
		document.getElementById("mNIF").style.display = "block";
		document.frmInscripcion.NIF.focus(); 
		flag=false;
    }else{
		document.getElementById("NIF").style.backgroundColor = "#ffffff";
		document.getElementById("mNIF").style.display = "none";
	}

	//valido la direccion del usuario
    if (document.frmInscripcion.Direccion.value.length==0){
		document.getElementById("mDireccion").className = "mensajeError";
		document.getElementById("Direccion").style.backgroundColor = "#FFCC00";
		document.getElementById("mDireccion").innerHTML = "Debe introducir la direccion";
		document.getElementById("mDireccion").style.display = "block";
		document.frmInscripcion.Direccion.focus(); 
		flag=false;
    }else{
		document.getElementById("Direccion").style.backgroundColor = "#ffffff";
		document.getElementById("mDireccion").style.display = "none";
	}

	//valido la población del usuario
    if (document.frmInscripcion.Poblacion.value.length==0){
		document.getElementById("mPoblacion").className = "mensajeError";
		document.getElementById("Poblacion").style.backgroundColor = "#FFCC00";
		document.getElementById("mPoblacion").innerHTML = "Debe introducir una población";
		document.getElementById("mPoblacion").style.display = "block";
		document.frmInscripcion.Poblacion.focus(); 
		flag=false;
    }else{
		document.getElementById("Poblacion").style.backgroundColor = "#ffffff";
		document.getElementById("mPoblacion").style.display = "none";
	}

	//valido el codigo postal del usuario
    if (document.frmInscripcion.CP.value.length==0){
		document.getElementById("mCP").className = "mensajeError";
		document.getElementById("CP").style.backgroundColor = "#FFCC00";
		document.getElementById("mCP").innerHTML = "Debe introducir un código postal";
		document.getElementById("mCP").style.display = "block";
		document.frmInscripcion.CP.focus(); 
		flag=false;
    }else{
		document.getElementById("CP").style.backgroundColor = "#ffffff";
		document.getElementById("mCP").style.display = "none";
	}

	//el formulario se envia 
	//return flag;
	if (flag){
		document.frmInscripcion.submit(); 
	}
} 

function QuitarEspacios(t){
// función para limpiar todos los espacios en blanco de una cadena (usada principalmente para el dni)
	CadenaTexto = t;
	CadenaaReemplazar = " ";
	CadenaReemplazo = "";
	CadenaTextoNueva = CadenaTexto.split(CadenaaReemplazar).join(CadenaReemplazo);
	return CadenaTextoNueva;
}

function limpiar_frm_inscripcion(){
//	document.frmInscripcion.reset();
	document.frmInscripcion.Nombre.value	= "";
	document.frmInscripcion.Apellidos.value	= "";
	document.frmInscripcion.NIF.value		= "";
	document.frmInscripcion.CP.value		= "";
	document.frmInscripcion.Email.value		= "";
	document.frmInscripcion.Direccion.value	= "";
	document.frmInscripcion.Poblacion.value	= "";
	document.frmInscripcion.Telefono.value	= "";
} 

function nuevoAjax()
{ 
	/* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
	lo que se puede copiar tal como esta aqui */
	var xmlhttp=false; 
	try 
	{ 
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// Creacion del objet AJAX para IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) { xmlhttp=false; }
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") { xmlhttp=new XMLHttpRequest(); } 

	return xmlhttp; 
}

// solicitud ajax para marcar el pago del formulario especificado para el usuario especificado como recibido
function marcar_pago_recibido(frm,id_usuario){
	var divResultado = document.getElementById("pagosLoading"+id_usuario); // capa donde va el resultado

	//validar opción válida
	// Deshabilito inputs y botones para evitar dobles clicks
	divResultado.innerHTML = "<img src='/images/loading.gif' alt='Cargando' width='16' height='16' />";
	
	// Creo la conexion con el servidor y le envio la variable evento (que le indica si debe ingresar o verificar) y el dato a utilizar
	var ajax = nuevoAjax();
	ajax.open("POST", "/actos/g/ajax_marcar_pago_recibido.asp", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("frm=" + frm + "&id=" + id_usuario);
	
	//debug
	//alert("frm=" + frm + "&id=" + id_usuario);
	
	ajax.onreadystatechange=function(){
		if (ajax.readyState==4){
			// Proceso el mensaje enviado desde el servidor
			var Respuesta = ajax.responseText;
			if (Respuesta.substring(Respuesta.length-1,Respuesta.length) != "0") {
				//divInformation.innerHTML = "<img src='/images/icons/tick.png' alt='Activación completada' width='16' height='16' />";
				//refrescarPagina();
				//alert(Respuesta);
				var acumulador = "";
				acumulador += "<a href='#' onclick=\"marcar_pago_pendiente(" + frm + ",'" + id_usuario + "')\">";
				acumulador += "<img src='/images/icons/money_euro_green.png' alt='Marcar pago pendiente' width='16' height='16' /></a>";
				//alert(acumulador); //debug
				divResultado.innerHTML = acumulador;
				//refrescarPagina();
			}else{
				divResultado.innerHTML = "<img src='/images/icons/cross.png' alt='Proceso incompleto' width='16' height='16' align='top' />";
			}
		}
	}
}

function marcar_pago_pendiente(frm,id_usuario){
	var divResultado = document.getElementById("pagosLoading"+id_usuario); // capa donde va el resultado

	//validar opción válida
	// Deshabilito inputs y botones para evitar dobles clicks
	divResultado.innerHTML = "<img src='/images/loading.gif' alt='Cargando' width='16' height='16' />";
	
	// Creo la conexion con el servidor y le envio la variable evento (que le indica si debe ingresar o verificar) y el dato a utilizar
	var ajax = nuevoAjax();
	ajax.open("POST", "/actos/g/ajax_marcar_pago_pendiente.asp", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("frm=" + frm + "&id=" + id_usuario);
	
	//debug
	//alert("frm=" + frm + "&id=" + id_usuario);
	
	ajax.onreadystatechange=function(){
		if (ajax.readyState==4){
			// Proceso el mensaje enviado desde el servidor
			var Respuesta = ajax.responseText;
			if (Respuesta.substring(Respuesta.length-1,Respuesta.length) != "0") {
				//divInformation.innerHTML = "<img src='/images/icons/tick.png' alt='Activación completada' width='16' height='16' />";
				//refrescarPagina();
				//alert(Respuesta);
				var acumulador = "";
				acumulador += "<a href='#' onclick=\"marcar_pago_recibido(" + frm + ",'" + id_usuario + "')\">"; //onclick=\'editaCategoria(" + idRegistro + ");\' >";
				acumulador += "<img src='/images/icons/money_euro_red.png' alt='Marcar pago recibido' width='16' height='16' /></a>";
				//alert(acumulador); //debug
				divResultado.innerHTML = acumulador;
				//refrescarPagina();
			}else{
				divResultado.innerHTML = "<img src='/images/icons/cross.png' alt='Proceso incompleto' width='16' height='16' align='top' />";
			}
		}
	}
}

// solicitud ajax para eliminar un usuario inscrito en un formulario de actividad
function eliminar_inscrito(frm,id_usuario){
	var respuesta;
	respuesta = confirm("¿Está seguro de que desea eliminar el registro?");
	if (respuesta == false){
		return;
	};
	
	var divResultado = document.getElementById("inscrito"+id_usuario); // capa donde va el resultado

	//validar opción válida
	// Deshabilito inputs y botones para evitar dobles clicks
	divResultado.innerHTML = "<img src='/images/loading.gif' alt='Cargando' width='16' height='16' />";
	
	// Creo la conexion con el servidor y le envio la variable evento (que le indica si debe ingresar o verificar) y el dato a utilizar
	var ajax = nuevoAjax();
	ajax.open("POST", "/actos/g/ajax_eliminar_inscrito.asp", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("frm=" + frm + "&id=" + id_usuario);
	
	//debug
	//alert("frm=" + frm + "&id=" + id_usuario);
	
	ajax.onreadystatechange=function(){
		if (ajax.readyState==4){
			// Proceso el mensaje enviado desde el servidor
			var Respuesta = ajax.responseText;
			if (Respuesta.substring(Respuesta.length-1,Respuesta.length) != "0") {
				//divInformation.innerHTML = "<img src='/images/icons/tick.png' alt='Activación completada' width='16' height='16' />";
				//refrescarPagina();
				//alert(Respuesta);
				var acumulador = "";
				acumulador += "<img src='/images/icons/tick.png' alt='Proceso completo' width='16' height='16' />";
				//alert(acumulador); //debug
				divResultado.innerHTML = acumulador;
				//refrescarPagina();
			}else{
				divResultado.innerHTML = "<img src='/images/icons/cross.png' alt='Proceso incompleto' width='16' height='16' align='top' />";
			}
		}
	}
}


function refrescarPagina(){
	location.reload(true);
}

function activar_forma_pago(){
	document.getElementById("divFormaPago").style.display = "block";
}

function desactivar_forma_pago(){
	document.getElementById("divFormaPago").style.display = "none";
}

function validar_frm_Buscar_Inscritos(){
	var flag;
	flag = true;
    //valido que haya alguna condición de búsqueda 
    if ((document.frmBusquedaInscritos.Apellidos.value.length==0) && (document.frmBusquedaInscritos.email.value.length==0) && (document.frmBusquedaInscritos.NIF.value.length==0)){
		document.getElementById("Apellidos").style.backgroundColor = "#FFCC00";
		document.getElementById("email").style.backgroundColor = "#FFCC00";
		document.getElementById("NIF").style.backgroundColor = "#FFCC00";
		document.getElementById("mensajeResultado").innerHTML = "Introduzca condiciones de búsqueda";
		document.getElementById("mensajeResultado").className = "mensajeError";
		document.getElementById("mensajeResultado").style.display = "block";
		document.frmBusquedaInscritos.Apellidos.focus(); 
		flag=false;
    }else{
		//document.getElementById("mTitulo").style.display = "none";
		document.frmBusquedaInscritos.submit();
	}
}