function getBrowser()	{
	if(window.navigator.userAgent.indexOf ("Opera") >= 0)
	{
		return 'Opera';
	}
	else if (window.navigator.userAgent.indexOf ("Gecko") >= 0) // (Mozilla, Netscape, FireFox, Chrome, Safari?)
	{
		return 'Gecko';
	}
	else if (window.navigator.userAgent.indexOf ("MSIE") >= 0)
	{
		return 'IE';
	} 
	else
	{
		return window.navigator.userAgent;
	}
}

var oldHash;
var loggedIn = false;
var notSaved = 0;
var checkInterval;
var logoutMessage = "Are you sure you want to leave?";
	
function backInit(message)
{
	logoutMessage = message;
}

function unloadHandler()
{
	if(notSaved > 0)
		return logoutMessage;
	else 
		return null;
}
	  
function setLoggedIn (value)
{
	loggedIn = value;
	if (getBrowser() == 'Opera')
	{
		if (loggedIn)
			location.hash = (initialHash == "#in") ? "" : "#in";
		else
			history.back();
			
		if (!loggedIn && checkInterval)
		{
			notSaved = 1;
			setNotSaved(false);
		}
	}
}

function setNotSaved (value)
{
	if (value)
		notSaved++;
	else
		notSaved--;
		
	if (notSaved < 0)
		notSaved = 0;
		
	if(getBrowser() == 'Opera')
	{	
		if (notSaved == 0 && checkInterval)
		{
			clearInterval(checkInterval);
		}	
		else if(!checkInterval)
		{
			oldHash = location.hash;
			checkInterval = setInterval(checkHash, 50);
		}
	}
	else
	{
		window.onbeforeunload = (notSaved > 0) ? unloadHandler : null;
	}
}

function checkHash ()
{
	if (oldHash != location.hash)
	{
		result = confirm(logoutMessage);
		if (result)
		{
			notSaved = 1;
			setNotSaved(false);
			
			oldHash = location.hash;
			setTimeout(location.back, 300);
		}
		else
		{
			history.forward();  
		}
	}
 }
