var ajaxLoginReference = undefined;

function manageLoginStatusDisplay() {	
	if(ajaxLoginReference.data == "false") {
		setLoggedOutDisplay();
	} else {
		setLoggedInDisplay();
	}
}

function setLoggedOutDisplay(){
	var topMessageWelcome = document.getElementById('topMessageWelcome');
	var topMessageUsername = document.getElementById('topMessageUsername');
	var topMessageNot = document.getElementById('topMessageNot');
	var topMessageLinkTextURL = document.getElementById('topMessageLinkTextURL');
	var topMessageLinkText = document.getElementById('topMessageLinkText');
	
	if(topMessageWelcome == undefined)
		return;
	
	var headerAccountLink = document.getElementById('headerAccountLink');
	var headerHelp = document.getElementById('headerHelp');
	var headerSupport = document.getElementById('headerSupport');
	
	jQuery("#topMessageWelcome").hide();//topMessageWelcome.style.display = 'none';
	jQuery("#topMessageNot").show();//topMessageNot.style.display = 'inline'; //'visible';
	topMessageLinkTextURL.href = "/login/";
	removeNodeChildren(topMessageLinkText);
	topMessageLinkText.appendChild(document.createTextNode("Login"));	
	
	headerAccountLink.href="/login/";
	if(headerHelp != undefined)
		jQuery("#headerHelp").show();//headerHelp.style.display = 'inline'; //keeping this in always for now 'none';
	if(headerSupport != undefined)
		jQuery("#headerSupport").hide();//headerSupport.style.display = 'none';
}

function setLoggedInDisplay(){
	var topMessageWelcome = document.getElementById('topMessageWelcome');
	var topMessageUsername = document.getElementById('topMessageUsername');
	var topMessageNot = document.getElementById('topMessageNot');
	var topMessageLinkTextURL = document.getElementById('topMessageLinkTextURL');
	var topMessageLinkText = document.getElementById('topMessageLinkText');
	
	if(topMessageWelcome == undefined)
		return;
	
	var headerAccountLink = document.getElementById('headerAccountLink');
	var headerHelp = document.getElementById('headerHelp');
	var headerSupport = document.getElementById('headerSupport');
	
	jQuery("#topMessageWelcome").show();//topMessageWelcome.style.display = 'inline'; //'visible';
	removeNodeChildren(topMessageUsername);
	topMessageUsername.appendChild(document.createTextNode(ajaxLoginReference.data));
	jQuery("#topMessageNot").hide();//topMessageNot.style.display = 'none';
	topMessageLinkTextURL.href = "/logout/";
	removeNodeChildren(topMessageLinkText);
	topMessageLinkText.appendChild(document.createTextNode("Logout"));
	
	if(headerAccountLink != undefined)
		headerAccountLink.href="/account/";
	if(headerHelp != undefined)
		jQuery("#headerHelp").show();//headerHelp.style.display = 'inline'; //'visible';
	if(headerSupport != undefined)
		jQuery("#headerHelp").hide();//headerSupport.style.display = 'inline'; //'visible';
}

function getLoginStatus(){
	ajaxLoginReference = checkLoginStatus(function(){manageLoginStatusDisplay()});
}

function logOut(){
	setLoggedOutDisplay();
	doLogout();
}

function conditionalGetLoginStatus(){
	hideDummy();
	if(logoutPage == true){
		logOut();
	} else{
		getLoginStatus();
	}
}

//addOnloadFunction(function(){conditionalGetLoginStatus()});
jQuery(document).ready(function(){conditionalGetLoginStatus()});