var arrFive = [0,0,0,0,0]; //数组用来保存弹出窗口的位置
var msgTitle = "";
var msgCont = "";
var msgLink = "";
var msgId = 0;
var msgFlag = "";
var boxId = -1;
var sysMsgId = getCookie("sysMsgId");
var userId = getDicCookie("ask1user","ask1user1userid");
var userType = getDicCookie("ask1user","ask1user1usertype");
var ajaxInter;

function NoticeGoNew(){
	arrFive[boxId] = 1;//窗口占位
	document.getElementById("noticeW").src = "http://www.120ask.com/notice/c.html?msgTitle="+escape(msgTitle)+"&msgCont="+escape(msgCont)+"&msgLink="+escape(msgLink)+"&msgId="+msgId+"&boxId="+boxId
}

function GetMsgFromHost()
{
	var userType = getDicCookie("ask1user","ask1user1usertype");
	if (userType != 2)
	{
		clearInterval(ajaxInter);
		return;
	}
	var i;
	for(i=0;i<5;i++)  //检查当前是否还有新窗口位置
	{
		if(arrFive[i] == "0")
		{	
			boxId = i;
			break;
		}
		boxId = -1;
	}
	if(boxId == -1)  //没有新窗口的位置，返回，不再创建信息窗口
		return;

	var arrTemp;
	var strTemp;
	var xhr = XHR();
	xhr.open("get","data.asp?userId="+userId+"&r="+(new Date()).getTime(),true);
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
		{
			if(xhr.status == 200)
			{
				if(xhr.responseText != "")
				{
					arrTemp = xhr.responseText.split("|");
					if (arrTemp.length == 4) //系统消息
					{
						msgTitle = arrTemp[1];
						msgCont = arrTemp[2];
						msgLink = arrTemp[3];
						document.cookie='sysMsgId='+arrTemp[0]+';domain=120ask.com;expires='+(new Date(2100,1,1)).toGMTString()+';path=/';
						NoticeGoNew();
					}
					else
					{
						msgId = arrTemp[0];
						msgTitle = arrTemp[1];
						msgFlag = parseInt(arrTemp[2]);
						msgCont = "";
						switch(msgFlag)
						{
							case 0:
								strTemp = "MsgXT.asp";
								break;
							case 1:
								strTemp = "MsgHY.asp";
								break;
							case 2:
								strTemp = "MsgMSR.asp";
								break;
						}
						msgLink = "/personalCenter/"+strTemp+"?id="+msgId;
						NoticeGoNew();
					}
					
				}
			}
		}
	}
	xhr.send(null);
}

function getDicCookie(name, property){
	var allcookie = getCookie(name);
	if(!allcookie)
		return "";
	var a = allcookie.split("&");
	for(var i = 0; i < a.length; i++){
		a[i] = a[i].split("=");
		if (a[i][0] == property){
			var r=("undefined"==typeof(a[i][1])||"undefined"==a[i][1])?"":a[i][1];
			return r;
		}
	}
	return "";
}


function getCookie(name){
	var allcookies = document.cookie;
	name = name + "=";
	var pos = allcookies.indexOf(name);
	if(pos == -1)
		return "";
	else{
		var start = pos + name.length;
		var end =  allcookies.indexOf(";", start);
		if (end == -1)
			end = allcookies.length;
		var value = allcookies.substring(start, end);
		value = unescape(value);
		if("undefined"==typeof(value)) value="";
		if("undefined"==value) value="";
		return value;
	}
}

function XHR(){
	var xhr;
	try{
		xhr = new XMLHttpRequest();	
	}catch(e){
		var a = ["MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
		for (var i=0;i<a.length;i++){
			try{
				xhr = new ActiveXObject(a[i]); 
				break;
			}catch(e){}
		}
	}
	return xhr;
}

if (userType == 2)
{
	setTimeout(GetMsgFromHost,2000);
	ajaxInter = setInterval(GetMsgFromHost,30000);
}
