var errorQty = 0;

function valid()
{
	if($F("userName") == "")
	{
		alert("请输入用户名");
		$("userName").focus();
		return false;
	}
	if($F("password") == "")
	{
		alert("请输入密码");
		$("password").focus();
		return false;
	}
	if($("validPic") != null)
	{
		if( $F("validPic") == "")
		{
			alert("请输入验证码");
			$("validPic").focus();
			return false;
		}
	}
	return true;
}
function login(userName,password,validPic,picId)
{
	if(!valid())
	{
	 	return ;
	}
	var para = "userName="+$F(userName)+"&password="+$F(password);
	if($("validPic") != null)
	{
		para = para + "&validPic=" + $F(validPic);
	}
	para = para + "&tmp=" + new Date().getTime();
	new Ajax.Request("/zongzi/action/checkLogin.jsp", 
		{
			method:"get", parameters:para, onComplete:reb=function(request)
			{
				var back=request.responseText;			
				back = back.substring(back.length-1);
				
				//成功登录
				if(back=="0")
				{
					errorQty = 0;
					top.location.reload();
					return;
				}
				else if(back=="1")
				{
					alert("请输齐用户名和密码");
					reloadPic(picId);
					$("userName").focus();
					return;
				}
				else if(back=="2")
				{
					alert("验证码错误");
					reloadPic(picId);
					$("userName").focus();
					return;
				}
				else if(back=="3")
				{
					errorQty++;
					alert("用户名或密码错误");
					
					if(errorQty >= 3)
					{
						top.location.reload();
					}
					return;
				}
				else if(back=="4")
				{
					errorQty++;
					alert("用户名或密码错误");
					reloadPic(picId);
					return;
				}
				else
				{
					errorQty++;
					alert("登陆失败");
					if(errorQty >= 3)
					{
						top.location.reload();
					}
					else
					{
						reloadPic(picId);
					}
					return;
				}
			}
		}
	);
}

function reloadPic(picId)
{
	if($(picId) != null)
	{
		$(picId).src = "http://main.1001m.com/server/randomImg.jsp?tmp="+new Date().getTime();
	}
}

function logout()
{
	new Ajax.Request("/commont/logout.jsp?tmp="+new Date().getTime(), 
		{
			method:"get", parameters:"", onComplete:reb1=function(request)
			{			
					top.location.reload();
			}
		}
	);
}