var MyHost=parent.location.hostname;		//本机地址
var MyPort;
var myself;						//本人ID
var isleave=false;				//用户是否离开
var isFiltration=true;			//是否分屏过滤
var isWriteHeader = false;

function getNowDate() {
	var d=new Date();
	var y=d.getYear();
	if ( y < 200 ) y=y+1900;
	return y+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日"
}

function getNowTime() {
        var d;
        var nowtime="";

        d=new Date();
        nowtime += d.getHours() + ":";
        if(d.getMinutes().toString().length==1) {
                nowtime += "0" + d.getMinutes() + ":";
        } else {
                nowtime += d.getMinutes() + ":";
        }
        if(d.getSeconds().toString().length==1) {
                nowtime += "0" + d.getSeconds();
        } else {
                nowtime += d.getSeconds();
        }
	return nowtime;
}

/* 直播发言 */
function Zhibo(msg, color, font, size, name, score, score_t) {
	var color_str="";
	var size_str="";
	var font_str="";
	var name_str="";

	if(color) color_str=' color='+color;
	if(size) size_str=' size='+size;
	if(font) font_str=' face='+font;
	if(name) name_str='<font color=blue>'+name+'</font>： ';
	msg=name_str+'<font'+color_str+size_str+font_str+'>'+msg+'</font> '+score_t+'<BR>\n';
	parent.ChatFrame.document.writeln(msg);
	try {
		parent.ListFrame.score.innerText=score;
	} catch(e) {}
	parent.ChatFrame.scroll(0, 65000);
}

function displayPersonOnline(personcount)
{
	try 
	{
		parent.FormFrame.person_count.innerText=personcount;
	} catch(e) {}
}

/* 直播发言 */

/* 用户发言 */
function displayMessage(from, fromnick, to, tonick, msg, color, font, type){
	var outstr="";
	var display=true;

	var nowtime="("+getNowTime()+")";
	if(!fromnick) fromnick=from;
	if(!tonick) tonick=to;

	var fontstr = "";
	if(font) {
		fontstr = " face=\""+font+"\"";
	}

	var pattern=/^[\/]/; 
	if(pattern.test(msg)) {
		var emote=getEmote(msg.substring(1, msg.length), from, fromnick, to, tonick);
		if(!emote) return;
		outstr="<font color='"+color+"'"+fontstr+">"+getEmote(msg.substring(1, msg.length), from, fromnick, to, tonick)+"</font> "+nowtime;
		if(outstr) {
			outstr+="<BR>";
		}
	}
	if(!outstr) {
		if(!to) {
			outstr=getUserUrl(from, fromnick, 0, 0)+" 说： <font color='"+color+"'"+fontstr+">"+msg+"</font>"+nowtime+"<BR>";
		} else if(type==2) {
			outstr="【私】"+getUserUrl(from, fromnick, 0, 0)+" 对 "+getUserUrl(to, tonick, 0, 0)+" 说： <font color='"+color+"'"+fontstr+">"+msg+"</font>"+nowtime+"<BR>";
		} else {
			outstr=getUserUrl(from, fromnick, 0, 0)+" 对 "+getUserUrl(to, tonick, 0, 0)+" 说： <font color='"+color+"'"+fontstr+">"+msg+"</font>"+nowtime+"<BR>";
		}
	}
	if(type==0 || type==null) {
		this.MyChatFrame.document.writeln(outstr);
		//alert(outstr);
	} 
	else if(type==2)
	{
		modifyNick(from, tonick, 1, fromnick);
	}
	else if(type==3)
	{
		announce(msg, 1, "") ;
	}
	else {
		this.ChatFrame.document.writeln(outstr);
	}
	parent.MyChatFrame.scroll(0, 65000);
}

function displayChatMessage(msg){
	this.MyChatFrame.document.close();
	this.MyChatFrame.document.open();
	this.MyChatFrame.document.writeln('<html>');
	this.MyChatFrame.document.writeln('<head>');
	this.MyChatFrame.document.writeln('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">');
	this.MyChatFrame.document.writeln('<link href="js/chat.css" rel=stylesheet>');
	this.MyChatFrame.document.writeln('</head>');
	this.MyChatFrame.document.writeln('<body class=bj1_4 bgcolor="#EFEFEF" onLoad="parent.changeColor(parent.ColorID);">');
	this.MyChatFrame.document.writeln('<hr size="1">');
	this.MyChatFrame.document.writeln('<p class="p9" align="left">');
	parent.MyChatFrame.document.writeln(msg);
	parent.MyChatFrame.document.writeln('<script type="text/javascript" language="javascript">window.scroll(0, 65000)</script>');
	parent.MyChatFrame.scroll(0, 65000);
}
/* 用户发言 */

/* 发送表情信息 */
function sendEmote(cmd){
	if (cmd=='') {
		this.FormFrame.document.inputform.msg.focus();
		this.FormFrame.document.inputform.emote.options[0].selected=true;
	} else {
		this.FormFrame.document.inputform.emote.options[0].selected=true;
		this.FormFrame.document.inputform.msg.value="/"+cmd;
		this.FormFrame.document.inputform.SubmitButton.click();
	}
}
/* 发送表情信息 */

/* 得到表情内容 */
function getEmote(key, from, fromnick, to, tonick) {
	var self=getUserUrl(from, fromnick, 0, 0);
	var target=getUserUrl(to, tonick, 0, 0);
	var str;
	var s="%self%";
	var t="%target%";
	var outEmote="";
	var outTargetEmote="";
	var tempKey="";

	var re=/^[a-z 0-9 \-():?@.]*$/i;
	if(re.test(key)) {
		outEmote=Emote[key];
		outTargetEmote=TargetEmote[key];
	} else {
		tempKey=emoteKey[key];
		if(tempKey) {
			outEmote=Emote[tempKey];
			outTargetEmote=TargetEmote[tempKey];
		}
	}

	if(to && outTargetEmote!="") {
		str=outTargetEmote;
		while(str.match(s)) {
			str=str.replace(s, self);
		}
		while(str.match(t)) {
			str=str.replace(t, target);
		}
	} else if(!to && outEmote!="") {
		str=outEmote;
		while(str.match(s)) {
			str=str.replace(s, self);
		}
	} else {
		return;
	}
	return str;
}
/* 得到表情内容 */

/* 得到用户名链接 */
function getUserUrl(name, nick, islogined, color) {
	var loginedsign="";
	var colornick;
	var str="";

	if(islogined==1) {
		loginedsign="<font color=\"red\">*</font>";
	}

	if(color) {
		colornick="<font color=\""+color+"\">"+nick+"</font>";
	} else {
		colornick=nick;
	}
	if(myself==name) {
		str="[<a href=\"javascript:parent.choose('"+name+"', '"+nick+"');\" target=\"FormFrame\">"+loginedsign+colornick+"</a>]";
	} else {
		str="<a href=\"javascript:parent.choose('"+name+"', '"+nick+"');\" target=\"FormFrame\">"+loginedsign+colornick+"</a>";
	}
	return str;
}
/* 得到用户名链接 */

/* 发通知警告 */
function announce(msg, t, admin) {
	if(!t) return false;
	var timestr='('+getNowTime()+')';
	switch(t) {
		case 1:
			this.MyChatFrame.document.writeln("<font color=green>【通知】</font>"+msg+' '+timestr+"<br>");
			break;
		case 2:
			this.MyChatFrame.document.writeln("<font color=red>【警告】</font>"+msg+' '+timestr+"<br>");
			break;
		case 3:
			this.ChatFrame.document.writeln("<font color=red>【警告】</font>"+msg+' '+timestr+"<br>");
			break;
		case 4:
			this.ChatFrame.document.writeln("<font color=green>【系统通知】</font>"+msg+' '+timestr+"<br>");
			break;
		case 5:
			this.MyChatFrame.document.writeln("<font color=green>【注意】</font>"+msg+' '+timestr+"<br>");
			break;
		case 6:
			this.ChatFrame.document.writeln("<font color=green>【通知】</font>"+msg+' '+timestr+"<br>");
			break;
		case 7:
			this.ChatFrame.document.writeln("<font color=green>【管理员消息】</font><font color=red>"+msg+'</font> by '+admin+' '+timestr+"<br>");
			break;
		case 8:
			this.ChatFrame.document.writeln("<font color=green>【求助信息】</><font color=red>"+msg+"</font>" +timestr+"<br>");
			break;
		default:
			break;
	}
}
/* 发通知警告 */


/* 改变颜色 */
var ColorID=0;					//颜色ID
function changeColor(colorid) {

//alert("ColorID=" + colorid)
	
	if(colorid) {
		var ChatFrameColor="";
		var MyChatFrameColor="";
		var BannerColor="";
		var FormFrameColor="";
		var ListFrameColor="";
		
		switch(colorid) {
			case "1":
				ChatFrameColor="EDFFD9";
				MyChatFrameColor="F6FDED";
				BannerColor="4BC115";
				FormFrameColor="A2D576";
				ListFrameColor="F5FFDD";
				break;
			case "2":
				ChatFrameColor="E8D9FF";
				MyChatFrameColor="F8EBFD";
				BannerColor="8115CB";
				FormFrameColor="CD99FE";
				ListFrameColor="DBC6FF";
				break;
			case "3":
				ChatFrameColor="F4F4F4";
				MyChatFrameColor="FFFFFF";
				BannerColor="494949";
				FormFrameColor="A3A4A6";
				ListFrameColor="D8D8D8";
				break;
			case "4":
				ChatFrameColor="E8EFFF";
				MyChatFrameColor="FFFDF4";
				BannerColor="4358D5";
				FormFrameColor="729FE2";
				ListFrameColor="BBD3F5";
				break;
			case "5":
				ChatFrameColor="FFFCB5";
				MyChatFrameColor="FFFCEB";
				BannerColor="FE0000";
				FormFrameColor="FFC600";
				ListFrameColor="FFF8D3";
				break;
			case "6":
				ChatFrameColor="FBF9E4";
				MyChatFrameColor="FFFFED";
				BannerColor="A68100";
				FormFrameColor="DBC768";
				ListFrameColor="FFFED8";
				break;
			case "7":
				ChatFrameColor="CAE8FF";
				MyChatFrameColor="EFF6FF";
				BannerColor="288EFA";
				FormFrameColor="6FC0FF";
				ListFrameColor="B3D9FF";
				break;
			case "8":
				ChatFrameColor="FFEAFB";
				MyChatFrameColor="FFF8FF";
				BannerColor="E7309C";
				FormFrameColor="F299D3";
				ListFrameColor="FFD6F6";
				break;
			default:
				ColorID=0;
				break;
		}
		this.ChatFrame.document.bgColor=ChatFrameColor;
		this.MyChatFrame.document.bgColor=MyChatFrameColor;
		//alert("111111111111")
		//this.banner.document.bgColor=BannerColor;
		this.FormFrame.document.bgColor=FormFrameColor;
		this.ListFrame.document.bgColor=ListFrameColor;
		ColorID=colorid;
		this.FormFrame.document.inputform.skin.options[0].selected=true;
	}
	//alert("ColorID" + ColorID)
	
}
/* 改变颜色 */


function reloadFrame() {
	isWriteHeader = false;
	writeHeader();
}


function reloadClientFrame() {
	this.MyChatFrame.document.close();
	this.MyChatFrame.document.open();
	this.MyChatFrame.document.writeln('<html>');
	this.MyChatFrame.document.writeln('<head>');
	this.MyChatFrame.document.writeln('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">');
	this.MyChatFrame.document.writeln('<link href="js/chat.css" rel=stylesheet>');
	this.MyChatFrame.document.writeln('</head>');
	this.MyChatFrame.document.writeln('<body class=bj1_4 bgcolor="#EFEFEF" onLoad="parent.changeColor(parent.ColorID);">');
	this.MyChatFrame.document.writeln('<hr size="1">');
	this.MyChatFrame.document.writeln('<p class="p9" align="left">');
}



/* 用户发言记录 */
var nowTalkID=0;
var searchTalkID=0;
var talkContent=new Array();

function frontTalk() {
	if(searchTalkID > 0) {
		searchTalkID--;
	}
	if(talkContent[searchTalkID]) {
		this.FormFrame.document.inputform.msg.value=talkContent[searchTalkID];
	} else {
		this.FormFrame.document.inputform.msg.value="";
	}
}

function nextTalk() {
	if(nowTalkID > searchTalkID) {
		searchTalkID++;
	}
	if(talkContent[searchTalkID]) {
		this.FormFrame.document.inputform.msg.value=talkContent[searchTalkID];
	} else {
		this.FormFrame.document.inputform.msg.value="";
	}
}
/* 用户发言记录 */

/* 用户改变发言对象 */
function choose(name, nick){
	if(!name || name=="所有人") {
		nick="所有人";
		name="所有人";
	}
	this.FormFrame.document.inputform.talkto.value=name;
	this.FormFrame.document.inputform.talktonick.value=nick;
	this.FormFrame.document.inputform.msg.focus();
}
/* 用户改变发言对象 */

/* 用户提交发言信息的验证和信息编辑 */
function checksay(){

	if (isleave) {
		announce('你已经退出');
		return false;
	}
	if(this.FormFrame.document.inputform.msg.value==''){
		announce('发言内容为空', 5);
		this.FormFrame.document.inputform.msg.focus();
		return false;
	}
	if ((this.FormFrame.document.inputform.msg.value==this.FormFrame.document.fh.message.value) && ((this.FormFrame.document.inputform.talkto.value==this.FormFrame.document.fh.talkto.value) || (this.FormFrame.document.inputform.talkto.value=="所有人" && this.FormFrame.document.fh.talkto.value=="_all"))){
		announce('发言重复', 5);
		this.FormFrame.document.inputform.msg.value="";
		this.FormFrame.document.inputform.msg.focus();
		return false;
	}
	this.FormFrame.document.fh.action='userchat.jsp';				//改变操作
	this.FormFrame.document.fh.message.value=this.FormFrame.document.inputform.msg.value;

	this.FormFrame.document.inputform.msg.value ='';
	this.FormFrame.document.fh.color.value=this.FormFrame.document.inputform.color.value;
	this.FormFrame.document.fh.font.value=this.FormFrame.document.inputform.font.value;
	if(this.FormFrame.document.inputform.talkto.value=="所有人") {
		this.FormFrame.document.fh.talkto.value="_all";
	} else {
		this.FormFrame.document.fh.talkto.value=this.FormFrame.document.inputform.talkto.value;
	}
	talkContent[nowTalkID]=this.FormFrame.document.fh.message.value;
	nowTalkID++;
	searchTalkID=nowTalkID;
	this.FormFrame.document.fh.submit();
	this.FormFrame.document.inputform.msg.focus();
	//alert("sfasfasdfasdfsad")
	return false;
}
/* 用户提交发言信息的验证和信息编辑 */

/* 用户离开聊天室 */
function out(type){
	if (isleave) {
		announce('你已经退出', 5);
		return;
	}else {
//		this.FormFrame.document.fh.target='h';
//		this.FormFrame.document.fh.message.value='';
//		this.FormFrame.document.fh.talkto.value='';
//		this.FormFrame.document.fh.submit();
		this.FormFrame.document.inputform.SubmitButton.disabled=true;
		isleave=true;
//	
		if(type == 3)
		{
			alert("你已经被管理员请出了直播室！");
			isleave = true;
		}
		this.close();
		return;
	}
}
/* 用户离开聊天室 */


/* 用户动作 */
function doAction(a) {
	if(a) {
		switch(a) {
			case "/pFrame":
				modifyFrame("pFrame");
				this.FormFrame.document.inputform.selectAction.options["0"].selected=true;
				break;
			case "/uFrame":
				modifyFrame("uFrame");
				this.FormFrame.document.inputform.selectAction.options["0"].selected=true;
				break;
			case "/cFrame":
				modifyFrame("cFrame");
				this.FormFrame.document.inputform.selectAction.options["0"].selected=true;
				break;
			case "/myUrl":
				var myurl='http://'+MyHost+':'+MyPort+'/login';
				prompt("此房间网址", myurl);
				this.FormFrame.document.inputform.selectAction.options["0"].selected=true;
				break;
			default:
				this.FormFrame.document.inputform.selectAction.options["0"].selected=true;
				break;
		}
	} else {
		this.FormFrame.document.inputform.selectAction.options["0"].selected=true;
	}
}

/* 改变页面Frame的形势 */
function modifyFrame(a) {
	if(a=="uFrame") {
		this.TopFrame.cols="*,30%";
		this.TopFrame.rows="*";
		this.FormFrame.document.inputform.it.checked=true;
	} else if(a=="pFrame") {
		this.TopFrame.cols="*";
		this.TopFrame.rows="*,30%";
		this.FormFrame.document.inputform.it.checked=true;
	} else if(a=="cFrame") {
		this.TopFrame.cols="*";
		this.TopFrame.rows="*,0";
		this.FormFrame.document.inputform.it.checked=false;
	}
}

function mFrame() {
	if(this.FormFrame.document.inputform.it.checked) {
		modifyFrame("pFrame");
		this.chatUserMsg_win.location.href="http://iptv.zgzcw.com/chatUserMsg.htm";
	} else {
		modifyFrame("cFrame");
		this.chatUserMsg_win.location.href="about:blank"
	}
}
/* 改变页面Frame的形势 */

/* 改名字 */
function changeNick() {
	var nick=prompt("请输入新的昵称", "");
	if(nick==null) return;
	//alert(nick);
	this.FormFrame.document.fh.action='nickchange.jsp';
	//this.FormFrame.document.fh.talkto.value=nick;
	this.FormFrame.document.fh.message.value=nick;
	this.FormFrame.document.fh.submit();
	//alert(nick);
}

/* 修改用户昵称 */
function modifyNick(username, newnick, t, oldnick) {
	var outmsg=getUserUrl(username, oldnick)+" 修改昵称为 "+getUserUrl(username, newnick)+"。";
	announce(outmsg, 1);
}
/* 修改用户昵称 */

function ZhiboD(msg) {
	this.ChatFrame.document.close();
	this.ChatFrame.document.open();
	this.ChatFrame.document.writeln('<html>');
	this.ChatFrame.document.writeln('<head>');
	this.ChatFrame.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=gb2312">');
	this.ChatFrame.document.writeln('<link href="js/chat.css" rel=stylesheet>');
	this.ChatFrame.document.writeln('</head>');
	this.ChatFrame.document.writeln('<body class=bj1_0 topmargin="0" marginwidth="0" marginheight="0" bgcolor="#EFEFEF" background="">');
	this.ChatFrame.document.writeln('<table width="100%" border=0 class=bj1_0>');

	this.ChatFrame.document.writeln('<tr><td align=center>欢迎您进入[<font color=red>直播室</font>]</td></tr>');
	this.ChatFrame.document.writeln('</table>');
	this.ChatFrame.document.writeln('<hr size="1">');
	this.ChatFrame.document.writeln('<p class="p9" align="left">');
	parent.ChatFrame.document.writeln(msg);
	parent.ChatFrame.document.writeln('<script type="text/javascript" language="javascript">window.scroll(0, 65000)</script>');
	parent.ChatFrame.scroll(0, 65000);
}

/* 用户过多，重新定向 */
function relogin(h, p) {
	this.location="http://"+h+":"+p+"/login";
}

var lastid = 0;
var liveLastid = 0;

//parent.writeHeader();
function writeMsg(d) 
{
		parent.writeHeader();
		
		if(d.msg.length > 0)
		{
			for (i = 0;i< d.msg.length; i++)
			{
				msg = d.msg[i];
				
				if(msg.type >= 100)
				{
					
					var msgid = parseInt(msg.id)
					if(liveLastid < msgid)
					{
						
						liveLastid =  Math.max(liveLastid,msgid);
						parent.Zhibo(msg.msg, msg.color, msg.font, '', msg.from, msg.tonick, '');
						
					}
					
				}
				else if(msg.type == 10)
				{
					if(msg.to == selfid)
					{
						//alert("msg.to --->"+msg.to);
						//alert("selfid---->"+selfid);
						alert(msg.msg);
						parent.out(msg.type);
					}
				}
				else
				{
					
					
					var msgid = parseInt(msg.id)
					if(lastid < msgid)
					{
						lastid =  Math.max(lastid,msgid);
						parent.displayMessage(msg.from, msg.fromnick, msg.to, msg.tonick, msg.msg, msg.color, msg.font, msg.type);								
					}

				}

			}
			
		}
    }