var Quote = 0;
var Bold  = 0;
var Italic = 0;
var Underline = 0;
var Code = 0;
var editObj;
var fontbegin;
var fontend;
var ToolBar = Array(
						Array('Ubb_fly', 'fly.gif','飞行字'),
						Array('Ubb_move', 'move.gif', '移动字'),
						Array('Ubb_glow', 'glow.gif', '发光字'),
						Array('Ubb_shadow', 'shadow.gif', '阴影字'),
						Array('Ubb_bold', 'bold.gif', '粗体字'),
						Array('Ubb_italicize', 'italicize.gif', '斜体字'),
						Array('Ubb_underline', 'underline.gif', '下划线'),
						Array('Ubb_sup', 'sup.gif', '上标'),
						Array('Ubb_sub', 'sub.gif', '下标'),
						Array('Ubb_code', 'code.gif', '插入代码'),
						Array('Ubb_fontsize', 'fontsize.gif', '字体大小'),
						Array('Ubb_fontcolor', 'fontcolor.gif', '字体颜色'),
						Array('Ubb_center', 'center.gif', '居中'),
						Array('Ubb_image', 'image.gif', '图片'),
						Array('Ubb_Shockwave', 'Shockwave.gif', 'Shockwave文件'),
						Array('Ubb_rm', 'rm.gif', 'realplayer文件'),
						Array('Ubb_mp', 'mp.gif',  'media player文件'),
						Array('Ubb_url', 'url.gif', '超链接'),
						Array('Ubb_email', 'email.gif', 'Email链接'),
						Array('Ubb_csound', 'csound.gif', '背景音乐'),
						Array('Ubb_ra', 'ra.gif', 'real音频文件'),
						Array('Ubb_6kbbs', '6kbbs.gif', '特殊帖子'),
						Array('Ubb_face', 'face.gif', '表情')
				  );

var FontColor = Array(
						"#FF0000", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF", "#FF00FF", "#FFFFFF", "#F5F5F5", "#DCDCDC", "#FFFAFA",
						"#D3D3D3", "#C0C0C0", "#A9A9A9", "#808080", "#696969", "#000000", "#2F4F4F", "#708090", "#778899", "#4682B4",
						"#4169E1", "#6495ED", "#B0C4DE", "#7B68EE", "#6A5ACD", "#483D8B", "#191970", "#000080", "#00008B", "#0000CD",
						"#1E90FF", "#00BFFF", "#87CEFA", "#87CEEB", "#ADD8E6", "#B0E0E6", "#F0FFFF", "#E0FFFF", "#AFEEEE", "#00CED1",
						"#5F9EA0", "#48D1CC", "#00FFFF", "#40E0D0", "#20B2AA", "#008B8B", "#008080", "#7FFFD4", "#66CDAA", "#8FBC8F",
						"#3CB371", "#2E8B57", "#006400", "#008000", "#228B22", "#32CD32", "#00FF00", "#7FFF00", "#7CFC00", "#ADFF2F",
						"#98FB98", "#90EE90", "#00FF7F", "#00FA9A", "#556B2F", "#6B8E23", "#808000", "#BDB76B", "#B8860B", "#DAA520",
						"#FFD700", "#F0E68C", "#EEE8AA", "#FFEBCD", "#FFE4B5", "#F5DEB3", "#FFDEAD", "#DEB887", "#D2B48C", "#BC8F8F",
						"#A0522D", "#8B4513", "#D2691E", "#CD853F", "#F4A460", "#8B0000", "#800000", "#A52A2A", "#B22222", "#CD5C5C",
						"#F08080", "#FA8072", "#E9967A", "#FFA07A", "#FF7F50", "#FF6347", "#FF8C00", "#FFA500", "#FF4500", "#DC143C",
						"#FF0000", "#FF1493", "#FF00FF", "#FF69B4", "#FFB6C1", "#FFC0CB", "#DB7093", "#C71585", "#800080", "#8B008B",
						"#9370DB", "#8A2BE2", "#4B0082", "#9400D3", "#9932CC", "#BA55D3", "#DA70D6", "#EE82EE", "#DDA0DD", "#D8BFD8",
						"#E6E6FA", "#F8F8FF", "#F0F8FF", "#F5FFFA", "#F0FFF0", "#FAFAD2", "#FFFACD", "#FFF8DC", "#FFFFE0", "#FFFFF0",
						"#FFFAF0", "#FAF0E6", "#FDF5E6", "#FAEBD7", "#FFE4C4", "#FFDAB9", "#FFEFD5", "#FFF5EE", "#FFF0F5", "#FFE4E1"
					);

function ClearHtml()
{
	document.getElementById('MenuName').innerHTML ='';
	document.getElementById('MenuData').innerHTML ='';
}
function GetObjTop(id)
{
	var obj = document.getElementById(id);
	var top = obj.offsetTop;
	while (obj=obj.offsetParent) {
		top += obj.offsetTop;
	}
	return top;
}


function GetObjLeft(id)
{
	var obj = document.getElementById(id);
	var left = obj.offsetLeft;
	while (obj=obj.offsetParent) {
		left += obj.offsetLeft;
	}
	return left;
}

function GetMenuStyle(top, left)
{
	var str = 'padding:5px;width:150px;position:absolute;top:'+top+'px;left:'+left+'px;background-color:#FFFFFF;border:solid 1px #cccccc;z-index:1;';
	return str;
}


function DrawMenu(Obj,content)
{
	var oldHtml=document.getElementById('MenuName').innerHTML;
	if (oldHtml==Obj)
	{
		ClearHtml();
	}
	else
	{
		var top = GetObjTop(Obj) + 32;
		var left = GetObjLeft(Obj) + 1;
		var str = '';
		str += '<div style="'+GetMenuStyle(top, left)+'">';
		str += content;
		str += '</div>';
		document.getElementById('MenuName').innerHTML = Obj;
		document.getElementById('MenuData').innerHTML = str;
	}
}

function ShowFontSize()
{
	var str='';
	for(var i=1;i<6;i++)
		str+='<a onclick="javascript:ubbsize('+i+')"><font size='+i+'>'+i+'</font></a><br>';
	
	str="<p style='line-height:150%;margin:5px;cursor:pointer;;'>"+str+"</p>";
	DrawMenu("Ubb_fontsize",str);
}
function ShowFontColor()
{
	var str='';
	var str = '';
	str += '<table cellpadding="0" cellspacing="2" border="0">';
	for (i = 0; i < FontColor.length; i++) {
		if (i == 0 || (i >= 10 && i%10 == 0)) {
			str += '<tr>';
		}
		str += '<td style="width:12px;height:12px;border:1px solid #AAAAAA;font-size:10px;cursor:pointer;background-color:' +
		FontColor[i] + ';" onmouseover="javascript:this.style.borderColor=\'#000000\';" '+
		'onmouseout="javascript:this.style.borderColor=\'#AAAAAA\';" ' +
		'onclick="javascript:COLOR(\''+FontColor[i]+'\');">&nbsp;</td>';
		if (i >= 9 && i%(i-1) == 0) {
			str += '</tr>';
		}
	}
	str += '</table>';
	DrawMenu("Ubb_fontcolor",str);
}
function Show6kbbs()
{
	var str='';
		str+="<p style='line-height:150%;margin:5px;cursor:pointer;;'><a onclick='javascript:grade()'>等级可见</a><br><a onclick='javascript:reply()'>回复可见</a><br><a onclick='javascript:gname()'>指定读者</a><br><a onclick='javascript:smark()'>积分可见</a><br><a onclick='javascript:showdate()'>日期可见</a><br><a onclick='javascript:mustlogin()'>登陆可见</a></p>";
	DrawMenu("Ubb_6kbbs",str);
}
function ShowFace()
{
	var str='';
		for(var i=1;i<=15;i=i+1)
			str+='<A href=javascript:AddUbbCode(\'\',\'[em'+i+']\')><img border=0 src=images/emote/em'+i+'.gif></A>&nbsp;';
	DrawMenu("Ubb_face",str);
}
function UbbEditor(ObjName,Cols,Rows,DefaultValue,ImagePath)
{
	var iconStr;
	editObj=ObjName;
	this.cols=Cols;
	this.imagepath=
	this.rows=Rows;
	this.defaultValue=DefaultValue;
	this.imagePath=ImagePath;
	this.showeditor=function ()
	{
		iconStr="";
			for (var i = 0; i < ToolBar.length; i++) {
			iconStr += '<img id="'+ ToolBar[i][0] +'" src="' + this.imagePath + ToolBar[i][1] + '" alt="' + ToolBar[i][2] + '" align="absmiddle" onclick=RunMethod("'+ToolBar[i][0]+'") style="cursor:pointer;"> ';
			}

			iconStr='<div id="ubbeditordiv" style="display:none;">'+iconStr+'</div>';
		document.write(iconStr);
		//var editorstr="<textarea rows="+this.rows+" name="+editObj+"  id="+editObj+" cols="+this.cols+">"+this.defaultValue+"</textarea>";
		document.write('<span id="MenuName" style="display:none;"></span><span id="MenuData"></span>');
	}

}
function RunMethod(str)
{
	switch(str)
	{
		case'Ubb_fly':
			fly();
		break;

		case'Ubb_move':
			move();
		break;

		case'Ubb_glow':
			light();
		break;

		case'Ubb_shadow':
			ying();
		break;

		case'Ubb_bold':
			Cbold();
		break;

		case'Ubb_italicize':
			Citalic();
		break;

		case'Ubb_underline':
			Cunder();
		break;
		
		case'Ubb_sup':
			AddUbbCode('[sup]','[/sup]');
		break;

		case'Ubb_sub':
			AddUbbCode('[sub]','[/sub]');
		break;

		case'Ubb_code':
			AddUbbCode('[code]','[/code]');
		break;

		case'Ubb_fontsize':
			ShowFontSize();
		break;

		case'Ubb_fontcolor':
			ShowFontColor();
		break;

		case'Ubb_6kbbs':
			Show6kbbs();
		break;

		case'Ubb_face':
			ShowFace();
		break;

		case'Ubb_center':
			center();
		break;

		case'Ubb_image':
			image();
		break;

		case'Ubb_Shockwave':
			Cdir();
		break;

		case'Ubb_rm':
			Crm();
		break;

		case'Ubb_mp':
			Cwmv();
		break;

		case'Ubb_url':
			Curl();
		break;

		case'Ubb_email':
			Cemail();
		break;

		case'Ubb_csound':
			Csound();
		break;

		case'Ubb_ra':
			Cra();
		break;

		default: 
		break;

	}
}
function WordAdd(){
	if ((document.selection)&&(document.selection.type == "Text")) {
		var range = document.selection.createRange();
		var ch_text=range.text;
		range.text = fontbegin + ch_text + fontend;
	} 
	else {
		document.getElementById(editObj).value=fontbegin+document.getElementById(editObj).value+fontend;
		document.getElementById(editObj).focus();
	}
}
function AddText(text) {
	if (document.getElementById(editObj).createTextRange && document.getElementById(editObj).caretPos) {      
		var caretPos = document.getElementById(editObj).caretPos;      
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
		text + ' ' : text;
	}
	else document.getElementById(editObj).value += text;
	document.getElementById(editObj).focus(caretPos);
}

helpstat = false;
stprompt = true;
basic = false;
function thelp(swtch){
	if (swtch == 1){
		basic = false;
		stprompt = false;
		helpstat = true;
	} else if (swtch == 0) {
		helpstat = false;
		stprompt = false;
		basic = true;
	} else if (swtch == 2) {
		helpstat = false;
		basic = false;
		stprompt = true;
	}
}
function Crm() {
	if (helpstat) {
               alert("realplay\n播放realplay文件.\n用法: [rm=宽度, 高度]文件地址[/rm]");
	} else if (basic) {
		AddTxt="[rm=480,400][/rm]";
		AddText(AddTxt);
	} else { 
		txt2=prompt("视频的宽度，高度","480,400"); 
		if (txt2!=null) {
			txt=prompt("视频文件的地址","请输入");
			if (txt!=null) {
				if (txt2=="") {
					AddTxt="[rm=480,400]"+txt;
					AddText(AddTxt);
					AddTxt="[/rm]";
					AddText(AddTxt);
				} else {
					AddTxt="[rm="+txt2+"]"+txt;
					AddText(AddTxt);
					AddTxt="[/rm]";
					AddText(AddTxt);
				}         
			} 
		}
	}
}

function Cwmv() {
	if (helpstat) {
               alert("Media Player\n播放Media Player文件.\n用法: [mp=宽度, 高度]文件地址[/mp]");
	} else if (basic) {
		AddTxt="[mp=480,400][/mp]";
		AddText(AddTxt);
	} else { 
		txt2=prompt("视频的宽度，高度","480,400"); 
		if (txt2!=null) {
			txt=prompt("视频文件的地址","请输入");
			if (txt!=null) {
				if (txt2=="") {
					AddTxt="[mp=480,400]"+txt;
					AddText(AddTxt);
					AddTxt="[/mp]";
					AddText(AddTxt);
				} else {
					AddTxt="[mp="+txt2+"]"+txt;
					AddText(AddTxt);
					AddTxt="[/mp]";
					AddText(AddTxt);
				}         
			} 
		}
	}
}
function Cdir() {
	if (helpstat) {
               alert("Shockwave\n插入Shockwave文件.\n用法: [dir=宽度, 高度]文件地址[/dir]");
	} else if (basic) {
		AddTxt="[dir=480,400][/dir]";
		AddText(AddTxt);
	} else { 
		txt2=prompt("Shockwave文件的宽度，高度","480,400"); 
		if (txt2!=null) {
			txt=prompt("Shockwave文件的地址","请输入地址");
			if (txt!=null) {
				if (txt2=="") {
					AddTxt="[dir=480,400]"+txt;
					AddText(AddTxt);
					AddTxt="[/dir]";
					AddText(AddTxt);
				} else {
					AddTxt="[dir="+txt2+"]"+txt;
					AddText(AddTxt);
					AddTxt="[/dir]";
					AddText(AddTxt);
				}         
			} 
		}
	}
}
function COLOR(color){
		ClearHtml();
		document.getElementById(editObj).focus();
		var text=prompt("请输入要设置颜色的文字", "");
	if(text){
		document.getElementById(editObj).value += '[color=' + color + ']'+ text + '[/color]';
	}
}

function ubbsize(size){
		ClearHtml();
		document.getElementById(editObj).focus();

	var text=prompt("请输入要设置字体大小的文字", "");
	if(text){
		document.getElementById(editObj).value += '[size=' + size + ']'+ text + '[/size]';
	}
}
function image() {
var FoundErrors = '';
var enterURL   = prompt("请输入图片地址","http://");
if (!enterURL) {
FoundErrors +="\n";
}
if (FoundErrors) {
return;
}
var ToAdd = "[IMG]"+enterURL+"[/IMG]";
document.getElementById(editObj).value+=ToAdd;
document.getElementById(editObj).focus();
}
function fly() {
fontbegin="[fly]";
fontend="[/fly]";
WordAdd();
}
function move() {
fontbegin="[move]";
fontend="[/move]";
WordAdd();
}
function center() {
fontbegin="[align=center]";
fontend="[/align]";
WordAdd();
}
function light() {
fontbegin="[light]";
fontend="[/light]";
WordAdd();
}
function grade() {
	ClearHtml();
	var ToAdd = "[showtograde=1]内容[/s]";
	document.getElementById(editObj).value+=ToAdd;
	document.getElementById(editObj).focus();
}
function gname() {
	ClearHtml();
	var ToAdd = "[showtoname=对方昵称]内容[/s]";
	document.getElementById(editObj).value+=ToAdd;
	document.getElementById(editObj).focus();
}
function reply() {
	ClearHtml();
	var ToAdd = "[showtoreply]内容[/s]";
	document.getElementById(editObj).value+=ToAdd;
	document.getElementById(editObj).focus();
}
function ying() {
	fontbegin="[SHADOW=255,yellow,1]";
	fontend="[/shadow]";
	WordAdd();
}
function smark() {
	ClearHtml();
	var ToAdd = "[smark=10]内容[/s]";
	document.getElementById(editObj).value+=ToAdd;
	document.getElementById(editObj).focus();
}

function Curl() {
var FoundErrors = '';
var enterURL   = prompt("请输入连接网址", "http://");
var enterTxT   = prompt("请输入连接说明", enterURL);
if (!enterURL)    {
FoundErrors += "\n" + "您必须输入网址";
}
if (!enterTxT)    {
FoundErrors += "\n" + "您必须连接说明";
}
if (FoundErrors)  {
alert("错误！"+FoundErrors);
return;
}
var ToAdd = "[URL="+enterURL+"]"+enterTxT+"[/URL]";
document.getElementById(editObj).value+=ToAdd;
document.getElementById(editObj).focus();
}

function showdate() {
	ClearHtml();
	var FoundErrors = '';
	var enterdate=prompt("请输入日期按这样格式2012-12-21,该帖将这天以后方能浏览","2012-12-21");
	if (!enterdate) {
	FoundErrors += "\n" +"请输入正确日期";
	}

	if (FoundErrors) {
	alert("错误！"+FoundErrors);
	return;
	}
	var ToAdd = "[showdate="+enterdate+"]内容[/s]";
	document.getElementById(editObj).value+=ToAdd;
	document.getElementById(editObj).focus();
}

function Cemail() {
var emailAddress = prompt("请输入邮件地址","");
if (!emailAddress) { alert("请输入正确的email地址"); return; }
var ToAdd = "[email]"+emailAddress+"[/email]";
document.getElementById(editObj).value+=ToAdd;
document.getElementById(editObj).focus();
}
function Cbold() {
fontbegin="[B]";
fontend="[/B]";
WordAdd();
}
function Citalic() {
fontbegin="[I]";
fontend="[/I]";
WordAdd();
}
function Cunder() {
fontbegin="[U]";
fontend="[/U]";
WordAdd();
}

function mustlogin() {
	ClearHtml();
	var ToAdd = "[slogin]内容[/s]";
	document.getElementById(editObj).value+=ToAdd;
	document.getElementById(editObj).focus();
}

function DoTitle(addTitle){
	document.getElementById("topic").value=addTitle+document.getElementById("topic").value;
	document.getElementById("topic").focus();

}
function openwin()
{
document.forms[1].topic.value=document.forms[0].topic.value;
document.forms[1].content.value=document.forms[0].content.value;
var popupWin = window.open('post.asp?action=preview', 'showgg', 'width=500,height=400,resizable=1,scrollbars=yes,menubar=no,status=yes');
document.forms[1].submit()
}

function Csound() {
var FoundErrors = '';
var enterURL   = prompt("请输入背景音乐地址", "http://");
if (!enterURL) {
FoundErrors += "\n" + "您必须要输入有效地址";
}
if (FoundErrors) {
alert("错误！"+FoundErrors);
return;
}
var ToAdd = "[SOUND]"+enterURL+"[/SOUND]";
document.getElementById(editObj).value+=ToAdd;
document.getElementById(editObj).focus();
}

function Cra() {
var FoundErrors = '';
var enterURL   = prompt("请输入real音频文件地址", "http://");
if (!enterURL) {
FoundErrors += "\n" + "您必须输入有效的地址";
}
if (FoundErrors) {
alert("错误！"+FoundErrors);
return;
}
var ToAdd = "[RA]"+enterURL+"[/RA]";
document.getElementById(editObj).value+=ToAdd;
document.getElementById(editObj).focus();
}

function AddUbbCode(code1,code2){
	ClearHtml();
fontbegin=code1;
fontend=code2;
WordAdd();
}
