var UBB_Edit_edit;
var UBB_Edit_RangeType;
var UBB_Edit_selection;
var UBB_Edit_charset="UTF-8";
var UBB_Edit_bLoad=false;
var UBB_Edit_pureText=true;
var UBB_Edit_bTextMode=1;
var Maxtitlelength=100;
var MaxConlength=16240;
var ispostnew=0;
var ischeck=false
//
function UBB_Edit_InitDocument(hiddenid, charset)
{	
	if (charset!=null)
	UBB_Edit_charset=charset;
	if (UBB_Edit_bIsIE5){
		var UBB_Edit_bodyTag="<style type=text/css>.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{boder:0px}.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 bgcolor=\"#FFFFFF\" title=\"Ctrl+Enter直接提交贴子\" onkeydown=\"ctlent();\">";
	}else
	{
		var UBB_Edit_bodyTag="<style type=text/css>.quote{margin:5px 20px;border:1px solid #CCCCCC;padding:5px; background:#F3F3F3 }\nbody{boder:0px}.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 bgcolor=\"#FFFFFF\">";
	}
	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 (UBB_Edit_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&&ispost==0)');
	IframeID.document.write ('		{');
	IframeID.document.write ('			parent.UBB_Edit_CopyData("'+hiddenid+'"); ');
	IframeID.document.write ('			parent.Checkdata();');
	IframeID.document.write ('			if(parent.ischeck==true){');
	IframeID.document.write ('			parent.document.Xuform.Submit.disabled=true;');
	IframeID.document.write ('			parent.document.Xuform.Submit2.disabled=true;');
	IframeID.document.write ('			parent.document.Xuform.submit();}');
	IframeID.document.write ('		}');
	IframeID.document.write ('	}');
	IframeID.document.write ('<\/script>');
	}
	IframeID.document.write(UBB_Edit_bodyTag);
	IframeID.document.write("</body>");
	IframeID.document.write("</html>");
	if (h!="")
	{
		IframeID.document.body.innerText=h;
	}
	IframeID.document.close();
	IframeID.document.body.contentEditable = "True";
	IframeID.document.charset=UBB_Edit_charset;
	UBB_Edit_bLoad=true;
	
	UBB_Edit_setMode(1,1);
	IframeID.focus();
}

function ctlent(){
	var ispost=0;
	if (document.all)
	{
		if(event.ctrlKey && event.keyCode==13)
		{
		UBB_Edit_CopyData('Body')
		Checkdata()
		if (ischeck==true){this.document.Xuform.submit();}
		}
	}
}

function UBB_Edit_setMode(n,v)
{
	UBB_Edit_setStyle();
	var cont;
	var UBB_Edit_Toolbar0=document.getElementById("ExtToolbar0");
	var UBB_Edit_Toolbar1=document.getElementById("ExtToolbar1");
	var UBB_Edit_Toolbar2=document.getElementById("ExtToolbar2");
	var UBB_Edit_Toolbar3=document.getElementById("old_Toolbars");
	if (v&&UBB_Edit_Mode!=3)
	{
		
		switch (UBB_Edit_Mode){
			case 0:
				document.getElementById("UBB_Edit_TabHtml").style.display="none";
				break;
			case 1:
				document.getElementById("UBB_Edit_TabOldTools").style.display="none";
				break;
			case 2:
				document.getElementById("UBB_Edit_TabHtml").style.display="none";
				document.getElementById("UBB_Edit_TabDesign").style.display="none";
				n=3;
				break;
		}
	}
	switch (n){
		case 1:
				UBB_Edit_Toolbar0.style.display="";
				UBB_Edit_Toolbar1.style.display="";
				UBB_Edit_Toolbar2.style.display="";
				UBB_Edit_Toolbar3.style.display="none";		//ubb工具栏关闭
				if (UBB_Edit_bIsIE5){
					cont=IframeID.document.body.innerText;
					IframeID.document.body.innerHTML=cont;
				}else{
					var html = IframeID.document.body.ownerDocument.createRange();
					html.selectNodeContents(IframeID.document.body);
					IframeID.document.body.innerHTML = html.toString();
				}
				break;
		case 2:
				UBB_Edit_Toolbar0.style.display="none";	//关闭工具栏
				UBB_Edit_Toolbar1.style.display="none";
				UBB_Edit_Toolbar2.style.display="none";
				UBB_Edit_Toolbar3.style.display="none";	//关闭UBB
				if(UBB_Edit_bTextMode!=1){
					cont=IframeID.document.body.innerText;
				}
				else{
					cont=IframeID.document.body.innerHTML;
				}
				if (cont)
				{
					if (UBB_Edit_bIsIE5){					//IE
						IframeID.document.body.innerText=cont;
					}else{								//Nc
						var html=document.createTextNode(cont);
						IframeID.document.body.innerHTML = "";
						IframeID.document.body.appendChild(html);
					}
				}
				break;

		case 3:
				UBB_Edit_Toolbar0.style.display="";
				UBB_Edit_Toolbar1.style.display="none";
				UBB_Edit_Toolbar2.style.display="none";
				UBB_Edit_Toolbar3.style.display="";		//打开UBB
				if(UBB_Edit_bTextMode!=1){
					cont=IframeID.document.body.innerText;
				}
				else{
					cont=IframeID.document.body.innerHTML;
				}
				if (cont)
				{
					if (UBB_Edit_bIsIE5){					//IE
						if (v)
						{
							IframeID.document.body.innerHtml=cont;
						}else{
							IframeID.document.body.innerText=cont;
						}
						
					}else{								//Nc
						var html=document.createTextNode(cont);
						IframeID.document.body.innerHTML = "";
						IframeID.document.body.appendChild(html);
					}
				}
				break;
	}
	UBB_Edit_setTab(n);
	UBB_Edit_bTextMode=n;
}

function UBB_Edit_setTab(n)
{
	//html和design按钮的样式更改
	var mhtml=document.getElementById("UBB_Edit_TabHtml");
	var mdesign=document.getElementById("UBB_Edit_TabDesign");
	var mOldTool=document.getElementById("UBB_Edit_TabOldTools");
	if (n==1)
	{
		mhtml.className="UBB_Edit_TabOff";
		mdesign.className="UBB_Edit_TabOn";
		mOldTool.className="UBB_Edit_TabOff";
	}
	else if (n==2)
	{
		mhtml.className="UBB_Edit_TabOn";
		mdesign.className="UBB_Edit_TabOff";
		mOldTool.className="UBB_Edit_TabOff";

	}
	else if (n==3)
	{
		mhtml.className="UBB_Edit_TabOff";
		mdesign.className="UBB_Edit_TabOff";
		mOldTool.className="UBB_Edit_TabOn";
	}
}

function UBB_Edit_setStyle()
{
	var bs = IframeID.document.body.style;	
	if (UBB_Edit_bTextMode==2) {
		bs.fontFamily="Arial";
		bs.fontSize="10pt";
	}else{
		bs.fontFamily="Arial";
		bs.fontSize="10.5pt";
	}
	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 UBB_Edit_validateMode()
{
	if (UBB_Edit_bTextMode!=2) return true;
	alert("请取消“查看HTML源代码”选项再使用系统编辑功能或者提交!");
	IframeID.focus();
	return false;
}

function UBB_Edit_CleanCode()
{
	var editor=IframeID;
	editor.focus();
	if (UBB_Edit_bIsIE5){
	// 0bject based cleaning
		var body = editor.document.body;
		for (var index = 0; index < body.all.length; index++) {
			tag = body.all[index];
		//*if (tag.Attribute["className"].indexOf("mso") > -1)
			tag.removeAttribute("className","",0);
			tag.removeAttribute("style","",0);
		}
	// Regex based cleaning
		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(UBB_Edit_bTextMode==3){
		switch (command)
		{
			case 'fontsize':
				funx_ubb("size",option)
				break;
			case 'fontname':
				funx_ubb("face",option)
				break;
		}
	return
	}

	if (UBB_Edit_bIsIE5){
		if (option=="removeFormat"){
			command=option;
			option=null;
		}
		IframeID.focus();
	  	IframeID.document.execCommand(command, false, option);
		UBB_Edit_pureText = false;
		IframeID.focus();
	}else{
		if ((command == 'forecolor') || (command == 'backcolor')) {
				parent.command = command;
				buttonElement = document.getElementById(command);
				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";
				}
				//get current selected range
				//var sel = IframeID.document.selection;
				//if (sel != null) {
					//colour = sel.createRange();
				//}
		}
	}

}

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

function lookmagic()
{
	var obj=document.getElementById("magicframe");
	var buttonElement = document.getElementById("magicfacepic");
	if (obj.style.visibility=="hidden")
	{
		obj.style.top = (getOffsetTop(buttonElement) + buttonElement.offsetHeight)+"px";
		obj.style.left = (getOffsetLeft(buttonElement)-410)+"px";
		obj.style.visibility="visible";
	}else {
		obj.style.visibility="hidden";
	}
}
function closemagic()
{
	var cm=document.getElementById("magicframe");
	if (cm.style.visibility=="visible")
	{
		cm.style.visibility = "hidden";
	}
}