//检查是否已经登陆
var Turl = null;
function check_isLogin(linkurl){
	
	Turl = linkurl;
	
	createXMLHttpRequest();
	
	//document.getElementById("login").setAttribute("disabled","disabled");//将按钮置为锁定
	
	xmlHttp.open("GET", "/IsLogin.asp?date="+new Date().getTime(), true);
	
	xmlHttp.onreadystatechange = isLoginResponse;
	
	//xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	xmlHttp.send(null);
}

function isLoginResponse() {
	
	if(xmlHttp.readyState == 4) {
		
		if(xmlHttp.status == 200) {
			
			var msg=xmlHttp.responsetext;
			
			if(msg=="0"){
				//alert("您还未登陆");
				showLoginWin();
			}else if(msg=="1"){
				window.location=Turl;
			}
			//alert(xmlDoc.xml)
			//document.getElementById("message").innerHTML=msg;
			//document.getElementById("login").removeAttribute("disabled");//将按钮置为击活
		}
		else {
			alert("服务器异常!");
			//document.getElementById("login").removeAttribute("disabled");//将按钮置为击活
		}
    }
	//else {
		//document.getElementById("message").innerHTML=WaitPic;
	//}
}

//登陆窗口
var loginWin = null;

function showLoginWin(){
	if(loginWin!=null){
		document.body.removeChild(loginWin);
		loginWin = null;
	}
	
	var sc;
	//如果页面的头加了W3C标准，IE 版本对下面取值会有区别；
	//2008新版本将使用如下规范；
	if (typeof(window.pageYOffset) != 'undefined')
	{
		sc = window.pageYOffset; 
	} 
	else if (typeof(document.compatMode) != 'undefined' && document.compatMode != 'BackCompat')
	{
		sc = document.documentElement.scrollTop; 
	} 
	else if (typeof(document.body) != 'undefined')
	{
		sc = document.body.scrollTop; 
	}
	//alert(sc);/*
	var bodyH = parseInt(getPageSizeLogin('h'));
	var bodyW = parseInt(getPageSizeLogin('w'));
	var bodyC = parseInt(getPageSizeLogin('c'));
	var sHeight = 200;
	var sWidth = 400;
	var LEFT =  parseInt((bodyW-sWidth)/2);
	var TOP = parseInt(sc+((bodyC-sHeight)/2)); 
	
	
	//alert(LEFT+"  "+TOP);
	//var HEIGHT = parseInt(document.getElementById(id).offsetHeight);
	//alert(TOP+HEIGHT);

	var win = document.createElement("div");
	win.setAttribute('id','loginWin');
   	win.style.position="absolute";
	//ViewFX.style.background="#000";
	win.style.border="1px solid #008BF1";
   	win.style.top=TOP + "px";
   	win.style.left=LEFT + "px";
   	win.style.width=sWidth + "px";
   	win.style.minHeight=sHeight + "px";
   	win.style.zIndex = "11";
	
	var title = document.createElement("div");
	title.style.background="#008BF1";
	title.style.width=sWidth + "px";
	title.style.height=25 + "px";
	title.style.lineHeight=25 + "px";
	//title.style.textAlign="right";
	title.style.color="#FFFFFF";
	title.innerHTML="<div style='width:355px;text-align:left;font-size:14px;float:left;'><b>&nbsp;会员登陆</b></div><a onclick='removeLoginWin()' style='cursor:pointer;'><img src='../images/close.gif' width='13' height='13' border='0' align='absmiddle' />关闭</a>&nbsp;";
	//title.onclick=removeViewFX;
	win.appendChild(title);
	
	var content = document.createElement("div");
	content.setAttribute('id','loginWin_content');
	content.style.background="#FFFFFF";
	content.style.width=sWidth + "px";
	content.style.minHeight=(sHeight-25) + "px";
	content.innerHTML=createLoginWin();
	win.appendChild(content);
	
   	document.body.appendChild(win);//在body内添加该div对象
	loginWin = win;  //把创建好的DIV给VIEW记住 用于控制不能同时出现2个浮动框
	
	//requestPageGETvalue('room_info_content'+house_id,'GET_HOUSE_ROOM_INFO','asp','house_id='+house_id+"&act="+act);
	
}

//滚动条触发函数
window.onscroll = moveWin;

//moveWin函数功能，首先判断loginWin是否存在，如果存在则让其在滚动条滚动的过程中一直居中；
//思路 如何让其弹出的loginWin窗口触发时就能居中显示，先得到body的clientHeight高度来减去loginWin的高度在除2在加上滚动条所滚动的象素；
function moveWin(){
	if(loginWin!=null){
		var sc;
		//如果页面的头加了W3C标准，IE 版本对下面取值会有区别；
		//2008新版本将使用如下规范；
		if (typeof(window.pageYOffset) != 'undefined')
		{
			sc = window.pageYOffset; 
		} 
		else if (typeof(document.compatMode) != 'undefined' && document.compatMode != 'BackCompat')
		{
			sc = document.documentElement.scrollTop; 
		} 
		else if (typeof(document.body) != 'undefined')
		{
			sc = document.body.scrollTop; 
		}
		
		var bodyC = parseInt(getPageSizeLogin('c'));
		var sHeight = 200;
		document.getElementById("loginWin").style.top=parseInt(sc+((bodyC-sHeight)/2)) + "px";
		
	}
}

function removeLoginWin(){
	if(loginWin!=null){
		document.body.removeChild(loginWin);
		loginWin = null;
	}
}

function getPageSizeLogin(i){
	//检测浏览器的渲染模式
	var body = (document.compatMode&&document.compatMode.toLowerCase() == "css1compat")?document.documentElement:document.body;

	//var bodyOffsetWidth = 0;
	//var bodyOffsetHeight = 0;
	//var bodyScrollWidth = 0;
	//var bodyScrollHeight = 0;
	var pageDimensions = 0;
   	if(i=="h"){    
		pageDimensions=body.scrollHeight; 
	}else if(i=="w"){
		pageDimensions=body.clientWidth;
	}else if(i=="c"){
		pageDimensions=body.clientHeight;
	}
	//pageDimensions[1]=body.clientWidth; 
       
	//bodyOffsetWidth=body.offsetWidth;
	//bodyOffsetHeight=body.offsetHeight;
	//bodyScrollWidth=body.scrollWidth;
	//bodyScrollHeight=body.scrollHeight;

	//if(bodyOffsetHeight > pageDimensions){
		//pageDimensions=bodyOffsetHeight;
	//}    
       
	//if(bodyOffsetWidth > pageDimensions[1]){
		//pageDimensions[1]=bodyOffsetWidth;
	//}    
       
	//if(bodyScrollHeight > pageDimensions){
		//pageDimensions=bodyScrollHeight; 
	//}     
       
	//if(bodyScrollWidth > pageDimensions[1]){
		//pageDimensions[1]=bodyScrollWidth;
	//}   
	return pageDimensions; 
}

//登陆表单给loginWin用的
function createLoginWin(){
	var str = "";
	str+='<table width="400" border="0" cellspacing="0" cellpadding="0" height="170">'
	str+='<tr>'
	str+='<td width="147" height="30"></td>'
	str+='<td width="253">&nbsp;</td>'
	str+='</tr>'
	str+='<tr>'
	str+='<td height="30" align="right">账 号：</td>'
	str+='<td><input id="userName" type="text" maxlength="15" style="width:150px; border:1px solid #0081C3; font-family:Arial;" /></td>'
	str+='</tr>'
	str+='<tr>'
	str+='<td height="30" align="right">密　码：</td>'
	str+='<td><input id="passWord" type="password" maxlength="15" style="width:150px; border:1px solid #0081C3; font-family:Arial;" /></td>'
	str+='</tr>'
	str+='<tr>'
	str+='<td colspan="2" align="center"><div id="loginMsg" style="color:#F00; font-size:12px;"></div></td>'
	str+='</tr>'
	str+='<tr>'
	str+='<td height="40" colspan="2" align="center"><input id="lg" type="button" value="登陆" onclick="checkLoginwin();" />　<input type="button" value="撤消" onclick="resetLoginwin();" />　<a href="/user/register/">注册</a></td>'
	str+='</tr>'
	str+='<tr>'
	str+='<td height="10">&nbsp;</td>'
	str+='<td>&nbsp;</td>'
	str+='</tr>'
	str+='</table>'
	
	return str;
}

function resetLoginwin(){
	document.getElementById("userName").value="";
	document.getElementById("passWord").value="";
}


function checkLoginwin(){
	var username = document.getElementById("userName").value;
	var password = document.getElementById("passWord").value;
	if(username==""){
		alert("请输入账号!");
		document.getElementById("userName").focus();
		return false;
	}else if(password==""){
		alert("请输入密码!");
		document.getElementById("passWord").focus();
		return false;
	}else{
		Login("/AJAX_login",username,password,"lg");
	}
}


function checkLoginwin2(userName,passWord,url,lg){
	
	var username = document.getElementById(userName).value;
	var password = document.getElementById(passWord).value;
	if(username==""){
		alert("请输入账号!");
		document.getElementById(userName).focus();
		return false;
	}else if(password==""){
		alert("请输入密码!");
		document.getElementById(passWord).focus();
		return false;
	}else{
		Turl=url;
		Login("/AJAX_login",username,password,lg);
	}
}


function Login(path,username,password,lg) {
	
	
	var url=path+".asp?time="+new Date().getTime();
	
	var QueryString="username="+username+"&password="+password;
	
	createXMLHttpRequest();
	
	document.getElementById(lg).setAttribute("disabled","disabled");//将按钮置为锁定
	
	
	xmlHttp.open("POST", url, true);
	
	xmlHttp.onreadystatechange = LoginRsp;
	
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	xmlHttp.send(QueryString);
	
}

function LoginRsp() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var msg=xmlHttp.responsetext
			
			if(msg=="1"){
				document.getElementById("loginMsg").innerHTML="用户名或密码错误！";
			}else if(msg=="2"){
				document.getElementById("loginMsg").innerHTML="用户名和密码不能为空！"
			}else if(msg=="3"){
				if(Turl==null){Turl="/User/Index.asp"}
				window.location=Turl;
			}
			document.getElementById("lg").removeAttribute("disabled");//将按钮置为击活
		}
		else {
			alert("服务器异常!");
			document.getElementById("lg").removeAttribute("disabled");//将按钮置为击活
		}
    }
	//else {
		//document.getElementById("message").innerHTML=WaitPic;
	//}
}
