UserLoginPage = function(){}; 
UserLoginPage.MenuLeft = null; 
UserLoginPage.DataBridgeObj = null; 
UserLoginPage.PostLoginAction = null; 

UserLoginPage.LoadPage = function(subPage, user, password, msg, signIn, showRegister)
{		
	switch(subPage)
	{
		case "logout":
		{		
			Globals.ClearUserData(); 
			UserLoginPage.Load("","","You are now logged out");		
			break; 
		}
		default:
		{
			UserLoginPage.Load(user, password, msg, signIn, showRegister);		
			break; 
		}
	}
	
};

UserLoginPage.Load = function(user, password, msg, signIn, showRegister)
{
	if(msg == null)
	{
		msg = ""; 
	}
	else if(msg != "")
	{
		msg = '<p class="error" style="margin-top:10px;font-weight:bold;">' + msg + '</p>';
	}

	if(signIn == null)
	{
		signIn = 	'<b>Returning Users</b><br />' + 
					'<p style="width:270px;">Please sign in before continuing to access convenient features.</p>'; 
	}
	
	Globals.MainMenu.SelectMenuItem(-1, true); 
	Globals.UI.Navigation.LoadPage("User Login", "Sign In", "<div id='userloginform' style='padding:20px;'></div>", "", "",""); 
	
	var registrationTxt = ""; 
	if(showRegister)
	{	
		registrationTxt = 	'<div style="margin-top:60px;float:left;margin-right:10px;font-size:14pt;">-or-</div>' + 							
							'<div style="margin-top:65px;float:left;margin-right:10px;"><button onclick="' + showRegister + '">Register New User</button></div>';
	}
	
	var container = $("userloginform");
	container.innerHTML = '<div style="padding:20px;">' + 
							'<div>' + 
								signIn + 
								msg + 
							'</div>' + 
							'<div style="display:block;clear:both">' + 
								'<div style="float:left;margin-right:10px;">' + 
									'<div id="user_name" style="margin-top:20px;"></div>' + 
									'<br />' + 
									'<div id="user_password"></div><a href="javascript:UserLoginPage.EmailPassword();">I forgot my password</a>' + 
								'</div>' + 
								registrationTxt + 
							'</div>' +
							'<div style="display:block;clear:both">' + 
								'<hr style="margin-top:20px;"/>' + 
								'<button onclick="UserLoginPage.Submit();">Login</button>' + 
								'<span style="margin-left:20px;width:40px;" id="status"></span>' + 
							'</div>' + 
						'</div>';
	
	UserLoginPage.UserControl = new UIControls.EmailControl("email", $("user_name")); 
	UserLoginPage.UserControl.Label = "Login Email:";
	UserLoginPage.UserControl.Required = true; 
	UserLoginPage.UserControl.ValidationErrorWriteOut = false; 
	UserLoginPage.UserControl._onEnterF = UserLoginPage.Submit; 
	UserLoginPage.UserControl.Render(user); 

	UserLoginPage.PasswordControl = UIControls.PasswordControl("password", $("user_password"));
	UserLoginPage.PasswordControl.Label = "Password:";
	UserLoginPage.PasswordControl.Required = true; 
	UserLoginPage.PasswordControl.ValidationErrorWriteOut = true; 
	UserLoginPage.PasswordControl._onEnterF = UserLoginPage.Submit; 
	UserLoginPage.PasswordControl.Render(password); 

	UserLoginPage.Form = new HtmlForm.Control("UserLoginForm");

	UserLoginPage.Form.AddObject(UserLoginPage.UserControl);
	UserLoginPage.Form.AddObject(UserLoginPage.PasswordControl);
	
	window.setTimeout("UserLoginPage.UserControl.Focus()", 100); 
	window.setTimeout("UserLoginPage.UserControl.Select()", 100); 
}; 

UserLoginPage.Form = null; 
UserLoginPage.UserControl = null;
UserLoginPage.PasswordControl = null; 

UserLoginPage.Submit = function()
{
    if(UserLoginPage.Form.Validate() == "")
    {
        var serverData = new ServerData.SenderObject("authenticate", null, Globals.Location.ASHX.User, UserLoginPage.Form.GetURIFormat());             
        Globals.ShowSpinner("status", "Authenticating..."); 
        
        serverData.SendData(function(results)
        {                
    		if(!this.ReturnObject.Data)//True if authenticated, false otherwise.
    		{
	    		UserLoginPage.Load(UserLoginPage.UserControl.GetValue(), UserLoginPage.PasswordControl.GetValue(), "Cannot login. Check your login email and password and try again.");     		
				UserLoginPage.UserControl.Focus(); 
    		}
    		else
    		{
				Globals.UserData = this.ReturnObject.Data; 
				if(UserLoginPage.PostLoginAction)
				{
					var funPointer = UserLoginPage.PostLoginAction; 
					UserLoginPage.PostLoginAction = null; 
					funPointer(); 
				}
				else
				{
					Globals.UI.Navigation.LoadPage("User Login", "Sign In", "<div id='userloginform' style='padding:20px;'><span style='font-weight:bold'>You have successfully logged in.</span></div>", "", "","");     		    			
				}
    		}
        });
    }    
    else
    {
        alert("Some required fields are missing or incorrect."); 
    }    
};

UserLoginPage.EmailPassword = function()
{
	if(!UserLoginPage.UserControl.Validate())
	{
		alert("Please enter your email address to recieve a new password."); 
	}
	else
	{
		alert("NEED TO IMPLIMENT!"); 
	}
}