var xmlHttp;
function createXMLHttpRequest(){
	if(window.XMLHttpRequest){	
		var xmlObj = new XMLHttpRequest();
	}else{
		var MSXML = ['Microsoft.XMLHTTP', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
		for(var n = 0; n < MSXML.length; n++){
			try{
				var xmlObj = new ActiveXObject(MSXML[n]);        
				break;
			}catch(e){
			}
		}
	} 
	xmlHttp = xmlObj;
}
function loginState(){
	    if(xmlHttp.readyState==4){
		   if(xmlHttp.status==200){
		   var getLoginState = unescape(xmlHttp.responseText);
				   if(getLoginState.indexOf("loginsuccess2fe7566e4ae9d598")>=0){
					   ajaxget("LoginRight","/ajax/chklogin_new.asp?a=loginsubmit");
				   }else if(getLoginState=="1"){
						document.getElementById("loginButton").className = "submit";
						alert("用户名不存在！");
						window.location.reload();
						formUserName.select();
				   }else if(getLoginState=="2"){
						document.getElementById("loginButton").className = "submit_change";
						alert("密码输入不正确！");
						window.location.reload();
						formPassWord.select();
				   }
				}else if(xmlHttp.status==404){
				  alert("非法登录数据,网页不存在!");
				}else if(xmlHttp.status<500){
				  alert("登录数据错误,请检查安全设置以及是否重复登录!错误代码:0-400!\n请刷新页面再次登录!");
				}else if(xmlHttp.status<600){
				  alert("书盟系统忙或在维护,请稍后再试!错误代码:500\n如有疑问请联系在线客服 \n联系电话：010-85788938!.\n请刷新页面再次登录!");
				}else{
				  alert("非知错误,请联系在线客服 \n联系电话：010-85788938!");
				}
		//alert(xmlHttp.responseText);

		}
}

function inputusername(){
	document.getElementById("username").value=xmemoryusername;
}
var xmemoryusername=getCookie("memoryusername");
if((xmemoryusername+"x")=="x" || xmemoryusername==null || xmemoryusername==";"){
}else{
	window.onload=inputusername;
}
	

function loginSubmit(){
	if(document.getElementById("loginButton").className != "submit" && document.getElementById("login_Button").value != "登录"){
		return;
	}
	var formUserName=document.getElementById("username").value;
    var formPassWord=document.getElementById("password").value;
	 var SaveCookie=document.getElementById("SaveCookie").value;
	 
    if(document.getElementById("user_name")){
       if(((document.getElementById("user_name").value)!="") && ((document.getElementById("pass_word"))!="")){
       formUserName=document.getElementById("user_name").value;
       formPassWord=document.getElementById("pass_word").value;
       }
    }
    var savecookie = "";
   if(document.getElementById("SaveCookie")){
    if(document.loginForm.SaveCookie.checked==true){
		savecookie="1";
	}else{
		savecookie="0";
	}
	}
	
	var errorMsg="";
	if(formUserName==""){
		errorMsg = "用户名不能为空！";
		arouseFocus("username");
	}else if(formUserName.legnth<1 || formUserName.length>15){
		errorMsg = "用户名长度限1-15个字符";
		arouseFocus("username");
	}else if(formPassWord==""){
		errorMsg = "密码不能为空！";
		arouseFocus("password");
	}else if(formPassWord.length<6 || formPassWord.length>16){
		errorMsg = "密码长度限6-16个字符";
		arouseFocus("password");
	}
	if(errorMsg!=""){
		alert(errorMsg);
		return;
	}

	document.getElementById("loginButton").className = "submit_change"
	if(document.getElementById("user_name")){
	   document.getElementById("login_Button").value = "登录中"
	}
	createXMLHttpRequest();
	var url = "/ajax/login_new.asp?Action=loginSubmit&savecookie="+savecookie+"&Time="+new Date().getTime();
	xmlHttp.open("POST",url,true);
	xmlHttp.onreadystatechange = loginState;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlHttp.send("username="+escape(formUserName)+"&password="+escape(formPassWord));
}	


function exitLogin(x){
	createXMLHttpRequest();
	var url = "/ajax/login_new.asp?Action=loginExit&Time="+new Date().getTime();
	xmlHttp.open("POST",url,true);
	xmlHttp.onreadystatechange = exitLoginState;
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlHttp.send();
}

function exitLoginState(){
	
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){
			
			if(xmlHttp.responseText.indexOf("exitLoginOK")>=0){
				document.getElementById("LoginRight").innerHTML = "载入中 请稍后..";
				 ajaxget("LoginRight","/ajax/chklogin_new.asp?a=exitlogin");
			}
		}
	}
}