
//
// Funciones para el envio del form de registro
//

var handleSuccess_enviarFormComentarioPublicacion = function(o){
	if(o.responseText !== undefined){
				
		var messages = [];
		var mensajeError = '';
        // Use the JSON Utility to parse the data returned from the server
        try {
            messages = YAHOO.lang.JSON.parse(o.responseText);
            for(var campo in messages) { 
             		for (var razon in messages[campo])	{
             			if (messages[campo][razon] !== undefined && messages[campo][razon] != '')	
             				mensajeError += "<div id='mensajes-error'>&raquo; " + messages[campo][razon] + "</div>\n";
             				
             		} 
             }
             if (mensajeError != '')	{
             	document.getElementById("mensaje-error").innerHTML = mensajeError;
             	document.getElementById("mensaje-error").className = "mensaje-error";
             }
             else	{
             	document.getElementById('form-comentario-publicacion').submit();
             }
            
        }
        catch (x) {
            alert("JSON Parse failed! " + x);
            return;
        }
	}
};

var handleFailure_enviarFormComentarioPublicacion = function(o){
	if(o.responseText !== undefined){
		mensaje = "<li>Transaction id: " + o.tId + "</li>";
		mensaje += "<li>HTTP status: " + o.status + "</li>";
		mensaje += "<li>Status code message: " + o.statusText + "</li>";
		alert(mensaje);
	}
};

var callback_enviarFormComentarioPublicacion =
{
  success:handleSuccess_enviarFormComentarioPublicacion,
  failure:handleFailure_enviarFormComentarioPublicacion
};

function enviarFormComentarioPublicacion()	{

	// argument formId can be the id or name attribute value of the
	// HTML form, or an HTML form object.
	var formObject = document.getElementById('form-comentario-publicacion');
	YAHOO.util.Connect.setForm(formObject);
	// This example facilitates a POST transaction.
	// An HTTP GET can be used as well.
	var request = YAHOO.util.Connect.asyncRequest('POST', '/validar/formulario', callback_enviarFormComentarioPublicacion);
	
	return false;
}


///////////////////////////////

var handleSuccess_toogleNotificaComentario = function(o){
	if(o.responseText !== undefined){
				
		var messages = [];
		var mensajeError = '';
        // Use the JSON Utility to parse the data returned from the server
        try {
            messages = YAHOO.lang.JSON.parse(o.responseText);
        }
        catch (x) {
            alert("JSON Parse failed! " + x);
            return;
        }
	}
};

var handleFailure_toogleNotificaComentario = function(o){
	if(o.responseText !== undefined){
		mensaje = "<li>Transaction id: " + o.tId + "</li>";
		mensaje += "<li>HTTP status: " + o.status + "</li>";
		mensaje += "<li>Status code message: " + o.statusText + "</li>";
		alert(mensaje);
	}
};

var callback_toogleNotificaComentario =
{
  success:handleSuccess_toogleNotificaComentario,
  failure:handleFailure_toogleNotificaComentario
};

function toogleNotificaComentario(id_publicacion)	{
	var request = YAHOO.util.Connect.asyncRequest('GET', '/publicacion/notificarusuario/id/' + id_publicacion, callback_toogleNotificaComentario);
}

///////////////////////////////

var handleSuccess_toogleFavorito = function(o){
	if(o.responseText !== undefined){
				
		var messages = [];
		var mensajeError = '';
        // Use the JSON Utility to parse the data returned from the server
        try {
            messages = YAHOO.lang.JSON.parse(o.responseText);
            
            if (messages['respuesta'] == "AGREGADO")	{
	        	document.getElementById("texto_favoritos").innerHTML	= "<a href=\"javascript:toogleFavoritos(" + messages['id_publicacion'] + ",true);\">Borrar de mis favoritos</a>";
	        }
	        else if (messages['respuesta'] == "ELIMINADO")	{
	        	document.getElementById("texto_favoritos").innerHTML	= "<a href=\"javascript:toogleFavoritos(" + messages['id_publicacion'] + ",true);\">Aņadir a mis favoritos</a>"; 
	        }
            
        }
        catch (x) {
            alert("JSON Parse failed! " + x);
            return;
        }
	}
};

var handleFailure_toogleFavorito = function(o){
	if(o.responseText !== undefined){
		mensaje = "<li>Transaction id: " + o.tId + "</li>";
		mensaje += "<li>HTTP status: " + o.status + "</li>";
		mensaje += "<li>Status code message: " + o.statusText + "</li>";
		alert(mensaje);
	}
};

var callback_toogleFavorito =
{
  success:handleSuccess_toogleFavorito,
  failure:handleFailure_toogleFavorito
};

function toogleFavoritos(id_publicacion, estaAutenticado)	{
	if (estaAutenticado == false) {
		window.location.href= '/login/formlogin';
	}
	else {
		var request = YAHOO.util.Connect.asyncRequest('GET', '/publicacion/tooglefavorito/id/' + id_publicacion, callback_toogleFavorito);
	}
}

////////////////////////////////



var handleSuccess_toogleVoto = function(o){
	if(o.responseText !== undefined){
				
		var messages = [];
		var mensajeError = '';
        // Use the JSON Utility to parse the data returned from the server
        try {
            messages = YAHOO.lang.JSON.parse(o.responseText);
            
            if (messages['respuesta'] == "AGREGADO")	{
	        	document.getElementById("texto_votos").innerHTML	= "Ya has votado";
	        }
	        else if (messages['respuesta'] == "ELIMINADO")	{
	        	document.getElementById("texto_votos").innerHTML	= "<a href=\"javascript:toogleVoto(" + messages['id_publicacion'] + ",true);\">Votar</a>"; 
	        }
        }
        catch (x) {
            alert("JSON Parse failed! " + x);
            return;
        }
	}
};

var handleFailure_toogleVoto = function(o){
	if(o.responseText !== undefined){
		mensaje = "<li>Transaction id: " + o.tId + "</li>";
		mensaje += "<li>HTTP status: " + o.status + "</li>";
		mensaje += "<li>Status code message: " + o.statusText + "</li>";
		alert(mensaje);
	}
};

var callback_toogleVoto =
{
  success:handleSuccess_toogleVoto,
  failure:handleFailure_toogleVoto
};

function toogleVoto(id_publicacion, estaAutenticado)	{
	if (estaAutenticado == false) {
		window.location.href= '/login/formlogin';
	}
	else {
		var request = YAHOO.util.Connect.asyncRequest('GET', '/publicacion/tooglevoto/id/' + id_publicacion, callback_toogleVoto);
	}
}


////////////////////////////////



var handleSuccess_toogleGrupoVoto = function(o){
	if(o.responseText !== undefined){
				
		var messages = [];
		var mensajeError = '';
        // Use the JSON Utility to parse the data returned from the server
        try {
            messages = YAHOO.lang.JSON.parse(o.responseText);
            
            if (messages['respuesta'] == "AGREGADO")	{
	        	document.getElementById("texto_votos").innerHTML	= "Ya has votado";
	        }
	        else if (messages['respuesta'] == "ELIMINADO")	{
	        	document.getElementById("texto_votos").innerHTML	= "<a href=\"javascript:toogleVoto(" + messages['id_publicacion'] + ",true);\">Votar</a>"; 
	        }
        }
        catch (x) {
            alert("JSON Parse failed! " + x);
            return;
        }
	}
};

var handleFailure_toogleGrupoVoto = function(o){
	if(o.responseText !== undefined){
		mensaje = "<li>Transaction id: " + o.tId + "</li>";
		mensaje += "<li>HTTP status: " + o.status + "</li>";
		mensaje += "<li>Status code message: " + o.statusText + "</li>";
		alert(mensaje);
	}
};

var callback_toogleGrupoVoto =
{
  success:handleSuccess_toogleGrupoVoto,
  failure:handleFailure_toogleGrupoVoto
};

function toogleGrupoVoto(id_grupo, estaAutenticado)	{
	if (estaAutenticado == false) {
		window.location.href= '/login/formlogin';
	}
	else {
		var request = YAHOO.util.Connect.asyncRequest('GET', '/grupo/tooglevoto/id/' + id_grupo, callback_toogleGrupoVoto);
	}
}

///////////////////////////////

var handleSuccess_GrupoFavorito = function(o){
	if(o.responseText !== undefined){
				
		var messages = [];
		var mensajeError = '';
        // Use the JSON Utility to parse the data returned from the server
        try {
            messages = YAHOO.lang.JSON.parse(o.responseText);
            
            if (messages['respuesta'] == "AGREGADO")	{
	        	document.getElementById("texto_favoritos").innerHTML	= "<a href=\"javascript:toogleGrupoFavoritos(" + messages['id_grupo'] + ",true);\">Borrar de mis favoritos</a>";
	        }
	        else if (messages['respuesta'] == "ELIMINADO")	{
	        	document.getElementById("texto_favoritos").innerHTML	= "<a href=\"javascript:toogleGrupoFavoritos(" + messages['id_grupo'] + ",true);\">Aņadir a mis favoritos</a>"; 
	        }
            
        }
        catch (x) {
            alert("JSON Parse failed! " + x);
            return;
        }
	}
};

var handleFailure_GrupoFavorito = function(o){
	if(o.responseText !== undefined){
		mensaje = "<li>Transaction id: " + o.tId + "</li>";
		mensaje += "<li>HTTP status: " + o.status + "</li>";
		mensaje += "<li>Status code message: " + o.statusText + "</li>";
		alert(mensaje);
	}
};

var callback_toogleGrupoFavorito =
{
  success:handleSuccess_GrupoFavorito,
  failure:handleFailure_GrupoFavorito
};

function toogleGrupoFavoritos(id_grupo, estaAutenticado)	{
	if (estaAutenticado == false) {
		window.location.href= '/login/formlogin';
	}
	else {
		var request = YAHOO.util.Connect.asyncRequest('GET', '/grupo/tooglefavorito/id/' + id_grupo, callback_toogleGrupoFavorito);
	}
}


////////////////////////////////




function enviar_publicacion(id_publicacion)	{
	url_compartir = document.getElementById('form-acciones').url_compartir_encoded.value;

	window.open ("/publicacion/formularioenviar/?url=" + url_compartir,"mywindow","width=350,height=200"); 

}



