/*用户登陆*/
var xmlHttp =null;
var test=false;
function jh()	{
	var A=null;

	try	{A=new ActiveXObject("Msxml2.XMLHTTP")} catch(e){try{A=new ActiveXObject("Microsoft.XMLHTTP")} catch(oc) {A=null}}
	if ( !A && typeof XMLHttpRequest != "undefined" ){A=new XMLHttpRequest()}
	
	return A;
}

function checkLogin()	{
	if(checkInput()==false){return false;}
	xmlHttp =jh();
	xmlHttp.onreadystatechange = displayResult;
//	alert($("name").value+'-------'+$("pwd").value);
	xmlHttp.open("GET","/user/login.do?userid="+$("name").value+"&pwd="+$("pwd").value, true);
	xmlHttp.setRequestHeader("If-Modified-Since","0");
	xmlHttp.send(null);
	return test;
}
function displayResult() {
	if(xmlHttp.readyState == 4) //0(未初始化);1(正在装载);2 (装载完毕);3 (交互中);4 (完成) 
	{
	   if(xmlHttp.status == 200) //200(OK);404(not found)
	   {	
	        if (xmlHttp.responseText.length!=0) {	                		
				if ((xmlHttp.responseText)=="0") {    						
					$("hint").innerHTML='<span class="green">登录成功！该窗口即将关闭...</span>';
			//		window.setTimeout("closewin()",1400);
					document.location.reload();
					return true;
			    } else if ((xmlHttp.responseText)=="1") {
					$("hint").innerHTML='<span class="red">帐号或密码输入有误！</span>';
					return false;
			    }
	        }                    
	   }
	}
}
function checkInput() {
		var user_name = $("name").value;		
		var user_pwd = $("pwd").value;		
		var patternChar = /\d{3}-?\d{4,}/i;
		
		if (user_name.length < 7)	{		
			$("hint").innerHTML = '<span class="red">号码不应少于7位!</span>';
			return false;
		} else if (user_name.length > 16) {		
			$("hint").innerHTML = '<span class="red">号码不应大于16位!</span>';
			return false;
		} else if (!patternChar.test(user_name)) {			
			$("hint").innerHTML = '<span class="red">帐号格式输入有误！</span>';
			return false;			
		} else if (user_pwd.length < 6) {			
			$("hint").innerHTML = '<span class="red">密码不应少于6位!</span>';
			return false;			
		} else{
			$("hint").innerHTML = '&nbsp;';
			return true;
		}
}
function loginUser(path) {
	var msgw,msgh,bordercolor;
    msgw=300;//提示窗口的宽度
    msgh=150;//提示窗口的高度
    titleheight=25 //提示窗口标题高度
    bordercolor="#709CD2";//提示窗口的边框颜色
    titlebgcolor="#8BAEDA";//提示窗口的标题颜色
    var sWidth,sHeight;
    //获取当前窗口尺寸
    sWidth = document.body.offsetWidth;
    sHeight = window.screen.availHeight;
    //创建提示窗口的div
    var msgObj = document.createElement("div");
    msgObj.setAttribute("id","msgDiv");
    msgObj.setAttribute("align","center");
    msgObj.style.background="white";
    msgObj.style.border="1px solid " + bordercolor;
    msgObj.style.position = "absolute";
    msgObj.style.left = "50%";
    msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
    //窗口距离左侧和顶端的距离 
    msgObj.style.marginLeft = "-200px";
    //窗口被卷去的高+（屏幕可用工作区高/2）-200
    msgObj.style.top = document.body.scrollTop+(window.screen.availHeight/2)-200 +"px";
    msgObj.style.width = msgw + "px";
    msgObj.style.height = msgh + "px";
    msgObj.style.textAlign = "center";
    msgObj.style.lineHeight ="25px";
    msgObj.style.zIndex = "10001";
    document.body.appendChild(msgObj);
    //提示信息标题
    var title=document.createElement("div");
    title.setAttribute("id","msgTitle");
    title.setAttribute("align","right");
    title.style.margin="0";
    title.style.padding="3px";
    title.style.background = titlebgcolor;
    title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
    title.style.opacity="0.75";
    title.style.border="1px solid " + bordercolor;
    title.style.height="20px";
    title.style.font="14px Verdana, Geneva, Arial, Helvetica, sans-serif";
    title.style.color="white";
    title.style.fontWeight="bold";
    title.innerHTML="<span class=\"left\">用户登录</span><span class=\"right\"><a onclick='closewin()' style='cursor:pointer;'><img src=\""+path+"/images/dialogclose.gif\" /></a></span><div class=\"clear\"></div>";
    document.getElementById("msgDiv").appendChild(title);
    //提示信息
    var txt = document.createElement("p");
    txt.setAttribute("id","msgTxt");
    txt.setAttribute("align","center");
    txt.style.margin="16px 0";
    txt.innerHTML = '<table onkeydown="keydown(event)"><tr><td>号 码：&nbsp;</td><td><input id="name" class="input1" type="text"/></td></tr><tr><td>密 码：&nbsp;</td><td><input name="pwd" id="pwd" class="input1" type="password" /></td></tr><tr><td colspan="2" align="center"><button onclick="checkLogin()">登 录</button> <button onclick="closewin();">关 闭</button></td></tr><tr><td colspan="2" align="center"><span id="hint">&nbsp;</span></td></tr></table>';
    document.getElementById("msgDiv").appendChild(txt);

}
function keydown(e){
	var key = window.event?e.keyCode:e.which;
    if(key==13){
    	checkLogin();
    }
}
function closewin() {
    document.getElementById("msgDiv").removeChild(document.getElementById("msgTitle"));
    document.body.removeChild(document.getElementById("msgDiv"));
}