var archivoAjax_Login = "./login_procesar.php";
var archivoAjax_Logout = "./logout.php";

function fn_ListaParametrosAjax_Login (loginComentarios, loginSoluciones)
{
	var lista = "";
	
	if (loginSoluciones)
		var txt_LoginSoluciones = "true";
	else
		var txt_LoginSoluciones = "false";

	if (loginComentarios)
		var txt_LoginComentarios = "true";
	else
		var txt_LoginComentarios = "false";
		
	if (loginSoluciones)
	{
		var controlEmail = document.getElementById ("txt_Email_Soluciones");
		var controlContrasena = document.getElementById ("txt_Password_Soluciones");
	}
	else if (loginComentarios)
	{
		var controlEmail = document.getElementById ("txt_Email_2");
		var controlContrasena = document.getElementById ("txt_Password_2");
	}	
	else
	{
		var controlEmail = document.getElementById ("txt_Email");
		var controlContrasena = document.getElementById ("txt_Password");
	}

	lista = lista + "Ajax_Email=" + fn_Escape (trim (controlEmail.value));
	lista = lista + "&Ajax_Password=" + fn_Escape (trim (controlContrasena.value));
	lista = lista + "&Ajax_LoginSoluciones=" + txt_LoginSoluciones;
	lista = lista + "&Ajax_LoginComentarios=" + txt_LoginComentarios;
	
	return lista;
}

function fn_ValidarIngreso_Login (loginComentarios, loginSoluciones)
{
	if (loginSoluciones)
	{
		var controlEmail = document.getElementById ("txt_Email_Soluciones");
		var controlContrasena = document.getElementById ("txt_Password_Soluciones");
	}
	else if (loginComentarios)
	{
		var controlEmail = document.getElementById ("txt_Email_2");
		var controlContrasena = document.getElementById ("txt_Password_2");
	}	
	else
	{
		var controlEmail = document.getElementById ("txt_Email");
		var controlContrasena = document.getElementById ("txt_Password");
	}
	
	if (trim (controlEmail.value) == "")
	{
		alert ("Debe ingresar el e-mail.");
		controlEmail.focus ();
		return false;
	}	
	
	if (fn_ValidarEmailValido (controlEmail.value, false) == false)
	{
		alert ("El e-mail ingresado es inv" + String.fromCharCode (225) + "lido.");
		controlEmail.focus ();
		return false;
	}		

	if (trim (controlContrasena.value) == "")
	{
		alert ("Debe ingresar la contraseņa.");
		controlContrasena.focus ();
		return false;
	} 

	var pwdIngresada = trim (controlContrasena.value);

	if (pwdIngresada.length < 4)
	{
		alert ("La contraseņa no puede tener menos de cuatro caracteres. Por favor vuelva a ingresarla.");
		controlContrasena.value = "";
		controlContrasena.focus ();
		return false;
	}

	if (fn_ValidarSoloLetrasNumeros (pwdIngresada) == false)
	{
		alert ("La contraseņa s" + String.fromCharCode (243) + "lo puede contener letras y n" + String.fromCharCode (250) + "meros. Por favor vuelva a ingresarla.");
		controlContrasena.value = "";
		controlContrasena.focus ();
		return false;
	}		
	
	return true;
}

function fn_Ingresar_Login (loginComentarios)
{
	if (fn_ValidarIngreso_Login (loginComentarios, false))
	{
		fn_BloquearBotones ();

		var parametrosAjax = fn_ListaParametrosAjax_Login (loginComentarios, false);
		fn_InvocarAjax_Post (archivoAjax_Login, parametrosAjax);
	}
}

function fn_Ingresar_Login_Soluciones ()
{
	if (fn_ValidarIngreso_Login (false, true))
	{
		fn_BloquearBotones ();

		var parametrosAjax = fn_ListaParametrosAjax_Login (false, true);
		fn_InvocarAjax_Post (archivoAjax_Login, parametrosAjax);
	}
}

function fn_KeyPress_Login (evento, loginComentarios)
{
	if (!evento)
		var evento = window.event;

	if (evento.keyCode == 13)
		fn_Ingresar_Login (loginComentarios);
}

function fn_KeyPress_Login_Soluciones (evento)
{
	if (!evento)
		var evento = window.event;

	if (evento.keyCode == 13)
		fn_Ingresar_Login_Soluciones ();
}

function fn_LoginOk (nickname, foto)
{
	if (foto != "")
	{
		document.getElementById ("img_FotoUsuario").src = "./fotosusuarios/" + foto;

		try { document.getElementById ("td_FotoUsuario").style.display = "table-cell"; }
		catch (err) { document.getElementById ("td_FotoUsuario").style.display = "block"; };

		document.getElementById ("div_Nickname").style.width = "94px";
	}
	else
	{
		document.getElementById ("td_FotoUsuario").style.display = "none";
		document.getElementById ("div_Nickname").style.width = "144px";
	}


	document.getElementById ("span_Nickname").innerHTML = nickname;

	document.getElementById ("div_UsuarioSinLoguear").style.display = "none";
	document.getElementById ("div_UsuarioLogueado").style.display = "block";
	
	if (document.getElementById ("div_LoginParaDejarComentario"))
	{
		if (document.getElementById ("div_DejarComentario"))
			document.getElementById ("div_DejarComentario").style.display = "block";
	
		if (document.getElementById ("div_LoginParaDejarComentario"))
			document.getElementById ("div_LoginParaDejarComentario").style.display = "none";
	}

	if (document.getElementById ("div_LoginParaDejarSolucion"))
	{
		if (document.getElementById ("div_DejarSolucion"))
			document.getElementById ("div_DejarSolucion").style.display = "block";
	
		if (document.getElementById ("div_LoginParaDejarSolucion"))
			document.getElementById ("div_LoginParaDejarSolucion").style.display = "none";
	}

	if (document.getElementById ("txt_UsuarioLogueado"))
		document.getElementById ("txt_UsuarioLogueado").value = "-1";		
}

function fn_LoginIncorrecto (loginComentarios, loginSoluciones)
{
	if (loginSoluciones)
		var controlContrasena = document.getElementById ("txt_Password_Soluciones");
	else if (loginComentarios)
		var controlContrasena = document.getElementById ("txt_Password_2");
	else
		var controlContrasena = document.getElementById ("txt_Password");

	alert ("El e-mail y/o la contraseņa son incorrectos.");

	controlContrasena.value = "";
	controlContrasena.focus ();
}

function fn_LogOut_Home ()
{
	fn_BloquearBotones ();

	fn_InvocarAjax_Post (archivoAjax_Logout, "");
}

function fn_LogOutOk ()
{
	if (document.getElementById ("txt_PaginaRequiereLogin").value == "-1")
		window.location.href = document.getElementById ("txt_UrlSitio").value;
	else
	{
		document.getElementById ("txt_Email").value = "";
		document.getElementById ("txt_Password").value = "";
	
		document.getElementById ("div_UsuarioLogueado").style.display = "none";
		document.getElementById ("div_UsuarioSinLoguear").style.display = "block";
	
		if (document.getElementById ("div_LoginParaDejarComentario"))
		{
			document.getElementById ("txt_Email_2").value = "";
			document.getElementById ("txt_Password_2").value = "";
		
			if (document.getElementById ("div_DejarComentario"))
				document.getElementById ("div_DejarComentario").style.display = "none";
				
			if (document.getElementById ("div_LoginParaDejarComentario"))
				document.getElementById ("div_LoginParaDejarComentario").style.display = "block";		
		}
		
		if (document.getElementById ("div_LoginParaDejarSolucion"))
		{
			document.getElementById ("txt_Email_2").value = "";
			document.getElementById ("txt_Password_2").value = "";
		
			if (document.getElementById ("div_DejarSolucion"))
				document.getElementById ("div_DejarSolucion").style.display = "none";
				
			if (document.getElementById ("div_LoginParaDejarSolucion"))
				document.getElementById ("div_LoginParaDejarSolucion").style.display = "block";		
		}

		if (document.getElementById ("txt_UsuarioLogueado"))
			document.getElementById ("txt_UsuarioLogueado").value = "0";

		fn_HabilitarBotones ();
	}
}

