
var flag;

Ext.onReady(function(){
	
	Ext.get("username").focus();
	
	//获取项目名称
	var contextPath=getContextPath();
	
	Ext.get('login').on('click',function (){
		requestCheck();
	});

	//给帐号文本框注册回车发请求验证事件
	Ext.get("username").on("keypress",function(e){
		if(e.keyCode==Ext.EventObject.ENTER){
			requestCheck();
		}
	});
	
	//给密码文本框注册回车发请求验证事件
	Ext.get("password").on("keypress",function(e){
		if(e.keyCode==Ext.EventObject.ENTER){
			requestCheck();
		}
	});
	
	//给正式考模拟考的考试类型下拉框注册回车发请求验证事件
	Ext.get("typeselect").on("keypress",function(e){
		if(e.keyCode==Ext.EventObject.ENTER){
			requestCheck();
		}
	});
	
	//发送验证帐号密码的函数
	function requestCheck(){
		var username=document.getElementById("username").value;
		var password=document.getElementById("password").value;
		var kstype=document.getElementById("kstype").value;
		if(check()){
			
			document.getElementById("msg").setAttribute("innerHTML","<font color='red'>正在验证帐号密码，请稍后......</font>");
						
			Ext.Ajax.request({
			url:contextPath+"/checkAccount2.htm?kstype="+kstype+"",
			success:function (response){
				//alert(response.responseText.length);
				
				if(response.responseText.length!=63){
						//alert(response.responseText.length);
						if(response.responseText.length==62){
							document.getElementById("msg").innerHTML="<font color='red'>抱歉！您的账号未激活，请与管理员联系！</font>";
						}else{
							document.getElementById("msg").setAttribute("innerHTML","<font color='red'>帐号密码正确，即将进入系统......</font>");
							document.location.href=contextPath+"/exam2/information.htm";
						}
				}else{
					Ext.Msg.alert("鲁班大学考试系统",response.responseText,function (btn){
					login(btn,response.responseText.length);
					document.getElementById("msg").setAttribute("innerHTML","");
					});
				}
			},
			failure:function(){
				//alert(response.responseText.length);
				Ext.Msg.alert("鲁班大学考试系统","请求发生错误，请稍后重试！");
			},
			params:{'username':username,'password':password}
		});
		}
	}
	
	//帐号密码正确就跳转到系统首页，否则不跳转
	function login(btn,strlength){
		if(btn=='ok'&&strlength!=63){
			document.location.href=contextPath+"/exam2/information.htm";
		}
    }
    
    function checkspace(checkstr) {
  		var str = '';
  		for(i = 0; i < checkstr.length; i++) {
   			 str = str + ' ';
  		}
  		return (str == checkstr);
	}
	
	function check(){
  		if(checkspace(document.getElementById('username').value)) {
    		Ext.Msg.alert("鲁班大学考试系统","用户帐号不能为空！");
    		Ext.get("username").focus();
			return false;
  		}
  		if(checkspace(document.getElementById('password').value)) {
    		Ext.Msg.alert("鲁班大学考试系统","用户密码不能为空！");
    		Ext.get("password").focus();
			return false;
  		}
  		return true;
  	}
    
});
