function cookies( key, value ){
	if( StringUtil.notNull(value)!='' ){
		var days = 300; 
		var exp  = new Date();    
		exp.setTime(exp.getTime() + days*24*60*60*1000);
		document.cookie = key + "="+ escape(value) +";expires="+ exp.toUTCString()+ ";path="+XSP.URI();
	}else{
		var arr = document.cookie.match(new RegExp("(^| )"+key+"=([^;]*)(;|$)"));
		if(arr != null) {
			return unescape(arr[2]); 
		}else{
			return null;
		}
	}
}

function delCookies( key ){
	var exp = new Date();
	exp.setTime(exp.getTime() - 1000);
	document.cookie=key+"=;expires="+exp.toUTCString()+ ";path="+XSP.URI();
}
/**
 * 记住账号
 */
function remember( o ){
	var email = $('#user_email').val();
	var pwd = $('#user_password').val();
	xsp.cookie(XSP.COOKIE.KEY, email);
	xsp.cookie(XSP.COOKIE.PWD, pwd);
}

/** 自动登录 */
function autologin(){
	if( $('#logErr').html() ){
		return false;
	}
	
	var email = StringUtil.notNull( xsp.cookie(XSP.COOKIE.KEY)).trim();
	var pwd = StringUtil.notNull(cookies(XSP.COOKIE.PWD)).trim();
	if( email ){
		$('#user_email').val( email );
	}
	
	( email && pwd ) && $('#logErr').after('<div id="autoLoginMsg">系统自动登录中...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" onclick="cancelAutoLogin(this)">[取消自动登录]</a></div>');
	
	XSP.tiemoutId = window.setTimeout( function(){
		$('#autoLoginMsg').find('a').fadeOut(500,function(){
			$(this).remove();
		});
		if( pwd && email + pwd ){
			$('#user_password').val( pwd );
			if( !$('#logErr').html() ){
			  $('#btnLogin').click();
			}
		}
	}, 3000);
}

function cancelAutoLogin(o){
	XSP.tiemoutId && (function(){ 
		$('#btnLogin').unbind("click");
		var $$ = $(o).closest('#autoLoginMsg');
		xsp.cookie.remove.all();
		$('#remember').removeAttr("checked");
		$$.html("取消自动登录成功，请重新输入").fadeOut(3000,function(){
			$$.remove();
		});
		window.clearTimeout( XSP.tiemoutId ); 
	})(); 
}

/** 退出 */
function logout(){
	xsp.cookie.remove.all();
	goUrl( XSP.URI()+'/logout' );
}

function login( o, path ){
	$('#user_password').val( $('#user_password').val().trim() );
	$('#user_email').val( $('#user_email').val().trim() );
//	remember( $('#rememberEmail').get(0) );

	$("#loginForm").validate({
//		meta: "validate",
        submitHandler: function( form ) { 
        	$(o).attr('src', $(o).attr('src').replace('login','wait' ) );
        	if( $('remember:checked') ){
        		remember( $('remember:checked'), XSP.URI() )
        	}
        	setTimeout(function(){ form.submit(); },500);
        },
        
		rules: {
			 "user.email": {
				  required:true,
				  hasSysAccount:true
			 },
			 "user.password": {
				  required:true
			 }
		},
		
		messages: {
			"user.email":{ 
				required: "请输入邮箱地址",
				hasSysAccount:"你的账号格式不对" 
			},
			"user.password":"请输入密码"
		},
		
		errorLabelContainer: (function(){
			$('#autoLoginMsg').remove();
			return "#logErr";
		})()
	});
	
	jQuery.validator.addMethod("hasSysAccount", function(value, element) {
		var _email = $('#user_email').val().trim();
		var _re = /\d{8}/i;
		var _ret = false;
			
		if( _email.length === 8 ){
			_ret = _re.test( _email );
		}
		
		if( !_ret ){
			_re =/(\S)+[@]{1}(\S)+[.]{1}(\w)+/
			_ret = _re.test( _email );
		}
		
	    return this.optional(element) || _ret;
	}, "你的账号格式不对");
}

var xsp = {};
xsp.cookie = function(key){
	var a = arguments;
	return a.length == 1 ? (function(){
		var arr = document.cookie.match(new RegExp("(^| )"+key+"=([^;]*)(;|$)"));
		return arr?decodeURI( arr[2] ):null;
	})() : (function(){
		var exp  = new Date();
		
		var val = a[1];
		
		var opts = $.extend({}, {
			path: XSP.COOKIE.PATH,
			domain: XSP.COOKIE.DOMAIN,
			expires: XSP.COOKIE.EXPIRES
		}, a[2] );
		
		exp.setTime(exp.getTime() + opts.expires*24*60*60*1000);
		var s = key + "=" + encodeURI(val);
		s = s + ";expires=" + exp.toUTCString();
		s = s + ";path=" + opts.path;
		document.cookie = s;
	})()
}

xsp.cookie.remove = function(key,opts){
	xsp.cookie(key,"",$.extend({expires:-30},opts || {} ) );
}

xsp.cookie.remove.all = function(){
	var str = document.cookie;
	var arr = str.split(";");
	jQuery.each( arr, function( i, j ){
	    var re = /(.+)(\=.+)/g;
	    var key =j.replace( re, "$1" );
	    xsp.cookie.remove(key);
	})
}
