<!--

	
	if(nombreUsuario == null || emailUsuario == null) {
		var nombreUsuario = emailUsuario = '';
	}
    var formatoFecha = 'dd/mm/yyyy';	// Formato de fecha que vamos a utilizar
	var enplay = 0;
	var timeout;
  	
	var miurl = document.location.href;
	var midominio = document.domain;

	miurl = miurl.substring(miurl.indexOf(midominio) + midominio.length);
	if(miurl.indexOf("tus_fotos") >= 0) {
		miurl = miurl.substring(0, miurl.indexOf("tus_fotos")) + "tus_fotos/";
	} else if(miurl.indexOf("tus-fotos") >= 0) {
		miurl = miurl.substring(0, miurl.indexOf("tus-fotos")) + "tus-fotos/";
	}


	
  // Función de validación de email
  function emailValido(valor) {
	var EmailOk = true;
	var AtSym = valor.indexOf('@');
	var Period = valor.lastIndexOf('.');
	var Space = valor.indexOf(' ');
	var Length = valor.length - 1;
	if ((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space  != -1)) {  
		  EmailOk = false;
	}
	return EmailOk;
  }
  
  // Función de validación de fecha
  function fechaValida(valor) {
  	var formatoFechaReg = formatoFecha;
  	formatoFechaReg = formatoFechaReg.replace('dd','([0-2][0-9]|3[0-1])');
  	formatoFechaReg = formatoFechaReg.replace('mm','(0[0-9]|1[0-2])');
  	formatoFechaReg = formatoFechaReg.replace(/y/g,'[0-9]');
  	formatoFechaReg = "^" + formatoFechaReg + "$";
  	var ExpReg = new RegExp(formatoFechaReg);
  	var fechaOK = ExpReg.test(valor);
  	return fechaOK;
  }
  
  // Función de validación de fotos
  /*
  function fotoValida(valor) {
  	// var ExpRegFoto = /^[\w: \\/_-]+\.(jpeg|jpg)$/i;
	// var formatoFoto = "^[\\w: \\.\\\\/_-]+\\.(jpeg|jpg|gif)$";
	var formatoFoto = "^(.*)(\\\\|/)[\\w:/_-]+\\.(jpeg|jpg|gif)$";
  	var ExpRegFoto = new RegExp(formatoFoto,"i");
  	var fotoOK = ExpRegFoto.test(valor);
  	return fotoOK;
  }
  */

  function fotoValida(valor) {
		var formatoFile = "[\\w-]+\\.*[gif|png|jpg]$";
		var ExpRegFile = new RegExp(formatoFile,"i");
		var fileOK = ExpRegFile.test(valor);
		return fileOK;
	}
  
  function checkForm(frm) {
    for(i=0;i<frm.length;i++) {
    	campo = frm.elements[i];
    	var datosCheck = campo.id.split('#');
    	if(datosCheck[0].indexOf('*') != -1) {
    		if(campo.value == "") {
    			alert('No has rellenado el campo ' + datosCheck[1]);
    			campo.focus();
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('numero') != -1) {
    		if(isNaN(campo.value)) {
    			alert('El campo ' + datosCheck[1] + ' tiene que ser numérico');
    			campo.focus();
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('email') != -1) {
    		if(!emailValido(campo.value)) {
    			alert('El campo ' + datosCheck[1] + ' tiene que ser un email');
    			campo.focus();
    			return false;
    		}
    	}
    	if((datosCheck[0].indexOf('fecha') != -1) && (campo.value != "")) {
    		if(!fechaValida(campo.value)) {
    			alert('El formato del campo ' + datosCheck[1] + ' no es válido');
    			campo.focus();
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('checkbox') != -1) {
    		if(campo.checked == false) {
    			alert('No has rellenado el campo ' + datosCheck[1]);
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('radio') != -1) {
    		var radioCheck = 0;
    		eval('var radioElemento = frm.' + campo.name);
    		for(j=0;j<radioElemento.length;j++) {
    			if(radioElemento[j].checked) {
    				radioCheck = 1;
					break;
				}
    		}
    		if(radioCheck == 0) {
    			alert('No has rellenado el campo ' + datosCheck[1]);
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('select') != -1) {
    		if(campo.selectedIndex == 0) {
    			alert('No has rellenado el campo ' + datosCheck[1]);
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('selectMultiple') != -1) {
    		seleccion = 0
    		for(j=1;j<campo.length;j++) {
    			if(campo.options[j].selected == true)
    				seleccion = 1
    		}
    		if(seleccion == 0) {
    			alert('No has rellenado el campo ' + datosCheck[1]);
    			return false;
    		}
    	}
    	var expReg = /[^A-Za-z0-9ñÑáéíóúÁÉÍÓÚüÜ_\s\¿\?\¡\!\<\>\.\,\:\;\(\)\@\#\$\€\%\&\\\/\*\=\+\-\{\}\[\]\ç\º\ª]/i;
    	if(datosCheck[0].indexOf('parsear') != -1) {
    		if(expReg.test(campo.value)) {
    			alert('El campo ' + datosCheck[1] + ' no es válido\n');
    			campo.focus();
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('min') != -1) {
    		posicion = datosCheck[0].indexOf('min') + 3;
    		limite = "0";
    		while((!isNaN(datosCheck[0].substring(posicion,posicion + 1))) && (posicion < datosCheck[0].length)) {
    			limite = limite + datosCheck[0].substring(posicion,posicion + 1);
    			posicion = posicion + 1;
    		}
    		limite = parseInt(limite,10);
    		if(campo.value.length < limite) {
    			alert('El campo ' + datosCheck[1] + ' tiene que tener al menos ' + limite + ' caracteres\n');
    			campo.focus();
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('max') != -1) {
    		posicion = datosCheck[0].indexOf('max') + 3;
    		limite = "0";
    		while((!isNaN(datosCheck[0].substring(posicion,posicion + 1))) && (posicion < datosCheck[0].length)) {
    			limite = limite + datosCheck[0].substring(posicion,posicion + 1);
    			posicion = posicion + 1;
    		}
    		limite = parseInt(limite,10);
    		if(campo.value.length > limite) {
    			alert('El campo ' + datosCheck[1] + ' tiene que tener como máximo ' + limite + ' caracteres\n');
    			campo.focus();
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('foto') != -1) {
    		if(!fotoValida(campo.value)) {
    			alert('El formato de la foto no es correcto. Verifique que es un JPEG o un GIF o que el nombre no tiene espacios o caracteres extraños.');
    			campo.focus();
    			return false;
    		}
    	}
    	if(datosCheck[0].indexOf('acepto') != -1) {
    		if(campo.checked == false) {
    			alert('Tienes que aceptar las condiciones');
    			return false;
    		}
    	}
    }
    return true;
  }

	
	function createRequestObject() {
		var ro;
		var browser = navigator.appName;

		if(browser == "Microsoft Internet Explorer"){
			ro = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
			ro = new XMLHttpRequest();
		}
		return ro;
	}  

  function pagCategorias(numpag) {
	  var frm = document.frmCategorias;
		var http = createRequestObject();
		http.open("get", miurl + "fotospeques.php?id_galeria=" + frm.id_galeria.value + "&pag_peques=" + numpag + "&num_fotos_peques=" + frm.num_fotos_peques.value + "&total_peques="+frm.total_peques.value+"&r=" + Math.random());
		http.onreadystatechange = function () {
			if(http.readyState == 4){
				var response = http.responseText;
				if(response) actualizaFotosPeques(response);
			}
		}
		http.send(null);
  }

  

	function cambioFoto(pag) {
		//alert(pag);
		var frm = document.frmFoto;
		var frm2 = document.frmComentarios;
		frm.pag.value = pag;
		var http = createRequestObject();
		http.open("get", miurl + "foto.php?id_galeria=" + frm.id_galeria.value + "&id_campo_categoria=" + frm.id_campo_categoria.value + "&id_valor_categoria=" + frm.id_valor_categoria.value + "&num_fotos_categoria=" + frm.num_fotos_categoria.value + "&num_fotos_grandes=" + frm.num_fotos_grandes.value + "&pag=" + pag + "&paginacion_comentarios=" + frm2.paginacion_comentarios.value + "&alto_max_fotos=" + frm.alto_max_fotos.value + "&enplay=" + enplay + "&r=" + Math.random());
		http.onreadystatechange = function () {
			if(http.readyState == 4){
				var response = http.responseText;
				if(response) actualizaFoto(response);
			}
		}
		http.send(null);
	}

	function actualizaFoto(response) {
		if(response){
			var arr = response.split("###");
			window.document.getElementById("div_foto").innerHTML = arr[0];
			if(document.frmComentarios.concomentarios.value == 1) {
				window.document.getElementById("div_numcomentarios").innerHTML = arr[1];
				window.document.getElementById("div_comentarios").innerHTML = arr[2];
			}
			document.frmComentario.id_foto.value = arr[3];
			if (document.frmVotarFoto){
				document.frmVotarFoto.id_foto.value = arr[3];
			}
			document.frmComentarios.id_foto.value = arr[3];
			document.envio_noticia.id_foto.value = arr[3];
			getEstrellasFoto();
		}
	}

	function pagComentarios(pag) {
		var frm = document.frmComentarios;

		var http = createRequestObject();
		http.open("get", miurl + "comentariosfoto.php?id_galeria=" + frm.id_galeria.value + "&id_foto=" + frm.id_foto.value + "&pag_comentarios=" + pag + "&num_comentarios=" + frm.num_comentarios.value + "&paginacion_comentarios=" + frm.paginacion_comentarios.value + "&r=" + Math.random());
		http.onreadystatechange = function () {
			if(http.readyState == 4){
				var response = http.responseText;
				if(response) actualizaComentarios(response);
			}
		}
		http.send(null);
	}


	function play() {
		enplay = 1;
		var interval = document.frmFoto.intervalo.value;
		var numpag = parseInt(document.frmFoto.pag.value);
		numpag++;
		if(numpag > parseInt(document.frmFoto.num_fotos_categoria.value)) numpag = 1;
		cambioFoto(numpag);
		timeout = setTimeout("play()", interval*1000);
	}

	function pause() {
		window.clearTimeout(timeout);
		window.document.getElementById("div_play1").innerHTML = "<a href=\"javascript:play();\" class=\"boton\" title=\"Reproducir\"><img src=\"/img/ico_boton_play.gif\" alt=\"Reproducir\" />&nbsp;play</a>";
		window.document.getElementById("div_play2").innerHTML = "<a href=\"javascript:play();\" class=\"boton\" title=\"Reproducir\"><img src=\"/img/ico_boton_play.gif\" alt=\"Reproducir\" />&nbsp;play</a>";
		enplay = 0;
	}

	

	function validarDatos() { 
		conectarCKT(window.document.frmComentario);
		return;

		var frm = document.frmComentario;	
		if(checkForm(frm)) {
			var httpPost = createRequestObject();
			httpPost.open("post", miurl + "validarusuario.php", true);
			httpPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			httpPost.onreadystatechange = function () {
				if(httpPost.readyState == 4){
					if (httpPost.responseText) {
						actualizaFormuComentario(httpPost.responseText);
					}
				}
			}
			httpPost.send(getParametros(frm));
		}
	}

	function enviarMas() { 
		
		var frm = document.frmFotoEnviada;	
		var httpPost = createRequestObject();
		httpPost.open("post", miurl + "validarusuario.php", true);
		httpPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		httpPost.onreadystatechange  =  function () {
			if(httpPost.readyState == 4){
				if (httpPost.responseText) {
					actualizaFormuEnvioFoto(httpPost.responseText);
				}
			}
		}
		httpPost.send(getParametros(frm));
	}


	function conectarCKT(frm) {
		if(typeof(queid_master) == 'undefined') {
		
			var direccion;
         
                if (window.document.location.href.indexOf('?') > 0) {
                        var campos = window.document.location.href.split('?');
                        direccion = campos[0];
                }
                else {                 
                        direccion = window.document.location.href;          
                }
         
                var qs = 'url=' + direccion;
                qs += '&email=' + frm.email.value;
                qs += '&contrasena=' + frm.pwd.value;
                window.location = frm.frontal.value + '/backend/conectar.php?' + qs;

		} else {
			//queid_login('barra');
			
			frm.method = 'post';
			frm.action = "http://" + queid_master + "/login.php";
			frm.url_retorno.value = window.document.location.href;
			

			frm.servicio.value = queid_serv;
			frm.queid_email.value = frm.email.value;
			frm.queid_pass.value = frm.pwd.value;

			frm.submit();
			

		}
	}
	function enviarMasComentarios() { 

		var frm = document.frmComentarioEnviado;	
		var httpPost = createRequestObject();
		httpPost.open("post", miurl + "validarusuario.php", true);
		httpPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		httpPost.onreadystatechange  =  function () {
			if(httpPost.readyState == 4){
				if (httpPost.responseText) {
					actualizaFormuComentario(httpPost.responseText);
				}
			}
		}
		httpPost.send(getParametros(frm));
	}

	function validarDatosEnvio() {
		conectarCKT(window.document.frmEnvioFoto);     
                return;

		var frm = document.frmEnvioFoto;	
		if(checkForm(frm)) {
			var httpPost = createRequestObject();

			httpPost.open("post", miurl + "validarusuario.php", true);
			httpPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			httpPost.onreadystatechange = function () {
				if(httpPost.readyState == 4){
					if (httpPost.responseText) {
						actualizaFormuEnvioFoto(httpPost.responseText);
					}
				}
			}
			httpPost.send(getParametros(frm));
		}
	}

	function enviarFoto() {		
		var frm = document.frmEnvioFoto;	
		if(checkForm(frm)) {
			frm.target = "ifr_enviofoto";
			frm.action = miurl + "enviarfoto.php";
			frm.submit();
		}
	}

	
	
	

	function enviarComentario() {
		var frm = document.frmComentario;	
		
		if(checkForm(frm)) {
			var httpPost = createRequestObject();
			httpPost.open("post", miurl + "enviarcomentario.php", true);
			httpPost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			httpPost.onreadystatechange = function () {
				if(httpPost.readyState == 4){
					if (httpPost.responseText) {
						actualizaFormuComentario(httpPost.responseText);
					}
				}
			}
			httpPost.send(getParametros(frm));
		}
		
	}


	function getParametros(formu) {
		var parametros = "";
		for(var i=0;i<formu.length;i++) {
			if(formu.elements[i].type == "button" || formu.elements[i].type == "reset" || formu.elements[i].type == "submit") continue;
			
			if(parametros != "") {
				if (formu.elements[i].type == "radio" || formu.elements[i].type == "checkbox") {
					if (formu.elements[i].checked) {
						parametros += "&" + formu.elements[i].name + "=" + formu.elements[i].value;	
					}
				}
				else {
					parametros += "&" + formu.elements[i].name + "=" + encodeURIComponent(formu.elements[i].value);
				}
			}
			else {
				if (formu.elements[i].type == "radio" || formu.elements[i].type == "checkbox") {
					if (formu.elements[i].checked) {
						parametros += formu.elements[i].name + "=" + formu.elements[i].value;	
					}
				}
				else {
					parametros += formu.elements[i].name + "=" + encodeURIComponent(formu.elements[i].value);
				}
			}
		}	
		return parametros;
	}

	function actualizaComentarios(response) {
		if(response)
			window.document.getElementById("div_comentarios").innerHTML = response;
	}

	function actualizaFormuComentario(response) {
		if(response) {
			window.document.getElementById("div_formucomentario").innerHTML = response;
		}
	}

	function actualizaFormuEnvioFoto(response) {
		if(response)
			window.document.getElementById("div_formuenviofoto").innerHTML = response;

			$(document).ready(function() {
				$(".formulariocomentarios p").each(function() {
					if($(this).html().indexOf('<span class="label">Nombre<span>') >= 0 || $(this).html().indexOf('<span class="label">Autor<span>') >= 0) {
						$(this).find("input").val(nombreUsuario);
					} else if($(this).html().indexOf('<span class="label">Email<span>') >= 0) {
						$(this).find("input").val(emailUsuario);
					}
				});
				
			 });
	}

	function actualizaFotosPeques(response) {
		if(response)
			window.document.getElementById("div_fotospeques").innerHTML = response;
	}


	function envioAmigo() {
		document.getElementById("envialafoto").style.display = "block";
	}
	
	function enviarAmigo() {
			
			var frm = document.envio_noticia;
			if(checkForm(frm)) {
				var nombre = frm.nombre.value;
				var email_remitente = frm.email_remitente.value;
				var para = frm.para.value;
				var email_destinatario = frm.email_destinatario.value;
				var comentario = frm.comentario.value;
				var miurlenvio = document.location.href;
				if(miurlenvio.indexOf('otrasGalerias') > -1) {
					var url = "http://" + document.domain + miurl + "fotos/otrasGalerias/" + frm.categoria.value + "/" + frm.id_foto.value;
				} else {
					var url = "http://" + document.domain + miurl + "fotos/" + frm.categoria.value + "/" + frm.id_foto.value;
				}
				
				
				var max_longitud = 120;


				var http = createRequestObject();

				var qs = "nombre=" + nombre + "&email_remitente=" + email_remitente + "&para=" + para;
				qs += "&email_destinatario=" + email_destinatario + "&comentario=" + comentario;
				qs += "&url=" + url;
				http.open("get", miurl + "enviaramigo.php?" + qs +"&r=" + Math.random());
				http.onreadystatechange = function () {
					if(http.readyState == 4){
						if (http.responseText) {
							if (http.responseText.indexOf('OK') > -1) {
								var url_mostrar = (url.length > max_longitud) ? url.href.substr(0, max_longitud) + "..." : url;
								elHTML = "<form action=><div class=envianoticia><div class=envianoticiaOK><p>Su correo ha sido enviado correctamente.</p><p>Página enviada:</p><p class=url><a href='" + url + "' title='" + url + "'>" + url + "</a></p></div><div class=clear></div></div><div class='envianoticia-botones boton'><a href=javascript:cerrarEnviarFoto();>Cerrar</a></div></form>";
							}
							else {
								elHTML = "<form action=><div class=envianoticia><div class=envianoticiaOK><p>Ha ocurrido un ERROR al enviar la página, por favor intentelo más tarde.</p></div><div class=clear></div></div><div class='envianoticia-botones boton'><a href=javascript:cerrarEnviarFoto();>Cerrar</a></div></form>";
					
							}
							frm.reset();
							window.document.getElementById("div-enviar-foto").innerHTML = elHTML;
						}
					}
				}
				http.send(null);
			}
	}

	function cerrarEnviarFoto() {
		document.getElementById("envialafoto").style.display = "none";
	}


	function votarfoto(puntos) {
		
		if (document.frmVotarFoto){
			var id_foto = document.frmVotarFoto.id_foto.value;
			var ip = document.frmVotarFoto.ip.value;

			var http = createRequestObject();
			http.open("get", "/envio_fotos/votar-foto.php?puntos=" + puntos + "&id_foto=" + id_foto + "&ip=" + ip + "&r=" + Math.random());
			http.onreadystatechange = function () {
				if(http.readyState == 4) {
					if (http.responseText) {
						gracias = "<div class=label>Gracias por votar</div>";
						document.getElementById('contenidoestrellas').innerHTML = gracias;
						getEstrellasFoto();
						
					}
				}	
			}
			http.send(null);		
		}
	}
	function getEstrellasFoto() {
		
		if (document.frmVotarFoto){
			var id_foto = document.frmVotarFoto.id_foto.value;
	
			var http = createRequestObject();
			http.open("get", "/envio_fotos/estrellas.php?id_foto=" + id_foto +"&r=" + Math.random());
			http.onreadystatechange = function () {
				if(http.readyState == 4){
					if (http.responseText) {
						var estrellas = http.responseText;
					
						if (estrellas.indexOf(".")) {
			
							campos = estrellas.split(".");
							estrellas = campos[0];	
							var media = (campos[1] > 0) ? 1 : 0;
						}
					
						var contenido = '';
					
						for (i = 0; i < estrellas; i++) {
							contenido += '<img src="/img/star_on.gif" alt="' + i +' votos" />';
						}
					
						if (media == 1) {
							contenido += '<img src="/img/star_on_md.gif" alt="' + estrellas +' votos" />';
							estrellas++;
						}
					
						for (i = estrellas; i < 5; i++) {
							contenido += '<img src="/img/star_off.gif" alt="' + i +' votos" />';	
						}
					
						window.document.getElementById('resultados-votos').innerHTML = contenido;
						
					}
				}	
			}
			http.send(null);
		}		
	}
//-->
