// SINOPSIS
// Funciones javascript para interactuar con funcionalidad evento voy
// ASSUMPTIONS & BUGS
// BITACORA
// GB 4-6-2009 Creacion

// SINOPSIS 
// Prende el boton de evento voy
// BITACORA
// GB 4-6-2009 Creacion
function EventoVoyInit()
{
	// alert ("En EventoVoyInit");
 	var element = document.getElementById("boton-evento-voy");
	element.style.display = "block";
}

// SINOPSIS 
// Registra que el usuario va al evento en esa fecha
// INPUT
// idEvento - al que el usuario dice que va a ir
// fecha - fecha del evento, en formato yyyy-mm-dd
// idUsuario - el id del usuario que va al evento
// OUTPUT
// > 0 - Cantidad de personas que van al evento
// = 0 - Error, no pudo sumar (siempre deberia ser por lo menos uno!)
// ASSUMPTIONS & BUGS
// Debe estar logueado el usuario (idUsuario distinto de cero). Asumimos que al llegar aca lo esta.
// BITACORA
// GB 4-6-2009 Creacion
// GB 8-6-2009 Asumimos que esta logueado el usuario
// GB 9-6-2009 Le cambio el nombre y orden parametros
// GB 11-6-2009 Agrego el usuario como parametro para pasar a la barra
// function onEventoVoy( idUsuario, idEvento, fecha )
function onEventoVoy( idUsuario, idEvento, fechaEvento, usuario )
{
	// GB 8-6-2009 por las dudas lo dejamos.... idUsuario no deberia ser 0
	// alert ("On onEventoVoy("+idUsuario+","+idEvento+","+fechaEvento+","+usuario+")");
	if ( idUsuario == 0 )
	{
		mensajeDeLogin();
		return;
	}
	
	var ajax = creaAjax();
	if ( ajax == false )
	{
		postAccionEventoVoyFailed( "-1" );
		return;
	}

	ajax.onreadystatechange = function() 
	{ 
		var response;

		if (ajax.readyState == 4)
	  	{
			response = analyzeResponse( ajax.responseText );
			// alert("response = " + response );
			if ( response < 0 )
				postAccionEventoVoyFailed( response );
			else
				postAccionEventoVoyOk( response );
			// Gb 11-6-2009 agrego esto aca para cerrar el form de login si existe!
			// alert( "antes de closeLoginForm");
			closeLoginForm( usuario );
      	} 
    }

	var url = "v2-grabar-voy-al-evento.php?idEvento="+idEvento+"&fechaEvento="+fechaEvento+"&idUsuario="+idUsuario;	 
	ajax.open("GET", url, true ); 
    ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    ajax.send(null);
}

// SINOPSIS
// Acciones posteriores a una accion exitosa
// INPUT
// Response, viene la cantidad de personas que van al evento en la fecha
// ASSUMPTION & BUGS
// Sacar la imagen del boton solo cuando la respuesta sea correcta
// BITACORA
// GB 4-6-2009 Creacion
function postAccionEventoVoyOk( response )
{
	// Cambio cantidad de personas
	// alert( "En postAccionEventoVoyOk("+ response +")");
	var element = document.getElementById('msg-evento-voy');
	// gb 8-6 Parche, cambiar
	if ( element == null )
		element = parent.document.getElementById('msg-evento-voy');
	// alert( element );
	// fin 8-6
	if ( element != null )
	{ 
		var persona = "persona";
		var va = "va";
		if (response > 1)
		{
			persona +="s";
			va += "n";
		}	
		element.innerHTML = response + " " + persona + " " + va + "!";
	}
	
	apagarBotonEventoVoy();
}


// SINOPSIS
// Apago boton de evento voy
// INPUT
// none
// ASSUMPTION & BUGS
// BITACORA
// GB 16-6-2009 Creacion
function apagarBotonEventoVoy()
{
	// Saco la imagen del boton
	// Ver que sea si la respuesta fue ok!
	element = document.getElementById('boton-evento-voy');
	// gb 8-6- parche, cambiar
	if ( element == null )
		element = parent.document.getElementById('boton-evento-voy');
	// alert( element );
	// fin 8-6
	if ( element != null )
		element.style.display = "none";
}

// SINOPSIS
// Acciones posteriores a una accion que falla
// INPUT
// Response, viene la cantidad de personas que van al evento en la fecha
// ASSUMPTION & BUGS
// BITACORA
// GB 11-6-2009 Creacion
function postAccionEventoVoyFailed( response )
{
	// alert( "En postAccionEventoVoyFailed, response = " + response);
	var msg = "Error";
	if ( response == -1 || response == 0 )
		msg = "Oops. Problemas. Por favor proba de nuevo.";
	else
	if ( response == -2 )
		msg = "Ya estas registrado en esta fecha!";
	mostrarMensajeEventoVoy( msg );
	apagarBotonEventoVoy();
}

// SINOPSIS
// Muestra mensaje de evento voy
// INPUT
// Response, viene la cantidad de personas que van al evento en la fecha
// ASSUMPTION & BUGS
// BITACORA
// GB 16-6-2009 Creacion
function mostrarMensajeEventoVoy( msg )
{
	element = document.getElementById('msg-evento-voy-error');
	if ( element == null )
		element = parent.document.getElementById('msg-evento-voy-error');
	if ( element != null )
	{
		element.style.display =  "block";
		element.innerHTML = msg;
	}
}

// SINOPSIS
// Abre un shadowbox que indica que debes estar logueado para usar esta funcionalidad
// BITACORA
// gb 4-6-2009 creacion
function mensajeDeLogin()
{
	// gb 8/6/2009 - intentamos abrir php de login desde aca
	var content = '<div style="background-color:#ffffff; height:100px; width: 250px; font-size:13px; padding:10px; text-align:center">Lo sentimos, debes estar logueado para poder registrar que vas!<br/><br/>Hacelo desde la barra superior por favor, o registrate.</div>';
	var player = "html";
	var title = "Login!";
	openShadowbox(content, player, title);
}

// SINOPSIS
// INPUT
// Response text
// OUTPUT
// 1, OK
// BITACORA
// gb 8-6-2009 Sacar, no se usa.
function analyzeResponse( response )
{
	// alert( "En analyzeResponse, response = "+ response);
	valor = parseInt(trim(response));
	// gb 11-6 aca solo puede dar cero
	if ( valor == 'NaN' )
		valor = 0;
	return valor;
}

	
	


