var showingLoggedIn = false;
var messageElement = false;
var messageP = false;

// focusField method: called when username and password gain focus
function focusField()
{
	try {
		if (messageElement != false)
		{
			document.getElementById('message').removeChild(messageElement);
		}
		getSeed();
	}
	catch (e)
	{ } // do nothing... hides an apparent firefox bug: https://bugzilla.mozilla.org/show_bug.cgi?id=236791
}

// blurField method: called when username and password are blurred
function blurField()
{
	try {
		validateLogin();
	}
	catch (e)
	{ } // hide bug https://bugzilla.mozilla.org/show_bug.cgi?id=236791
}

// showLogin method: displays if necessariy that we are logged in
function showLogin() {
	if (loggedIn)
	{
		//alert(redo);
		//upload attraction
		if(redo == 'upla' || redo == 'upl' || redo == 'mkr')
			window.location = 'upload/index.php?redo=upla';
		//upload photos
		else if(redo == 'uplp')
			window.location = 'upload/index.php?redo=uplp';
		//upload videos
		else if(redo == 'uplv')
			window.location = 'upload/index.php?redo=uplv';
		//add nearby	
		else if(redo == 'upln')
			window.location = 'upload/index.php?redo=upln';
		//add nearby	
		else if(redo == 'uplnu')
			window.location = 'upload/index.php?redo=uplnu';
		//directly login
		else if(redo == 'log')
			window.location = 'myplaces/loggedin.php';			
		//for ASK  - chat for answer
		//else if(redo == 'chat_for_answer')	
			//window.location = 'ask/index.php?redo=cfa';
		else if(redo == 'create_a_ask')	
			window.location = 'upload/index.php?redo=cra';
		//for Itinerary
		else if(redo == 'create_a_itinerary')	
			window.location = 'upload/index.php?redo=iti';			
		//for myplaces - my_account
		else if(redo == 'my_account')	
			window.location = 'myplaces/index.php?redo=mac';
		//for myplaces - my_answer
		else if(redo == 'my_answer')	
			window.location = 'myplaces/index.php?redo=man';
		//for myplaces - my_ask
		else if(redo == 'my_ask')	
			window.location = 'myplaces/index.php?redo=mas';
		//for myplaces - my_attraction
		else if(redo == 'my_attraction')	
			window.location = 'myplaces/index.php?redo=mat';
		//for myplaces - my_itinerary
		else if(redo == 'my_itinerary')	
			window.location = 'myplaces/index.php?redo=mit';
		//for myplaces - my_favorite
		else if(redo == 'my_favorite')	
			window.location = 'myplaces/index.php?redo=mfa';
		//for myplaces - my_photo
		else if(redo == 'my_photo')	
			window.location = 'myplaces/index.php?redo=mph';
		//for myplaces - my_video
		else if(redo == 'my_video')	
			window.location = 'myplaces/index.php?redo=mvi';
			
		//for help - report a bug
		else if(redo == 'rptbug')	
			window.location = 'help/index.php?redo=rptbug';
		//for help - send feedback
		else if(redo == 'feedback')	
			window.location = 'help/index.php?redo=feedback';
		//for make a request
		//else if(redo == 'mkr')	
			//window.location = 'attraction/index.php?redo=mkr';
		//the default page
		else
			window.location = 'myplaces/loggedin.php';

	}
	
	/*	
	if (messageElement != false)
	{
		try {
			document.getElementById('message').removeChild(messageElement);
		}
		catch (e) { }
	}
	
	if (loggedIn)
	{
		showingLoggedIn = true;
		
		loginPanel = document.getElementById('login');
		p = document.createElement('p');
		pre = document.createTextNode('Logged in as ');
		strong = document.createElement('strong');
		strong_text = document.createTextNode(fullname);
		strong.appendChild(strong_text);
		mid = document.createTextNode(' [');
		a = document.createElement('a');
		a.href='javascript:logout();';
		a_text = document.createTextNode('logout');
		a.appendChild(a_text);
		post = document.createTextNode(']');
		
		p.appendChild(pre);
		p.appendChild(strong);
		p.appendChild(mid);
		p.appendChild(a);
		p.appendChild(post);
		
		messageP = document.getElementById('message');
		loginPanel.removeChild(messageP);
		loginPanel.appendChild(p);
		
		messageElement = p;
		
		document.getElementById('username').disabled = 'disabled';
		document.getElementById('password').disabled = 'disabled';
		
		document.getElementById('comments').focus();
	}
	else
	{
		messageElement = document.createElement('strong');
		messageElement.appendChild(document.createTextNode(' ' + messages));
		messageElement.style.color = '#ff0000';
		document.getElementById('message').appendChild(messageElement);
	} */
	
	
}

// logout method: prepares for a new login
function logout()
{
	resetLogin();
	username = document.getElementById('username');
	password = document.getElementById('password');
	loginPanel = document.getElementById('login');
	username.value = '';
	password.value = '';
	username.disabled = null;
	password.disabled = null;
	
	loginPanel.removeChild(messageElement);
	loginPanel.appendChild(messageP);
	
	messageElement = false;
	showingLoggedIn = false;
	
	username.focus();
}

// setupLogin method: to be called on page load, sets up the login script
function setupLogin()
{
	username = document.getElementById('username');
	password = document.getElementById('password');
	addEvent(username, 'focus', focusField);
	//addEvent(username, 'blur', blurField);
	addEvent(password, 'focus', focusField);
	//addEvent(password, 'blur', blurField);	
}

// function to add events to the page in a cross-browser manner
function addEvent(objObject, strEventName, fnHandler) { 
	// DOM-compliant way to add an event listener 
 	if (objObject.addEventListener) {
		objObject.addEventListener(strEventName, fnHandler, false); 
	}
	// IE/windows way to add an event listener 
	else if (objObject.attachEvent) {
		objObject.attachEvent('on' + strEventName, fnHandler); 
	}
}
addEvent(window,'load',setupLogin);
