//--EXPLICACIÓN:
//*********************************************************************************************
//Nombre archivo: gestion_galeria.js
//						-	Realiza las funciones de gestion de usuarios Generales: Insertar, Editar y Ver.
//						-  .js activado desde el archivo inc_abierto.php
//*********************************************************************************************
//              s = id mensaje seleccionado
//              p = Apartado actual
//              a = Accion (1 = Insertar, 2 = Editar, 3 = Ver.)
//              b = id formulario oculto
//              c = Dia
//              d = Mes
//              e = Año
//              f = Asunto
//              g = **libre
//              h = Texto
//              i = Leido
//              j = Emisor_id
//              k = Imagen
//              l = Icono
//              m = Emisor email
//              n = Receptor id
//              o = Receptor email
//              r = Emisor nombre
//              t = **libre
//              u = **libre
//              v = **libre

							
num = 0
function gestion_mensajes(s,p,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,r,t,u,v)
{	
  //Visualizamos el Formulario
	document.getElementById(b).style.display ="inline";
	//Inicio campos no visibles
	document.getElementById("fecha_blo").style.display = "none"
	document.getElementById("emisor_blo").style.display = "none"
	document.getElementById("receptor_blo").style.display = "none"
	document.getElementById("asunto_blo").style.display = "none"
	document.getElementById("texto_blo").style.display = "none"
	document.getElementById("option_cla_blo").style.display = "none"
	
	document.getElementById("emisor_blo_v").style.display = "none"
	document.getElementById("receptor_blo_v").style.display = "none"
	document.getElementById("asunto_blo_v").style.display = "none"
	document.getElementById("texto_blo_v").style.display = "none"
		
	 
	//Nombres de VAriables
	blo_fecha = document.getElementById("fecha_blo")
	blo_emisor = document.getElementById("emisor_blo")
	blo_receptor = document.getElementById("receptor_blo")
	blo_asunto = document.getElementById("asunto_blo")
	blo_texto = document.getElementById("texto_blo")
	blo_option_cla = document.getElementById("option_cla_blo")
	
	blo_fecha_v = document.getElementById("fecha_blo_v")
	blo_emisor_v = document.getElementById("emisor_blo_v")
	blo_receptor_v = document.getElementById("receptor_blo_v")
	blo_asunto_v = document.getElementById("asunto_blo_v")
	blo_texto_v = document.getElementById("texto_blo_v")
	
	
	document.forms[0].insertar.style.display="none";
	document.forms[0].actualizar.style.display="none";
	document.forms[0].buscar.style.display="none";
	//Variables de botones
	bot_insertar = document.forms[0].insertar
	bot_actualizar = document.forms[0].actualizar
	bot_buscar = document.forms[0].buscar
	bot_volver = document.forms[0].imagen_volver
	
 	//Valores de Campos
	cam_fecha = document.forms[0].fecha
	cam_emisor = document.forms[0].emisor
	cam_asunto = document.forms[0].asunto
	cam_receptor = document.forms[0].receptor
	cam_texto = document.forms[0].comentario
	cam_emisor.disabled = false;
	cam_receptor.disabled = false;
	cam_emisor.style.background = "white";
	cam_receptor.style.background = "white";
	
	cam_emisor_v = document.forms[0].emisor_v
	cam_asunto_v = document.forms[0].asunto_v
	cam_receptor_v = document.forms[0].receptor_v
	cam_texto_v = document.forms[0].comentario_v


	
	//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
	hid_imagenes = document.forms[0].imagenes	//Campo oculto Tipo de Usuario
	hid_minombre = document.forms[0].minombre	//Campo oculto Tipo de Usuario
	hid_me = document.forms[0].mail_emisor	//Campo oculto Tipo de Usuario
	hid_ie = document.forms[0].id_emisor	//Campo oculto Tipo de Usuario
	hid_mr = document.forms[0].mail_receptor	//Campo oculto Tipo de Usuario
	hid_ir = document.forms[0].id_receptor	//Campo oculto Tipo de Usuario
	hid_ne = document.forms[0].nombre_emisor	//Campo oculto Tipo de Usuario
	hid_texto = document.forms[0].texto_mensaje	//Campo oculto Tipo de Usuario
	if (a == 1) //Nuevo Mensaje
	{  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_mensaje.php"; //Activamos la accion al boton
    	//Activamos los bloques.
    	
		if (p == 1)
		{	hid_2.value=o //Asignamos a la variable iden_type el valor de la opcion icono.
			
			if (o=="men_tab")	//Nuevo Mensaje
			{	blo_emisor.style.display="";
				blo_receptor.style.display="";
				blo_asunto.style.display="";	
				blo_texto.style.display="";
				cam_emisor.value = hid_minombre.value;
				cam_emisor.disabled = true;
				
				
			}
			if (o == "ico_new")
			{	blo_option_cla.style.display="";
				blo_asunto.style.display="";	
				blo_texto.style.display="";
				hid_escape.value="default"
				
			}			
		
		}
   }
	
//*************************************************************************************
//*************************************************************************************
	if (a == 2) //responder 
	{	if (p == 1)
		{			hid_2.value="resp";
					bot_actualizar.style.display ="";
					document.forms[0].action = "inc/inc_ingresar_mensaje.php";
					hid_1.value = s; //Añado el valor de id (s) al campo hidden_1
					
					blo_emisor.style.display="";
					blo_receptor.style.display="";
					blo_asunto.style.display="";	
					blo_texto.style.display="";
					tinyMCE.setContent("")
					cam_emisor.value = hid_minombre.value;
					
					cam_receptor.value = r;
					cam_emisor.disabled = true;
					cam_receptor.disabled = true;
					cam_emisor.style.background = "#EEEEEE";
					cam_receptor.style.background = "#EEEEEE";
					cam_fecha.value = g;
					
					
					hid_ir.value = j;
					hid_mr.value = o;
					hid_me.value = m;
					
					cam_receptor_v.value = hid_minombre.value;
					cam_asunto.value="Re:"+f;
			
		}
	}
	
	if (a == 3) //Ver
	{		hid_1.value = s; //Añado el valor de id (s) al campo hidden_1
			blo_fecha.style.display="";
			blo_emisor_v.style.display="";
			blo_receptor_v.style.display="";
			blo_asunto_v.style.display="";	
			blo_texto_v.style.display="";
			cam_emisor_v.disabled = true;
			cam_receptor_v.disabled = true;
			cam_emisor_v.style.background = "#EEEEEE";
			cam_receptor_v.style.background = "#EEEEEE";
			cam_emisor_v.value = r;
			cam_fecha.value = g;
			
			cam_receptor_v.value = hid_minombre.value;
			cam_asunto_v.value=f;
			cam_texto_v.value = h
			hid_ie.value = j;
			
			hid_ir.value = n;
			hid_mr.value = o;
			hid_me.value = m;
			tinyMCE.setContent(h)
			
	}
} //Fin de la funcion
function campos_men(id_emisor,nombre_emisor)
{	hid_ie.value = id_emisor;

		 
}
function anadir_imagen()
{	
	
	fi = document.getElementById('fiel'); // 1
	
	
	contenedor = document.createElement('div'); // 2
	//alert(typeof(num))
	contenedor.id = 'gal'+parseFloat(num); // 3
	fi.appendChild(contenedor); // 4
	//Hiddens
	//Entradas
  	heled = document.createElement('input'); // 5
  		heled.type = 'hidden'; 
  		heled.id = 'hdia'+num;  
  		heled.name = 'hdia'+num;
  		heled.value = 'as'; 
  	helem = document.createElement('input'); // 5
  		helem.type = 'hidden'; 
  		helem.id = 'hmes'+num;  
  		helem.name = 'hmes'+num;
  		helem.value = '';
  	helea = document.createElement('input'); // 5
  		helea.type = 'hidden'; 
  		helea.id = 'hano'+num;  
  		helea.name = 'hano'+num;
  		helea.value = '';
	helet = document.createElement('input'); 
  		helet.type = 'hidden'; 
  		helet.id = 'htexto'+num; 
  		helet.name = 'htexto'+num;
  		helet.value = ''; 
	hele = document.createElement('input'); 
  		hele.type = 'hidden';
  		hele.id = 'hfile'+num; 
  		hele.name = 'hfile'+num;
  		hele.value = '';
	contenedor.appendChild(heled); // Añadir Dia
 	contenedor.appendChild(helem); // Añadir Mes
 	contenedor.appendChild(helea); // Añadir Año
 	contenedor.appendChild(helet); // Añadir Texto
 	contenedor.appendChild(hele); // Añadir File
 	
	//Entradas
  	eled = document.createElement('input'); // 5
  		eled.name = 'dia'+num; // 6
  		eled.type = 'text'; // 6
  		eled.id = 'fecha2'; // 6 
  		eled.value = 'dd'; // 6
  		eled.onblur = function () {anadir_campo_hidden(this.value,this.name)} // 9; // 6
  	elem = document.createElement('input'); // 5
  		elem.name = 'mes'+num; // 6
  		elem.type = 'text'; // 6
  		elem.id = 'fecha2'; // 6
  		elem.value = 'mm'; // 6
  		elem.onblur = function () {anadir_campo_hidden(this.value,this.name)} // 9; // 6
  	elea = document.createElement('input'); // 5
  		elea.name = 'ano'+num; // 6
  		elea.type = 'text'; // 6
  		elea.id = 'fecha2'; // 6
  		elea.value = 'aa'; // 6
 		elea.onblur = function () {anadir_campo_hidden(this.value,this.name)} // 9; // 6
	elet = document.createElement('input'); // 5
  		elet.name = 'texto'+num; // 6
  		elet.type = 'text'; // 6
  		elet.id = 'entrada3'; // 6
  		elet.value = 'texto introductorio'; // 6
  		elet.onblur = function () {anadir_campo_hidden(this.value,this.name)} // 9; // 6  		
	ele = document.createElement('input'); // 5
  		ele.type = 'file'; // 6
  		ele.name = 'file'+num; // 6
  		ele.id = 'file2'; // 6
	eleb = document.createElement('input'); // 5
	  	eleb.type = 'button'; // 6
	  	eleb.value = ''; // 8
	  	eleb.id = 'boton_b'
  		eleb.name = 'gal'+num; // 8
  		eleb.onclick = function () {borrar(this.name)} // 9
	eleh = document.createElement('hr'); // 5
	contenedor.appendChild(eleh); // Añadir HR
 	contenedor.appendChild(eled); // Añadir Dia
 	contenedor.appendChild(elem); // Añadir Mes
 	contenedor.appendChild(elea); // Añadir Año
 	contenedor.appendChild(elet); // Añadir Texto
 	contenedor.appendChild(ele); // Añadir File
 	contenedor.appendChild(eleb); // Aádir Borrar
 	
 	//Creamos file.
	
	  	
  	
	
	//Borrar
	
  	 
 	
	
 	
	num++
	hid_imagenes.value=num;
}
function anadir_campo_hidden(valor,nombre)
{	
	
	document.getElementById("h"+nombre).value=valor;
}
//******************************************************************************
//******************************************************************************
function borrar(obj)
{	fi = document.getElementById('fiel'); // 1 
  	fi.removeChild(document.getElementById(obj)); // 10
  	num = num-1;
  	hid_imagenes.value = num
  	
  	
}
function valorenhidden_mensajes()
{	option_seleccionado = document.forms[0].option_cla.selectedIndex;
	
	hid_escape_v = document.forms[0].option_cla.options[option_seleccionado].id
	
	hid_escape.value = hid_escape_v.substring(3)
	
}

function valorenhidden_albums_s()
{	option_seleccionado = document.forms[0].option_alb_s.selectedIndex;
	hid_escape_v = document.forms[0].option_alb_s.options[option_seleccionado].id
	hid_escape.value = hid_escape_v.substring(3)
	
}
function ext_imagen_users(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.
				
		}
	}
}
