function login_err_msg($obj){
	var login_messages = {
		"required" : I18n.t("front.alert.login required"), 
		"email invalid": MESSAGES["email invalid"] 
	};
	
	if ( $.trim($obj.val()) == '' ){
		return login_messages['required'];
	}
	else
	if( $obj.attr('type') == 'text' && !/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test($.trim($obj.val()))){
		return login_messages['email invalid'];
	}
	return '';
};
$(function(){
	$('input[toast]').toastValidate();
	var $err = ( MY_ID[1] == '' ) ? $('div.login_error:first') : $('#login_form').parent().prev();
	var $login_form = $('#login_form');
	
	$('input[value=""]:first', $login_form).focus();

	$('input:text, input:password', $login_form).bind('focus', function(e){
		if (!$(this).val().length && $(this).hasClass("input_focus"))
			$(this).removeClass("input_focus").addClass("guide_text");
	}).bind('keyup', function(e){
		if( !$(this).val().length ) {
			$(this).removeClass('input_focus').addClass('guide_text');
		}
		else
		if( $(this).val().length && $(this).hasClass('guide_text') ) {
			$(this).removeClass('guide_text').addClass('input_focus');
		}
		
		if ( $err.is(':visible') && e.keyCode != '13' ) {
			if( $err.attr('class') == 'login_error' ){
				$err.hide()
					.parent().next().next().show();
			}
			else{
				$err.hide()
						.prev().show();
			}
		}
	});
	
	$login_form.bind('submit', function(){
		
		if (!/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test($('input[name="email"]:first').val())) {
			alert(MESSAGES['email invalid']);
			$('input[name="email"]:first').focus();
			return false;
		}
		
		if( MY_ID[1] == '' ){
			if( !toast_validate_form(this, { message: login_err_msg, label_id: 'div.login_error:first>p' })){
				$err.show()
					.parent().next().next().hide();
				return false;
			}
		}
		else{
			if( !toast_validate_form(this, { message: login_err_msg })) return false;
		}
		
		if( $login_form.attr('action') == '/main/user/login' ) {
			$.ajax({
				type: 'post', 
				url: $login_form.attr('action'), 
				data: $login_form.serialize()+"&teamurl="+TEAM_URL, 
				dataType: 'json', 
				success: function(data){
					if( JSON_SUCCESS == data.status ){
						var teamoffice_url = (data.teamoffice_url) ? data.teamoffice_url : "";
						document.location.href = teamoffice_url+$('#rtnurl', $login_form).val();
					}
					else
					if( JSON_USER_DEFINED == data.status && data.error_msg && data.error_msg.key == 'check_confirmkey' ){
						document.location.href = '/main/user/confirmkey';
					}
					else
					if ( JSON_USER_DEFINED == data.status && data.error_msg && data.error_msg.key == 'require_join' ){
						document.location.href = '/'+data.teamurl+'/member/checkinviteauth/'+data.email_key;
					}
					else
					if( JSON_USER_DEFINED == data.status && data.error_msg ){
						$err.prev().hide();
						$err.show()
							.children('p.negative').text(data.error_msg.title)
							.next('p.note').html(data.error_msg.note);
						
						if( 'password' == data.focus ){
							$('input[name="password"]:first', $login_form).select();
						}
						else{
							$('input[name="email"]:first', $login_form).focus();
						}
					}
					else{
						toast_error_handle(data);
					}
				}, 
				error: function(res, e){
					toast_ajax_error(res, '[user login] : '+e);
				}, 
				complete: function(res, e){
					toast_validate_form_return($login_form.attr('id'));
				}
			});
			return false;
		}
	});

	$('a.btn_login:first', $login_form).bind('click', function(){
		$('#login_form').submit();
	});
	
	if( $('#email_check_form').length ) {
		var $email_check_form = $('#email_check_form');
		$email_check_form.bind('submit', function(){
			if( !toast_validate_form(this) ) return false;
			$.ajax({
				type: 'post', 
				url: $email_check_form.attr('action'), 
				data: $email_check_form.serialize(), 
				dataType: 'json', 
				success: function(data){
					if( data.status == JSON_SUCCESS ){
						document.location.href = '/main/user/joinform?email='+$('input:text', $email_check_form).val();
					}
					else
					if( data.status == JSON_USER_DEFINED ){
						alert(data.title);
						toast_validate_form_return($email_check_form.attr('id'));
					}
					else{
						toast_error_handle(data);
						toast_validate_form_return($email_check_form.attr('id'));
					}
				}, 
				error: function(res, e){
					toast_ajax_error(res, '[user dupcheck]:'+e);
					toast_validate_form_return($email_check_form.attr('id'));
				}
			});
			return false;
		});
		$('a.btn_normal_m:first', $email_check_form).bind('click', function(){
			$email_check_form.submit();
		});
	}
});
