//常用变量定义
var timerid = 0;
var timerid_showtip = 0;
var xmlhttp = false;
var msgmaxlenght = 500;
var refreshtime = 1500;
var showtiptime = 3000;
var rnd = Math.random();

//创建Ajax
function createXmlhttp(){
	try{ xmlhttp = new ActiveXObject('MSXML2.XMLHTTP.4.0'); }
	catch(e){
		try{ xmlhttp = new ActiveXObject('MSXML2.XMLHTTP.3.0'); }
		catch(e){	
			try{ xmlhttp = new ActiveXObject('MSXML2.XMLHTTP.2.6'); }
			catch(e){	
				try{ xmlhttp = new ActiveXObject('MSXML2.XMLHTTP'); }
				catch(e){	
					try{ xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); }
					catch(e){ xmlhttp = false; } 
				} 
			} 
		} 
	} 
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); }
	return xmlhttp;
}

//发送消息
function sendMsg(sMsgValue){ 
	var sMsg = $("sendmsgtb").value;
	sMsg = formatMsg(sMsg);
	var sType = "";
	switch(sMsgValue){
		case "leave":
			sMsg = "已离开!";
			sType = "system";
			break;
		case "talk":
			sType = "guest";
			break;
	}
	if(sMsg!=""){
		if(checkBadwords(sMsg)){
			if(sMsg.length > msgmaxlenght){
				alert("注意,信息内容不能超过" + msgmaxlenght + "个字!");
				$("sendmsgtb").focus();
			}else{
				rnd ++;
				xmlhttp = createXmlhttp();
				xmlhttp.onreadystatechange = function(){ 
					if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ 
						updateMsglist();
					} 
				} 
				xmlhttp.open("GET", "server.aspx?action=sendmsg&msg=" + escape(sMsg) + "&type=" + sType + "&session=" + rnd, true);
				xmlhttp.send(null);
				$("sendmsgtb").focus();
				$("sendmsgtb").innerText = "";
			}
		}else{
			alert("注意,发言不能包含敏感词语!");
		}
	}else{
		alert("请输入洽谈内容!");
		$("sendmsgtb").focus();
	}
}

//获取洽谈记录
function getMsglist(){ 
	rnd ++;
	xmlhttp = createXmlhttp();
	xmlhttp.onreadystatechange = function(){ 
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){ 
			if(xmlhttp.responseText!=""){
				addMsg(xmlhttp.responseText);
			}
		} 
	} 
	xmlhttp.open("GET", "server.aspx?action=getmsg&session=" + rnd, true);
	xmlhttp.send(null);
}

//删除访客
function closeWin(){ 
	xmlhttp = createXmlhttp();
	xmlhttp.open("POST", "server.aspx?action=logout", true);
	xmlhttp.send(null);
}

//欢迎消息
function comeintoMsg(){
	var sTalkstr = "<font color='#ff6600'>系统消息 " + getTimestr() + "</font><br /><img src='images/help.gif' height='14' hspace='3' border='0' /><font color='#000800'>欢迎您,有什么可以帮助您吗?</font><br />";
	addMsg(sTalkstr);
}

//添加信息
function addMsg(str){
    str = formatFace(str);
	var stab = this.receivertb.$("tb_message");
	var sLen = stab.rows.length - 1;
	var tr = stab.insertRow();
	if( sLen%2 ){ 
		tr.bgColor="#ffffff"; 
	}else{ 
		tr.bgColor="#fafafa"; 
	}
	var td = tr.insertCell(0);
	td.className = "msglist";
	td.innerHTML = str;
	this.receivertb.scroll(0,200000);
}

//保存洽谈记录
function saveasMsg(){
	try{
		var winSave = window.open();
		winSave.document.open("text/html","gb2312");
		winSave.document.write("<META http-equiv=Content-Type content='text/html; charset=gb2312'>");
		winSave.document.write(this.receivertb.document.body.outerHTML);
		winSave.document.execCommand ("SaveAs",true,"洽谈记录.html");
		winSave.close();
	}
	catch(e){
		alert(e);
	}
}

//打印洽谈记录
function printMsg(){	
	try{
		var winSave = window.open();
		winSave.document.open("text/html","gb2312");
		winSave.document.write("<META http-equiv=Content-Type content='text/html; charset=gb2312'>");
		winSave.document.write(this.receivertb.document.body.outerHTML);
		winSave.document.execCommand('print','',null);
		winSave.close();
	}
	catch(e){
		alert(e);
	}
}


//默认加载
function loadInit(){
	$("sys_ad").innerHTML = "暂时没有公告!";
	$("sendmsgtb").focus();
	comeintoMsg();
	setTimer();
}

//即时更新洽谈记录
function setTimer(){
	timerid = window.setInterval("updateMsglist()", refreshtime);
}

function updateMsglist(){
	window.clearInterval(timerid);
	getMsglist();
	setTimer();
}

//插入表情
function selectFace(){
	var e = sEvent(event);
	var w = 320;
	var h = 165;
	var x = get_left(e.srcElement) + 50;
	var y = get_top(e.srcElement) - h;
	var button = "ok";
	var content = "";
	var title = "<strong>选择表情</strong>";
	for(var i = 1; i <= 52; i++){
	    content += "<img src='images/expression/"+i+".gif' border='0' onclick='insertFace(\"[Face" + i + "]\")' style='cursor:pointer;' \/>";
	}
	dialog_show(title, content, button, w, h, x, y);
}

function insertFace(ubbcode){
	$("sendmsgtb").value += ubbcode;
	$("sendmsgtb").focus();
	dialog_hide();
}

function formatFace(str){
	if(str.indexOf("Face") > -1){
		for(var i = 1; i <= 52; i++){
			var regex = new RegExp("\\[Face" + i + "\\]", "gi");
			str = str.replace(regex, "<img src='images/expression/" + i + ".gif' alt='' />");
		}
	}
	return str;
}

//过滤HTML标记
function formatMsg(str){
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	return str;
}

//过滤脏话
function checkBadwords(str){
	var bad_words_str = "走私|道友|枪支|抢支|弹药|毒品|海洛因|摇头丸|滚吧|六合彩|大便|正法|镇压|达赖|迫害|李宏志|李洪志|发伦|发抡|发沦|发轮|发仑|发囵|简鸿章|六四|"
	+"真善忍|拉登|拉丹|黄色|国研新闻邮件|自由运动|淫秽|E周刊|龙卷风|大纪元|政变|造反|分裂|人民大众时事参考|示威|罢工|游行|共匪|疆独|民运|印尼伊斯兰祈祷团|民运|"
	+"中俄边界新约|政治运动|压迫|非典|博讯|共产党|反革命|十六大|胡锦涛|共铲党|九评|温家宝|吴邦国|江独裁|江泽民|台湾|东突厥斯坦伊斯兰运动|一边一国|人民内情真相|回民|"
	+"新闻封锁|鸡毛信文汇|新华举报|新华内情|中华民国|政治风波|江八点|吕秀莲|古怪歌|民猪|台湾|突厥斯坦|广闻|简鸿章|人民报|联总之声传单|人民报讯|东突|美国之音|人民真实报道|"
	+"教徒|打倒|推翻|人权|小灵通|操你奶奶|陈水扁|分裂|造反|示威|政变|李鹏|朱容基|主席|改革|人权|迫害|新华内情|达赖|镇压|共产党|新闻封锁|他妈的|东突|吕秀莲|正法|新华举报|"
	+"人民大众时事参考|人民内情真相|人民真实报导|大参考|大纪元|鸡毛信文汇|杂志|联总之声传单|舆论|龙卷风|美国之音|民运|人民报讯|E周刊|博讯|人民报|中俄|边界新约|国研新闻邮件|"
	+"诽闻|迫害|人权|淫秽|打倒|政变|压迫|反革命|无能|突厥斯坦|印尼依斯兰祈祷团|东突|疆独|维吾尔自由运动|自由运动|宝马|公投|法伦|法纶|法抡|法囵|法仑|法轮|大法弟子|"
	+"宋祖英|何清涟|黄菊|撞人|黑龙江|禽流感|竹影青瞳|陷阱|温家宝|梦网|非典|新唐人电视台|六合采|同胞书|修炼|腐败|五四|文化大革命|天安门|台独|回龙观|民主|老毛|游行|国军|"
	+"九评共产党|人民报|退党|大纪元|退党|六四|天安门事件|自由亚州|三陪|嫖娼|自焚|杀死|无界浏览|极景|无界|无网界浏览|无网界|美国之音|自由亚洲|色情|情色|口交|功友|"
	+"阴茎|阴毛|性虐待|黄色网站|成人网站|成人小说|成人文学|成人电影|成人影视|黄色电影|黄色影视|黄色小说|黄色文学|成人图片|黄色图片|黄色漫画|成人漫画|成人电影|三级片|"
	+"黄色电影|坐台|应召|应招|妓女|成人论坛|手机铃声下载|铃声下载|手机铃声|和弦|手机游戏|性免费电影|小电影|强奸|卖国轮功|法伦功|伦功|大纪元|真善忍|妹痛|打炮|"
	+"开苞|插她|鸡巴|操你|操死|阴蒂|肉棍|肉棒|肉洞|口交|洪智|口淫|操死|我操|妈的|垃圾论坛|国家政权|王八蛋|奶奶的|他娘的|操你妈|唐人电视台|fuck|shit|bitch|sex|flg|";
	var bad_words = new Array();
	bad_words = bad_words_str.split("|");
	if(str && typeof(bad_words) == 'object'){
		str = str.replace(/\s*/ig,"");
		str = str.replace(/[\_\..]*/ig,"");
		for(var i in bad_words){
			if(bad_words[i].length > 0 && str.indexOf(bad_words[i]) >= 0){
				return false;
			}
		}
	}
	return true;
}

//闪屏
function shakeWin(){
	var a;
	var b;	
	if (window.moveBy){ 		
		for (a=6;a>0;a--){  
			for (b=1;b>0;b--){	
				var x = this.rndNumber(0, 20);
				var y = this.rndNumber(0, 20);
				window.moveBy(0,a + y); 
				window.moveBy(a + x,0); 
				window.moveBy(0,-a - y); 
				window.moveBy(-a - x,0);
			}  
		} 
	}	
}

//获取当前时间
function getTimestr(){
	var hour = new Date().getHours();
	var minute = new Date().getMinutes();
	if ( minute<10 ) minute = "0" + minute;
	var second = new Date().getSeconds();
	if ( second<10 ) second = "0" + second;
	var time = hour + ":" + minute + ":" + second;
	return time;
}

//获取随机数
function rndNumber(min, max){
	var r = 0;
	switch(arguments.length){ 
		case 1: r = parseInt(Math.random() * min + 1); 
		case 2: r = parseInt(Math.random() * (max-min+1) + min);  
	}
	return r > min && r < max ? r : 0;
}

//对话框
function dialog_show(title, content, button, w, h, x, y) {
	var obj = $("dialog");
	var obj_shadow = $("dialog_bg");
	var obj_title = $("dialog_title");
	var obj_body = $("dialog_body");
	var obj_btn = $("dialog_btn");
	var bodyWidth = scroll_left() + body_width();
	var bodyHeight = scroll_top() + body_height();
	var width = get_width(obj);
	var height = get_height(obj);
	var offset_left = (bodyWidth  - width) / 2;
	var offset_top = (bodyHeight - height) / 2;
	if(parseInt(x) > 0){ obj.style.left = x + "px"; }else{ obj.style.left = offset_left + 'px'; }
	if(parseInt(y) > 0){ obj.style.top = y + "px"; }else{ obj.style.top = offset_top + 'px'; }
	if(parseInt(w) > 0){ obj.style.width = w + "px"; }
	if(parseInt(h) > 0){ obj.style.height = h + "px"; }
	obj.style.display = 'block';
	//标题
	obj_title.innerHTML = title;
	//内容
	obj_body.innerHTML = content;	
	//按钮
	var sbutton = "";
	if(button.indexOf('ok') != -1){ sbutton = "<input type='button' class='dialog_button' value=' 确定 ' onclick='dialog_hide()'>"; }
	if(button.indexOf('cancel') != -1){ sbutton = "<input type='button' class='dialog_button' value=' 取消 ' onclick='dialog_hide()'>"; }
	if(button.indexOf('close') != -1){ sbutton = "<input type='button' class='dialog_button' value=' 关闭 ' onclick='dialog_hide()'>"; }
	sbutton = "<div class='dialog_line'>" + sbutton + "</div>";	
	if(sbutton.length > 0){ 
		obj_btn.innerHTML = sbutton;
	}else {
		obj_btn.style.height = '0px';
	}
	//遮罩层
	var shadowalpha = 20;
	obj_shadow.style.display = 'block';
	obj_shadow.style.width = body_width();
	obj_shadow.style.height = body_height();
	obj_shadow.style.filter = "alpha(opacity=" + shadowalpha + ")"; 
	obj_shadow.style.mozOpacity = shadowalpha / 100;
	obj_shadow.style.opacity = shadowalpha / 100; 
}

function dialog_hide() {
	$("dialog").style.display = 'none';
	$("dialog_bg").style.display = 'none';
}

//通用函数定义
function $(obj){
	var element = null;
	if(document.getElementById){
		element = document.getElementById(obj);
	}else if(document.all){
		element = document.all[obj];
	}else if(document.layers){
		element = document.layers[obj];
	} 
	return element;
} 

function sEvent(e){
	if (typeof(e) == 'undefined'){ 
		e = window.event;
	}
	if (typeof(e.layerX) == 'undefined'){ 
		e.layerX = e.offsetX;
	}
	if (typeof(e.layerY) == 'undefined'){ 
		e.layerY = e.offsetY;
	}
	if (typeof(e.srcElement) == 'undefined'){ 
		e.srcElement = e.target;
	}
	return e;
}

function scroll_top(){
	var s = 0; 
	if (typeof(window.pageYOffset) != 'undefined'){ 
		s = window.pageYOffset; 
	} else if (typeof(document.compatMode) != 'undefined' && document.documentElement.scrollTop > 0) { 
		s = document.documentElement.scrollTop; 
	} else if (typeof(document.body) != 'undefined'){ 
		s = document.body.scrollTop; 
	} 	
	return parseInt(s, 10);
}

function scroll_left(){
	var s = 0; 
	if (typeof(window.pageXOffset) != 'undefined'){ 
		s = window.pageXOffset; 
	} else if (typeof(document.compatMode) != 'undefined' && document.documentElement.scrollLeft > 0){ 
		s = document.documentElement.scrollTop; 
	} else if (typeof(document.body) != 'undefined'){ 
		s = document.body.scrollLeft; 
	} 
	return parseInt(s, 10);
}

function body_width(){ 
	var w = 0;
	if (!document.all) { 
		w = document.body.clientWidth > document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth; 
	}else{ 
		w = document.documentElement.clientWidth == 0 ? document.body.clientWidth : document.documentElement.clientWidth; 
	}
	return parseInt(w, 10);
}

function body_height(){
	var h = 0;
	if (!document.all) { 
		h = document.body.clientHeight > document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight; 
	}else{ 	
		h = document.documentElement.clientHeight == 0 ? document.body.clientHeight : document.documentElement.clientHeight; 
	}
	return parseInt(h, 10);
}

function get_width(obj){
	var w = parseInt(obj.style.width, 10);
	if(!w && document.all){ 
		w = parseInt(obj.offsetWidth, 10); 
	}
	if(!w && document.all){ 
		w = parseInt(obj.currentStyle.width, 10); 
	} 
	if(!w){ 
		w = 150; 
	}
	return w;
}

function get_height(obj){
	var h = parseInt(obj.style.height, 10);
	if(!h && document.all){ 
		h = parseInt(obj.offsetHeight, 10); 
	} 
	if(!h && document.all){ 
		h = parseInt(obj.currentStyle.height, 10); 
	} 
	if(!h){ 
		h = 280; 
	}
	return h;
}

function get_left(obj) {
	return (obj.offsetParent==null ? obj.offsetLeft : obj.offsetLeft + get_left(obj.offsetParent));
}

function get_top(obj) {
	return (obj.offsetParent==null ? obj.offsetTop : obj.offsetTop + get_top(obj.offsetParent));
}

//打开窗口
function openWin(url, strWidth, strHeight){
	leftStr = (screen.width-strWidth)/2;	
	topStr = (screen.height-strHeight)/2;	
	features = "toolbar=no, menubar=no, scrollbars=0, resizable=no, location=no, status=no, height="+strHeight+", width="+strWidth+", left="+leftStr+", top="+topStr+"";	
	window.open(url,'Chat',features);	
}

//设置与屏蔽快捷键
document.onkeydown = function(){
	if ((event.keyCode==116)||(event.ctrlKey && event.keyCode==82)){
		event.keyCode = 0; 
		event.returnValue = false; 
		return false;
	} 
	if (event.ctrlKey && event.keyCode==13){
		sendMsg('talk');
		return true;
	} 
	if (event.altKey && (event.keyCode==83 || event.keyCode==115)){ 
		sendMsg('talk');
		return true;
	}	
}

//主动交谈提示
function showTip(){
	setTimer_showtip();
}

function setTimer_showtip(){
	timerid_showtip = window.setInterval("updateTip()", refreshtime);
}

function updateTip(){
	window.clearInterval(timerid_showtip);
    var strWidth = 380;
	var strHeight = 135;
	$("dialog").style.display = "block";
	$("dialog").style.width = strWidth + "px";
	$("dialog").style.height = strHeight + "px";
	var talkdiv = new scrolldiv(10,15); 
	talkdiv.add($("dialog")); 
	talkdiv = null; 
}