function addLogInFormListener(formName, toSubmit){
	if($(formName)){
		$(formName).addEvent('submit', function(e) {
			(new Event(e)).stop();
			var elements 		= $(formName).getElements('input[type=text]');
			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 && toSubmit){
				$(formName).submit();
			}else{
				if(formComplete)
					return true;
				return false;
			}
		});
	}
}
function checkBoxListener(){
	if($('chkRegister')){
		$('chkRegister').addEvent('click', function(e) {
			if($('chkRegister').checked){
				$('ulRegisterPass')		.removeClass('hide');
				$('ulRegisterRePass')	.removeClass('hide');
				$('brRegister')			.removeClass('hide');				
			}else{
				$('ulRegisterPass')		.addClass('hide');
				$('ulRegisterRePass')	.addClass('hide');
				$('brRegister')			.addClass('hide');
			}
		});
	}
}

function formActionListener(){
	if($('contactForm')){
		$('contactForm').addEvent('submit', function(e) {
			(new Event(e)).stop();
			var checkEmail = false;
			var password 		= ($('contactForm').getElements('input[id=txtPassword]'))[0];
			var rePassword 		= ($('contactForm').getElements('input[id=txtRePassword]'))[0];
			var textAreaQuery 	= ($('contactForm').getElements('textarea[name=textAreaQuery]'))[0];
			
			if($('chkRegister') && $('chkRegister').checked){
				if($(password))
					$(password)		.addClass("minLength").setProperty('validatorProps', "{minLength:5}");
				if($(rePassword))
					$(rePassword)	.addClass("required confirm ").setProperty('validatorProps', "{confirms:'txtPassword'}");
				if($(textAreaQuery))
					$(textAreaQuery).addClass("required");
				checkEmail = true;
				
				$('contactForm').action = "/" + jsLang.REGISTER_LINK;
			}else{
				if($(password))
					$(password)		.removeClass("minLength").setProperty('validatorProps', "");
				if($(rePassword))
					$(rePassword)	.removeClass("required confirm ").setProperty('validatorProps', "");
				if($(textAreaQuery))
					$(textAreaQuery).removeClass("required");
				checkEmail = false;
				
				$('contactForm').action = "/" + jsLang.CONTACT_LINK;
			}
			if(checkEmail){
				var eMail = ($('contactForm').getElements('input[id=txtEmail]'))[0];
				if(eMail.value==''){
					//controller check this
					$('contactForm').submit();
				}
				var jsonRequest = new Request.JSON({
			        url: "/request/verifyEmailAjax"
					,method: 'post'
					,data: {email : eMail.value}
					,onSuccess: function(responseJSON){
						switch (responseJSON.status){
							case 0:
								alert(jsLang.LOGIN_MSG_REGISTER_ERROR_CHECKING);
							break;
							case 1:
								if(responseJSON.data.exist){
									alert(jsLang.LOGIN_MSG_REGISTER_ERROR_EMAIL_ALREADY_EXIST);
								}else{
									$('contactForm').submit();
								}
							break;
						}
			        }
					,onFailure: function(xhr){
			            alert(jsLang.LOGIN_MSG_REGISTER_ERROR_CHECKING);
			        }
				}).send();
			}else{
				$('contactForm').submit();
			}
		});
	}
}
window.addEvent('domready', function() {
	checkBoxListener();
	formActionListener();
	addLogInFormListener('loginRightForm', true);
	if(expandBox || isSignUp){
		if($('chkRegister')){
			$('chkRegister').checked = true;
			$('chkRegister').fireEvent('click');
		 	$('chkRegister').focus();
		}
	}
});