if(!window.Login){
	window.Login = {
			
		LBForgotPass: null,
		LBChangePass: null,
		
		initTempPassword: function(){
			Consulte.toggleBanner(false);
		},
			
		/* ESQUECEU A SENHA */
		showForgotScreen: function(){
			this.LBForgotPass = new LightBox($("forgotPass").value, {title:Bundle.getMessage("txt.forgotAccessData").capitalize()});
			this.LBForgotPass.show();
			Mask.numberField("ForgotForm.cpf", Mask.CPF);
		},
		
		cancelForgotScreen: function(){
			this.LBForgotPass.close();
		},
		
		forgotPassword: function(){
			if(!this._validateForgotPassword()) return;
			
			new Ajax.Request(window.contextPath+"/forgotPassword.action", {
				parameters: {cpf:Mask.clear($F("ForgotForm.cpf")), username:$F("ForgotForm.username")},
				onComplete: function(response){
					if(!Consulte.hasError(response)){
						alert(response.responseText.strip());
						this.LBForgotPass.close();
						this.LBForgotPass = null;
						
					}
				}.bind(this),
				consulteOptions: {
					processing:{
						fullScreen: true
					}
				}
			});
		},
		
		_validateForgotPassword: function(){
			if(String.isNotEmpty($F("ForgotForm.cpf")) && !Consulte.validateCPF("ForgotForm.cpf", {returnBoolean:true})){
				return false;
			}

			if(Consulte.isEmpty($("ForgotForm.cpf")) && Consulte.isEmpty($("ForgotForm.username"))){
				Consulte.showErrorMessage(Bundle.getMessage("message.mandatory.field.xor", "label.user.name", "label.cpf"));
				return false;
			}

			return true;
		},
		/*-----------*/
		
		/* ALTERAR SENHA */
		showChangeScreen: function(){
			this.LBChangePass = new LightBox($("changePass").value, {title:Bundle.getMessage("txt.change.password").capitalize()});
			this.LBChangePass.show();
		},
		
		cancelChangeScreen: function(){
			if(this.LBChangePass){
				this.LBChangePass.close();
			}else{
				this.doLogout();
			}
		},
		
		changePassword: function(){
			if(!this._validateChangePassword()) return;
			new Ajax.Request(window.contextPath+"/changePassword.action", {
				parameters: {password:$F("ChangeForm.current"), newPassword:$F("ChangeForm.new"), validateAgainst:$F("ChangeForm.validateAgainst")},
				onComplete: function(response){
					if(!Consulte.hasError(response)){
						alert(response.responseText.strip());
						if(this.LBChangePass){
							this.LBChangePass.close();
							this.LBChangePass = null;
						}else{
							$("ChangeForm").action = window.contextPath+"/login.action";
							$("ChangeForm").appendChild(new Element("input", {name:"sessionLoginInformation", type:"hidden", value:"true"}));
							$("ChangeForm").submit();
						}
					}
				}.bind(this),
				consulteOptions: {
					handleConfirmation: function(errorJson){
						var confirmed = Consulte.showConfirmMessage(errorJson.message);
						if(confirmed){
							$("ChangeForm.validateAgainst").value = "false";
							this.changePassword();
						}else{
							$("ChangeForm.validateAgainst").value = "true";
						}
					}.bind(this),
					processing:{
						fullScreen: true
					}
				}
			});
		},
		
		_validateChangePassword: function(){
			try{
				var currentPass = $("ChangeForm.current");
				var newPass = $("ChangeForm.new");
				var confimrPass = $("ChangeForm.confirm");
				
				Consulte.mandatory(currentPass, "label.current.password");
				Consulte.mandatory(newPass, "label.new.password");
				Consulte.mandatory(confimrPass, "label.confirm.password");
				Consulte.validatePassword(newPass, confimrPass, {setValidateAgainst:true, equalConfirmPasswordMessage:"message.password.equal.new.confirm.password"});

				return true;
			}catch(e){
				if(e != Consulte.$error) throw e;
				return false;
			}
		},
		/*-----------*/

		doLogin: function(){
			if(!this._validateLogin()) return;
			$("LoginForm").submit();
		},

		doLogout: function(){
			Processing.show(true);
			if(Consulte.scheduleWindow != null){
				Consulte.scheduleWindow.close();
				//FIXME: Não funciona no IE, meia boa.
			}
			window.setTimeout(function(){
				if(Consulte.scheduleWindow == null || Consulte.scheduleWindow.closed){
					Consulte.scheduleWindow = null;
					new Ajax.Request(window.contextPath+"/logout.action", {
						onComplete: function(response){
							if(!Consulte.hasError(response)){
								window.location = window.contextPath;
							}
						}
					});
				}else{
					Processing.hide();
				}
			}, 3000);
		},

		doLoginOnEnter: function(event){
			if(Mask.isEnter(event)){
				this.doLogin();
			}
		},

		goSupport: function(){
			//FIXME: Não tem mais acesso ao e-mail do cadastro:
			alert("Vai pro Supporte");
		},

		_validateLogin: function(){
			if(Consulte.isEmpty("login.username") || Consulte.isEmpty("login.password")){
				alert(Bundle.getMessage("message.empty.login.fields"));
				return false;
			}
			return true;
		},


		_invoke: function(id, method){
			var elem = $(id);
			if(elem != null && Object.isFunction(elem[method])){
				elem[method]();
			}
		}
	};
}