// FICHEROS

function inicializarFormulario(accion){
	switch(accion){
		case "buscar":
			listarParaDoctorando();
			break;
		case "listar":
			inicializarUniversidad();
			inicializarEstadoCarga();
			inicializarFormParaDoctorando();
			break;
		case "alta":
			inicializarTipoRepositorio();
			inicializarGenerales();
			break;
		case "modificar":
			inicializarTipoRepositorio();
			//inicializarLocalizacion();
			//inicializarIsbn();
			break;
		default:
			break;
	}
}

function listarParaDoctorando(){
	var idRol = getIdRol();
	var permisoFicheros = getPermisoFicheros();
	if(idRol==1 && permisoFicheros==true){
		document.BusquedaListaForm.submit();
	}
}
function inicializarUniversidad(){
	var idUni = getIdUni();
	try{
		document.getElementById("idUni").value = idUni;
	}catch(e){/* solamente esta disponible para ccu */}
}
function inicializarEstadoCarga(){
	var conFichero = getConFichero();
	document.getElementById("conFichero").value = conFichero;
}
function inicializarFormParaDoctorando(){
	var idRol = getIdRol();
	var permisoFicheros = getPermisoFicheros();
	if(idRol==1 && permisoFicheros==true){
		document.getElementById("nif").value = getNifSession();
		document.getElementById("conFichero").value = 2;
		document.getElementById("fechaLectura").disabled = true;
		document.getElementById("nif").disabled = true;
		document.getElementById("doctorando").disabled = true;
		document.getElementById("conFichero").disabled = true;
	}
}

function inicializarTipoRepositorio(){
	var ruta = document.getElementById("ruta").value;
	var externa = document.getElementById("rutaExterna").value;
	
	var tipoRepo = "FS";
	if(ruta!=""){
		if(externa!="http://"){
			tipoRepo = "EX";
		}
	}
	document.getElementById("tipoRepositorio").value = tipoRepo;
	actualizarTipoRepo(tipoRepo);
}


/*
function inicializarTipoRepositorio(){
	var tipoRepo = getTipoRepo();
	if(tipoRepo == "EX")
		document.getElementById("tipoRepositorio").value = "EX";
	else
		document.getElementById("tipoRepositorio").value = "FS";
	actualizarTipoRepo(tipoRepo);
}

function inicializarRuta(){
	var tipoRepo = getTipoRepo();
	var ruta = getRuta();
	if(tipoRepo=="EX")
		document.getElementById("rutaExterna").value = ruta;
	else if(tipoRepo=="FS")
		document.getElementById("rutaLocal").value = ruta;
}
*/
function inicializarGenerales(){
	var localizacion = document.getElementById("localizacion").value;
	var isbn = document.getElementById("isbn").value;

	// si reset == Finalizar => se continua insertando ficheros.
	var primerFichero = (document.getElementById("reset").value == "Cancelar") ? true : false;

	if(primerFichero==false){
		document.getElementById("localizacion").setAttribute('readonly',true);
		document.getElementById("isbn").setAttribute('readonly',true);
	}
}

function prepararRuta(){
	var tipoRepo = document.getElementById("tipoRepositorio").value;
	var ruta = "";
	if(tipoRepo=="EX")
		ruta = document.getElementById("rutaExterna").value;
	else if(tipoRepo=="FS")
		ruta = document.getElementById("rutaLocal").value;

	document.getElementById("ruta").value = ruta;
}

// DEPRECATED
function limpiarForm(accion){
	try{
		document.getElementById("accion").value = accion;
	}catch(e){ alert(e);/* si no encuentra el elemento, continua el script */ }try{
		document.getElementById("fechaLectura").value = "";
	}catch(e){ alert(e);/* si no encuentra el elemento, continua el script */ }try{
		document.getElementById("nif").value = "";
	}catch(e){ alert(e);/* si no encuentra el elemento, continua el script */ }try{
		document.getElementById("doctorando").value = "";
	}catch(e){ alert(e);/* si no encuentra el elemento, continua el script */ }try{
		document.getElementById("conFichero").value = 0;
	}catch(e){ alert(e);/* si no encuentra el elemento, continua el script */ }
}
function limpiarFormBusqueda(accion){
	abrirFormBusqueda();
}

function abrirDetalleFichero(idFicha){
	document.getElementById("idFicha").value = idFicha;
	document.formFicha.action = "/teseo/teseo/abrirDetalleFichero.do";
	document.formFicha.submit();
}
function abrirAltaFichero(idFicha){
	document.getElementById("idFicha").value = idFicha;
	document.formFicha.action = "/teseo/teseo/abrirFormNewFicheros.do";
	document.formFicha.submit();
}
function abrirUpdateFichero(idFicha){
	document.getElementById("idFicha").value = idFicha;
	document.formFicha.action = "/teseo/teseo/abrirFormMasFicheros.do";
	document.formFicha.submit();
}
function abrirListadoDeleteFicheros(idFicha){
	document.getElementById("idFicha").value = idFicha;
	document.formFicha.action = "/teseo/teseo/abrirListadoDeleteFicheros.do";
	document.formFicha.submit();
}
function abrirListadoValidateFicheros(idFicha){
	document.getElementById("idFicha").value = idFicha;
	document.formFicha.action = "/teseo/teseo/abrirListadoValidateFicheros.do";
	document.formFicha.submit();
}

function abrirFormBusqueda(){
	document.formFicha.action = "/teseo/teseo/abrirFormBusquedaFichasFicheros.do";
	document.formFicha.submit();
}
function imprimirFicheroTesis(idFichero){
	try {
		document.FicheroForm.elements["idFichero"].value = idFichero;
		document.FicheroForm.submit();	
	} catch (e){
		document.forms[1].elements["idFichero"].value = idFichero;
		document.forms[1].submit();	
	}
}

function deleteFichero(idFichero,nombre){
	var confirmado = confirm("Est\u00e1 seguro que desea eliminar el fichero " + nombre + "?");

	if(confirmado){
		document.formFicha.idFichero.value = idFichero;
		document.formFicha.submit();
	}
}

function deleteFicheroGral(){
	var localizacion = getLocalizacion();
	var confirmado = confirm("Se va a eliminar el fichero con localizaci\u00f3n en " + localizacion + ".");
		
	if(confirmado){
		document.formFicha.idFichero.value = 0;
		document.formFicha.submit();
	}
}

function validateFichero(idFichero, nombre){
	var confirmado = confirm("Desea marcar como v\u00e1lido el fichero " + nombre + "?");
		
	if(confirmado){
		document.formFicha.action = "/teseo/teseo/validateFichero.do";
		document.formFicha.idFichero.value = idFichero;
		document.formFicha.submit();
	}
}

function actualizarTipoRepo(tipoRepo){
 	if(tipoRepo == 'EX'){	
 		document.getElementById("externa").style.display = "block";
 		document.getElementById("local").style.display = "none";
 		document.getElementById("asterisco").innerHTML = "";
 	}else if(tipoRepo == 'FS'){
 		document.getElementById("externa").style.display = "none";
 		document.getElementById("local").style.display = "block";
 		document.getElementById("asterisco").innerHTML = "* ";
 	}
 }
 
function actualizarPaginaFichas(numPag){
	document.formFicha.action = "/teseo/teseo/actualizarPaginaFichasFicheros.do";
	document.formFicha.elements['pagina'].value = numPag;
	document.formFicha.submit();
}
function volver(numPag){ // al listado de fichas
	actualizarPaginaFichas(numPag);
}
function finalizar(numPag){
	document.formFicha.action = "/teseo/teseo/finalizarOperacionFicheros.do";
	document.formFicha.elements['pagina'].value = numPag;
	document.formFicha.submit();
}