/**
 * Helps the login form to allow user to login, register, etc.
 */
var LoginManager = 
{
	loginDiv : null, 
	loginAction : null,
	loginUrl : APP_BASE + "/services/login",
	
	init : function()
	{
		this.loginDiv = document.getElementById("loginForm");
		this.loginAction = document.getElementById("loginAction");
	},
	
	
	/**
	 * Shows the login page in the header
	 */
	showLoginDiv : function()
	{
 		this.loginDiv.style.display = "block";
		this.loginAction.style.display = "none";
	},
	
	closeLoginDiv : function()
	{
 		this.loginDiv.style.display = "none";
		this.loginAction.style.display = "block";
	},
	
	/**
	 * Logs the user in via AJAX
	 */
	loginUser : function()
	{
		YAHOO.log("Logging in user");
		var callback = 
		{
			success : this.loginHandler, 
			failure : this.loginHandler,
			scope	: LoginManager
			
		}
		
		var params = "?" + "name=" + document.getElementById("name").value;
		params += "&password=" + document.getElementById("password").value
		this.loginTransaction = YAHOO.util.Connect.asyncRequest('GET', this.loginUrl + params, callback, null);
		
		return false;
	},
	
	loginHandler : function(o)
	{
		YAHOO.log("Login Result" + o.responseText);
		if (o.status != 0)
		{
			var loginResult = eval("(" + o.responseText + ")");
			if (loginResult.response == "valid")
			{
				this.loginDiv.innerHTML = "Welcome " + loginResult.userName + 
				 "| <a href = 'accounts' class = 'black-link'>Account Settings</a>" +
				 "| <a href = 'javascript:ShoppingCart.showCart()' class = 'black-link'>Shopping Cart</a>" + 
				 "| <a href = 'logout' class = 'black-link'> Logout </a> ";				
			}
			else
			{
				this.showLoginError("username password link is wrong");	
			}
		}
		else
		{
			this.showLoginError("Count not connect to server. Please check the connection");
		}
	},
	
	/**
	 * Shows the error that is thrown on an invalid login
	 */
	showLoginError : function(status)
	{
		YAHOO.log("Login was wrong");
		var errorDiv = document.getElementById("loginError");
		errorDiv.style.display = "";
		//window.setTimeout(function(){errorDiv.style.display = "none"}, 10000);
	}
}
LoginManager.init();