//--------------------------AJAX function-----------------------------
var http_request = false;

function getmyxml(url) {
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		window.alert('浏览器无法正确获得XML对象！');
		return false;
	}
	http_request.onreadystatechange = WriteContents;
	http_request.open('GET', url, true);
	http_request.send(null);

}
function WriteContents() {
	var htmlbody='';
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			var sess = xmldoc.getElementsByTagName('sess').item(0);
			var user = xmldoc.getElementsByTagName('user').item(0);
			var img = xmldoc.getElementsByTagName('img').item(0);
			var mess = xmldoc.getElementsByTagName('mess').item(0);
			var club = xmldoc.getElementsByTagName('club').item(0);
			
			 if (sess!=null){
					defaulthtml=gethtmlbody(user,img,sess,mess,"default");
					if(sess.firstChild.data!=0){						
						htmlbody=gethtmlbody(user,img,sess,mess,"login");						
						if(club.firstChild.data!=0){							
							g('space_login').innerHTML= htmlbody;
						}else{
							makeclub(wrong["UE107"],htmlbody,defaulthtml);
						}
					}else{							
						g('space_login').innerHTML= defaulthtml;
						if (user.firstChild.data!=0)
						{
							g('username').style.background='';
							g('username').value=user.firstChild.data;
						}
					}										
			 } else {
				alert('无法建立与服务器通讯的 XMLHTTP 实例 !');
			 }
		}
	}
}
function makeclub(msg,htmlbody,defaulthtml){
	
	if (confirm(msg)) {
	   getmyxml('/passport/ScriptLogin.php?action=joinclub');
    }else {       
	   getmyxml('/passport/ScriptLogin.php?action=deny');
    }
	
}
function gethtmlbody(user,img,sess,mess,type){
	var htmlbody;
	if(type=="login"){
		htmlbody='<div id="login_img">';
		htmlbody+='    <a href="/passport/?mod=memcp&act=userinfo"  style="color:blue;" title="单击此处可以修改您的头像"><img src="'+img.firstChild.data+'" width="48" height="48"  border="0"/></a>';
		htmlbody+='</div>';
		htmlbody+='<div id="login_info">';
		htmlbody+='    <ul>';
		htmlbody+='        <li>欢迎您，</li>';
		htmlbody+='        <li>'+user.firstChild.data+' ! </li>';
		htmlbody+='        <li>[<a href="/passport/?mod=memcp&act=password"  style="color:blue;">修改密码</a> <a href="/passport/?mod=login&act=logout" style="color:red;">注销</a>]</li>';
		htmlbody+='    </ul>';
		htmlbody+='</div>';
		htmlbody+='<div id="login_mess"> <img src="/images/message.gif" width="12" height="12" />';
		if (mess.firstChild.data == 0) {
			htmlbody += '    您有<strong style="font-family:Arial;">0</strong>条未读消息!';
		}else {
			htmlbody += '    您有<a href="/myspace/message/" target="_blank">您有<strong style="font-family:Arial;color:red;">'+mess.firstChild.data+'</strong>条未读消息!</a>条未读消息!';
		}
		htmlbody+='</div>';	
	}else{
		htmlbody ='<form name="login" id="login" method="post" action="" onsubmit="userlogin(); return false;" style="margin:0px; padding:0px;">';
		htmlbody+='<iframe name="UserLogin" src="" frameborder="0" marginheight="0" marginwidth="0" id="UserLogin" width="0" height="0" scrolling="no" ></iframe>';
		htmlbody+='<input name="username" type="text"  id="username" style="width:100px; font-size:9pt; font-family:tahoma,Verdana,Arial; background:url(/images/username.gif) no-repeat left center;" value="" size="11" value="" onFocus="initlogin(this.value,0);" onBlur="editlogin(this.value,0);" />';
		htmlbody+='<input type="submit" name="submitbtn" id="submitbtn" value="登录"/>';
		htmlbody+='<br />';
		htmlbody+='<input name="password" type="password" id="password"  style="width:100px; font-size:9pt; font-family:tahoma,Verdana,Arial; background:url(/images/password.gif) no-repeat left center;" value="" onFocus="initlogin(this.value,1);"  onBlur="editlogin(this.value,1);" />';
		htmlbody+='<a href="/passport/index.php?mod=forgetpass" class="blue12_line">忘记密码</a>';
		htmlbody+='<label  for="remember_me"  style="cursor:pointer;"><input type="checkbox" name="remember_me"  id="remember_me" value="checkbox" />';
		htmlbody+='<span class="blue12_line">记住我</span></label>&nbsp;&nbsp; <a href="/passport/index.php?mod=register" class="red">获取通行证</a>';
		htmlbody+='</form>';
	}
							
	return htmlbody;
} 
function userlogin(){
	filePath=g('UserLogin').src;
	fileName = ((filePath.indexOf('/') > -1) ? filePath.substring(filePath.lastIndexOf('/')+1,filePath.length) : filePath.substring(filePath.lastIndexOf('\\')+1,filePath.length));
	src=fileName.substring(fileName.lastIndexOf('/')+1,fileName.length);

	if (src!="ScriptLogin.php"){		
		g('UserLogin').src="/passport/ScriptLogin.php";
	}else{
		UserLogin.forlogin();
	}
}
//辅助性函数
function g(sid){
	return document.getElementById(sid);
}
//用户接口
function changebtn(bs){
	if(bs==1){
		g('submitbtn').disabled=true;
		//g('submitbtn').value='<img src="/images/admin/ajax-loader.gif" width="15" height="15" hspace="3" />&nbsp;&nbsp;登录中请稍候……';
	}else{
		g('submitbtn').disabled=false;
		//g('submitbtn').value='<input type="image" name="submitbtn" src="/images/btn_login02.gif">';
	}
}
//初始表单的检查
function initlogin(value, lb){
	if(lb==0){
		if (value==''){
			g('username').style.background='';
		}
	}
	if(lb==1){
		if (value==''){
			g('password').style.background='';
		}
	}
}
function editlogin(value, lb){
	if(lb==0){
		if(value==''){
			g('username').style.background='url(/images/username.gif) no-repeat left center';
		}
	}
	if(lb==1){
		if(value==''){
			g('password').style.background='url(/images/password.gif) no-repeat left center';
		}
	}
}
