var xmlHttpRequest;   
function createXMLHttpRequest(){   
	try  
	{
		xmlHttpRequest=new XMLHttpRequest();   
	}   
	catch (e)   
	{
		try  
		{   
			xmlHttpRequest=new ActiveXObject("Msxml2.XMLHTTP");   
		}   
		catch (e)   
		{
			try  
			{   
				xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");   
			}   
			catch (e)   
			{   
				alert("您的浏览器不支持AJAX！");   
				return false;   
			}   
		}   
	}   
}   

function doMyGet(url,param,ok_func,err_func){ 
	createXMLHttpRequest(); 
	xmlHttpRequest.onreadystatechange = function (){   
		if(xmlHttpRequest.readyState == 4){   
			if(xmlHttpRequest.status == 200){   
				var res = xmlHttpRequest.responseXML;   
				//alert(res);
				if(ok_func){
					ok_func(res);
				}   
			}else{   
				alert("请求页面异常");
				if(err_func){
					err_func();
				}
			}   
		} else{
			//alert(xmlHttpRequest.readyState);
		}  
	}
	//xmlHttpRequest.open("GET",url+"?"+param+"&ttiimmee="+(new Date()).getTime(),true);   
	//xmlHttpRequest.send(null);
	param += "&ttiimmee="+(new Date()).getTime();
	xmlHttpRequest.open("POST",url,true);  
	xmlHttpRequest.setRequestHeader("Content-Length",param.lenght);
	xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	xmlHttpRequest.send(param);
}

function saveCookie(name,value,expires,path,domain,secure){
	var strCookie = name + "=" + value;
	if(expires){
		var curTime = new Date();
		//curTime.setTime(curTime.getTime()+expires*24*60*60*1000);
		curTime.setTime(curTime.getTime()+expires*1000);
		strCookie += "; expires=" + curTime.toGMTString();
	}
	domain = "516008.com";
	strCookie += (path)?"; path=" + path : "";
	strCookie += (domain)?"; domain=" + domain : "";
	strCookie += (secure)?"; secure" : "";
	document.cookie = strCookie;
}

function getCookie(name){
	var strCookies = document.cookie;
	var cookieName = name + "=";
	var valueBegin,valueEnd,value;
	valueBegin = strCookies.indexOf(cookieName);
	if(valueBegin==-1){
		return null;
	}
	valueEnd = strCookies.indexOf(";",valueBegin);
	if(valueEnd==-1){
		valueEnd = strCookies.length;
	}
	value = strCookies.substring(valueBegin+cookieName.length,valueEnd);
	return value;
}

function checkCookieExist(name){
	if(getCookie(name)){
		return true;
	}else{
		return false;
	}
}

function deleteCookie(name,path,domain){
	domain = "516008.com";
	var strCookie;
	if(checkCookieExist(name)){
		strCookie = name + "=";
		strCookie += (path) ? "; path=" + path : "";   
		strCookie += (domain) ? "; domain=" + domain : "";   
		strCookie += "; expires=Thu, 01-Jan-70 00:00:01 GMT";   
		document.cookie = strCookie; 
	}
}

function login_ok_func(data){
	var xmlRoot = data.documentElement;
	try 
	{ 
		var xmlItem=xmlRoot.getElementsByTagName("result"); 
		//alert(xmlItem[0].firstChild.data); 
		var result = parseInt(xmlItem[0].firstChild.data);
		//alert(result);
		if(result==0){
			var uin = (xmlRoot.getElementsByTagName("uin"))[0].firstChild.data;
			var check = parseInt((xmlRoot.getElementsByTagName("check"))[0].firstChild.data);
			var login_cookie = document.all.login_cookie.checked;
			if(login_cookie){
				saveCookie("m_uin",""+uin,60*60*24*7);
				saveCookie("m_check",""+check,60*60*24*7);
			}else{
				saveCookie("m_uin",""+uin);
				saveCookie("m_check",""+check);
			}
			window.location.reload(true);
			
			var url = "http://"+document.domain+"/autologin.php";
			doMyGet(url,"",null,null);
	
		}else{
			var errmsg = xmlRoot.getElementsByTagName("errmsg"); 
			alert(errmsg[0].firstChild.data);
		}
	} 
	catch(e) 
	{ 
		alert(e.message); 
	} 
}
function login_err_func(){
	alert("服务器繁忙,登陆失败");
}

function doLogin(){
	var login_uin = document.all.login_uin.value;
	var login_password = document.all.login_password.value;
	var login_cookie = document.all.login_cookie.checked;
	//alert(login_uin+" "+login_password+" "+login_cookie);
	var param = "";
	param += "&m_uin="+login_uin;
	param += "&m_password="+login_password;
	var url = "http://"+document.domain+"/login.php";
	//alert(url);
	doMyGet(url,param,login_ok_func,login_err_func);
}
function doLogout(){
	deleteCookie("m_uin");
	deleteCookie("m_check");
	window.location.reload(true);
}


function outputWelcome()
{
	var m_uin = '';
	var m_check = 0;
	var m_flag = true;
	if(checkCookieExist("m_uin")){
		m_uin = getCookie("m_uin");
	}else{
		m_flag = false;
	}
	if(checkCookieExist("m_check")){
		m_check = getCookie("m_check");
	}else{
		m_flag = false;
	}
	var url = "http://"+document.domain+"/register.html";
	var mypage = "http://"+document.domain+"/user.php"+"?uin="+m_uin;
	var info = "http://"+document.domain+"/info.html";
	var beginurl = "http://"+document.domain;
	if(m_flag){
		var welcome = "<font style='FONT-SIZE: 16pt;'><br></font>用户:"+m_uin+"&nbsp;|&nbsp;&nbsp;<a href='"+beginurl+"'>首页</a> |&nbsp;&nbsp;<a href='"+url+"'>注册</a> | &nbsp;&nbsp;<a href='#' onclick='doLogout()'>退出</a>";
		welcome+="<br><font style='FONT-SIZE: 5pt;'><br></font>";
		welcome+="<a href='"+info+"'>修改资料</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='"+mypage+"'>我的主页</a>";
		welcome+="<br><font style='FONT-SIZE: 5pt;'><br></font>";
		welcome+="客服QQ：&nbsp;<a href=\"http://sighttp.qq.com/cgi-bin/check?sigkey=230616e4b4b1e41c8b84305e3127dada8763544f8c37f393d0151bf3bd52423d\"; target=_blank; onclick=\"var tempSrc='http://sighttp.qq.com/wpa.js?rantime='+Math.random()+'&sigkey=230616e4b4b1e41c8b84305e3127dada8763544f8c37f393d0151bf3bd52423d';var oldscript=document.getElementById('testJs');var newscript=document.createElement('script');newscript.setAttribute('type','text/javascript'); newscript.setAttribute('id', 'testJs');newscript.setAttribute('src',tempSrc);if(oldscript == null){document.body.appendChild(newscript);}else{oldscript.parentNode.replaceChild(newscript, oldscript);}return false;\"><img border=\"0\" SRC='http://wpa.qq.com/pa?p=1:46431079:42' alt=\"\"></a>&nbsp;";
		welcome+="&nbsp;&nbsp;<a href=\"http://sighttp.qq.com/cgi-bin/check?sigkey=8a137c92331ece7c6c35446f24b1c3dfcaf077f3d86f01790348fd90733a9733\"; target=_blank; onclick=\"var tempSrc='http://sighttp.qq.com/wpa.js?rantime='+Math.random()+'&sigkey=8a137c92331ece7c6c35446f24b1c3dfcaf077f3d86f01790348fd90733a9733';var oldscript=document.getElementById('testJs');var newscript=document.createElement('script');newscript.setAttribute('type','text/javascript'); newscript.setAttribute('id', 'testJs');newscript.setAttribute('src',tempSrc);if(oldscript == null){document.body.appendChild(newscript);}else{oldscript.parentNode.replaceChild(newscript, oldscript);}return false;\"><img border=\"0\" SRC='http://wpa.qq.com/pa?p=1:352017797:42' alt=\"\"></a>";
		document.write(welcome);
	}else{
		var welcome = "";
		welcome+="<font style='FONT-SIZE: 16pt;'><br></font>";
		welcome+="用户名:<INPUT TYPE='text' NAME='login_uin' size='12' maxlength='12'>&nbsp;密码:<INPUT TYPE='password' name='login_password' size='12' maxlength='24'>";
		welcome+="<br>记录登陆信息一周:<INPUT TYPE='checkbox' NAME='login_cookie'>&nbsp;&nbsp;&nbsp;&nbsp;<a href='#' onclick='doLogin();' >登陆</a>";
		welcome += " |&nbsp;&nbsp;<a href='"+url+"'>注册</a>";
		document.write(welcome);
	}
}

function getDate(str){
	var tmpDate = new Date();
	//2010-03-19 00:18:00
	tmpDate.setYear(parseInt(str));
	var tmp = str.indexOf("-");
	if(tmp>0){
		str = str.substring(tmp+1);
		var month = parseInt(str)-1;
		tmpDate.setMonth(month);
		
		tmp = str.indexOf("-");
		if(tmp>0){
			str = str.substring(tmp+1);
			var day = parseInt(str);
			tmpDate.setDate(day);
			
			tmp = str.indexOf(" ");
			if(tmp>0){
				str = str.substring(tmp+1);
				var hour = parseInt(str);
				tmpDate.setHours(hour);
				
				tmp = str.indexOf(":");
				if(tmp>0){
					str = str.substring(tmp+1);
					var m = parseInt(str);
					tmpDate.setMinutes(m);
					
					tmp = str.indexOf(":");
					if(tmp>0){
						str = str.substring(tmp+1);
						var s = parseInt(str);
						tmpDate.setSeconds(s);
					}
				}
			}
		}
	}
	
	return tmpDate;
}
