// JavaScript Document

function enviarGaleria()
{
	var msg = "";
	
	if(document.formuGaleria.txtTitulo.value == "")
	{
		msg += "Debes rellenar el campo \"Título\"\n";
	}//fin if
	if(document.formuGaleria.txtDescripcion.value == "")
	{
		msg += "Debes rellenar el campo \"Descripción\"\n";
	}//fin if
	if(document.formuGaleria.filePortada.value == "" && document.formuGaleria.hdnPortada.value == "")
	{
		msg += "Debes rellenar el campo \"Portada\"\n";
	}//fin if
	
	if(msg)
	{
		alert(msg);
	}//fin if
	else
	{
		textCounter(document.formuGaleria.txtDescripcion, document.formuGaleria.remLen, 700);
		document.formuGaleria.submit();
	}//fin else	
}//fin funcion

function enviarFoto()
{
	var msg = "";
	
	if(document.formuFotos.fileFoto.value == "")
	{
		msg += "Debes rellenar el campo \"Foto\"\n";
	}//fin if
	if(document.formuFotos.txtComentarioFoto.value == "")
	{
		msg += "Debes rellenar el campo \"Comentario\"\n";
	}//fin if
	
	if(msg)
	{
		alert(msg);
	}//fin if
	else
	{
		document.formuFotos.submit();
	}//fin else	
}//fin funcion

function enviarComentario()
{
	if(document.formu.txtComentario.value == "")
	{
		alert( "Debes rellenar el campo \"Cometario\"");
	}//fin if
	else
	{
		document.formu.submit();
	}//fin else
}//fin funcion


	function cargaFoto(foto, posicion, id)
	{
		document.getElementById("fotoGrande").src = foto;
		
		if (posicion > 0)
		{
			document.getElementById("btn1").style.display = "block";
		}//fin if
		else
		{
			document.getElementById("btn1").style.display = "none";
		}//fin else
		
		if (longitudArray == posicion)
		{
			document.getElementById("btn2").style.display = "none";
		}//fin if
		else
		{
			document.getElementById("btn2").style.display = "block";		
		}//fin else
		
		avanza = posicion + 1;
		atras = posicion - 1;
		
		verComentarios(id);
	}//fin funcion
		
	function mueveFoto(valor)
	{
		document.getElementById("fotoGrande").src = "imagenes/nuestros_gatos/fotosGaleria/" + arrayFotos[valor][0];
		document.getElementById("foto1").src = "imagenes/nuestros_gatos/fotosGaleria/thumbs/" + arrayFotos[valor][0];
		document.getElementById("descripcionFoto").innerHTML = "<br />" + arrayFotos[valor][2] + "<br />";
		document.getElementById("hdnIdF")	.value = arrayFotos[valor][1];
		
		if ((valor+1) <= longitudArray)
		{
			document.getElementById("foto2").style.visibility = "visible";
			document.getElementById("foto2").src = "imagenes/nuestros_gatos/fotosGaleria/thumbs/" + arrayFotos[valor+1][0];
		}//fin if
		else
		{
			document.getElementById("foto2").style.visibility = "hidden";
		}//fin else
		
		avanza = valor + 1;
		atras = valor - 1;
		
		if (valor > 0)
		{
			document.getElementById("btn1").style.visibility = "visible";
		}//fin if
		else
		{
			document.getElementById("btn1").style.visibility = "hidden";
		}//fin else
		
		if (longitudArray == valor)
		{
			document.getElementById("btn2").style.visibility = "hidden";
		}//fin if
		else
		{
			document.getElementById("btn2").style.visibility = "visible";		
		}//fin else		
		
		verComentarios(arrayFotos[valor][1]);
	}//fin funcion


	var conexion1;
	var id;

	function introducirComentario()
	{
		var url;	  
		var rdm = parseInt(Math.random()*99999999); 
		
		var idFoto = document.getElementById("hdnIdF").value;
		var idGaleria = document.getElementById("hdnIdG").value;
		
		url="modulos/nuestrosgatos/introducirComentarioFoto.cfm?comentario=" + encodeURIComponent(document.getElementById("comentFoto").value) + "&idFoto=" + idFoto + "&idGaleria="+ idGaleria +"&rdm=" + rdm;
		
		id = idFoto;
		
		conexion1=crearXMLHttpRequest();
		conexion1.onreadystatechange = procesarEventos2;
		conexion1.open("GET", url, true);
		conexion1.send(null);  	  
	}//fin funcion
	
	function procesarEventos2()
	{
		var detalles;
		
		detalles = document.getElementById("mensaje"); 
		
		if(conexion1.readyState == 4)
		{
			if (conexion1.responseText > 0)
			{
				document.getElementById("comentFoto").value = "";
				alert("Comentario subido.");
				verComentarios(id);
			}//fin if
			else
			{
				alert("Ha ocurrido un error.");
			}//fin else
			
			detalles.innerHTML = ''
		} //fin if
		else 
		{
			detalles.innerHTML = "<img src='imagenes/ajax-loader.gif'>";
		}//fin else
	}//fin funcion
	
	
	function verComentarios(idCom) 
	{
		var url;
		var rdm = parseInt(Math.random()*99999999); 
		
		url="modulos/nuestrosgatos/sacaComentario.cfm?id=" + idCom + "&rdm=" + rdm;
		
		conexion1=crearXMLHttpRequest();
		conexion1.onreadystatechange = procesarEventos;
		conexion1.open("GET", url, true);
		conexion1.send(null);
	}//fin funcion
	
	function procesarEventos()
	{
		var detalles;
		
		detalles = document.getElementById("comentarios");
		
		if(conexion1.readyState == 4)
		{
			detalles.innerHTML = conexion1.responseText;
		} //fin if
		else 
		{
			detalles.innerHTML = "<img src='imagenes/ajax-loader.gif'>";
		}//fin else
	}//fin funcion
	
	function eliminarComentario(idCom, idFoto, idG) 
	{
		var url;
		var rdm = parseInt(Math.random()*99999999); 
		id = idFoto;
		idGaleria = idG;
		url="modulos/nuestrosgatos/eliminaComentario.cfm?id=" + idCom + "&idGaleria="+ idGaleria +"&rdm=" + rdm;
		
		conexion1=crearXMLHttpRequest();
		conexion1.onreadystatechange = procesarEventos3;
		conexion1.open("GET", url, true);
		conexion1.send(null);
	}//fin funcion
	
	function procesarEventos3()
	{
		var detalles;
		
		detalles = document.getElementById("mensaje"); 
		
		if(conexion1.readyState == 4)
		{
			if (conexion1.responseText > 0)
			{
				alert("Comentario eliminado.");
				verComentarios(id);
			}//fin if
			else
			{
				alert("Ha ocurrido un error.");
			}//fin else
		
			detalles.innerHTML = '';
		} //fin if
		else 
		{
			detalles.innerHTML = "<img src='imagenes/ajax-loader.gif'>";
		}//fin else
	}//fin funcion
	
	/****************************************
	 Funciones comunes a todos los problemas
	****************************************/
	
	function crearXMLHttpRequest() 
	{
		var xmlHttp=null; //de primeras la inicializamos a "null", que será lo que devuelva si no pasa las siguientes comprobaciones...
		
		try
		{
			xmlHttp=new XMLHttpRequest(); //primero probamos en Firefox, Opera, Safari, etc.
		}//fin try
		
		catch (e) // Internet Explorer
		{			
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); //versiones anteriores de ie
			}//fin try
	
			catch (e)
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); //ie 6 (creo) ie 7
			}//fin catch
		}//fin catch
		
		return xmlHttp;
	}//fin funcion


	function pasarPagina(p)
	{
		document.formu2.hdnPagina.value=p;
		document.formu2.submit();
	}
	
	function retrocederPagina(p)
	{
		document.formu3.hdnPagina.value=p;
		document.formu3.submit();
	}

	function textCounter(field, countfield, maxlimit) 
	{
		if (field.value.length > maxlimit) // if too long...trim it!
			field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
		else 
			countfield.value = maxlimit - field.value.length;
	}

