// mensajes para las alertas
var docIntroducirTipoDoc = "Debe introducir Tipo de Documento";
var docElegirCat = "Debe elegir un catálogo";
var docIntroducirTituloDoc = "Debe introducir un titulo para el documento";
var docElegirFichero = "Debe elegir un fichero";
var docRellenarCampoTitulo = "Debe rellenar el campo título";
var docNombreFicheroSinEsp = "El nombre del fichero no puede contener espacios.\n\rPor favor, eliminelos o si lo desea sustituyalos por \"-\".";
var docNombreFicheroSinGuion = "El nombre del fichero no puede contener guiones bajos.\n\rPor favor, eliminelos o si lo desea sustituyalos por \"-\".";

// textos para la funcion vaciarCombo
var textoSeleccioneTipoProd = "Seleccione Tipo Producto";
var textoAnyo = "Año";
var textoSerie = "Serie";
var textoSeleccioneTipoDoc = "Seleccione un Tipo Documento";
var textoSeleccioneCat = "Seleccione un Catálogo";
var textoTodasFam = "Todas las Familias";
var textoTodosModelos = "Todos los Modelos";
var textoSeleccioneGama = "Seleccione Gama";

//var rutaInicialAplicacion="http://localhost:8080/Web/";
//var rutaInicialAplicacion="http://www.vaillantdesa.es/Web/";

function buscarCatalogo(capa){
	if (document.getElementById(capa))
		document.getElementById(capa).style.display = "block";
	if (document.getElementById("form1").VISIBLE)
		document.getElementById("form1").VISIBLE.value="_si";
}

function ordenarDocumentos(campoOrden, capa, jsp){
	if (document.getElementById("form1").VISIBLE){
		if (document.getElementById("form1").VISIBLE.value == "_si"){
			if (document.getElementById(capa))document.getElementById(capa).style.display = "block";
		}
	}
	
	document.getElementById("form1").orden.value=campoOrden;
	document.getElementById("form1").FUNCION.value="buscar";
	document.getElementById("form1").action=jsp;
	document.getElementById("form1").submit();
}

/*
* Función que desactiva los campos correspondientes según sea 'Documentacion de producto'
* u 'Otros documentos o  programas'

function activarDesactivar(valor, jsp){
	if ( document.getElementById("form1").documentacionProducto[0].checked == true){
		document.getElementById("form1").idCatalogo.disabled = false;
		document.getElementById("form1").idFamilia.disabled = false;
		document.getElementById("form1").idModelo.disabled = false;
		if ( valor == "S"){
			if ( document.getElementById("form1").idGama.disabled==false){
				//document.getElementById("form1").idGama.value="";
				vaciarCombo (document.getElementById("form1").idGama, textoSeleccioneTipoProd);
				document.getElementById("form1").idGama.disabled= true;
			}
			if ( document.getElementById("form1").anyo.disabled == false){
				//document.getElementById("form1").anyo.value="";
				vaciarCombo (document.getElementById("form1").anyo, textoAnyo);
				document.getElementById("form1").anyo.disabled=true;
			}
			if ( document.getElementById("form1").serie.disabled == false){
				//document.getElementById("form1").serie.value="";
				vaciarCombo (document.getElementById("form1").serie, textoSerie);
				document.getElementById("form1").serie.disabled= true;
			}
			//Vaciar la combo de Tipos de Documentos
			vaciarCombo (document.getElementById("form1").tipoDocumento, textoSeleccioneTipoDoc);
			document.getElementById("form1").action=jsp;
			document.getElementById("form1").FUNCION.value='';
			document.getElementById("form1").submit();
		}
	}else{
	//otros documentos o programas
		//vaciarCombo (document.getElementById("form1").idCatalogo, textoSeleccioneCat);
		document.getElementById("form1").idCatalogo.value = "";
		document.getElementById("form1").idCatalogo.disabled = true;
		vaciarCombo (document.getElementById("form1").idFamilia, textoTodasFam);
		//document.getElementById("form1").idFamilia.value="";
		document.getElementById("form1").idFamilia.disabled = true;
		vaciarCombo (document.getElementById("form1").idModelo, textoTodosModelos);
		//document.getElementById("form1").idModelo.value="";
		document.getElementById("form1").idModelo.disabled = true;
		if ( document.getElementById("form1").idGama.disabled==false){
			//document.getElementById("form1").idGama.value="";
			vaciarCombo (document.getElementById("form1").idGama, textoSeleccioneTipoProd);
			document.getElementById("form1").idGama.disabled= true;
		}
		if ( document.getElementById("form1").anyo.disabled == false){
			//document.getElementById("form1").anyo.value="";
			vaciarCombo (document.getElementById("form1").anyo, textoAnyo);
			document.getElementById("form1").anyo.disabled=true;
		}
		if ( document.getElementById("form1").serie.disabled == false){
			//document.getElementById("form1").serie.value="";
			vaciarCombo (document.getElementById("form1").serie, textoSerie);
			document.getElementById("form1").serie.disabled= true;
		}

		//Se tiene que recargar la combo de Tipos de Documentos
		if ( valor == "S"){
			//Vaciar la combo de Tipos de Documentos
			vaciarCombo (document.getElementById("form1").tipoDocumento, textoSeleccioneTipoDoc);
			document.getElementById("form1").action=jsp;
			document.getElementById("form1").FUNCION.value='';
			document.getElementById("form1").submit();
		}
	}
}

*/

/*
* Método que elimina las opciones de una determinada combo
*/
function vaciarCombo(combo, texto){
	cont = combo.length;
	while (cont >= 0) combo.options[cont--] = null;
	if (texto!=null){
	var varOpcion3 = document.createElement("OPTION");
	varOpcion3.text = texto;
        varOpcion3.value = "";
	combo.options.add(varOpcion3);
	}else{
		document.getElementById("form1").idModelo.size=1;
	}
}

/*
* Método que recarga las combos
*/
function cargarCombo(jsp, capa){
	if (document.getElementById("form1").VISIBLE)
		document.getElementById("form1").VISIBLE.value="_si";
	if (document.getElementById(capa))
		document.getElementById(capa).style.display = "block";
	document.getElementById("form1").idCatalogo.selectedIndex=0;
	vaciarCombo(document.getElementById("form1").idFamilia,textoTodasFam);
	vaciarCombo(document.getElementById("form1").idGama,textoSeleccioneTipoProd);
	document.getElementById("form1").FUNCION.value="";
	document.getElementById("form1").selected.value="";
	document.getElementById("form1").action=jsp;
	document.getElementById("form1").submit();
}
/*
* Método que recarga las combos
*/
function cargarComboFamilia(jsp, capa){
	if (document.getElementById("form1").VISIBLE)
		document.getElementById("form1").VISIBLE.value="_si";
	if (document.getElementById(capa))
		document.getElementById(capa).style.display = "block";
	document.getElementById("form1").FUNCION.value="";
	if(document.getElementById("form1").mostrarEn[1].checked == true){
		document.getElementById("form1").selected.value="documentacion";
	}else{
		document.getElementById("form1").selected.value="catalogo";
	}
	document.getElementById("form1").action=jsp;
	document.getElementById("form1").submit();
}

/*
* Método que recarga las combos cuando se ha elegido una opción en el combo de gama
*/
function cambiarComboGama(jsp, capa){
	if (document.getElementById("form1").VISIBLE)
		document.getElementById("form1").VISIBLE.value="_si";
	if (document.getElementById(capa))
		document.getElementById(capa).style.display = "block";
	if(document.getElementById("form1").mostrarEn[1].checked == true){
		document.getElementById("form1").selected.value="documentacion";
	}else{
		document.getElementById("form1").selected.value="catalogo";
	}
	document.getElementById("form1").cambioGama.value="S";
	document.getElementById("form1").FUNCION.value="";
	document.getElementById("form1").action=jsp;
	document.getElementById("form1").submit();
}

/*
* Método que recarga las combos cuando se ha elegido una opción en el combo de catálogo
*/
function cargarComboCatalogo(jsp, capa){
	if (document.getElementById(capa))
		document.getElementById(capa).style.display = "block";
	if (document.getElementById("form1").VISIBLE)
		document.getElementById("form1").VISIBLE.value="_si";
	if(document.getElementById("form1").mostrarEn[1].checked == true){
		document.getElementById("form1").selected.value="documentacion";
	}else{
		document.getElementById("form1").selected.value="catalogo";
	}
	document.getElementById("form1").FUNCION.value="";
	document.getElementById("form1").tipoDoc.value=document.getElementById("form1").tipoDocumento.selectedIndex;
	
	//vaciar las combos
	vaciarCombo(document.getElementById("form1").idGama,textoSeleccioneTipoProd);
	vaciarCombo(document.getElementById("form1").tipoDocumento,textoSeleccioneTipoDoc);
	document.getElementById("form1").action=jsp;
	document.getElementById("form1").submit();
}

/*
* Funcion que busca documentos
*/
function buscarDocumentos(jsp, capa){
	if (document.getElementById("form1").VISIBLE){
		if (document.getElementById("form1").VISIBLE.value == "_si"){
			if (document.getElementById(capa))
				document.getElementById(capa).style.display = "block";
		}
	}
	document.getElementById("form1").action=jsp;
	document.getElementById("form1").FUNCION.value="buscar";
	document.getElementById("form1").submit();
}

/*
* Función que busca la página que se le pasa como parámetro
*/
function damePagina(pagina,jsp){
	document.getElementById("form1").action=jsp;
	document.getElementById("form1").pagina.value=pagina;
	document.getElementById("form1").FUNCION.value="buscar";
	document.getElementById("form1").submit();
}

/*
* Función que se usa en la pantalla de dar de alta un tipoDocumento
*/
function deshabilitarRadio(){
	if ( document.getElementById("form1").documentacionProducto[0].checked == true){
		//"Documentacion de Producto"
		document.getElementById("form1").idCatalogo.disabled=false;
		document.getElementById("form1").idGama.disabled=false;
		document.getElementById("form1").submit();
	}else{
		//"Otros documentos o Programas"
		vaciarCombo(document.getElementById("form1").idCatalogo,textoSeleccioneCat);
		document.getElementById("form1").idCatalogo.disabled=true;
		vaciarCombo(document.getElementById("form1").idGama,textoSeleccioneGama);
		document.getElementById("form1").idGama.disabled=true;
	}
}

/*
* Método para dar de alta un TipoDocuemnto
*/
function altaTipoDoc(){
	//comprobamos que ha introducido datos
	if ( document.getElementById("form1").tipoProducto.value==""){
		document.getElementById("form1").tipoProducto.focus();
		return;
	}else{
		document.getElementById("form1").FUNCION.value="enviar";
		document.getElementById("form1").action="documentacionAdminTipoDoc.jsp";
//		document.getElementById("form1").nombreCatalogo.value= document.getElementById("form1").idCatalogo.options[document.getElementById("form1").idCatalogo.selectedIndex].text;
//		document.getElementById("form1").nombreGama.value= document.getElementById("form1").idGama.options[document.getElementById("form1").idGama.selectedIndex].text;
		document.form1.submit();
	}
}

/*
* Método para dar de alta un documento
*/
function altaDocumento (){
	//compruebo que tengo elegido al menos un modelo
	var nSelModelo = document.getElementById("idModelo").options.selectedIndex; // Num. primera opción seleccionada
	var nSelFamilia = document.getElementById("idFamilia").options.selectedIndex;
	var nSelGama = document.getElementById("idGama").options.selectedIndex;
	var nSelCatalogo = document.getElementById("idCatalogo").options.selectedIndex;
	if(document.getElementById("form1").mostrarEn[0].checked == true){//Solo si es para catalogo
		if ((nSelModelo == -1) && (nSelFamilia == 0) && (nSelGama == 0) && (nSelCatalogo == 0)) {//Solo en el caso de que no se halla seleccionado ninguna entrada
			alert('Debe seleccionar al menos un modelo');
			return;
		} 
	}
	//comprobamos que ha introducido datos
	tit =document.getElementById("form1").titulo.value;
	if ( document.getElementById("form1").tipoDocumento.value==""){
		alert(docIntroducirTipoDoc);
		document.getElementById("form1").tipoDocumento.focus();
		return;
	}else{
		if (tit ==''){
			alert(docIntroducirTituloDoc);
			document.getElementById("form1").titulo.focus();
			return;
		}else{
			
			if ( document.getElementById("form1").fichero.value==''){
				alert(docElegirFichero);
				document.getElementById("form1").fichero.focus();
				return;
			}else{
				var comparador = "\\";
				var nombreFichero = document.getElementById("form1").fichero.value;
				nombreFichero = nombreFichero.substring(nombreFichero.lastIndexOf(comparador));
				
							
				if(nombreFichero.indexOf(" ")!=-1){
					alert(docNombreFicheroSinEsp);
				}else if (nombreFichero.indexOf("_")!=-1){
					alert(docNombreFicheroSinGuion);
				}else{
					document.getElementById("form1").action = "doc_AdminIntroducir.jsp";
					document.getElementById("form1").FUNCION.value="enviar";
					document.getElementById("form1").submit();
				}
			}
		}
	}
}

/*
* Método para eliminar un Documento
*/
function eliminarDoc(){
	document.getElementById("form1").FUNCION.value="ELIMINAR";
	document.getElementById("form1").action = "doc_AdminModificarFlotante.jsp";
	document.getElementById("form1").submit();
}

/*
* Método para modificar un Documento
*/
function modificarDoc(){
	//comprobamos que tiene los datos básicos
	if ( document.getElementById("form1").titulo.value==''){
		alert(docRellenarCampoTitulo);
		document.getElementById("form1").titulo.focus();
		return;
	}else{
		var comparador = "\\";
		var nombreFichero = document.getElementById("form1").fichero.value;
		nombreFichero = nombreFichero.substring(nombreFichero.lastIndexOf(comparador));
		if(nombreFichero.indexOf(" ")!=-1){
			alert(docNombreFicheroSinEsp);
		}else{
			document.getElementById("form1").FUNCION.value="MODIFICAR";
			document.getElementById("form1").action = "doc_AdminModificarFlotante.jsp";
			document.getElementById("form1").submit();
		}
	}
}

/*
* Método para borrar los datos de un formulario
*/
function borrarFormulario(){
	//borramos todos las combos menos la de catalogo
	document.getElementById("form1").idCatalogo.value="";
	document.getElementById("form1").documentacionProducto[0].checked = true;
	vaciarCombo (document.getElementById("form1").idFamilia, textoTodasFam);
	//document.getElementById("form1").idFamilia.disabled = true;
	vaciarCombo (document.getElementById("form1").idModelo, textoTodosModelos);
	//document.getElementById("form1").idModelo.disabled = true;
	if ( document.getElementById("form1").idGama.disabled==false){
		vaciarCombo (document.getElementById("form1").idGama, textoSeleccioneTipoProd);
		document.getElementById("form1").idGama.disabled= true;
	}
	if ( document.getElementById("form1").anyo.disabled == false){
		vaciarCombo (document.getElementById("form1").anyo, textoAnyo);
		document.getElementById("form1").anyo.disabled=true;
	}
	if ( document.getElementById("form1").serie.disabled == false){
		vaciarCombo (document.getElementById("form1").serie, textoSerie);
		document.getElementById("form1").serie.disabled= true;
	}
	vaciarCombo (document.getElementById("form1").tipoDocumento, textoSeleccioneTipoDoc);
	//borramos el campo titulo
	document.getElementById("form1").titulo.value="";
	//borramos el campo file
	//document.getElementById("form1").file.value="";
	document.getElementById("form1").mostrarEn[0].checked=true;
}

/*
* Método que habilita o deshabilita las check de la pantalla de alta de TipoDocumento
*/
function deshabilitarCheks(){
	if ( document.getElementById("form1").publicoPrivado[0].checked==true){
		//público
		for(var i=0; i<document.getElementById("form1").elements.length; i++){
			if(document.getElementById("form1").elements[i].type=='checkbox'){
				document.getElementById("form1").elements[i].disabled=true;
			}
		}
	}else{
		//privado
		for(var i=0; i<document.getElementById("form1").elements.length; i++){
			if(document.getElementById("form1").elements[i].type=='checkbox'){
				document.getElementById("form1").elements[i].disabled=false;
			}
		}
	}
}

/*
* Método para ir a la jsp de dar de alta un documento
*/
function nuevoDocumento(){
	if(!document.form1.PART){
		var input1 = document.createElement('INPUT');
		input1.type = "hidden";
		input1.value = "NO";
		input1.name = 'PART';
		document.form1.appendChild(input1);
	}
	if (document.getElementById("form1").VISIBLE)
		document.getElementById("form1").VISIBLE.value="";
	document.getElementById("form1").action="doc_AdminIntroducir.jsp";
	document.getElementById("form1").submit();
}

/*
* Método para ir a la jsp de dar de alta un documento
*/
function nuevoTipoDocumento(){
	document.formTipoDoc.action="documentacionAdminTipoDoc.jsp";
	document.formTipoDoc.submit();
}

/*
* Método para ver la jsp de modificar documento
*/
function verDocAdmin(valor){
	var ancho = 570;
	var alto = 550;
	if (document.all) var xMax = screen.width, yMax = screen.height;
	else
		if (window.outerWidth){
			var xMax = screen.width, yMax = screen.height;
		}
		else{
			var xMax = 800, yMax=600;
		}
	var xOffset = (xMax - ancho)/2, yOffset = (yMax - alto)/2 - 30;
	var parametros = 'scrollbars=yes,status=no,toolbar=no,location=no,directories=no,menubar=no,width='+ancho+',height='+alto+',resizable=0,top='+yOffset+',left='+xOffset;

	var jsp="doc_AdminModificarFlotante.jsp?ID="+valor+"&PART=NO";
	window.open(jsp,'Flotante_Documento',parametros);
}

/*
* Método que llama a la jsp poder ver el contenido de un fichero
*/
function verDocumento(valor,enlace){
	
	if(!document.getElementById('IDXX')){
		var input1 = document.createElement('INPUT');
		input1.type = "hidden";
		input1.value = valor;
		input1.name = 'ID';
		input1.id = 'IDXX';
		document.form1.appendChild(input1);
	} else {
		var inputID = document.getElementById('IDXX');
		inputID.value = valor;
	}
	var accion = enlace + "DocumentoServlet";
	document.form1.action=accion;
	document.form1.submit();
}


/*
* Función que llama a la jsp para anexar una noticia
*/
function anexarANoticia(valorid, nombreDoc){
	document.formAnexar.action='guardarNoticia.jsp';
	if(!document.formAnexar.idDocum){
		var input1 = document.createElement('INPUT');
		input1.type = "hidden";
		input1.value = valorid;
		input1.name = 'idDocum';
		document.formAnexar.appendChild(input1);
	}
	if(!document.formAnexar.nombreDocum){
		var input2 = document.createElement('INPUT');
		input2.type = "hidden";
		input2.value = nombreDoc;
		input2.name = 'nombreDocum';
		document.formAnexar.appendChild(input2);
	}
	if(!document.formAnexar.funcion){
		var input3 = document.createElement('INPUT');
		input3.type = "hidden";
		input3.value = 'anexarDoc';
		input3.name = 'funcion';
		document.formAnexar.appendChild(input3);
	}
	document.formAnexar.submit();
}

/*
* Función que recarga la jsp padre y cierra la ventana flotante
*/
function recargarPadre(){
	window.opener.document.getElementById("form1").FUNCION.value="buscar";
	window.opener.document.getElementById("form1").submit();
	window.close();
}

/*
*Función que vuelve a la pantalla de alta de noticia sin seleccionar ningún documento
*/
function volverANoticia(){
	document.formAnexar.action='guardarNoticia.jsp';
	
	if(document.formAnexar.funcion){
		var input2 = document.createElement('INPUT');
		input2.type = "hidden";
		input2.value = 'anexarDoc';
		input2.name = 'funcion';
		document.formAnexar.appendChild(input2);
	}
	document.formAnexar.submit();
}

function volverANoticia(idNoticia){
	document.formAnexar.action='guardarNoticia.jsp';
	
	if(document.formAnexar.funcion){
		var input2 = document.createElement('INPUT');
		input2.type = "hidden";
		input2.value = 'anexarDoc';
		input2.name = 'funcion';
		document.formAnexar.appendChild(input2);
	}
	var input3 = document.createElement('INPUT');
		input3.type = "hidden";
		input3.value = idNoticia;
		input3.name = 'idNoticia';
		document.formAnexar.appendChild(input3);
	document.formAnexar.submit();
}

/*
* Método que llama a la jsp poder ver el contenido de un fichero
*/
function verDocumentoBusqueda(valor){
	document.formBusquedaDoc.ID.value=valor;
	document.getElementById("formBusquedaDoc").action="DocumentoServlet";
	document.formBusquedaDoc.submit();
}


function showDocs(){
	document.formDoc.target='padre';
	document.formDoc.submit();
	if(window.opener){
		window.close();
	}
}
/*
 * Funcion para habilitar-deshabilitar los combos de modelo producto
 */
function disableModel(){
	if(document.getElementById("form1").mostrarEn[1].checked == true){
		document.getElementById("form1").idCatalogo.disabled=true;
		document.getElementById("form1").idCatalogo.selectedIndex=0;
		//vaciarCombo(document.getElementById("form1").idCatalogo,textoSeleccioneCat);
		document.getElementById("form1").idFamilia.disabled=true;
		vaciarCombo(document.getElementById("form1").idFamilia,textoTodasFam);
		document.getElementById("form1").idGama.disabled=true;
		vaciarCombo(document.getElementById("form1").idGama,textoSeleccioneTipoProd);
		document.getElementById("form1").idModelo.disabled=true;
		document.getElementById("form1").selected.value="documentacion";
		vaciarCombo(document.getElementById("form1").idModelo,null);
	
}else{
		document.getElementById("form1").idCatalogo.disabled=false;
		document.getElementById("form1").idCatalogo.selectedIndex=0;
		document.getElementById("form1").idFamilia.disabled=false;
		vaciarCombo(document.getElementById("form1").idFamilia,textoTodasFam);
		document.getElementById("form1").idGama.disabled=false;
		vaciarCombo(document.getElementById("form1").idGama,textoSeleccioneTipoProd);
		document.getElementById("form1").idModelo.disabled=false;
		document.getElementById("form1").selected.value="catalogo";
		vaciarCombo(document.getElementById("form1").idModelo,null);
	}

}

