/**
 * @author John Schneider
 * @copyright 2008 Reservoir Creative Group, LLC
 */
//set this here so we can change it if we need to.
var LoginUrl = 'Code/login.php';

function FollowUploadArtLink() {
	var url = "Code/login.php?q=check";
	var text = ResAjax.GetText(url, false);
	if (text) {
		window.location.href = "welcome.php?q=a";
	}
	else {
		alert("You must log in to upload artwork.");
	}
}
		

/**
 * Logs in a user.
 */
function Login()
{
	if (document.getElementById('loginUname').value.replace(' ', '').length == 0) {
		alert('You must specify a username and password.');
	}
	else {
		var text = ResAjax.GetText(LoginUrl, false, 'uname=' + document.getElementById('loginUname').value + '&passw=' + document.getElementById('loginPassw').value);
		Direct(text);
	}
}

/**
 * Logs a user out.
 */
function Logout()
{
	var text = ResAjax.GetText(LoginUrl + '?q=logout', false, false);
	Direct(text);
}

/**
 * Checks to see if a user is logged in and goes straight to the logged in view if so.
 */
function Check()
{
	var text = ResAjax.GetText(LoginUrl + '?q=check');
	Direct(text);
}

/**
 * Directs a user to the logged in view if he is logged in.
 * @param {Object} uname
 */
function Direct(uname)
{
	if (uname == "Denied") {
		alert("Invalid username/password combination.");
	}
	else if (uname == "Banned") {
		alert("That account has been banned.  Sorry.");
	}
	else if (uname != "Denied" && uname) {
		var bandLinks = ResAjax.GetText('checkInBand.php');
		var linksMenu =
					'<ul id="accountNav">'+
						'<li style="color: #fff; font-weight: bold;">Welcome, ' + uname + '.</li>'+
						//+'<li><a href="welcome.php">My Home</a></li>'
						'<li><a href="welcome.php?q=ep">My Profile</a></li>';
		if(bandLinks == 1)
		{
			linksMenu +=
						'<li><a href="welcome.php?q=eb">My Band/Artist Profile</a></li>'+
						'<li><a href="welcome.php?q=ms">My Music Store</a></li>';
		}
						
		linksMenu +=
						'<li><a href="welcome.php?q=a">My Artwork</a></li>'+
						'<li><a href="welcome.php?q=h">Order History</a></li>'+
						//+'<li><a href="javascript:void(0);" onclick="javascript:Logout();" class="menulink">Logout</a></li>'
						'<li><a href="Code/logout.php" class="menulink">Logout</a></li>'+
					'</ul>';
		document.getElementById('login').innerHTML = linksMenu;
	}
	else {
		document.getElementById('login').innerHTML = 
				'<table>' +
					'<tr>' +
						'<td><span style="color:#FFFFFF">username:</span></td>' +
						'<td><input type="text" id="loginUname" class="gborder" /></td>' +
						'<td><span style="color:#FFFFFF">password:</span></td>' +
						'<td><input type="password" id="loginPassw" class="gborder" onkeypress="return loginViaEnter(event)" /></td>' +
						'<td><a href="javascript:void(0);"><img src="Images/Site/login.png" alt="Login" onclick="Login();" class="button" /></a></td>' +
						'<td><a href="addaccount.php"><img src="Images/Site/createaccount.png" alt="Create Account" class="button" /></a></td>' +
						'<td><a href="forgotLogin.php"><img src="Images/Site/forgotLogin.png" alt="Forgot Login" class="button" /></a></td>' +
					'</tr>' +
				'</table>';
		
	}
}

/**
 * Goes to the add account page.
 */
function GoCreateAccount() {
	window.location.href = "addacount.php";
}


/**
 * Allows enter/return to trigger login
 */
function loginViaEnter(evt) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode :
		((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13) {
		Login();
		return false;
	}
	return true;
}
