var Kuba_filterScript = true;
var Kuba_charset="UTF-8";
var Kuba_bLoad=false
var Kuba_bTextMode=1;
var Kuba_Mode=1;
var Kuba_bIsIE5=document.all;
var Kuba_Mode = 3;
var Kuba_MaxImageCount = 10;
if (Kuba_bIsIE5){
}
else{
	var Kuba_bIsNC=true;
}
function Kuba_InitDocument(hiddenid, charset)
{
	if (charset!=null){Kuba_charset=charset;}
	if (Kuba_bIsIE5){
		var Kuba_bodyTag="<style type=\"text/css\" type=\"text/css\">.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{boder:0px;margin:2px;}.HtmlCode{margin:5px 20px;border:1px solid #CCCCCC;padding:5px;background:#FDFDDF;font-size:14px;font-family:Tahoma;font-style : oblique;line-height : normal ;font-weight:bold;}\nbody{boder:0px}</style></head><body class=\"IframeBg\" title=\"Ctrl+Enter直接提交贴子\" onkeyup=\"ctlent();\" onmouseover=\"ctlent();\">";
	}else
	{
		var Kuba_bodyTag="<style type=\"text/css\" type=\"text/css\">.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{boder:0px;margin:2px;}.HtmlCode{margin:5px 20px;border:1px solid #CCCCCC;padding:5px;background:#FDFDDF;font-size:14px;font-family:Tahoma;font-style : oblique;line-height : normal ;font-weight:bold;}\nbody{boder:0px}</style></head><body class=\"IframeBg\">";
	}
	var editor=IframeID
	var h=document.getElementById(hiddenid).value;
	if (navigator.appVersion.indexOf("MSIE 6.0",0)==-1){
	editor.document.designMode="On"
	}
	editor.document.open();
	//editor.document.write ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	editor.document.write ('<html><head>');
	if (Kuba_bIsIE5){
	editor.document.write ('<script language="javascript" type="text/javascript">');
	editor.document.write ('var ispost=0;');
	editor.document.write ('	function ctlent()');
	editor.document.write ('	{');
	editor.document.write ('			parent.copy_preview(); ')
//	editor.document.write ('		if(event.ctrlKey && window.event.keyCode==13&&ispost==0)');
//	editor.document.write ('		{');
//	editor.document.write ('			ispost=1;');
//	editor.document.write ('			parent.Kuba_CopyData("'+hiddenid+'"); ');
//	editor.document.write ('			parent.document.aspnetForm.submit();');
//	editor.document.write ('		}');
	editor.document.write ('	}');
	editor.document.write ('function imgresize(o){');
	editor.document.write ('if(o.width > 500 ){');
	editor.document.write ('	o.style.width=\'500px\';');
	editor.document.write ('}');
	editor.document.write ('if(o.height > 800){');
	editor.document.write ('	o.style.height=\'800px\';');
	editor.document.write ('}');
	editor.document.write ('}');
	editor.document.write ('<\/script>');
	}
	editor.document.write(Kuba_bodyTag);
	if (h!="")
	{
		editor.document.write(h);
	}
	editor.document.write ("</body>");
	editor.document.write ("</html>");
	editor.document.close();
	editor.document.body.contentEditable = "True";
	editor.document.charset=Kuba_charset;
	Kuba_bLoad=true;
	Kuba_setStyle();
}

function Kuba_setStyle()
{
	if (Kuba_Mode==2)
	{
		Kuba_bTextMode=3;
	}
	//var bs = IframeID.document.body.runtimeStyle;
}
function Kuba_CopyData(hiddenid)
{
    document.getElementById(hiddenid).value = IframeID.document.body.innerHTML;
    var imageCount = 0;
    for(var i=0;i<IframeID.document.images.length;i++)
    {
        if(IframeID.document.images[i].src.toLowerCase().indexOf("images/editer/face2/")!=-1)
        {
            imageCount++;
        }
    }
    if(imageCount>Kuba_MaxImageCount)
    {
        alert("表情符号不能超过"+Kuba_MaxImageCount+"个！");
        return false;
    }
    return true;
}
//格式化链接
function Kuba_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 Kuba_formatimg()
{
		if (Kuba_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+">"
		}
}
//清理多余HTML代码
function Kuba_cleanHtml(content)
{
	if(Kuba_bTextMode!=1){
		content = content.replace(/<img([^>]*) (src\s*=\s*([^\s|>])*)([^>]*)>/gi,"<img $2>");
	}
	content = content.replace(/<p>&nbsp;<\/p>/gi,"")
	content = content.replace(/<p><\/p>/gi,"<p>")
	content = content.replace(/<div><\/\1>/gi,"")
//	content = content.replace(/<p>/gi,"<br>")
	content = content.replace(/(\r\n){10,}/gi,"$1")
	content = content.replace(/(<br>\s*){10,}/gi,"<br>")
	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, "");
	content = content.replace(/&nbsp;/gi, " ");
	//content = content.replace(/\r\n/g, '');
    //content = content.replace(/\n/g, '');
    //content = content.replace(/\r/g, '');
return content;
}

//代码过滤及JS提取
function Kuba_FilterScript(content)
{
return content;
	content = content.replace(/<(\w[^div|>]*) class\s*=\s*([^>|\s]*)([^>]*)/gi,"<$1$3") ;
	content = content.replace(/<(\w[^font|>]*) style\s*=\s*([^>|\s]*)([^>]*)/gi,"<$1$3") ;
	content = content.replace(/<(\w[^>]*) lang\s*=\s*([^>|\s]*)([^>]*)/gi,"<$1$3") ;
	var RegExp = /<(script[^>]*)>([\s\S]*?)<\/script>/gi;
	content = content.replace(RegExp, "");
	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 Kuba_CleanCode(){
	var editor=IframeID;
	editor.focus();
	checkword=1;
	editor.document.body.innerHTML= editor.document.body.innerHTML;
	checkword=0;

}

var colour
function FormatText(command, option)
{
var codewrite
if(Kuba_bTextMode==3&&command!="RemoveFormat"){
		switch (command)
		{
			case 'fontsize':
				Dv_ubb("size",option);
				break;
			case 'fontname':
				Dv_ubb("face",option);
				break;
			case 'bold':
				Dv_ubb("B");
				break;
			case 'italic':
				Dv_ubb("I");
				break;
			case 'underline':
				Dv_ubb("U");
				break;
		}
	return
}

if (Kuba_bIsIE5){
		if (option=="removeFormat"){
		command=option;
		option=null;}
	  	IframeID.document.execCommand(command, false, option);
		Kuba_pureText = false;
		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.document.execCommand(command, false, option);
		Kuba_pureText = false;
		IframeID.focus();
		}
	}
}

function setColor(color)
{
	IframeID.document.execCommand(parent.command, false, color);
	IframeID.focus();
	document.getElementById("colourPalette").style.visibility="hidden";
}

function AddSmileyIcon(imagePath)
{
if (Kuba_bIsIE5){
    IframeID.focus();
	IframeID.document.body.innerHTML += "<img src='"+imagePath+"' border='0'>";
	//IframeID.document.execCommand('InsertImage', false, imagePath);
	IframeID.focus();
	}else{
	IframeID.focus();
	IframeID.document.execCommand('InsertImage', false, imagePath);
	IframeID.focus();
	}
}

function Kuba_doSelectClick(str, el)
{
	var Index = el.selectedIndex;
	if (Index != 0){
		el.selectedIndex = 0;
		FormatText(str,el.options[Index].value);
	}
}
function Kuba_validateMode()
{
	if (Kuba_bTextMode!=2) return true;
	alert("请取消“查看HTML源代码”选项再使用系统编辑功能或者提交!");
	IframeID.focus();
	return false;
}
function Kuba_ColoredFont()
{
    if (!Kuba_validateMode()) return;
	if (Kuba_bIsIE5){
		var arr = showModalDialog("images/editer/selcolor.html?isfont=1", "", "dialogWidth:18.5em; dialogHeight:17.5em; status:0; help:0");
		if (arr != null){
			var arr2 = arr.split("\t");
			var string = "<table><tr><td style='filter: glow(color="+arr2[0]+" strength=3); color: #ffffff;'>"+arr2[1]+"</td></tr></table>";
			IframeID.document.body.innerHTML+=string;
		}
		else{ IframeID.focus();}
	}else
		{
	}
}
function Kuba_foreColor()
{
	if (!Kuba_validateMode()) return;
	if (Kuba_bIsIE5){
		var arr = showModalDialog("images/editer/selcolor.html", "", "dialogWidth:18.5em; dialogHeight:17.5em; status:0; help:0");
		if (arr != null){
			if(Kuba_bTextMode==3){
				Dv_ubb("color",arr);
			}else{
				FormatText('forecolor', arr);
			}
		}
		else{ IframeID.focus();}
	}else
		{
		FormatText('forecolor', '');
	}
}


function Kuba_backColor()
{
	if (!Kuba_validateMode()) return;
	if(Kuba_bTextMode==3){
		alert('当前编辑器不支持该UBB标记。');
		return ;
	}
	if (Kuba_bIsIE5)
	{
		var arr = showModalDialog("images/editer/selcolor.html", "", "dialogWidth:18.5em; dialogHeight:17.5em; status:0; help:0");
		if (arr != null) 
		{
			if(Kuba_bTextMode!=3){
			FormatText('backcolor', arr);
			}
		}
		else 
		{IframeID.focus();}
	}else
		{
		FormatText('backcolor', '');
		}
}

function Kuba_forwmv()
{
	var arr = showModalDialog("images/editer/wmv.html", "", "dialogWidth:30em; dialogHeight:13em; status:0; help:0");
	
	if (arr != null){
		var ss;
		ss=arr.split("*")
		path=ss[0];
		autostart=ss[1];
		width=ss[2];
		height=ss[3];
		ran=rand();
		var string;
		var ubbstring;
		if(path.toLowerCase().indexOf('http://')==0)
		{
		string="[MP="+ width +","+ height +","+ autostart +"]"+ path +"[/MP]";
		}
		else
		{
		string= path;
		}
		IframeID.document.body.innerHTML+=string;
	}
	else IframeID.focus();
}
function Kuba_forflash()
{
	var arr = showModalDialog("images/editer/flash.html", "", "dialogWidth:30em; dialogHeight:13em; status:0; help:0");
	
	if (arr != null){
		var ss;
		ss=arr.split("*")
		path=ss[0];
		width=ss[1];
		height=ss[2];
		ran=rand();
		var string;
		var ubbstring;
		if(path.toLowerCase().indexOf('http://')==0)
		{
		string="[FLASH="+ width +","+ height +"]"+ path +"[/FLASH]";
		}
		else
		{
		string= path;
		}
		IframeID.document.body.innerHTML+=string;
	}
	else IframeID.focus();
}
function Kuba_forkugoo()
{
	var arr = showModalDialog("images/editer/kugoo.html", "", "dialogWidth:24em; dialogHeight:8em; status:0; help:0");
	
	if (arr != null){
		var string;
		string="[KUGOO]"+ arr +"[/KUGOO]";
		IframeID.document.body.innerHTML+=string;
	}
	else IframeID.focus();
}

function rand() {
	return parseInt((1000)*Math.random()+1);
}

function Kuba_forrm()
{
	var arr = showModalDialog("images/editer/rm.html", "", "dialogWidth:30em; dialogHeight:13em; status:0; help:0");
	
	if (arr != null)
	{
		var ss;
		ss = arr.split("*")
		path = ss[0];
		row = ss[1];
		col = ss[2];
		autostart = ss[3];
		ran = rand();
		var string;
		if(path.toLowerCase().indexOf('http://')==0)
		{
		string = "[RM="+ row +","+ col +","+ autostart +"]"+ path +"[/RM]";
		}
		else
		{
		string= path;
		}
		IframeID.document.body.innerHTML+=string;
	}
	else IframeID.focus();
}

//图片与链接事件
function Kuba_UserDialog(what)
{
	if (!Kuba_validateMode()) return;
	IframeID.focus();
	//插入链接
	if (what == "CreateLink") {
		insertLink = prompt("请填写超级链接地址信息：", "http://");			
		if ((insertLink != null) && (insertLink != "") && (insertLink != "undefined")) {
			var sText = IframeID.document.selection.createRange();
			IframeID.document.execCommand('CreateLink', false, insertLink);
			sText.parentElement().outerHTML=sText.parentElement().outerHTML.replace("<A","<A   target=\"_blank\"");
		}else{
			IframeID.document.execCommand('unlink', false, null);
		}
	}
	//去掉添加图片时的src="file://
	if(what == "InsertImage"){
		if (Kuba_bIsNC)
		{
		imagePath = prompt('请填写图片链接地址信息：', 'http://');			
		if ((imagePath != null) && (imagePath != "")) {
			IframeID.document.execCommand('InsertImage', false, imagePath);
		}
		IframeID.document.body.innerHTML = (IframeID.document.body.innerHTML).replace("src=\"file://","src=\"");
		}else{
		Kuba_forimage();
		}
	}
	if(what == "InsertAlipay"){
		if (Kuba_bIsNC)
		{
		imagePath = prompt('请填写图片链接地址信息：', 'http://');			
		if ((imagePath != null) && (imagePath != "")) {
			IframeID.document.execCommand('InsertImage', false, imagePath);
		}
		IframeID.document.body.innerHTML = (IframeID.document.body.innerHTML).replace("src=\"file://","src=\"");
		}else{
		Kuba_foralipay();
		}
	}
	Kuba_pureText = false;
	IframeID.focus();
}

//------------------------------------------------------
function OpenSmiley()
{
	var arr = showModalDialog("images/post/smiley.htm", "", "dialogWidth:60em; dialogHeight:15.5em; status:0; help:0");
	if (arr != null){
		var ss;
		ss=arr.split("*")
		path=ss[0];
		ubbstring=ss[1];
		IframeID.document.body.innerHTML+=ubbstring;
	}
	else IframeID.focus();
}
function Kuba_forimage()
{
	var arr = showModalDialog("images/editer/img.html", window, "dialogWidth:34em; dialogHeight:12em; status:0; help:0");
	if (arr)
	{
		IframeID.document.body.insertAdjacentHTML("beforeEnd",arr);
	}
	IframeID.focus();
}
function Kuba_forface()
{
	
	var arr = showModalDialog("images/editer/face2.html", window, "dialogWidth:25em; dialogHeight:30em; status:0; help:0");
	if (arr)
	{AddSmileyIcon(arr);
	}
}
function Kuba_foralipay()
{
	var arr = showModalDialog("images/post/alipay.htm", window, "dialogWidth:20em; dialogHeight:34em; status:0; help:0");
	if (arr)
	{
		IframeID.document.body.innerHTML+=arr;
	}
	IframeID.focus();
}
function ShowForum_Emot(thepage)
{
	var Emot_PageCount;
	var Emot_Count=Forum_Emot.length-2;
	if(Emot_Count%Emot_PageSize==0)
	{
		Emot_PageCount=(Emot_Count)/Emot_PageSize
	}else{
		Emot_PageCount=Math.floor((Emot_Count)/Emot_PageSize)+1
	}
	thepage=parseInt(thepage);
	if (thepage<=Emot_PageCount){
	var istr
	var EmotStr='&nbsp;';
	var EmotPath=Forum_Emot[0];
	if (thepage!=1 && Emot_PageCount>1)
	{EmotStr+='<img style="cursor: pointer;" onClick="ShowForum_Emot('+(thepage-1)+');" src="/images/post/Previous.gif" width="14" height="14" title="上一页">&nbsp;';}
	for(i=(thepage-1)*Emot_PageSize;i<(thepage-1)*Emot_PageSize+Emot_PageSize;i++)
	{
		if (i==Emot_Count){break}
		if (i<9)
			{istr='em0'+(i+1)}
			else
			{istr='em'+(i+1)}
		EmotStr+='<img title="'+istr+'" style="cursor: pointer;" onClick=putEmot("'+istr+'"); src="'+EmotPath+Forum_Emot[i+1]+'">&nbsp;';
	}
	if (thepage!=Emot_PageCount)
	{EmotStr+='<img style="cursor: pointer;" onClick="ShowForum_Emot('+(thepage+1)+');" src="/images/post/Next.gif" width="14" height="14" title="下一页">&nbsp;';}
	EmotStr+='分页：<b>'+thepage+'</b>/<b>'+Emot_PageCount+'</b>，共<b>'+(Emot_Count)+'</b>个';
	EmotStr+="<select id=emotpage onchange=\"ShowForum_Emot(this.value);\">";
	for (i=1; i<=Emot_PageCount;i++ )
	{
		EmotStr+="<option value=\""+i+"\">"+i;
	}
	EmotStr+="<\/select>";
	var Forum_EmotObj=document.getElementById("emot")
	Forum_EmotObj.innerHTML=EmotStr;
	document.getElementById('emotpage').options[thepage-1].selected=true;
	}
}

function putEmot(thenNo)
{
	var ToAdd = '['+thenNo+']';
	IframeID.document.body.innerHTML+=ToAdd;
}
function copy_preview(){
	var dv_preview=document.getElementById("dv_preview");
	if (dv_preview && readCookie("preview")==1) {
		var html=IframeID.document.body.innerHTML;
		dv_preview.innerHTML=html;
		if (html==''|| html=='<p>&nbsp;</p>' || html=='<p></p>') dv_preview.innerHTML='<font color="#cccccc">回复效果预览</font> ';
		if (dv_preview.offsetHeight>200){
		dv_preview.style.overflow='auto';
		dv_preview.style.height='200px';
		}
}
	}
function CheckCount(message,total)
{
Kuba_CopyData(message);
copy_preview();
var Getmessage=document.getElementById(message);
var Gettotal=document.getElementById(total);
	var max;
	max = Gettotal.value;
	if (Getmessage.value.length > max) {
	Getmessage.value = Getmessage.value.substring(0,max);
	alert("内容不能超过 " + max + " 个字!");
	document.aspnetForm.Submit.disabled=true;
	}
	else {
	document.aspnetForm.Submit.disabled=false;
	}
	
}

var ispost=0;
function ctlent(){
	
	if (document.all)
	{
		if(event.ctrlKey && event.keyCode==13&&ispost==0)
		{
		ispost=1;
		Kuba_CopyData('Content')
		this.document.aspnetForm.submit();
		}
	}
}
