//--EXPLICACIÓN:
//*********************************************************************************************
//Nombre archivo: gestion_usuarios.js
//Accion:
//						-	Realiza las funciones de gestion de usuarios Generales: Insertar, Editar y Ver.
//						-  .js activado desde el archivo inc_abierto.php
//*********************************************************************************************
//              s = Item Seleccionado
//              p = Apartado donde nos encontramos
//              a = Accion a realizar (1 = Insertar, 2 = Editar, 3 = Ver.)
//              b = id del formulario oculto (Nuestro numero id de usuario)
//              l = Icono seleccionado
//              n = Campo Libre
//              o = Campo Libre
//              r = Campo Libre
//              t = Campo Libre
//Opciones Actuales:
//              g0 = mensajes
//              g1 = ayuda
//              g2 = mis datos
//              g3 = salir
//              g4 = volver a la web
//              g5 = organizacion escolar
//              g6 = nivel de educacion infantil
//              g7 = servicios
//              g8 = datos informativos
//              g9 = menus
//              g10 = calendario
//              g11 = consejos
//              g12 = actualidad
//              g13 = anuncios
//              g14 = galeria fotografica
//              g15 = alumnos
//              g16 = clases
//              g17 = usuarios
//              g18 = enlaces

function gestion_usuarios(s,p,a,b,l,n,o,r,t,ta,tb,tc,td,te,tf,tg,th,ti,tj,g0,g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13,g14,g15,g16,g17,g18,g19,g20,g21,g22,g23,hij0,hij1,hij2,hij3,hij4,hij5,hij6,hij7,hij8,hij9,hij10,cla0,cla1,cla2,cla3,cla4)
{	
	//alert(hij0)
	campo  = new Array();
	campo[0] = g0;
	campo[1] = g1;
	campo[2] = g2;
	campo[3] = g3;
	campo[4] = g4;
	campo[5] = g5;
	campo[6] = g6;
	campo[7] = g7;
	campo[8] = g8;
	campo[9] = g9;
	campo[10] = g10;
	campo[11] = g11;
	campo[12] = g12;
	campo[13] = g13;
	campo[14] = g14;
	campo[15] = g15;
	campo[16] = g16;
	campo[17] = g17;
	campo[18] = g18;
	campo[19] = g19;
	campo[20] = g20;
	campo[21] = g21;
	campo[22] = g22;
	campo[23] = g23;
	
	//Visualizamos el Formulario
	document.getElementById(b).style.display ="inline";
	//Incluismos a los campos como No visibles.
	document.getElementById("nombre_blo").style.display = ""
	document.getElementById("apellidos_blo").style.display = ""
	document.getElementById("user_blo").style.display = ""
	document.getElementById("pass_blo").style.display = ""
	document.getElementById("mail_blo").style.display = ""
	document.getElementById("option_use_blo").style.display = ""
	document.getElementById("option_cla_blo").style.display = "none"
	document.getElementById("option_alu_blo").style.display = "none"
	document.getElementById("fieldset").style.display = ""
	document.getElementById("fieldset2").style.display = "none"
	document.getElementById("activado_directora_blo").style.display = "none"

	document.getElementById("g0_blo").style.display = "none"
	document.getElementById("g1_blo").style.display = "none"
	document.getElementById("g2_blo").style.display = "none"
	document.getElementById("g3_blo").style.display = "none"
	document.getElementById("g4_blo").style.display = "none"
	document.getElementById("g5_blo").style.display = "none"
	document.getElementById("g6_blo").style.display = "none"
	document.getElementById("g7_blo").style.display = "none"
	document.getElementById("g8_blo").style.display = "none"
	document.getElementById("g9_blo").style.display = "none"
	document.getElementById("g10_blo").style.display = "none"
	document.getElementById("g11_blo").style.display = "none"
	document.getElementById("g12_blo").style.display = "none"
	document.getElementById("g13_blo").style.display = "none"
	document.getElementById("g14_blo").style.display = "none"
	document.getElementById("g15_blo").style.display = "none"
	document.getElementById("g16_blo").style.display = "none"
	document.getElementById("g17_blo").style.display = "none"
	document.getElementById("g18_blo").style.display = "none"
	document.getElementById("g19_blo").style.display = "none"
	document.getElementById("g20_blo").style.display = "none"
	document.getElementById("g21_blo").style.display = "none"
	document.getElementById("g22_blo").style.display = "none"
	document.getElementById("g23_blo").style.display = "none"
	

	//Nombres de VAriables
	blo_nombre = document.getElementById("nombre_blo")
	blo_apellidos = document.getElementById("apellidos_blo")
	blo_user = document.getElementById("user_blo")
	blo__pass = document.getElementById("pass_blo")
	blo__mail = document.getElementById("mail_blo")
	blo_option_use = document.getElementById("option_use_blo")
	blo_option_cla = document.getElementById("option_cla_blo")
	blo_option_alu = document.getElementById("option_alu_blo")
	blo_fieldset = document.getElementById("fieldset")
	blo_fieldset2 = document.getElementById("fieldset2")
	blo_activado_directora = document.getElementById("activado_directora_blo")
	blo_permisos = document.getElementById("permisos_blo")

	blo_g0 = document.getElementById("g0_blo").style.display
	blo_g1 = document.getElementById("g1_blo").style.display
	blo_g2 = document.getElementById("g2_blo").style.display
	blo_g3 = document.getElementById("g3_blo").style.display
	blo_g4 = document.getElementById("g4_blo").style.display
	blo_g5 = document.getElementById("g5_blo").style.display
	blo_g6 = document.getElementById("g6_blo").style.display
	blo_g7 = document.getElementById("g7_blo").style.display
	blo_g8 = document.getElementById("g8_blo").style.display
	blo_g9 = document.getElementById("g9_blo").style.display
	blo_g10 = document.getElementById("g10_blo").style.display
	blo_g11 = document.getElementById("g11_blo").style.display
	blo_g12 = document.getElementById("g12_blo").style.display
	blo_g13 = document.getElementById("g13_blo").style.display
	blo_g14 = document.getElementById("g14_blo").style.display
	blo_g15 = document.getElementById("g15_blo").style.display
	blo_g16 = document.getElementById("g16_blo").style.display
	blo_g17 = document.getElementById("g17_blo").style.display
	blo_g18 = document.getElementById("g18_blo").style.display
	blo_g19 = document.getElementById("g19_blo").style.display
	blo_g20 = document.getElementById("g20_blo").style.display
	blo_g21 = document.getElementById("g21_blo").style.display
	blo_g22 = document.getElementById("g22_blo").style.display
	blo_g23 = document.getElementById("g23_blo").style.display


	//Botones = No Visibles (No son bloques, son campos)
	document.forms[0].insertar.style.display="none";
	document.forms[0].actualizar.style.display="none";
	document.forms[0].buscar.style.display="none";


	bot_insertar = document.forms[0].insertar
	bot_actualizar = document.forms[0].actualizar
	bot_buscar = document.forms[0].buscar

 	//Campos
	cam_nombre = document.forms[0].nombre
	cam_apellidos = document.forms[0].apellidos
	//cam_user = document.forms[0].user
	//cam_pass = document.forms[0].pass
	cam_mail = document.forms[0].mail
	cam_option_use = document.forms[0].option_use //Combox Tipo de Usuarios
	cam_activado_directora = document.forms[0].activado_directora //Combox Tipo de Usuarios

	cam_g0 = document.forms[0].g0
	cam_g1 = document.forms[0].g1
	cam_g2 = document.forms[0].g2
	cam_g3 = document.forms[0].g3
	cam_g4 = document.forms[0].g4
	cam_g5 = document.forms[0].g5
	cam_g6 = document.forms[0].g6
	cam_g7 = document.forms[0].g7
	cam_g8 = document.forms[0].g8
	cam_g9 = document.forms[0].g9
	cam_g10 = document.forms[0].g10
	cam_g11 = document.forms[0].g11
	cam_g12 = document.forms[0].g12
	cam_g13 = document.forms[0].g13
	cam_g14 = document.forms[0].g14
	cam_g15 = document.forms[0].g15
	cam_g16 = document.forms[0].g16
	cam_g17 = document.forms[0].g17
	cam_g18 = document.forms[0].g18
	cam_g19 = document.forms[0].g19
	cam_g20 = document.forms[0].g20
	cam_g21 = document.forms[0].g21
	cam_g22 = document.forms[0].g22
	cam_g23 = document.forms[0].g23
	

	//Inicializacion Variables

	hid_1 = document.forms[0].id
	hid_2 = document.forms[0].iden_type
	hid_3 = document.forms[0].identificador_men
	hid_4 = document.forms[0].iden_men
	
	hid_5 = document.forms[0].iden_apa
	hid_6 = document.forms[0].iden_blo
	hid_7 = document.forms[0].nuevo_apartado
	hid_8 = document.forms[0].name_men
	hid_9 = document.forms[0].name_apa
	hid_10 = document.forms[0].name_apa
	hid_11 = document.forms[0].opt
	hid_12 = document.forms[0].c_bloques
	hid_13 = document.forms[0].imagen_anterior	//Campo oculto nombre de imagen
	hid_14 = document.forms[0].imagen_update	//Campo oculto nombre de imagen
	hid_menu_real = document.forms[0].menu_real //Campo oculto nombre de imagen
	hid_apa_real = document.forms[0].apa_real	//Campo oculto nombre de imagen
	hid_vincul = document.forms[0].vincul	//Campo oculto nombre de imagen
	hid_escape = document.forms[0].escape	//Campo oculto Tipo de Usuario

	if (a == 1) //Accion Insertar
	{  option_seleccionado = document.forms[0].option_use.selectedIndex;
		valor_hid_escape = document.forms[0].option_use.options[option_seleccionado].id
		hid_escape.value = valor_hid_escape.substring(3)
		//alert(hid_escape.value)
				
		espacio_vacio="";
		document.forms[0].reset(); //Reseteamos el formulario
   	document.forms[0].insertar.style.display=""; //Activamos el boton Insertar
     	document.forms[0].action = "inc/inc_ingresar_usuarios.php"; //Activamos la accion al boton
     	//Activamos los bloques.
		
      if (p == 18)		//Insertar Clase
		{  borrar_todo();
			if (l =="use_tab")
			{	num = 0; //Numero de Vinculaciones Existentes
				vincul = 0; //Numero de Vinculaciones Existentes
				user_type = new Array();
				user_type[1] = "ad"
				user_type[2] = "al"
				user_type[3] = "pa"
				user_type[4] = "pr"
			
				cam_option_use.options[0].selected = true;
		   	hid_4.value= "1";
				opciones_usuarios(hid_12.value)
			 
  			
		   	//Al entrar actvo cehckeados solo las opciones del tipo de usuario
				for (c=0; c<hid_12.value; c++)
				{  valor = document.getElementById(user_type[hid_4.value]+c).value;
			    	if ( valor == 1)
				   {	document.getElementById("si"+c).checked=true;
		   		}
					else
		   		{  document.getElementById("no"+c).checked=true;
	  				}
				}
			   activar_astro_usuarios();
			   //Aqui tengo que dirigirme para eliminar alumnos incrustados.
			}
			if (l =="use_tab_bus")
			{	bot_buscar.style.display="";
				bot_insertar.style.display="none";
				document.forms[0].action = "inc/inc_buscar_users.php";
				
				blo_nombre.style.display=""
				blo_apellidos.style.display="none"
				blo_user.style.display="none"
				blo__pass.style.display="none"
				blo__mail.style.display="none"
				blo_option_use.style.display="none"
				blo_option_cla.style.display="none"
				blo_option_alu.style.display="none"
				blo_fieldset.style.display="none"
				blo_permisos.style.display="none"
			}
		
		}
	 }

	
//*************************************************************************************
//*************************************************************************************
	if ((a == 2) || (a == 3)) //Editar y Ver
	{	
		if (a == 2) //Editar
		{	bot_actualizar.style.display ="";
			document.forms[0].action = "inc/inc_actualizar_usuarios.php";
			hid_1.value = s; //Añado el valor de id (s) al campo hidden_1
		}
		if (p == 18)		
		{	hid_4.value=tf
			num = 0; //Numero de Vinculaciones Existentes
			vincul = 0; //Numero de Vinculaciones Existentes
			
			cam_nombre.value =ta
			cam_apellidos.value =tb
			cam_user.value =tc
			cam_pass.value =td
			cam_mail.value =te
			
			cam_option_use.options[document.getElementById("use"+hid_4.value).value].selected = true;
		   user_type = new Array();
			user_type[1] = "ad"
			user_type[2] = "al"
			user_type[3] = "pa"
			user_type[4] = "pr"
			//variable a comprar del valor
 			d="0";
 			//Permisos
     		for (c=0;c<hid_12.value;c++)
			{  //Recojo los campso apropiados para el determinado  tipo de usuario
			   valor = document.getElementById(user_type[hid_4.value]+c).value;
				
		 		if ( valor == 1)
	   		{  document.getElementById("g"+c+"_blo").style.display="";

	   		   if (campo[c]=="0")
	   		   {  document.getElementById("no"+c).checked=true;
       		   }
       		   if (campo[c]=="1")
       		   {  document.getElementById("si"+c).checked=true;
       		   }

	   		}
			   else
	   		{  document.getElementById("g"+c+"_blo").style.display="none";
  			   }
			}
			
			//Recupero e incluyo los hijos
			if (hid_4.value==3)
			{		borrar_todo();
					vincular_hijo_act(hij0,tb+", "+ta,ti);
					vincular_hijo_act(hij1,tb+", "+ta,ti);
					vincular_hijo_act(hij2,tb+", "+ta,ti);
					vincular_hijo_act(hij3,tb+", "+ta,ti);
					vincular_hijo_act(hij4,tb+", "+ta,ti);
					vincular_hijo_act(hij5,tb+", "+ta,ti);
					vincular_hijo_act(hij6,tb+", "+ta,ti);
					vincular_hijo_act(hij7,tb+", "+ta,ti);
					vincular_hijo_act(hij8,tb+", "+ta,ti);
					vincular_hijo_act(hij9,tb+", "+ta,ti);
					vincular_hijo_act(hij10,tb+", "+ta,ti);
								
				
			}
			if (hid_4.value==4)
			{		blo_activado_directora.style.display="";
					borrar_todo();
					cam_activado_directora[tj].checked = true;
					vincular_clase_act(cla0,tb+", "+ta,ti);
					vincular_clase_act(cla1,tb+", "+ta,ti);
					vincular_clase_act(cla2,tb+", "+ta,ti);
					vincular_clase_act(cla3,tb+", "+ta,ti);
					vincular_clase_act(cla4,tb+", "+ta,ti);
			}
			option_seleccionado = document.forms[0].option_use.selectedIndex;
			valor_hid_escape = document.forms[0].option_use.options[option_seleccionado].id
			hid_escape.value = valor_hid_escape.substring(3)
			
			
			activar_astro_usuarios()
		}


   }

} //Fin de la funcion

//******************************************************************************
//******************************************************************************
function opciones_usuarios(a)  //a = contador de Apartados
{  
	user_type = new Array();
	user_type[1] = "ad"
	user_type[2] = "al"
	user_type[3] = "pa"
	user_type[4] = "pr"
 	d="0";
 	
 	if ((hid_4.value=="3")||(hid_4.value=="4"))
 	{ blo_fieldset.style.display="";
	}
	else
	{	blo_fieldset.style.display="none";
	}
	if (hid_4.value=="4")
	{	blo_activado_directora.style.display="";
	}
	if (hid_4.value=="3")
	{	blo_activado_directora.style.display="none";
	}
//Buscamos que inputs bloques deben activarse
	for (c=0; c<a ; c++)
	{  valor = document.getElementById(user_type[hid_4.value]+c).value;
    	if ( valor == 1)
	   {	document.getElementById("g"+c+"_blo").style.display="";
   	}
		else
	   {  document.getElementById("g"+c+"_blo").style.display="none";
  		}
		
	}
	for (c=0; c<hid_12.value; c++)
	{  valor = document.getElementById(user_type[hid_4.value]+c).value;
	   
	  	if ( valor == 1)
	   {	document.getElementById("si"+c).checked=true;
		}
		else
		{  document.getElementById("no"+c).checked=true;
	  	}
	}
	
	activar_astro_usuarios();
 	return;
}

//******************************************************************************
//******************************************************************************
function valorenhidden_usuarios()
{ 	
	option_seleccionado = document.forms[0].option_use.selectedIndex;
	valor_hid_4 = document.forms[0].option_use.options[option_seleccionado].id
	hid_4.value = valor_hid_4.substring(3)
	opciones_usuarios(hid_12.value)
}
function valorentipo_usuarios()
{ 	borrar_todo();
	option_seleccionado = document.forms[0].option_use.selectedIndex;
	valor_hid_4 = document.forms[0].option_use.options[option_seleccionado].id
	hid_4.value = valor_hid_4.substring(3)
	opciones_usuarios(hid_12.value)
}
function valorenmenu_usuarios()
{	option_seleccionado = document.forms[0].option_cla.selectedIndex;
	menu_apa = document.forms[0].option_cla.options[option_seleccionado].id
	hid_apa_real.value = menu_apa.substring(3)
	
}
//******************************************************************************
//******************************************************************************
/*num = 0; //Numro inicial de hijos.
function vincular_hijo(valor_id,valor_txt,nav)
{	
	valor_id = valor_id.substring(3)
	
	form = document.getElementById("fiel");
	if (nav == "OT")
	{	form = document.getElementById("fiel_o");
	}
	Div = document.createElement('div');
		Div.setAttribute("id","divhij"+num);
	
	InputAlumno = document.createElement('input'); 
  		Div.appendChild(InputAlumno);
  			InputAlumno.setAttribute("TYPE","text");
  			InputAlumno.setAttribute("NAME","hij"+num);
			InputAlumno.setAttribute("id","hij"+num);	
  			InputAlumno.setAttribute("class","entrada12");
  			InputAlumno.setAttribute("value",valor_txt);
  			InputAlumno.setAttribute("onFocus","javascript:{this.value=\"\";}");
  		Div.innerHTML = Div.innerHTML + "<input type=\"hidden\" name=\"hij_id"+num+"\" id=\"hij_id"+num+"\" value=\""+valor_id+"\">"
		Div.innerHTML = Div.innerHTML + "<input type=\"button\" id=\"boton_b\" name=\"hij"+num+"\" value=\"\" onclick=\"borrar_hij(this.name,'"+nav+"')\">";
  	Linea = document.createElement('hr'); 
		Div.appendChild(Linea);		//Entradas

	form.appendChild(Div)			  			
  	if(nav=="OT")
	{	usuario = document.getElementById("usuario");
		usuario.appendChild(form);
	}		
  		
  	hid_vincul.value=num+1;
	num++;
	
}*/

function escape()
{			option_seleccionado = document.forms[0].option_use.selectedIndex;
			valor_hid_escape = document.forms[0].option_use.options[option_seleccionado].id
			hid_escape.value = valor_hid_escape.substring(3)
		

}
function borrar_todo()
{	
	var fi = document.getElementById("fiel");
	while (fi.firstChild) {
  		fi.removeChild(fi.firstChild);
	}
	var fi = document.getElementById("fiel_o");
	while (fi.firstChild) {
  		fi.removeChild(fi.firstChild);
	}
	num = 0;
	hid_vincul.value=num;
  	
  
  	
  	
  	
  	
  	
}
/*function borrar_cla(obj)
{	
	fi = document.getElementById('fiel2'); // 1 
  	fi.removeChild(document.getElementById(obj)); // 10
  	
  
  	
  	//Los objetos que van delante hemos de modificarles el valor del hidden y del borrar
  	vincul = vincul-1;
  	num = num-1;
  	hid_vincul.value = vincul
  	
  	
}
*
function borrar_hij(obj,nav)
{	form = document.getElementById("fiel");
	if (nav == "OT")
	{	form = document.getElementById("fiel_o");
	}
	n_hijos = form.childNodes.length
	b_hijo = eval(obj.substring(3))+1
	
	for(b_c=b_hijo;b_c<n_hijos;b_c++)
	{	input_a = document.getElementById("hij"+[b_c-1])
		hidden_a = document.getElementById("hij_id"+[b_c-1])
		input_a.value = document.getElementById("hij"+b_c).value
		hidden_a.value = document.getElementById("hij_id"+b_c).value
				
	}

	var fi = document.getElementById("div"+n_hijos);
	if (nav = "OT")
	
	form.removeChild(form.lastChild);
	
	num--
	hid_vincul.value=num;
	  	
  	
  	//alert(vincul)
  	//alert(hid_vincul.value)
  	
}




function vincular_clase_act(valor_id,valor_txt,nav)
{	
	if (valor_id != 0)
	{	blo_fieldset2.style.display="";
		blo_fieldset.style.display="";
		n_alumnos = document.forms[0].option_cla.length;
	
		for (c_a = 0; c_a <n_alumnos;c_a++)
  		{	valor_option = document.forms[0].option_cla.options[c_a].id;
  			if (valor_option.substring(3) == valor_id)
  			{	valor_id = valor_option.substring(3)
			  	valor_txt = document.forms[0].option_cla[c_a].text;
			  	//Si existe, existe una vinculación-
  				hid_vincul.value=num+1;
				
			}
		}	
		
		
		form = document.getElementById("fiel");
		if (nav == "OT")
		{	form = document.getElementById("fiel_o");
		}
		Div = document.createElement('div');
			Div.setAttribute("id","divhij"+num);
	
		InputAlumno = document.createElement('input'); 
  			Div.appendChild(InputAlumno);
  				InputAlumno.setAttribute("TYPE","text");
	  			InputAlumno.setAttribute("NAME","hij"+num);	
  				InputAlumno.setAttribute("id","hij"+num);	
	  			InputAlumno.setAttribute("class","entrada12");
  				InputAlumno.setAttribute("value",valor_txt);
  				InputAlumno.setAttribute("onFocus","javascript:{this.value=\"\";}");
	  		Div.innerHTML = Div.innerHTML + "<input type=\"hidden\" name=\"hij_id"+num+"\" id=\"hij_id"+num+"\" value=\""+valor_id+"\">"
			Div.innerHTML = Div.innerHTML + "<input type=\"button\" id=\"boton_b\" name=\"hij"+num+"\" value=\"\" onclick=\"borrar_hij(this.name,'"+nav+"')\">";
	  	Linea = document.createElement('hr'); 
			Div.appendChild(Linea);		//Entradas
		
		form.appendChild(Div)			  			
  		if(nav=="OT")
		{	usuario = document.getElementById("usuario");
			usuario.appendChild(form);
		}
		num++;

		
 		
  				
	}
 
}
*/

function activar_astro_usuarios()
{  
	if (hid_4.value == 4)
	{  blo_option_cla.style.display="";
		blo_option_alu.style.display="none";
	}
	else
	{  if (hid_4.value == 3)
		{  
			blo_option_cla.style.display="none";
			blo_option_alu.style.display="";
		}
		else
		{  blo_option_cla.style.display="none";
			blo_option_alu.style.display="none";
		}
	}
	
}

function ext_imagen_usuarios(k)
{
	//Inicializacion Campo Imagen
	if ((k != 0)&&(k != 1)&&(k != 2)&&(k != 3))
	{
		hid_13.value = k; // imagen_anterior
		blo_imagen.style.display="none"
		blo_imagen_ext.style.display=""
		cam_imagen_ext.value=k;
		hid_14.value = k  // imagen_update
		//alert(hid_14.value)
		//alert(document.forms[0].imagen_update.value)
	}
	else
	{	if (k==0)	//Campo Vacio
		{	blo_imagen.style.display=""
			blo_imagen_ext.style.display="none"
			cam_imagen_ext.value="0"
			bot_volver.style.display="none";
			hid_14.value="0"

		}
		if (k==1)	//Volver
		{	blo_imagen.style.display="none"
			blo_imagen_ext.style.display=""
			cam_imagen_ext.value=hid_13.value;
			bot_volver.style.display="none";
			hid_14.value = hid_13.value;
			bot_volver.display="";


		}
		if (k==2)	//Eliminar
		{	blo_imagen.style.display=""
			blo_imagen_ext.style.display="none"
			bot_volver.style.display="none";
			hid_14.value="0";

 		}
 		if (k==3)	//Campo Modificar
		{
			blo_imagen.style.display=""
			blo_imagen_ext.style.display="none"
			cam_imagen_ext.value="0"
			bot_volver.style.display="";
			hid_14.value="1" // Indico que se ha de recoger o comprobar de $_FILE.

		}
	}
}

