/* Code for logging in a user via the login box on the header of every page */

var request_in_progress = false;			// Set this flag when there's a transaction in progress
var animation_timer = null;					// Use this to keep track of the next animation timeout
var animation_delay = 0.2;					// In seconds
var animation_color_counter = 1;			// This will allow us to cycle through the animation colors

// These are the field names for the login form
var username_fieldname = "lb_username";
var password_fieldname = "lb_userpass";
var remember_fieldname = "lb_remember";

// These are the colors we cycle through for the "Logging In..." animation
var login_animation_colors = new Array(
	"ffffff",
	"ffcccc",
	"ff9999",
	"ff6666",
	"ff3333",
	"ff0000"
);

// These are URLs that we redirect away from if the user is on them when they log out
var logout_exclusion_pages = new Array(
	"/account/.*",
	"/pm/.*"
);

function AttemptLogin()
{
	if(request_in_progress)		// Don't let them kick off simultaneous requests
	{
		return;
	}
	
	// First check for both fields being filled in
	var username_field = document.getElementById(username_fieldname);
	var password_field = document.getElementById(password_fieldname);
	
	if(username_field.value == "")
	{
		alert("Please enter your Grounds Gold username.");
		username_field.focus();
		return;
	}
	else if(password_field.value == "")
	{
		alert("Please enter your Grounds Gold password.");
		password_field.focus();
		return;
	}
	
	// Note that we're doing a request
	request_in_progress = true;
	
	// OK, if we're here, we've got a username and a pw
	var params = new Array();
	params["u"] = username_field.value;
	params["p"] = password_field.value;
	
	// See if they want to be remembered
	if(document.getElementById(remember_fieldname).checked)
	{
		params["r"] = 1;
	}
	
	// Swap out our "Login" button to show the "logging in" animation
	DoLoginAnimation("login");
	DivSwap("loginbox_button", "loginbox_animation_login");
	
	// Now do our HTTP request to see if this guy's valid
	var ajax = new AjaxRequest("/ajax/login.php");
	ajax.Send(params, HandleLoginResponse, LoginCleanup);
}

function AttemptLogout()
{
	if(request_in_progress)		// Don't let them kick off simultaneous requests
	{
		return;
	}

	request_in_progress = true;
	
	DoLoginAnimation("logout");
	DivSwap("loginbox_loggedin_msg", "loginbox_animation_logout");
	
	// Do the AJAX - this is a lightweight request
	var ajax = new AjaxRequest("/ajax/logout.php");
	ajax.Send(null, HandleLogoutResponse, LogoutCleanup);
}

function HandleLoginResponse(response)
{
	request_in_progress = false;
	
	// Check to see if we need to reload the page or not - currently, only reload for dynamic pages (PHP)
	if(IsDynamicPage())
	{
		// Let the page refresh take care of changing the loginbox to reflect being logged in
		// Note - refresh it this way, not using .reload(), to get rid of any lingering POST
		window.location.href = window.location.href;

		// Let's return here to keep the "Logging In..." animation running until page refresh occurs
		return;
	}
	else		// Just swap the loginbox around ourselves and stay right here
	{
		// Put their name into the form
		SetLoggedInUsername(response.GetField("username"));
		
		// Also slap in the # of unread PMs
		SetUnreadPMCount(response.GetField("pm_count"));
		
		// Clear out the password field - it'll be hidden, but you can't be too safe
		document.getElementById(password_fieldname).value = "";
		
		// Now swap in the "logged in" div
		DivSwap("loginbox_notloggedin", "loginbox_loggedin");
	}
	
	// End our animation and allow the user to interact again
	ResetLoginAnimation("login");
}

function LoginCleanup(error_code)
{
	// If they're not validated, give them the chance to reset stuff
	if((typeof error_code != "undefined") && (error_code == 3))		// Matches up with user.php
	{
		if(confirm("If you never received the Newgrounds validation e-mail, or need to change your e-mail address, click \"OK\"."))
		{
			window.location.href = "http://redirect.ngfiles.com/join/resend";
			return;
		}
	}
	
	request_in_progress = false;
	ResetLoginAnimation("login");
	document.getElementById(password_fieldname).value = "";
	document.getElementById(password_fieldname).focus();
}

function HandleLogoutResponse(response)
{
	request_in_progress = false;
	
	// OK, if we're here, the logout was successful.	
	if(IsExclusionPage())
	{
		// Redirect to the front page after logging out
		window.location.href = "/";
		return;
	}
	else if(IsDynamicPage())
	{
		// Let the page refresh take care of changing the loginbox to reflect being logged in
		// Note - refresh it this way, not using .reload(), to get rid of any lingering POST
		window.location.href = window.location.href;
		return;
	}
	else
	{
		// Just swap the loginbox around ourselves and stay right here
		DivSwap("loginbox_loggedin", "loginbox_notloggedin");
		ResetLoginAnimation("logout");
	}
}

function LogoutCleanup()
{
	request_in_progress = false;
	ResetLoginAnimation("logout");
}

function IsDynamicPage()
{
	// page_is_dynamic is a global defined in the header
	return(page_is_dynamic);
}

function IsExclusionPage()
{
	for(var i=0; i<logout_exclusion_pages.length; i++)
	{
		if(window.location.pathname.match(logout_exclusion_pages[i]))
		{
			return(true);
		}
	}
	
	return(false);
}

function DoLoginAnimation(animation_type)
{
	// First set the color
	var loginmsg_div = document.getElementById("loginbox_animation_" + animation_type);
	loginmsg_div.style.color = "#" + login_animation_colors[animation_color_counter];
	
	// Also see if we need to start over with our list of animation colors
	if(animation_color_counter == (login_animation_colors.length - 1))
	{
		animation_color_counter = 0;
	}
	else
	{
		animation_color_counter++;
	}
	
	// Call ourselves to keep the animation going
	animation_timer = setTimeout("DoLoginAnimation('" + animation_type + "')", animation_delay * 1000);
}

function ResetLoginAnimation(animation_type)
{
	// Reset the color
	var loginmsg_div = document.getElementById("loginbox_animation_" + animation_type);
	loginmsg_div.style.color = "#" + login_animation_colors[0];
	animation_color_counter = 1;
	
	// Cleanup our display to get rid of the animation text
	if(animation_type == "login")
	{
		DivSwap("loginbox_animation_login", "loginbox_button");
	}
	else if(animation_type == "logout")
	{
		DivSwap("loginbox_animation_logout", "loginbox_loggedin_msg");
	}
}
