/*
 *  jQuery Thinkfree SignIn plugin
 */

(function($){
    $.extend({
        tfAccount : {           
            error_msg_1    : "Please enter your e-mail.",
            error_msg_2    : "Please enter your password.",
            error_msg_3    : "The username or password you entered is incorrect.",      
	        error_msg_4    : "Invalid Email Address.",            
            nos_ssl_sigin_url  : "https://"+location.host+"/member/sign_in_box_action.action",
	        nos_sigin_url  : "/member/sign_in_box_action.action",
            
            sigup_url      : "signup.action",        
            login_url      : "goLandingPage.action",       
            return_url     : "/member/goLandingPage.action",
            forgot_password_url : "forgotpassword.action",
            needAuthenticated_url : "/member/needAuthenticated.action?smb_user=",
            
            settings : function (sets){
                this.error_msg_1   = sets.error_msg_1;
                this.error_msg_2   = sets.error_msg_2;
                this.error_msg_3   = sets.error_msg_3; 
                this.error_msg_4   = sets.error_msg_4;                 
            }   
            ,
            
            setRedirect : function(redirect_url) {
        		this.return_url = redirect_url;
        		
        	},
        	
            // sign in 
            signIn : function (){       	
            	$('#error').hide();
    			$(".focused").removeClass("focused");
    			
                var form_var = $("#signInForm").serialize();                
                var username = $.trim($("#smb_user").val());
                var password = $.trim($("#smb_password").val());
                
                var redirect = $.trim($("#redirect").val());
                
                if(redirect && redirect != ""){
                	$.tfAccount.return_url = redirect;
                }
                if(username == '') {
                    this.error(this.error_msg_1);
                    $("#smb_user").focus();                    
                } else if(!this.validatedEmail(username)){
					this.error(this.error_msg_4);
				} else if(password == '') {
                    this.error(this.error_msg_2);
                    $("#smb_password").focus();
                } else {
                	/* 보안로그인의 경우 */
                	if($("#sslLogin").is(":checked")) { 
                		var signInForm = $("#signInForm")[0];
                		signInForm.action = this.nos_ssl_sigin_url;
                		signInForm.target = "signinFrame";
                		signInForm.submit();
                		return;
                	} else {
		    			$.ajax({
		    				type: "POST",
		    				url: this.nos_sigin_url,
		    				data: form_var,
		    				dataType: 'json',
		    				success: function(json){	    				
		    					$.tfAccount.signinResult(json.error, json.msg) ;
		    				}
		    			});
                	}
				}
            
            }
            ,
            signinResult : function(retcode, msg) {
            	var username = $.trim($("#smb_user").val());
            	if(retcode> 0){	    						
					if (retcode == 300103){
						location.href= $.tfAccount.needAuthenticated_url+username;
                        return false;
					}	    						
				 		$.tfAccount.error(msg);
				 		return false;	
			 	}else{
			 		if ($("#saveID").is(":checked")) {
			 			$.cookie('smb_user', username, {path: "/", expires: 10 });
			 		}else{
			 			$.cookie('smb_user', null, {path: "/"});
			 		}
			 		if($("#sslLogin").is(":checked")) {
			 			$.cookie('sslLogin', "checked", {path: "/", expires: 10 });
			 		} else {
			 			$.cookie('sslLogin', null, {path: "/"});
			 		}
            	location.replace(this.return_url);
                return false;
			 	}
            },
            
            validatedEmail : function(email) {
            	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;            	
            	if(reg.test(email) == false) {
            		return false;
            	}else{
            		return true;
            	}
            }
            ,	            
            
			// signUp 
            login : function (){
                location.replace($.tfAccount.login_url);   
            }
            ,             
			// sign out 
            signOut : function (){
                jQuery.cookie('smb_session', null);
                location.replace($.tfAccount.login_url);
            }
            ,            
            
            signUp : function (){
                location.replace($.tfAccount.sigup_url);   
            }
            , 
             
            forgotPassword : function (){
                location.replace($.tfAccount.forgot_password_url);   
            }
            ,             
			error : function(msg) {
				$('#error').fadeIn("slow");
				$('#error').html(msg);
            }            
        }
    });
})(jQuery);
