	// amigo_enviar
	function amigo_enviar(fotoid){
		var tunombre = $('#tunombre').val();
		var tuemail = $('#tuemail').val();
		var amigoemail = $('#amigoemail').val();
		var comentario = $('#comentario').val();
		if(tunombre != "" && tuemail != "" && amigoemail != "")
		{
			var arrayemail = amigoemail.split(",");
			var i = 0;
			var validaremails = true;
			while(i < arrayemail.length && validaremails == true){
				if(validarEmail(trim(arrayemail[i])) == false){
					validaremails = false;
				}
				i++;
			}
			if(validarEmail(tuemail) && validaremails == true){
				$.ajax({
					type: "POST",
					url: "http://www.estodotuyo.com/wp-content/themes/open-air/cuento/amigo_enviar.php",
					data: "id="+fotoid+"&tunombre="+tunombre+"&tuemail="+tuemail+"&amigoemail="+amigoemail+"&comentario="+comentario,
					success: function(msg){
						if(msg == 1){
//							var texto = '<div style="text-align:center; margin-top:125px;">El mensaje se ha enviado con éxito.<br/><br/><input type="button" value="Cerrar" onClick="javascript:cerrar_ventana(\'#amigo\');" /></div>';
//							$('#amigo').html(texto);
							guardar_cookie('ettv_tunombre',tunombre,30);
							guardar_cookie('ettv_tuemail',tuemail,30);
							guardar_cookie('ettv_amigoemail',amigoemail,30);
							guardar_cookie('ettv_comentario',comentario,30);
							$('#envioleyenda').css("background-color","#00E05F");
							$('#envioleyenda').html("El mensaje se ha enviado con éxito.");
							$('#envioleyenda').fadeIn();
							$('#envioleyenda').fadeOut(5000);
						}
						else{
//							var texto = '<div style="text-align:center; margin-top:125px;">Ha habido un error al enviar el email.<br/><br/><input type="button" value="Cancelar" onClick="javascript:cerrar_ventana(\'#amigo\');" /></div>';
//							$('#amigo').html(texto);
							$('#envioleyenda').css("background-color","#F70B5D");
							$('#envioleyenda').html("Ha habido un error al enviar el email.");
							$('#envioleyenda').fadeIn();
							$('#envioleyenda').fadeOut(5000);
						}
					}
				});
			}
			else{
//				$('#envioleyenda').css("color","red");
				$('#envioleyenda').css("background-color","#F70B5D");
				$('#envioleyenda').html("Uno de los emails no es válido.");
				$('#envioleyenda').fadeIn();
				$('#envioleyenda').fadeOut(5000);
			}
		}
		else{
//			$('#envioleyenda').css("color","red");
			$('#envioleyenda').css("background-color","#F70B5D");
			$('#envioleyenda').html("Debes rellenar todos los campos.");
			$('#envioleyenda').fadeIn();
			$('#envioleyenda').fadeOut(5000);
		}
	}

// amigo_formulario
function amigo_formulario(fotoid){
	$.ajax({
		type: "POST",
		url: "http://www.estodotuyo.com/wp-content/themes/open-air/cuento/amigo_form.php",
		data: "id="+fotoid,
		success: function(msg){
			if(msg != 0){
				$('body').append(msg);
				centrar_ventana('#cubreamigo');
			}
		}
	});
}

function borrar_comentario(comentarioid){
	$.ajax({
		type: "POST",
		url: "http://www.estodotuyo.com/wp-content/themes/open-air/cuento/comentario_borrar.php",
		data: "id="+comentarioid,
		success: function(msg){
			if(msg != "error"){
				$('#comentario'+comentarioid).remove();
			}
			else{
				alert("Ha ocurrido un error, por favor, inténtelo de nuevo.");
			}
		}
	});
}

function borrar_frase(fraseid){
	$.ajax({
		type: "POST",
		url: "http://www.estodotuyo.com/wp-content/themes/open-air/cuento/frase_borrar.php",
		data: "id="+fraseid,
		success: function(msg){
			if(msg != "error"){
				$('#frase'+fraseid).remove();
			}
			else{
				alert("Ha ocurrido un error, por favor, inténtelo de nuevo.");
			}
		}
	});
}

// caduca
function caduca(dias) {  
	var hoy = new Date()                                        //coge la fecha actual  
	var msEnXDias = eval(dias) * 24 * 60 * 60 * 1000    //pasa los dias a mseg.  
	hoy.setTime(hoy.getTime() + msEnXDias)          //fecha de caducidad: actual + caducidad  
	return (hoy.toGMTString())  
} 

// cerrar
function cerrar_ventana(elemento){
	$(elemento).remove();
}

// centrar_ventana
function centrar_ventana(elemento){
	var datos = datospagina();
	$(elemento).css("width",datos[0]);
	$(elemento).css("height",datos[1]);
	var valor = 'left:50%;top:'+(document.documentElement.scrollTop+document.documentElement.clientHeight/2-160)+'px;margin-left:-225px;';
	$(elemento+" #amigo").attr({ 
		'style' : valor
	});
}

// coger_valor_cookie
function coger_valor_cookie(indice) {  
	//indice indica el comienzo del valor  
	var galleta = document.cookie  
	//busca el final del valor, dado por ;, a partir de indice  
	var finDeCadena = galleta.indexOf(";", indice)  
	//si no existe el ;, el final del valor lo marca la longitud total de la cookie  
	if (finDeCadena == -1)  
		 finDeCadena = galleta.length  
	return unescape(galleta.substring(indice, finDeCadena))  
}  

function datospagina(){
		var xScroll,yScroll;
		if(window.innerHeight&&window.scrollMaxY){
			xScroll=window.innerWidth+window.scrollMaxX;
			yScroll=window.innerHeight+window.scrollMaxY;
		}else if(document.body.scrollHeight>document.body.offsetHeight){
			xScroll=document.body.scrollWidth;yScroll=document.body.scrollHeight;
		}else{
			xScroll=document.body.offsetWidth;yScroll=document.body.offsetHeight;
		}
		var windowWidth,windowHeight;
		if(self.innerHeight){
			if(document.documentElement.clientWidth){
				windowWidth=document.documentElement.clientWidth;
			}else{
				windowWidth=self.innerWidth;
			}
			windowHeight=self.innerHeight;
		}else if(document.documentElement&&document.documentElement.clientHeight){
			windowWidth=document.documentElement.clientWidth;windowHeight=document.documentElement.clientHeight;
		}else if(document.body){
			windowWidth=document.body.clientWidth;windowHeight=document.body.clientHeight;
		}
		if(yScroll<windowHeight){
			pageHeight=windowHeight;
		}else{
			pageHeight=yScroll;
		}
		if(xScroll<windowWidth){
			pageWidth=xScroll;
		}else{
			pageWidth=windowWidth;
		}
		arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	}
	
function editar_frase_cerrar(fraseid){
	$('#editar'+fraseid).remove();
}

function editar_frase_mostrar(fraseid){
	var textofrase = $('#textofrase'+fraseid).html();
	var formulario = '<div id="editar'+fraseid+'"><form name"ffrase'+fraseid+'" method="post"><strong>El nuevo texto es:</strong><br><textarea name="texto'+fraseid+'" id="texto'+fraseid+'" class="campoeditar">'+textofrase+'</textarea><br /><input type="button" name="boton" value="Editar" onClick="editar_frase('+fraseid+')" /> <input type="button" name="boton" value="Cancelar" onClick="editar_frase_cerrar('+fraseid+');" /></form></div>';
	$('#frase'+fraseid).after(formulario);
}

function editar_frase(fraseid){
	var texto = $('#texto'+fraseid).val();
	if(texto != ""){
		$.ajax({
			type: "POST",
			url: "http://www.estodotuyo.com/wp-content/themes/open-air/cuento/frase_modificar.php",
			data: "id="+fraseid+"&texto="+texto,
			success: function(msg){
				if(msg != "error"){
					$('#textofrase'+fraseid).html(texto);
					$('#editar'+fraseid).remove();					
				}
				else{
					alert("Ha ocurrido un error, por favor, inténtelo de nuevo.");
				}
			}
		});
	}
	else{
		alert("El campo del comentario no puede estar vacío.");
	}
}

function enviar_comentario(){
	var autor = $('#author').val();
	var email = $('#email').val();
	var comentario = $('#comment').val();
	var cuentoid = $('#cuentoid').val();

	if(autor != "" && email != "" && comentario != "" && validarEmail(email)){
		$.ajax({
			type: "POST",
			url: "http://www.estodotuyo.com/wp-content/themes/open-air/cuento/comentario_enviar.php",
			data: "id="+cuentoid+"&autor="+autor+"&email="+email+"&comentario="+comentario,
			success: function(msg){
				if(msg != "error"){
					location.reload();
					/*
					$('#comentarioleyenda').css("background-color","#00E05F");
					$('#comentarioleyenda').html("El comentario se ha guardado con éxito.");
					$('#comentarioleyenda').fadeIn();
					$('#comentarioleyenda').fadeOut(5000);
					*/
				}
				else{
					$('#comentarioleyenda').css("background-color","#F70B5D");
					$('#comentarioleyenda').html("Ha ocurrido un error, por favor, inténtalo de nuevo.");
					$('#comentarioleyenda').fadeIn();
					$('#comentarioleyenda').fadeOut(5000);
				}
			}
		});
	}
	else{
		$('#comentarioleyenda').css("background-color","#F70B5D");
		$('#comentarioleyenda').html("No has rellenado todos los campos o el email no es válido.");
		$('#comentarioleyenda').fadeIn();
		$('#comentarioleyenda').fadeOut(5000);
	}
}

function enviar_formulario(){
	var nombre = $('#nombre').val();
	var telefono = $('#telefono').val();
	var email = $('#email').val();
	var titulo = $('#titulo').val();
	var cuento = $('#cuento').val();
	if(nombre != "" && telefono != "" && email != "" && titulo != "" && cuento != "" && validarEmail(email)){
		$.ajax({
			type: "POST",
			url: "http://www.estodotuyo.com/wp-content/themes/open-air/cuento/formulario_enviar.php",
			data: "nombre="+nombre+"&telefono="+telefono+"&email="+email+"&titulo="+titulo+"&cuento="+cuento,
			success: function(msg){
				if(msg != "error"){
					$('#comentarioleyenda').css("background-color","#00E05F");
					$('#comentarioleyenda').html("El cuento se ha enviado con éxito.");
					$('#comentarioleyenda').fadeIn();
					$('#comentarioleyenda').fadeOut(5000);
				}
				else{
					$('#comentarioleyenda').css("background-color","#F70B5D");
					$('#comentarioleyenda').html("Ha ocurrido un error, por favor, inténtalo de nuevo.");
					$('#comentarioleyenda').fadeIn();
					$('#comentarioleyenda').fadeOut(5000);
				}
			}
		});
	}
	else{
		$('#comentarioleyenda').css("background-color","#F70B5D");
		$('#comentarioleyenda').html("No has rellenado todos los campos o el email no es válido.");
		$('#comentarioleyenda').fadeIn();
		$('#comentarioleyenda').fadeOut(5000);
	}
}

function enviar_frase(){
	var falumno = $('#falumno').val();
	var ffrase = $('#ffrase').val();
	var fcuentoid = $('#fcuentoid').val();
	var ffraseid = $('#ffraseid').val();

	if(falumno != "" && ffrase != ""){
		$.ajax({
			type: "POST",
			url: "http://www.estodotuyo.com/wp-content/themes/open-air/cuento/frase_enviar.php",
			data: "id="+fcuentoid+"&alumno="+falumno+"&frase="+ffrase+"&ultimafraseid="+ffraseid,
			success: function(msg){
				if(msg != "error" && msg != "antiguo"){
					$('#fraseleyenda').css("background-color","#00E05F");
					$('#fraseleyenda').html("Tu aportación se ha guardado con éxito.");
					$('#fraseleyenda').fadeIn();
					$('#fraseleyenda').fadeOut(5000);
					var mensaje='<div id="frase'+(ffraseid + 1)+'" class="frase">'+ffrase+'<div class="fraseautor">Escrito por '+falumno+'</div></div>';
					$("#finfrases").before(mensaje);
					$('#ffraseid').val(msg);
				}
				else{
					$('#fraseleyenda').css("background-color","#F70B5D");
					if(msg == "antiguo")
						$('#fraseleyenda').html("Alguien se te ha adelantado siguiendo el cuento. Recarga la página e inténtalo de nuevo.");
					else
						$('#fraseleyenda').html("Ha ocurrido un error, por favor, inténtalo de nuevo.");
					$('#fraseleyenda').fadeIn();
					$('#fraseleyenda').fadeOut(5000);
				}
			}
		});
	}
	else{
		$('#fraseleyenda').css("background-color","#F70B5D");
		$('#fraseleyenda').html("Debes rellenar todos los campos.");
		$('#fraseleyenda').fadeIn();
		$('#fraseleyenda').fadeOut(5000);
	}
}

// guardar_cookie
function guardar_cookie (nombre, valor, caducidad) {  
	if(!caducidad)  
		 caducidad = caduca(0)  
	//crea la cookie: incluye el nombre, la caducidad y la ruta donde esta guardada  
	//cada valor esta separado por ; y un espacio  
	document.cookie = nombre + "=" + escape(valor) + "; expires=" + caducidad + "; path=/"  
//		document.cookie = nombre + "=" + escape(valor) + "; expires= 2592000000";  
}  

function mostrar_div(contenido){
	$('#presentacion .contenido').hide();
	$('#presentacion .enlace').removeClass('enlacesel');
	$('#presentacion #e'+contenido).addClass('enlacesel');
	$('#presentacion #'+contenido).fadeIn();
}

function mostrar_profesor(){
	var identificacion = '<form name="formidentificacion" method="post"><label for="profesor">Usuario: </label><input type="text" name="profesor" id="profesor" /> <label for="pass">Contraseña: </label><input type="password" name="pass" id="pass" /> <input type="submit" name="identboton" id="identboton" value="Enviar" /></form>';
	$('#formuprofesor').html(identificacion);
}

function trim(texto){
	a = texto.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
}

function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		return (true)
	} else {
	return (false);
	}
}

