function addFormListener(formName, toSubmit){
	try{
		if($(formName)){
			$(formName).addEvent('submit', function(e) {
				//(new Event(e)).stop();
				var elements 		= $(formName).getElements('input[type=text]');
				if(elements.length == 0){
					$(formName).submit(); //logOut
				}
				var accountText 	= elements[0].get('value');
				var formComplete 	= true;
				
				if(accountText == ''){
					alert(jsLang.LOGIN_MSG_ACCOUNT_REQUIRED);
					formComplete = false;
				}
	
				elements 			= $(formName).getElements('input[type=password]');
				var passwordText 	= elements[0].get('value');
				if(passwordText == ''){
					if(formComplete){
						alert(jsLang.LOGIN_MSG_PASSWORD_REQUIRED);
					}
					formComplete = false;
				}
	
				if(formComplete){
					var jsonRequest = new Request.JSON({
				        url: "/login/loginUserAjax"
						,method: 'post'
						,data: {account : accountText, password: passwordText}
						,onSuccess: function(responseJSON){
							switch (responseJSON.status){
								case 0:
									alert(jsLang.LOGIN_MSG_REGISTER_ERROR_LOGIN);
								break;
								case 1:
									if(!responseJSON.data.login){
										alert(jsLang.LOGIN_MSG_REGISTER_ERROR_LOGIN_DATA);
									}else{
										//Change the login form for the logOut
										$('loginMain').empty();
										$('loginMain').adopt(
											new Element('li').set('html', jsLang.HEADER_LABEL_WELCOME + ' <b>' + responseJSON.errorMsg + '</b>')
										).adopt(
											new Element('li').set('html', '<input type="submit" value="'+jsLang.HEADER_LABEL_LOG_OUT+'" id="loginBtnMain"/>')
										);
										$(formName).action = '/login/logOut';
									}
								break;
							}
				        }
						,onFailure: function(xhr){
							//failure on the system, we submit the form to login.
				            $(formName).submit();
				        }
					}).send();
				}
				return false;
			});
		}
		return false;
	}catch(e){
		alert(e);
		return false;
	}
}

window.addEvent('domready', function() {
	if($('frmLogIn')){
		addFormListener('frmLogIn', true);
	}
});