$(function() {
	
	$('#username').focus();
	$('#password').keyup(function(e) { if(e.keyCode == 13) LoginPage.LogIn(); });
	
});

var LoginPage = function()
{		
	return {
		
		LogIn: function()
		{
			if ($('#username').val() == "") 
			{
				$('#username').focus();
				alert('Please enter the user name.'); 
				return;
			}
			   	
			if ($('#password').val() == '') 
			{
				$('#password').focus();
				alert('Please enter the password.'); 
				return;
			}
			
			$.ajax(
			{
				type: "POST",
				url: window.location.pathname + "/validate-and-login",
				data: "username="+ $.base64.ee($('#username').val()) +"&password="+ $.base64.ee($('#password').val()),
				success: function (r)
				{
					r = eval(r);
					
					if( r.code == 0 )
					{
						window.location.pathname = "/home";
					}
					else if( r.code == 1 )
					{
						alert("Sorry, but user with such username and password doesn't exist");
					}
					else if( r.code == 2 )
					{
						alert("Your account has been blocked by administrator");
					}
				}
			});
		}	
	}
}();
