var edit;
var RangeType;
var selection;
var charset="UTF-8";
var bLoad=false
var bTextMode=1
var Maxtitlelength=100
var MaxConlength=16240
var ischeck=false

var IframeID;
var bIsNC;
//  CLIENT_SIDE SNIFFER CODE
// convert all characters to lowercase
var agt=navigator.userAgent.toLowerCase();

// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

// *** BROWSER TYPE ***
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
		&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
		&& (agt.indexOf('webtv')==-1));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav5 = (is_nav && (is_major == 5));
var is_nav5up = (is_nav && (is_major >= 5));
var is_nav6 = (is_nav && (is_major == 6));
var is_nav4up = (is_nav && (is_major >= 4));
var is_nav6up = (is_nav && (is_major >= 6));
var is_ie   = (agt.indexOf("msie") != -1);
var is_ie3  = (is_ie && (is_major < 4));
var is_ie4  = (is_ie && (is_major <= 4) && (agt.indexOf("msie 5.0")==-1) );
var is_ie5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5up  = (is_ie  && !is_ie3 && !is_ie4);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_opera = (agt.indexOf("opera") != -1);

// *** PLATFORM ***
var is_win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
var is_mac    = (agt.indexOf("mac")!=-1);
var is_sun   = (agt.indexOf("sunos")!=-1);
var is_irix  = (agt.indexOf("irix") !=-1);    // SGI
var is_hpux  = (agt.indexOf("hp-ux")!=-1);
var is_aix   = (agt.indexOf("aix") !=-1);      // IBM
var is_linux = (agt.indexOf("inux")!=-1);
var is_sco   = (agt.indexOf("sco")!=-1) || (agt.indexOf("unix_sv")!=-1);
var is_unixware = (agt.indexOf("unix_system_v")!=-1);
var is_mpras    = (agt.indexOf("ncr")!=-1);
var is_reliant  = (agt.indexOf("reliantunix")!=-1);
var is_dec   = ((agt.indexOf("dec")!=-1) || (agt.indexOf("osf1")!=-1) ||
		(agt.indexOf("dec_alpha")!=-1) || (agt.indexOf("alphaserver")!=-1) ||
		(agt.indexOf("ultrix")!=-1) || (agt.indexOf("alphastation")!=-1));
var is_sinix = (agt.indexOf("sinix")!=-1);
var is_freebsd = (agt.indexOf("freebsd")!=-1);
var is_bsd = (agt.indexOf("bsd")!=-1);
var is_unix  = ((agt.indexOf("x11")!=-1) || is_irix || is_sco || is_unixware || is_mpras || is_reliant ||
		is_dec || is_sinix || is_aix || is_bsd || is_freebsd);

function webEditor(show_Media,show_Emotion){
	if(!(is_nav4up && !is_nav5up ) && is_ie){
		document.write('<table width=100% border=0 cellpadding=0 cellspacing=0>');
		document.write('<tr ID="ExtToolbar0"><td ><select ID="formatSelect" onchange="doSelectClick(\'FormatBlock\',this)"><option>段落格式');
		document.write('<option VALUE="&lt;P&gt;">普通格式');
		document.write('<option VALUE="&lt;H1&gt;">标题 1');
		document.write('<option VALUE="&lt;H2&gt;">标题 2');
		document.write('<option VALUE="&lt;H3&gt;">标题 3');
		document.write('<option VALUE="&lt;H4&gt;">标题 4');
		document.write('<option VALUE="&lt;H5&gt;">标题 5');
		document.write('<option VALUE="&lt;H6&gt;">标题 6');
		document.write('<option VALUE="&lt;H7&gt;">标题 7');
		document.write('<option VALUE="&lt;PRE&gt;">已编排格式');
		document.write('<option VALUE="&lt;ADDRESS&gt;">地址</select>&nbsp;');
		document.write('<select id="FontName" onchange="FormatText(\'fontname\',this[this.selectedIndex].value);"> ');
		document.write('<option class="heading" selected>字体');
		document.write('<option value="宋体">宋体');
		document.write('<option value="黑体">黑体');
		document.write('<option value="楷体_GB2312">楷体');
		document.write('<option value="仿宋_GB2312">仿宋');
		document.write('<option value="隶书">隶书');
		document.write('<option value="幼圆">幼圆');
		document.write('<option value="新宋体">新宋体');
		document.write('<option value="细明体">细明体');
		document.write('<option value="Arial">Arial');
		document.write('<option value="Arial Black">Arial Black');
		document.write('<option value="Arial Narrow">Arial Narrow');
		document.write('<option value="Bradley Hand ITC">Bradley Hand ITC');
		document.write('<option value="Brush Script MT">Brush Script MT');
		document.write('<option value="Century Gothic">Century Gothic');
		document.write('<option value="Comic Sans MS">Comic Sans MS');
		document.write('<option value="Courier">Courier');
		document.write('<option value="Courier New">Courier New');
		document.write('<option value="MS Sans Serif">MS Sans Serif');
		document.write('<option value="Script">Script');
		document.write('<option value="System">System');
		document.write('<option value="Times New Roman">Times New Roman');
		document.write('<option value="Viner Hand ITC">Viner Hand ITC');
		document.write('<option value="Verdana">Verdana');
		document.write('<option value="Wide Latin">Wide Latin');
		document.write('<option value="Wingdings">Wingdings</option>');
		document.write('</select>');
		document.write('<select id="FontSize" onchange="FormatText(\'fontsize\',this[this.selectedIndex].value);">');
		document.write('<option selected>字号');
		document.write('<option value="1">1');
		document.write('<option value="2">2');
		document.write('<option value="3">3');
		document.write('<option value="4">4');
		document.write('<option value="5">5</option>');
		document.write('</select>');
		document.write('<img id="forecolor" name=forecolor TITLE="字体颜色" style="cursor:hand" onclick="foreColor();" src=http://images.chinaren.com/cs/sms/crm/images/fgcolor.gif width=16 height=16 border=0>');
		document.write('<img id="backcolor" TITLE="字体背景颜色" style="cursor:hand" onclick="backColor();" src=http://images.chinaren.com/cs/sms/crm/images/fbcolor.gif width=16 height=16 border=0>');
		document.write('<img TITLE="插入特殊符号" style="cursor:hand" onclick="insertSpecialChar();" src=http://images.chinaren.com/cs/sms/crm/images/specialchar.gif width=21 height=21 border=0>');
		document.write('<img TITLE="替换" style="cursor:hand" onclick="replace();" src=http://images.chinaren.com/cs/sms/crm/images/replace.gif width=16 height=16 border=0>');
		document.write('<img TITLE="心情图标" style="cursor:hand" onclick="OpenSmile();" src=http://images.chinaren.com/cs/sms/crm/images/smiley.gif width=21 height=21 border=0></td></tr>');
		document.write('</table><img src=http://images.chinaren.com/cs/sms/crm/imagesccc.gif width=1 height=5><br>');

		document.write('<table width=100% border=0 cellpadding=0 cellspacing=0>');
		document.write('<tr id="Toolbars" style="display:none">');
		document.write('<td height=33 align=left><img style="cursor:hand" onclick="ubb(\'B\')" src="http://images.chinaren.com/cs/sms/crm/images/bold.gif" alt="粗体" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="ubb(\'I\')" src="http://images.chinaren.com/cs/sms/crm/images/italicize.gif" alt="斜体" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="ubb(\'U\')" src="http://images.chinaren.com/cs/sms/crm/images/underline.gif" alt="下划线" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="ubb(\'CENTER\')" src="http://images.chinaren.com/cs/sms/crm/images/center.gif" alt="居中" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="setCtrl(\'WEB\')" src="http://images.chinaren.com/cs/sms/crm/images/url1.gif" alt="超级连接" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="setCtrl(\'MAIL\')" src="http://images.chinaren.com/cs/sms/crm/images/email1.gif" alt="Email连接" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="setCtrl(\'IMG\')" src="http://images.chinaren.com/cs/sms/crm/images/image.gif" alt="图片" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0">');
		if(show_Media) document.write('<img style="cursor:hand" onclick="setCtrl(\'SWF\')" src="http://images.chinaren.com/cs/sms/crm/images/swf.gif" alt="Flash图片" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="setCtrl(\'SHOCKWAVE\')" src="http://images.chinaren.com/cs/sms/crm/images/Shockwave.gif" alt="Shockwave文件" border="0"><img style="cursor:hand" onclick="setCtrl(\'RM\')" src="http://images.chinaren.com/cs/sms/crm/images/rm.gif" alt="realplay视频文件" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="setCtrl(\'MEDIA\')" src="http://images.chinaren.com/cs/sms/crm/images/mp.gif" alt="Media Player视频文件" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="setCtrl(\'QUICKTIME\')" src="http://images.chinaren.com/cs/sms/crm/images/qt.gif" alt="QuickTime视频文件" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0">');
		document.write('<img style="cursor:hand" onclick="ubb(\'QUOTE\')" src="http://images.chinaren.com/cs/sms/crm/images/quote1.gif" alt="引用" border="0"><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0">');
		if(screen.width<1024) document.write('<br>');
		document.write('<img style="cursor:hand" onclick="ubb(\'FLY\')" src="http://images.chinaren.com/cs/sms/crm/images/fly.gif" alt="飞行字" border=0><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="ubb(\'MOVE\')" src="http://images.chinaren.com/cs/sms/crm/images/move.gif" alt="移动字" border=0><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="setCtrl(\'LIGHT\')" src="http://images.chinaren.com/cs/sms/crm/images/glow.gif" alt="发光字" border=0><img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="setCtrl(\'SHADOW\')" src="http://images.chinaren.com/cs/sms/crm/images/shadow.gif" alt="阴影字" border=0>');
		if(show_Media) document.write('<img src="http://images.chinaren.com/cs/sms/crm/images/blank.gif" border="0"><img style="cursor:hand" onclick="setCtrl(\'SOUND\')" src="http://images.chinaren.com/cs/sms/crm/images/csound.gif" alt="背景音乐" border="0">');
		document.write('</td></tr>');
		document.write('</table>');


		if(show_Emotion){
			document.write("<div align=left  style='color: #FF6600;'><b>温馨提示：为了让校友录客服能尽快处理您遇到的问题，请您在发帖时尽量把详细情况<br>描述清楚。<br>您的ID(用户名)：<br>所在班级的classuuid：<br>注：http://alumni.chinaren.com/class/class_index.jsp?classuuid=******(进入班级后，在浏览器地址栏里的地址)</b></div>");
		}else{
			document.write("<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td><img src=http://images.chinaren.com/product/aluclub/images/club_emotion.gif border=0 onclick='quickaddemotion_e(this, event);'></td></tr></table>");
		//	document.write("<table width=100% border=0 cellspacing=0 cellpadding=0><tr><td><img src=/club_emotion.gif border=0 onclick='quickaddemotion_e(this, event);'></td></tr></table>");

			/*
			var TOTAL_EMOTIONS = 74;
			var COLS = 22;
			if(screen.width<1024) COLS =17;
			var ROWS = Math.ceil(TOTAL_EMOTIONS/COLS);
			var outstr = "";
			outstr += "<table width=100% border=0 cellpadding=0 cellspacing=0>";
			for (i=0; i<ROWS; i++) {
				outstr += "<tr><td>";
				for (j=0; j<COLS; j++) {
					var id = i*COLS + j + 1;
					if (id>TOTAL_EMOTIONS) break;
					id = (id<10)?"0"+id:id;
					outstr += '<img src=http://images.chinaren.com/cs/sms/crm/em/em' + ((id>64)?id+"s":id) + '.gif width=20 height=20 hspace=1 vspace=1 onclick="quickaddemotion_e(\'' + id + '\')" style="cursor:hand" title="[em' + id + ']" align=absmiddle>';
				}
				//if (i==ROWS-1) outstr += "<a href='javascript:OpenSmile()' title='更多表情' class=center02>更多>></a>";
				outstr += "</td></tr>";
			}
			outstr += "</table>";
			document.write(outstr);
			*/
			
		}

		document.write('<table width=100% border=0 cellpadding=0 cellspacing=1 bgcolor=#4ac5ef>');
		document.write('<tr bgcolor=white><td><iframe ID="Composition" MARGINHEIGHT="2" MARGINWIDTH="2" width="100%" height="100%"></iframe></td></tr>');
		document.write('</table>');

		var bIsIE5=document.all
			if (bIsIE5){
				IframeID=frames["Composition"];
			} else{
				IframeID=document.getElementById("Composition").contentWindow;
				bIsNC=true;
			}
		if (bLoad==false){
			InitDocument("Body","GB2312");
		}

		if (bIsNC){
			document.write('<iframe width="260" height="165" id="colourPalette" src="http://images.chinaren.com/cs/sms/crm/images/nc_selcolor.htm" style="visibility:hidden; position: absolute; left: 0px; top: 0px;" frameborder="0" scrolling="no" ></iframe>');
		}
	}else{
		document.write('<table width=100% border=0 cellpadding=0 cellspacing=0>');
		if(screen.width<1024){
			document.write('<tr><td><textarea name=msg_edit cols=50 rows=15 class="box1"></textarea></td><tr></table>');
		}else{
			document.write('<tr><td><textarea name=msg_edit cols=55 rows=15 class="box1"></textarea></td><tr></table>');
		}
		document.ubbForm.msg_edit.value=document.ubbForm.msg.value;
	}
}


function getPos(el,sProp) {
	var iPos = 0;
	while (el!=null) {
		iPos+=el["offset" + sProp];
		el = el.offsetParent;
	}
	return iPos;
}

var em_arr = new Array();
for (var i=0; i<74; i++) {
	var idx = i+1;
	idx = (idx<10)?("0"+idx):idx;
	em_arr[i] = "[em" + idx + "]";
}

function getIndex(o, e) {
	var debug = false;
	if (e) {
		var COLS=17;
		var ROWS=5;

		var unit_width = Math.ceil(o.offsetWidth/COLS);
		var unit_height = Math.ceil(o.offsetHeight/ROWS);
		if (debug) alert("u: " + unit_width + " x " + unit_height);

		var x_in_img = document.body.scrollLeft + e.clientX - getPos(o, "Left");
		var y_in_img = document.body.scrollTop + e.clientY - getPos(o, "Top");
		if (debug) alert("xy: " + x_in_img + " x " + y_in_img);

		var r = Math.floor(y_in_img/unit_height);
		var c = Math.floor(x_in_img/unit_width);
		if (debug) alert("rc: " + r + " x " + c);

		var idx = r*COLS + c;
		if (debug) alert("idx: " + idx);

		return idx;
	}
}

function quickaddemotion_e(o, e) {
	var idx = getIndex(o, e);
	if (idx<em_arr.length) {
		IframeID.focus();
		targetDoc = IframeID.document;
		var str = em_arr[idx];
		var sel = targetDoc.selection.createRange();
		sel.pasteHTML(str);
		sel.select();
	}
}

/*
function quickaddemotion_e(id) {
	IframeID.focus();
	targetDoc = IframeID.document;
	var str = "[em" + id + "]";
	var sel = targetDoc.selection.createRange();
	sel.pasteHTML(str);
	sel.select();
}
*/


function InitDocument(hiddenid, charset)
{	
	if (charset!=null)
	charset=charset;
	var h=document.getElementById(hiddenid).value;
	
	if (navigator.appVersion.indexOf("MSIE 6.0",0)==-1){
		IframeID.document.designMode="On"
	}
	IframeID.document.open();
	IframeID.document.write ('<html><head>');
	if (bIsIE5){
		IframeID.document.write ('<script language="javascript">');
		IframeID.document.write ('var ispost=0;');
		IframeID.document.write ('	function ctlent(eventobject)');
		IframeID.document.write ('	{');
			IframeID.document.write ('		if((event.ctrlKey  &&  window.event.keyCode==13)||(event.altKey  &&  window.event.keyCode==83))');
			IframeID.document.write ('		{');
			IframeID.document.write ('			parent.postMsg(); ');
			IframeID.document.write ('		}');
			IframeID.document.write ('	}');
			IframeID.document.write ('<\/script>');
			IframeID.document.write ('<style>p {margin:0px}</style>');
			IframeID.document.write ('</head><body title="Ctrl+Enter直接提交贴子" onkeydown="ctlent();">');

	} else {
		IframeID.document.write ('</head><body>');
	}	
	IframeID.document.write("</body>");
	IframeID.document.write("</html>");
	if (h!="") {
		h = h.replace(/\n/g, "<br>");
		var RegExp = /<script([^>]*)>([\s\S]*)<\/script([^>]*)>/gi;
		h = h.replace(RegExp, "&lt;script$1&gt;$2&lt;\/script$3&gt;");
		IframeID.document.body.innerHTML=h;
	}
	IframeID.document.close();
	IframeID.document.body.contentEditable = "True";
	IframeID.document.charset=charset;
	bLoad=true;
	setMode(3);
	IframeID.focus();
}

function setMode(n)
{
	setStyle();
	var cont;	
	var toolbar=document.getElementById("Toolbars");

	switch (n){		
		case 3:
			toolbar.style.display="";
			cont=IframeID.document.body.innerHTML;
			if (bIsIE5){
				IframeID.document.body.innerHTML=cont;
			}else{
				var html=document.createTextNode(cont);
				IframeID.document.body.innerHTML = "";
				IframeID.document.body.appendChild(html);
			}
			break;
	}	
}


function setStyle()
{
	var bs = IframeID.document.body.style;	
	if (bTextMode==2) {
		bs.fontFamily="Arial";
		bs.fontSize="12px";
	}else{
		bs.fontFamily="Arial";
		bs.fontSize="12px";
	}
	bs.scrollbar3dLightColor= '#D4D0C8';
	bs.scrollbarArrowColor= '#000000';
	bs.scrollbarBaseColor= '#D4D0C8';
	bs.scrollbarDarkShadowColor= '#D4D0C8';
	bs.scrollbarFaceColor= '#D4D0C8';
	bs.scrollbarHighlightColor= '#808080';
	bs.scrollbarShadowColor= '#808080';
	bs.scrollbarTrackColor= '#D4D0C8';
	bs.border='0';
}

function validateMode()
{
	if (bTextMode!=2) return true;	
	IframeID.focus();
	return false;
}

function CleanCode()
{
	var editor=IframeID;
	editor.focus();
	if (bIsIE5){	
		var body = editor.document.body;
		for (var index = 0; index < body.all.length; index++) {
			tag = body.all[index];

			tag.removeAttribute("className","",0);
			tag.removeAttribute("style","",0);
		}
		var html = editor.document.body.innerHTML;
		html = html.replace(/\<p>/gi,"[$p]");
		html = html.replace(/\<\/p>/gi,"[$\/p]");
		html = html.replace(/\<br>/gi,"[$br]");
		html = html.replace(/\<[^>]*>/g,"");
		html = html.replace(/\[\$p\]/gi,"<p>");
		html = html.replace(/\[\$\/p\]/gi,"<\/p>");
		html = html.replace(/\[\$br\]/gi,"<br>");
		editor.document.body.innerHTML = html;
	}else {
		var html = IframeID.document.body.ownerDocument.createRange();
		html.selectNodeContents(IframeID.document.body);
		IframeID.document.body.innerHTML = html.toString();
	}
}

var colour;

function FormatText(command, option)
{
	var codewrite;
	if (bIsIE5){
		if (option=="removeFormat"){
			command=option;
			option=null;
		}
		IframeID.focus();
		IframeID.document.execCommand(command, false, option);		
		IframeID.focus();

	} else {
		if ((command == 'forecolor') || (command == 'backcolor')) {
			parent.command = command;
			buttonElement = document.getElementById(command);
			IframeID.focus();
			document.getElementById("colourPalette").style.left = getOffsetLeft(buttonElement) + "px";
			document.getElementById("colourPalette").style.top = (getOffsetTop(buttonElement) + buttonElement.offsetHeight) + "px";

			if (document.getElementById("colourPalette").style.visibility=="hidden")
			{document.getElementById("colourPalette").style.visibility="visible";
			}else {
				document.getElementById("colourPalette").style.visibility="hidden";
			}

			var sel = IframeID.document.selection; 
			if (sel != null) {
				colour = sel.createRange();
			}
		} else{
			IframeID.focus();
			IframeID.document.execCommand(command, false, option);			
			IframeID.focus();
		}
	}

}

function setColor(color)
{
	IframeID.focus();
	IframeID.document.execCommand(parent.command, false, color);
	IframeID.focus();
	document.getElementById("colourPalette").style.visibility="hidden";
}

function saveData(hiddenid)
{
	ischeck=true
		//document.ubbForm.Submit1.disabled=true;
		//document.ubbForm.Submit2.disabled=true;
		d = IframeID.document;
	formatImg();
	cont = d.body.innerHTML;
	cont = correctUrl(cont);
	cont = cleanHtml(cont);
	cont = FilterScript(cont);	
	document.getElementById(hiddenid).value = cont;
}

function formatImg()
{
	if (bIsIE5){
		var tmp=IframeID.document.body.all.tags("IMG");
	}else{
		var tmp=IframeID.document.getElementsByTagName("IMG");
	}
	for(var i=0;i<tmp.length;i++){
		var tempstr='';
		if(tmp[i].align!=''){tempstr=" align="+tmp[i].align;}
		if(tmp[i].border!=''){tempstr=tempstr+" border="+tmp[i].border;}
		tmp[i].outerHTML="<IMG src=\""+tmp[i].src+"\""+tempstr+">";
	}
}

function cleanHtml(content)
{
	content = content.replace(/<p>/gi,"");
	content = content.replace(/<\/p>/gi,"");
	content = content.replace(/&nbsp;/gi, "");
	content = content.replace(/<br>/gi,"\n");
	content = content.replace(/\n\s{2,}/g,"\n\n");
	content = content.replace(/<div><\/\1>/gi,"");
	content = content.replace(/(<(meta|iframe|frame|span|tbody|layer)[^>]*>|<\/(iframe|frame|meta|span|tbody|layer)>)/gi, "");
	content = content.replace(/<\\?\?xml[^>]*>/gi, "") ;
	//content = content.replace(/o:/gi, "");
	return content;
}

function FilterScript(content)
{	
	content = content.replace(/<(\w[^div|>]*) class\s*=\s*([^>|\s]*)([^>]*)/gi,"<$1$3") ;
	content = content.replace(/<(\w[^font|>]*) style\s*=\s*\"[^\"]*\"([^>]*>)/gi,"<$1 $2") ;
	content = content.replace(/<(\w[^>]*) lang\s*=\s*([^>|\s]*)([^>]*)/gi,"<$1$3") ;

	//var RegExp = /<script([^>]*)>([\s\S]*)<\/script>/gi;
	//content = content.replace(RegExp, "[code]&lt;script$1&gt;<br>$2<br>&lt;/script&gt;[\/code]");

	var RegExp1 = /<script([^>]*)>/gi;
	var RegExp2 = /<\/script>/gi;
	content = content.replace(RegExp1, "[code]&lt;script$1&gt;").replace(RegExp2, "&lt;/script&gt;[\/code]");

	RegExp = /<(\w[^>|\s]*)([^>]*)(on(finish|mouse|Exit|error|click|key|load|change|focus|blur)=)(.[^>]*)/gi;
	content = content.replace(RegExp, "<$1");
	RegExp = /<(\w[^>|\s]*)([^>]*)(&#|window\.|javascript:|js:|about:|file:|Document\.|vbs:|cookie| name| id)(.[^>]*)/gi;
	content = content.replace(RegExp, "<$1");
	return content;
}

function Checkpostdata()
{
	Checkdata();
	if (ischeck==false)
	{
		//document.ubbForm.Submit1.disabled=false;
		//document.ubbForm.Submit2.disabled=false;
		return false;
	}
}

function Checkdata()
{
	ischeck=true	
		if (document.ubbForm.title.value.length==0)
		{		
			alert("标题不能为空");
			document.ubbForm.title.focus();
			ischeck=false
				return;
		}
	if (document.ubbForm.title.value.length>Maxtitlelength)
	{		
		alert("标题长度不能大于" + Maxtitlelength + "个字")
			document.ubbForm.title.focus();
		ischeck=false
			return;
	}
	if (document.getElementById('Body').value.length==0)
	{
		alert("请填写贴子内容");
		IframeID.focus();
		ischeck=false;
		return;
	}
	/*
	   else if (document.getElementById('Body').value.length>MaxConlength)
	   {
	   alert("贴子内容长度不能大于" + MaxConlength);
	   IframeID.focus();
	   ischeck=false
	   return;
	   }
	 */
}

function foreColor()
{	
	if (!validateMode()) return;
	if (bIsIE5){
		var arr = showModalDialog("selcolor.html", "", "dialogWidth:18.5em; dialogHeight:17.5em;dialogLeft:40;dialogTop:160; status:0; help:0");
		if (arr != null) FormatText('forecolor', arr);
		else IframeID.focus();
	}else {
		FormatText('forecolor', '');
	}
}

function backColor()
{
	if (!validateMode()) return;
	if (bIsIE5) {
		var arr = showModalDialog("selcolor.html", "", "dialogWidth:18.5em; dialogHeight:17.5em;dialogLeft:40;dialogTop:160; status:0; help:0");
		if (arr != null) FormatText('backcolor', arr);
		else IframeID.focus();
	}else {
		FormatText('backcolor', '');
	}
}

function correctUrl(cont)
{
	var regExp;
	regExp = /<a([^>]*) href\s*=\s*([^\s|>]*)([^>]*)/gi
		cont = cont.replace(regExp, "<a href=$2 target='_blank' ");
	regExp = /<a([^>]*)><\/a>/gi
		cont = cont.replace(regExp, "");
	return cont;
}

function rCode(s,a,b,i){
	a = a.replace("?","\\?");
	if (i==null)
	{
		var r = new RegExp(a,"gi");
	}else if (i) {
		var r = new RegExp(a,"g");
	}
	else{
		var r = new RegExp(a,"gi");
	}
	return s.replace(r,b); 
}

function InsertSymbol(str1)
{
	IframeID.focus();
	if (bIsIE5) selectRange();
	edit.pasteHTML(str1);
}

function selectRange(){
	selection =	IframeID.document.selection;
	edit	=	selection.createRange();
	RangeType =	selection.type;
}

function doSelectClick(str, el)
{
	var Index = el.selectedIndex;
	if (Index != 0){		
		FormatText(str,el.options[Index].value);
		el.selectedIndex = 0;
	}
}

function getOffsetTop(elm) {
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}

function getOffsetLeft(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}

function hideColourPallete() {
	document.getElementById("colourPalette").style.visibility="hidden";
}


function OpenSmile()
{
	showModelessDialog("smile.html", window, "dialogWidth:274px; dialogHeight:262px;dialogLeft:40;dialogTop:160; status:0; help:0");
}

function replace()
{
	var arr = showModalDialog("replace.html", "", "dialogWidth:16.5em; dialogHeight:13em; status:0; help:0");
	if (arr != null){
		var ss;
		ss = arr.split("*")
			a = ss[0];
		b = ss[1];
		i = ss[2];
		con = IframeID.document.body.innerHTML;
		if (i == 1)
		{
			con = rCode(con,a,b,true);
		}else{
			con = rCode(con,a,b);
		}
		IframeID.document.body.innerHTML = con;
	}
	else IframeID.focus();
}

function insertSpecialChar()
{
	var arr = showModalDialog("specialchar.html", "","dialogWidth:25em; dialogHeight:15em; dialogLeft:40;dialogTop:160;status:0; help:0");
	if (arr != null) InsertSymbol(arr);
	IframeID.focus() ;
}


//-----------------------------

var UbbUsed=0;
var bIsIE5		= document.all;
var text_enter_url      = "请输入连接网址";
var text_enter_txt	= "请输入连接说明";			var text_enter_image	= "请输入图片网址";			var text_enter_sound	= "请输入声音文件网址";
var text_enter_swf	= "请输入Flash动画网址";		var text_enter_ra	= "请输入Real音乐网址";			var text_enter_rm	= "请输入Real影片网址";
var text_enter_wmv	= "请输入Media影片网址";		var text_enter_wma	= "请输入Media音乐网址";		var text_enter_mov	= "请输入QuickTime音乐网址";
var text_enter_sw	= "请输入Shockwave音乐网址";		var text_enter_email    = "请输入邮件网址";
var text_enter_guang1	= "文字的长度、颜色和边界大小";		var text_enter_guang2	= "要产生效果的文字！";


function AddText(text) {
	if (IframeID.document.body.createTextRange && IframeID.document.body.caretPos) {      
		var caretPos = IframeID.document.body.caretPos;      
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
			text + ' ' : text;
	}
	else IframeID.document.body.innerHTML += text;
	IframeID.focus(caretPos);
}

function hyUrl() {
	var FoundErrors = '';
	var enterURL   = prompt(text_enter_url, "http://");
	var enterTxT   = prompt(text_enter_txt, enterURL);
	if (!enterURL)    {
		enterURL = "";
		return;
	}
	if (!enterTxT)    {
		enterTXT = "";
		return;
	}
	var ToAdd = "[URL="+enterURL+"]"+enterTxT+"[/URL]";
	IframeID.document.body.innerHTML+=ToAdd;
	IframeID.focus();
}

function ubb(code) {
	fontbegin="["+code+"]";
	fontend="[/"+code+"]";
	addFont();
}

function unUbb(code) {
	var editor=IframeID;
	var html = editor.document.body.innerHTML;
	var exp = "\\[\(\(" + code + "\)|\(\\/" + code + "\)\)\\]";
	var re = new RegExp(exp, "gi");	
	html = html.replace(re, "");
	editor.document.body.innerHTML = html;
}

	function setCtrl(ctrl) {
		switch (ctrl)
		{	
			case 'WEB':
				urlname="web.html";
				var ret=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (ret!=null) {
					a=ret.toString().split('@@@');		
					AddTxt="[URL="+a[1]+"]"+a[0]+"[/URL]";     
					AddText(AddTxt);        
				}
				break;
			case 'MAIL':
				urlname="mail.html";
				var ret=window.showModalDialog("/"+urlname,"","dialogWidth:32;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (ret!=null) {
					a=ret.toString().split('@@@');		
					AddTxt="[EMAIL]"+a[0]+"[/EMAIL]";     
					AddText(AddTxt);        
				}
				break;
			case 'IMG':
				urlname="pic.html";
				var ret=window.showModalDialog("/"+urlname,"","dialogWidth:32;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (ret!=null) {
					a=ret.toString().split('@@@');		
					AddTxt="[IMG]"+a[0]+"[/IMG]";     
					AddText(AddTxt);        
				}
				break;
			case 'SOUND':
				urlname="sound.html";
				var ret=window.showModalDialog("/"+urlname,"","dialogWidth:32;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (ret!=null) {
					a=ret.toString().split('@@@');		
					AddTxt="[SOUND]"+a[0]+"[/SOUND]";     
					AddText(AddTxt);        
				}
				break;
			case 'SWF':
				urlname="swf.html";
				var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (thisvalue!=null) 
				{
					a=thisvalue.toString().split('@@@');
					if (a.length==3) {
						AddTxt="[FLASH="+a[1]+","+a[2]+"]"+a[0]+"[/FLASH]";	
						AddText(AddTxt);	
					}
				}
				break;
			case 'SHOCKWAVE':
				urlname="swav.html";
				var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (thisvalue!=null) 
				{
					a=thisvalue.toString().split('@@@');
					if (a.length==3) {
						AddTxt="[DIR="+a[1]+","+a[2]+"]"+a[0]+"[/DIR]";	
						AddText(AddTxt);	
					}
				}
				break;
			case 'QUICKTIME':
				urlname="quicktime.html";
				var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (thisvalue!=null) 
				{
					a=thisvalue.toString().split('@@@');
					if (a.length==3) {
						AddTxt="[QT="+a[1]+","+a[2]+"]"+a[0]+"[/QT]";	
						AddText(AddTxt);	
					}
				}
				break;
			case 'RM':
				urlname="rm.html";
				var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (thisvalue!=null) 
				{
					a=thisvalue.toString().split('@@@');
					if (a.length==4) {
						AddTxt="[RM="+a[1]+","+a[2]+","+a[3]+"]"+a[0]+"[/RM]";
						AddText(AddTxt);
					}
				}
				break;
			case 'MEDIA':	
				urlname="media.html";
				var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (thisvalue!=null) 
				{
					a=thisvalue.toString().split('@@@');
					if (a.length==4){
						AddTxt="[MP="+a[1]+","+a[2]+","+a[3]+"]"+a[0]+"[/MP]";
						AddText(AddTxt);
					}
				}
				break;
			case 'LIGHT':	
				urlname="light.html";
				var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:32;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (thisvalue!=null) 
				{
					a=thisvalue.toString().split('@@@');
					if (a.length==4){
						AddTxt="[GLOW="+a[1]+","+a[2]+","+a[3]+"]"+a[0]+"[/GLOW]";
						AddText(AddTxt);
					}
				}
				break;
			case 'SHADOW':	
				urlname="shadow.html";
				var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:32;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
				if (thisvalue!=null) 
				{
					a=thisvalue.toString().split('@@@');
					if (a.length==4){
						AddTxt="[SHADOW="+a[1]+","+a[2]+","+a[3]+"]"+a[0]+"[/SHADOW]";
						AddText(AddTxt);
					}
				}
				break;

		}
}
function Sig(code) {
	var promptinfo='';
	var error_info='';
	var default_info='1000';
	var addtype=0;
	switch (code)
	{
		case 'SOUND':
			addtype=1;
			promptinfo=text_enter_sound;
			default_info="http://";
			break;
		case 'IMG':
			addtype=1;
			promptinfo=text_enter_image;
			default_info="http://";
			break;
		case 'EMAIL':
			addtype=1;
			promptinfo=text_enter_email;
			default_info="";
			break;
	}
	var enter_prompt  =prompt(promptinfo,default_info);
	if (!enter_prompt) {
		return;
	}
	if (addtype==1)
	{
		var ToAdd = "[" + code + "]" + enter_prompt + "[\/" + code + "]";
	}else{
		var ToAdd = "[" + code + "=" + enter_prompt + "][\/" + code + "]";
	}
	IframeID.document.body.innerHTML+=ToAdd;
	IframeID.focus();
}

function Swf() {    
	urlname="swf.html";
	var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
	if (thisvalue!=null) 
	{
		a=thisvalue.toString().split('&');
		if (a.length==3) {
			AddTxt="[FLASH="+a[1]+","+a[2]+"]"+a[0]+"[/FLASH]";	
			AddText(AddTxt);	
		}
	}
}
function Img() {
	urlname="img.html";
	var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
	if (thisvalue!=null) 
	{
		a=thisvalue.toString().split('&');
		if (a.length==3) {
			AddTxt="[IMG="+a[1]+","+a[2]+"]"+a[0]+"[/IMG]";     
			AddText(AddTxt);        

		}
	}
}
function Wav() {
	urlname="wav.html";
	var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
	if (thisvalue!=null) {
		AddTxt="[SOUND]"+a[0]+"[/SOUND]";     
		AddText(AddTxt);        
	}
}
function Ram() {
	urlname="rm.html";
	var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
	if (thisvalue!=null) 
	{
		a=thisvalue.toString().split('&');
		if (a.length==4) {
			AddTxt="[RM="+a[1]+","+a[2]+","+a[3]+"]"+a[0]+"[/RM]";
			AddText(AddTxt);
		}
	}
}

function Wmv() {
	urlname="media.html";
	var thisvalue=window.showModalDialog("/"+urlname,"","dialogWidth:28;dialogHeight:11;dialogTop:100;dialogLeft:200;help:off;status:no;scroll:off;");
	if (thisvalue!=null) 
	{
		a=thisvalue.toString().split('&');
		if (a.length==4){
			AddTxt="[MP="+a[1]+","+a[2]+","+a[3]+"]"+a[0]+"[/MP]";
			AddText(AddTxt);
		}
	}
}

function Mov() {
	txt2=prompt("视频的宽度，高度","500,350"); 
	if (txt2!=null) {
		txt=prompt("视频文件的地址","请输入");
		if (txt!=null) {
			if (txt2=="") {
				AddTxt="[QT=500,350]"+txt;
				AddText(AddTxt);
				AddTxt="[/QT]";
				AddText(AddTxt);
			} else {
				AddTxt="[QT="+txt2+"]"+txt;
				AddText(AddTxt);
				AddTxt="[/QT]";
				AddText(AddTxt);
			}
		}
	}
}

function Dir() {
	txt2=prompt("Shockwave文件的宽度，高度","500,350"); 
	if (txt2!=null) {
		txt=prompt("Shockwave文件的地址","请输入地址");
		if (txt!=null) {
			if (txt2=="") {
				AddTxt="[DIR=500,350]"+txt;
				AddText(AddTxt);
				AddTxt="[/DIR]";
				AddText(AddTxt);
			} else {
				AddTxt="[DIR="+txt2+"]"+txt;
				AddText(AddTxt);
				AddTxt="[/DIR]";
				AddText(AddTxt);
			}
		}
	}
}

function addFont(){
	if ((IframeID.document.selection)&&(IframeID.document.selection.type == "Text")) {
		var range = IframeID.document.selection.createRange();
		var ch_text=range.text;		
		range.text = fontbegin + ch_text + fontend;
	} 
	else {
		IframeID.document.body.innerHTML=fontbegin+IframeID.document.body.innerHTML+fontend;		
		IframeID.focus();
	}
}

function Guang() {
	var FoundErrors = '';
	var enterSET   = prompt(text_enter_guang1, "255,red,2");
	var enterTxT   = prompt(text_enter_guang2, "文字");
	if (!enterSET)    {
		return;
	}
	if (!enterTxT)    {
		return;
	}
	var ToAdd = "[GLOW="+enterSET+"]"+enterTxT+"[/GLOW]";
	IframeID.document.body.innerHTML+=ToAdd;
	IframeID.focus();
}

function Ying() {
	var FoundErrors = '';
	var enterSET   = prompt(text_enter_guang1, "255,blue,1");
	var enterTxT   = prompt(text_enter_guang2, "文字");
	if (!enterSET) {
		return;
	}
	if (!enterTxT) {
		return;
	}
	var ToAdd = "[SHADOW="+enterSET+"]"+enterTxT+"[/SHADOW]";
	IframeID.document.body.innerHTML+=ToAdd;
	IframeID.focus();
}

function ClearReset()
{
	IframeID.document.body.innerHTML='';
	IframeID.focus();
}
