////////////////////////////////////////////////////////////////////
// This is called from LOGIN-REGISTER.PHP
////////////////////////////////////////////////////////////////////

var js_username_cookie = "";
var js_password_cookie = "";
var js_username;
var js_password;
var js_loginCallback = null;
var our_domain = document.domain;

function setLoginCallback(cb)
{
	js_loginCallback = cb;
}

function setCookieNames(u,p)
{
	js_username_cookie = u;
	js_password_cookie = p;
}

function crumbleCookies()
{
	// Delete login cookies
	userCookie = js_username_cookie + "=; domain=" + our_domain + "; expires=0; path=/";
	passCookie = js_password_cookie + "=; domain=" + our_domain + "; expires=0; path=/";

	document.cookie = userCookie;
	document.cookie = passCookie;
}

function bakeCookiesFromScratch(u,md5)
{
	// Refresh cookies upon new login
	nextyear = new Date();
	nextyear.setFullYear(nextyear.getFullYear() + 1);
	
	userCookie = js_username_cookie + "=" + u + "; domain=" + our_domain + "; expires=" + nextyear.toGMTString() + "; path=/";
	passCookie = js_password_cookie + "=" + md5 + "; domain=" + our_domain + "; expires=" + nextyear.toGMTString() + "; path=/";
	
	document.cookie = userCookie;
	document.cookie = passCookie;
}

function bakeCookies()
{
	// Refresh cookies upon new login
	nextyear = new Date();
	nextyear.setFullYear(nextyear.getFullYear() + 1);
	
	userCookie = js_username_cookie + "=" + js_username + "; domain=" + our_domain + "; expires=" + nextyear.toGMTString() + "; path=/";
	passCookie = js_password_cookie + "=" + strings[1] + "; domain=" + our_domain + "; expires=" + nextyear.toGMTString() + "; path=/";
	
	document.cookie = userCookie;
	document.cookie = passCookie;
}

function refreshLoginArea(theText)
{
	document.getElementById("login-register").innerHTML = theText;
}

function getMainMenu()
{
	sendAjaxCallback("/login-ajax.php?action=refreshLoginArea", refreshLoginArea );
}

function loginResponse(theText)
{
	strings = theText.split(";");
	code = strings[0].trim();
	
	if( code == "success" )
	{
		bakeCookies();
		sendAjaxCallback("/login-ajax.php?action=refreshLoginArea&loggedin=1&username=" + js_username, refreshLoginArea );
		if( js_loginCallback )
			js_loginCallback(true);
	}
	else // failure
	{
		alert(theText);
		crumbleCookies();
		document.getElementById("username").value = "";
		document.getElementById("password").value = "";
		if( js_loginCallback )
			js_loginCallback(false);
		alert( "Login failed!\n\nIncorrect username and/or password!\n\nPossibly account has not yet been activated." );
	}
}

function login()
{
	js_username = escape(document.getElementById("username").value);
	js_password = escape(document.getElementById("password").value);
	
	requestUrl = "/login-ajax.php?action=login&name=" + js_username;
	requestData = "password=" + js_password;
	sendAjaxPostCallback( requestUrl, loginResponse, requestData );
	return false;
}

function logout()
{
	crumbleCookies();
	sendAjaxCallback("/login-ajax.php?action=refreshLoginArea&loggedin=-1&username=" + js_username, refreshLoginArea );
	if( js_loginCallback )
		js_loginCallback(false);
}

