/**  
 * @author Van  
 * @constructor indexPage
 * @description "indexPage"
 * @date 2010-11-17
 * @since version 1.0 
 */ 



	/**
	 * 窗口大小改变.
	 */
	$(window).resize(function(){
	
		location.reload();
	
    });

	/**
	 * 回车登录.
	 */
	document.onkeydown = function(evt){
		var evt = window.event?window.event:evt;
		if(evt.keyCode==13){
			checkBoforeLogin();
		}
	}
	
	/**
	 * 验证是否已经登录.
	 * @param vl
	 */
	function checkUserOnlie(vl){
		
		allowLoginFlag=true;
		
		$.each(onlineUsers, function(){   
			if(vl==this){
				msgAlert(i18n["msg_duplicate_logged"],'info');
				allowLoginFlag=false;
				return false;
			}
		});      
	}
	
	/**
	 * 刷新验证码.
	 */
	
//	//刷新验证码
//	function refreshCheckCode(){
//	      $("#validateCode_img").attr({"src":contextPath+"/security/captcha.jpg?random="+new Date()}); 
//	}
	
	/**
	 * 验证表单.
	 */
	function checkBoforeLogin(){	
		
		if(allowLoginFlag){
			
			if($('#userLogin').form('validate')){
				
				//验证用户是否被禁用
				var uname=$('#j_username').val();
				var pwd=$('#j_password').val();
				
				var url = 'user!findDisable.action';
				
				var prarm={"userName":uname,"pwd":pwd};
				
				$.post(url,prarm,function(res){
					
					if(res=="exist"){
						
						msgAlert(i18n['validate_userDisableOrNorexist'],'error');
						
					}else{
				
						 if($("#_spring_security_remember_me").attr("checked")==true){
							 
							setCookie('cookie_userName',uname,14);
							setCookie('cookie_userPassword',pwd,14);
						 }
						 
						 $("#userLogin").submit();
//						 $.get('getCode.jsp'+'?random='+Math.random()*10+1,function(res){
//							 
//							 	res=res.replace(/(^\s*)|(\s*$)/g, ""); 
//								var inputVerificationCode=$('#validateCode').val();
//								
//								if(res==inputVerificationCode){
//									$("#userLogin").submit();
//									
//								}else{
//									msgShow(i18n['validate_error_verificationCode'],'show');
//								}
//						 });
					}
				});
			}

		}else{
			
			msgAlert(i18n["msg_duplicate_logged"],'info');
		}
		
		
		

	}
	
	
	/**
	 * 设置COOKIE
	 */
	function setCookie(c_name,value,exdays){
		
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
	}
	
	/**
	 * 取得COOKIE
	 */
	function getCookie(c_name){

	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++){
		  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		  x=x.replace(/^\s+|\s+$/g,"");
		  if (x==c_name){
		    return unescape(y);
		  }
	  }
	}
	
	
	
	
	/**
	 * 加载.
	 */
	$(document).ready(function(){

		//refreshCheckCode();//加载验证码

		//自动登录
		if(getCookie("cookie_userName")!=null && getCookie("cookie_userPassword")!=null){
			
			$('#autoLoginWindow').window('open');
			
			$('#j_username').val(getCookie('cookie_userName'));
			$('#j_password').val(getCookie('cookie_userPassword'));
			
			checkBoforeLogin();
//			 $.get('getCode.jsp?random='+new Date(),function(res){
//				 	res=res.replace(/(^\s*)|(\s*$)/g, ""); 
//					$('#validateCode').val(res);
//					checkBoforeLogin();
//			 });
			
		}else{
			
			$('#loginWindow').window('open');
		}
	});