﻿var doc = null, box = null, win = null, mode = 'design', created = false;
var formname = "";
function createeditor(formid)
{
    formname = formid ;
	box	= form(formid).editbox;	 
	
	form(formid).submit= null;
	win = $('editwin');
		
	doc = editwin.document;

	doc.designMode = 'on';
	doc.contentEditable = true;
	doc.write('');
	doc.close();
	setTimeout('initcontent();', 100); 	  
	createtoolbar();
//	createemotion(1, 20);
	created = true;
	switchmode(mode);

}

function gettext() {
	return box.value;
}

function settext(text) {
	box.value = text;
}

function gethtml() {
	return doc.body.innerHTML;
}

function sethtml(html) {
	doc.body.innerHTML = html;
}

function switchmode(m, btn) {	
	switch(m) {
		case 'ubb':	 
			ubbmode();				
			break;
		case 'design':  
			designmode();
			break;
		case 'html': 
		    htmlmode();
			break;
		 
	}
	
	mode = m;
	if (btn == null) return;
	form(formname).htmlbtn.className = 'ubb_unselected';
	form(formname).ubbbtn.className = 'ubb_unselected';
	form(formname).designbtn.className = 'ubb_unselected';
	btn.className = 'ubb_selected';
	focuseditor();
}
	
function ubbmode() {
	win.style.display = 'none';
	box.style.display = '';
	if (!created || mode == 'ubb') return;
	if (mode == 'html') {
		settext(html2ubb(gettext()));
	}
	else {
		settext(html2ubb(gethtml()));
	}
}
function htmlmode() {
	win.style.display = 'none';
	box.style.display = '';
	if (!created || mode == 'html') return;
	if (mode == 'ubb') {
		settext(ubb2html(gettext()));
	}
	else {
		settext(gethtml());
	}
}
function designmode() {
	
	win.style.display = '';
	box.style.display = 'none';	
	if (!created || mode == 'design') return;

	if (mode == 'html') {
		sethtml(gettext());
	}
	else {
		sethtml(ubb2html(gettext()));
	}
}
function fixdata() {
    
	if (mode == 'ubb')
	{
		sethtml(ubb2html(gettext()));
		settext(html2ubb(gethtml()));
	}
	if (mode == 'html')
		settext(html2ubb(gettext()));
	else
		settext(html2ubb(gethtml()));
	
}
function focuseditor() {
	if (mode == 'design') {
		doc.body.focus();
	}
	else {
		box.focus();
	}
}

function createtoolbar() {
	var buttons = ['bold', '粗体', 'italic', '斜体', 'underline', '下划线', 
				   '-', 
				   'fontname', '字体', 'fontsize', '大小', 'fontcolor', '字体颜色',
				   '-',
				   'justifyleft', '居左', 'justifycenter', '居中', 'justifyright', '居右', 
				   '-',
				   'insertorderedlist', '数字顺序', 'insertunorderedlist', '符号顺序', 'indent', '缩进', 'outdent', '取消缩进' ,'-','subscript', '上标', 'superscript', '下标', 
				   //'',
				   'code', '插入程序源代码', 'hide', '插入回复可见内容','createlink', '超级链接', 'unlink', '删除超级链接', 'vote', '插入投票', 'mediaplay', '插入MediaPlay视频', 'flash', '插入Flash动画', 'real', '插入RealPlay视频','email', '插入Email地址', 'image',  '插入图片'
				  ];
	var bar = '<div style="width:500px;height:25px;">';
	
							
	for (var i = 0; i < buttons.length; i++) {
		if (buttons[i] == '') {
			bar += '<p class="partition"></p>';
			continue;
		}
		if (buttons[i] == '-') {
			bar += ' <img src="/images/ubb/separator.gif" alt="" />';
			continue;
		}
		 
//		if (!AllowMedia && ( buttons[i]=='mediaplay'||buttons[i]=='real')) { i++; continue;}
//		if (!AllowCode && buttons[i]=='code') { i++; continue;}
//		if (!AllowFlash && buttons[i]=='flash') { i++; continue;}
//		if (!AllowImage && buttons[i]=='image') { i++; continue;}
		if ( buttons[i]=='mediaplay'||buttons[i]=='real') { i++; continue;}
		if ( buttons[i]=='code') { i++; continue;}
		if ( buttons[i]=='flash') { i++; continue;}
		if ( buttons[i]=='image') { i++; continue;}
		if ( buttons[i]=='vote') { i++; continue;}
		if ( buttons[i]=='hide') { i++; continue;}
		bar += ' <img onclick="cmd(event);" class="hand" src="/images/ubb/' + buttons[i] + '.gif" name="' + buttons[i] + '" alt="' + buttons[i + 1] + '" class="ubb_button" />';
		i++
	} 
 
	bar += "</div>";
	 
	var obj = $('toolbar');
	if (obj != null)
	{
	 	obj.innerHTML = bar;
	}
	 
}

function createemotion(start, count)
{
	var bar = '';
	for (var i = start; i < start + count; i++) {


		bar +=' <img src="images/emotions/' + i + '.gif" alt="" onclick="insertemotion(' + i + ')"/>';
	}
	var obj = $('emotion');
	if (obj != null)
	 	obj.innerHTML = bar;
}

function cmd(even)
{  

	var srcEle = ie ? event.srcElement : even.target;
	var sCmd = srcEle.name;
	switch(sCmd)
	{
	    case "vote":
	        showInputVoteDialog(even);
	        break;
	    case "email":
	        showInputEmailDialog(even);
	        break;
	    case "createlink":
	        showInputLinkDialog(even);
	        break;
	    case "image":
	        showInputImageDialog(even);
	        break;
	    case "fontsize":
	        showFontSizeDialog(even);
	        break;
	    case "fontcolor":
	        showColorDialog(even);
	        break;
	    case "fontname":
	        showFontNameDialog(even);
	        break;
	    case "mediaplay":
	        showInputMediaDialog(even);
	        break;
	    case "flash":
	        showInputFlashDialog(even);
	        break;
	    case "real":
	        showInputRealDialog(even);
	        break;
	    case "hide":
	        showInputHideDialog(even);
	        break;
	    case "code":
	        showInputCodeDialog(even);
	        break;
	    default:
	        execmd(sCmd, false, '');
	        break;
	}
}
function execmd(sCmd, bInterface, vValue) {
//focuseditor();	
	
	if (mode == 'design') {
		doc.execCommand(sCmd, (bInterface?bInterface : false), (vValue ? vValue : ''));
	}
	else 
	{
		var index = 1;
		if (mode == 'ubb') index = 0;
		
		var arr = {
			bold : [[ '[b]', '[/b]' ],[ '<b>', '</b>' ]],
			italic : [[ '[i]', '[/i]' ],[ '<i>', '</i>' ]],
			underline : [[ '[u]', '[/u]' ],[ '<u>', '</u>' ]],
			justifyleft : [[ '[align=left]', '[/align]' ],[ '<p align="left">', '</p>' ]],
			justifycenter : [[ '[align=center]', '[/align]' ],[ '<p align="center">', '</p>' ]],
			justifyright : [[ '[align=right]', '[/align]' ],[ '<p align="right">', '</p>' ]],
			insertorderedlist : [[ '[list=1][*]', '[/list]'],[ '<ol><li>', '</li></ol>']],
			insertunorderedlist : [[ '[list][*]', '[/list]' ],[ '<ul><li>', '</li></ul>' ]],
			indent : [[ '[indent]', '[/indent]' ], [ '<blockquote>', '</blockquote>' ]],
			subscript : [[ '[sub]', '[/sub]' ],[ '<sub>', '</sub>' ]],
			superscript : [[ '[sup]', '[/sup]' ],[ '<sup>', '</sup>' ]],
			fontname : [['[font=' + vValue + ']', '[/font]' ],['<font face="' + vValue + '">', '</font>' ]],
			forecolor : [['[color=' + vValue + ']', '[/color]' ],['<font color="' + vValue + '">', '</font>' ]],
			fontsize : [['[size=' + vValue + ']', '[/size]' ],['<font size="' + vValue + '">', '</font>' ]]
		};
		for (var a in arr) {
			if (a == sCmd) {
				applyubb(arr[a][index][0], arr[a][index][1]);
				break;
			}
		}				
	}
}


function inserthtml(shtml) { 
	focuseditor();
	if (ie)	{
		var range = doc.selection.createRange();
		if (mode == 'html')
			range.text = shtml;
		else
			range.pasteHTML(shtml);
	}
	else
	{
		sethtml(gethtml()+shtml)
	}
}

function insertubb(text) {
	focuseditor();
	if (document.selection && document.selection.createRange) {
		var range = document.selection.createRange();
		range.text = text;
	} else {
		box.value += text;
	}
}

function applyubb(prepend, append) {
	focuseditor();
	if (document.selection && document.selection.createRange) {
		var range = document.selection.createRange();
		range.text = prepend + range.text + append;					
	} else {
		box.value += text;
	}
}

function insert(str) {
	if (mode == 'design') {
		inserthtml(str);
	}
	else {
		insertubb(str);
	}
}
function insertemotion(num) {
	if (mode == 'ubb')	{
		insert('[em]' + num + '[/em]');
	}	
	else{
		insert('<img src="images/emotions/' + num + '.gif" alt="em" />');
	}
}

function checksubmit()
{
	if (checkpost())
		return true;
	else
	{
		$('post').disabled = false;
		return false;
	}
}

function checkpost()
{  
	var f = form(formname);
	$('post').disabled = true;
	 
	if (f.title!='')
	{
	  
		if (isEmpty(f.title))
		{
			 
			alert('请输入标题!');
			f.title.focus();
			return false;
		}
		if (f.title.value.trim().length > 50)
		{

			alert("标题过长，已经超出所允许的长度，您当前允许标题的长度为 50 个字符！");
			f.title.focus();
			f.title.select();
			return false;			
		}
	}
	
	var quickmode = false;
	if (!quickmode) fixdata(); 
 
	if (isEmpty(f.editbox))
	{
		 
		alert('请输入发表内容！');
		if(!quickmode) focuseditor();
		else f.editbox.focus();		
		
		return false;
	} 

	if (f.editbox.value.length > 5000)
	{
		 
		alert("内容过长，已经超出所允许的长度，您当前允许发表内容的长度为 5000 个字符！");
		if(!quickmode) focuseditor();
		else f.editbox.focus();		
		
		return false;
	}
	 
 
	return true;
}

function setframestyle() { 
	var head = doc.getElementsByTagName('HEAD').item(0);
	
	var style = doc.createElement('link');
	
	style.href = $("iframestyle").value;
	style.rel = 'stylesheet';
	style.type = 'text/css';
	head.appendChild(style);
}

function initcontent() {
 
	sethtml(ubb2html(gettext()));
	stat();
	setframestyle();
	
	if (ie)
	{
		box.onkeydown = function()
		{
			
			return ctlent(event);
		}
		box.onfocus = function()
		{
			 hideDialog();
		}
		doc.body.onfocus = function()
		{
			 hideDialog();
		}
		 
		doc.body.onkeydown = function(){
			if(editwin.window.event.ctrlKey && editwin.window.event.keyCode == 13)
			{ 
				submitonce();
				return false;
			}
		}
	}
	else
	{		
		box.addEventListener("keydown", 
							 function(e) {
											if(e.ctrlKey && e.keyCode == 13)
											{
												submitonce();
											} 
										  }, true);
		box.addEventListener("focus", 
							 function(e) { 
							  hideDialog();
										  }, true);
		
		doc.addEventListener("focus", 
							 function(e) { 
							  hideDialog();
										  }, true);
		 
		doc.addEventListener("keydown", function(e)
										{
										  if(e.ctrlKey && e.keyCode == 13)
										  {
												submitonce();
												return false;
										  }
										}, true);
	}
}

//Ctrl + Enter
function ctlent(e)
{
	 if(e.ctrlKey && e.keyCode == 13)
		{
			submitonce();
			return false;
		} 
}
function submitonce()
{
	$('post').click();
}
 
var filecount = 0;
var temp = '<div style="margin-bottom:3px;"><input class="input1" type="file" name="uploader{id}" id="upload{id}" size="60" onchange="uploadfile(this);" contentEditable="false" />';
function onuploadfile()
{
    
	if (LeftCount < 1)
	{
		alert("您今天不能再上传文件了！");
		return;
	}
	filecount ++;
	
	var ctrl = temp.replace('{id}', filecount);	
	var div = document.createElement("div");
	div.setAttribute("id", "dup" + filecount);
	div.innerHTML = ctrl + '&nbsp;&nbsp;&nbsp;<input type="hidden" name="uploadfiletype' + filecount + '" value="-1"/> 售价：<input type="text" value="0" style="height:19px; line-height:19px;" name="downloadrequire' + filecount + '" size="5"/>&nbsp;金钱&nbsp;&nbsp;&rsaquo;&rsaquo;<a href="javascript:clearfile(' + filecount + ');">&nbsp;删除&nbsp;</a><span class="t1" id="size' + filecount + '"></span></div>';
	$('filecontainer').appendChild(div); 	 
	 
}
function endWith(s1,s2)  
  {   
      if(s1.length<s2.length)   
        return   false;   
      if(s1==s2)   
        return   true;   
      if(s1.substring(s1.length-s2.length)==s2)   
          return   true;   
  }
function checkuploadfile(file)
{ 
	var types = AttachType.split('|');
	var vext = false;
	for(var i = 0 ; i < types.length; i++)
	{
		if (endWith(file.value.toLowerCase(), types[i]))
		{
			vext = true;
			break;
		}
	}
	if (!vext)
	{
		alert("只允许上传扩展名为 " + AttachType + " 的文件！");
		return false;
	}
	
	var filesize = getFileSize(file);
	if (filesize != -1)
	{
		if (filesize > AllowAttachSize)
		{
			alert("文件过大，最大只允许上传" + size(AllowAttachSize) + "的文件！");
			return false;
		}
		if (filesize > LeftSize)
		{
			alert("文件过大，您今天只能上传" + size(LeftSize) + "了！");
			return false;
		}
		if (LeftCount < 1)
		{
			alert("您今天不能再上传文件了！");
			return false;
		}
		if (ie) $('size' + file.name.replace('uploader', '')).innerHTML = '(' + size(filesize) + ')';
		LeftSize -= filesize;
	}
	LeftCount --;
	return true;
}
function uploadfile(file)
{	
	if (!checkuploadfile(file))	
	{
		clearfile(file.name.replace('uploader', ''), true);
		removeuploadfile(file.name.replace('uploader', ''));
		return;
	}
	var old = file.name;
	fixdata();
	if (gettext().indexOf('[uploadimage]' + old.replace('uploader', '') + '[/uploadimage]') > -1 ||
		gettext().indexOf('[uploadfile]' + old.replace('uploader', '') + '[/uploadfile]') > -1)	
	{
		file.name = 'temp';
		file.id = 'temp';
		removeuploadfile(old.replace('uploader', ''));
		file.id = file.name = old;
	}
	else
	{
		focuseditor();
		var type = '';
		if (isimg(file.value))
		{  
			type = 'image';
			
		}
		else
		{ 
			type = 'file';
		}
		$("uploadfiletype" + filecount).value = type ;
		if (mode == 'ubb')	{
			insert('[upload'  + type + ']' + file.name.replace('uploader', '') + '[/upload' + type + ']');
		}	
		else{
			if (type == 'file')
				insert('<br /><img src="images/icon/hasrar.gif" alt="uploadfile' + file.name.replace('uploader', '') + '" /><br />');
			else
				insert('<br /><img src="' + file.value + '" alt="uploadimage' + file.name.replace('uploader', '') + '" /><br />');
		}
	}
}

function removeuploadfile(index)
{
	if (mode == 'design')	{
		sethtml(ubb2html(html2ubb(gethtml())));
 	}
	if (mode == 'html')	{
		settext(ubb2html(html2ubb(gettext())));
 	}
}

function clearfile(id, flag)
{
	var elem = $('dup' + id);
	elem.parentNode.removeChild(elem);
	removeuploadfile(id);
	if (undefined(flag))
	{
		var uploader = $('uploader' + id);
		LeftSize -= getFileSize(uploader);
		LeftCount++;
	}
}
 

function changetopictype(t)
{
	 
}
 
var votecount = 0;
function insertVote()
{
  
	if (isEmpty($('voteitems'))) 
	{
		alert('请输入投票项！');
		$('voteitems').focus();
		return;
	}
	if(trim($('voteitems').value).split('\n').length < 2)
	{
		alert('投票项必须2项或以上！');
		$('voteitems').focus();
		return;
	}
	if (isEmpty($('VoteExpirty')))
	{
		alert('请输入有效天数！');
		$('VoteExpirty').focus();
		return;
	}
	
	if (!isInt($('VoteExpirty')))
	{
		alert('有效天数必须是整数！');
		$('VoteExpirty').focus();
		$('VoteExpirty').select();
		return;
	}
	
	if (isEmpty($('VoteRequire')))
	{
		alert('请输入投票限制！');
		$('VoteRequire').focus();
		return;
	}
	
	if (!isInt($('VoteRequire')))
	{
		alert('投票限制必须是数字！');
		$('VoteRequire').focus();
		$('VoteRequire').select();
		return;
	}
	
	votecount ++;
	var div = document.createElement("div");
	div.setAttribute("id", "dvote" + votecount);
	div.innerHTML ='<textarea name="voteitems' + votecount + '">' + $('voteitems').value + '</textarea><input name="VoteExpirty' + votecount + '"  value="' + $('VoteExpirty').value + '" /></textarea><input name="VoteRequire' + votecount + '"  value="' + $('VoteRequire').value + '" />';
	if ($('multivote').checked)
		div.innerHTML += '<input name="multivote' + votecount + '"  value="1" />';
	$('votecontainer').appendChild(div);
	
	insert('[vote]' + votecount + '[/vote]');
	hideDialog();	
}

function showDialog(even, w, h, html)
{
	var src = document.all ? event.srcElement : even.target;	
	var x = src.offsetLeft, y = src.offsetTop; 
	var e = src.offsetParent;
	x += e.offsetLeft; 
	y += e.offsetTop;
	while(e=e.offsetParent) 
	{ 
		x += e.offsetLeft; 
		y += e.offsetTop;
	}
	
	var div = $('inputdialog');
	if (div == null)
	{
		div = document.createElement("div");
		div.id = 'inputdialog';
		div.className = 'inputdialog';
		div.style.position = 'absolute';
		div.style.padding = '5px';
		document.body.appendChild(div);
	}
	div.style.top = y + src.offsetHeight + 'px';
	div.style.left = x + 'px';
	div.style.width = w + 'px';
	div.style.height = h + 'px';
	div.innerHTML = html;
}
function showInputLinkDialog(even)
{  
	showDialog(even, 280, 75, '输入超级链接<p class="partition"></p><input type="text" class="input1" id="inputlink" size="50" value="http://" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertLink()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}
function showInputEmailDialog(even)
{  
	showDialog(even, 280, 75, '输入Email地址<p class="partition"></p><input type="text" class="input1" id="inputemail" size="50" value="" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertEmail()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}
function showInputImageDialog(even)
{  
	showDialog(even, 280, 75, '输入图像地址<p class="partition"></p><input type="text" class="input1" id="inputimage" size="50" value="http://" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertImage()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}
function showInputCodeDialog(even)
{  
	showDialog(even, 500, 210, '输入程序源代码<p class="partition"></p><textarea  id="inputcode" style="width:98%; height:150px;"></textarea><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertCode()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}
function showInputHideDialog(even)
{  
	showDialog(even, 500, 210, '输入回复可见内容<p class="partition"></p><textarea  id="inputhide" style="width:98%; height:150px;"></textarea><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertHide()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');
	
}

function showInputFlashDialog(even)
{ 	
	showDialog(even, 320, 100, '插入 Flash 对象<p class="partition"></p>宽&nbsp;&nbsp;：<input type="text" class="input1" id="flashwidth" value="600" size="5" /> px&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;高&nbsp;&nbsp;：<input type="text" class="input1" id="flashheight" value="400" size="5"/> px<p class="partition"></p>URL：<input type="text" class="input1" id="flashurl" value="http://" size="50" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertFlash()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');	 
}

function showInputRealDialog(even)
{ 	
	showDialog(even, 320, 100, '插入 RealPlayer 视频<p class="partition"></p>宽&nbsp;&nbsp;：<input type="text" class="input1" id="realwidth" value="600" size="5" /> px&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;高&nbsp;&nbsp;：<input type="text" class="input1" id="realheight" value="400" size="5"/> px<p class="partition"></p>URL：<input type="text" class="input1" id="realurl" value="http://" size="50" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertReal()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');	 
}
function showInputMediaDialog(even)
{ 	
	showDialog(even, 320, 100, '插入 MediaPlayer 视频<p class="partition"></p>宽&nbsp;&nbsp;：<input type="text" class="input1" id="mediawidth" value="600" size="5" /> px&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;高&nbsp;&nbsp;：<input type="text" class="input1" id="mediaheight" value="400" size="5"/> px<p class="partition"></p>URL：<input type="text" class="input1" id="mediaurl" value="http://" size="50" /><p class="partition"></p><input type="button" class="button" value=" 确 定 " onclick="insertMedia()" /> <input type="button" class="button" value=" 取 消 " onclick="hideDialog()" />');	 
}
function showInputVoteDialog(even)
{ 	
	showDialog(even, 450, 190, '投票项目&nbsp;<span style="color:#F00">*</span><p class="partition"></p><p class="an">有效天数&nbsp;<input name="VoteExpirty" id="VoteExpirty" class="input2" onBlur="this.className=\'input2\'" onFocus="this.className=\'input1\'" size="5" value="0" />&nbsp;(小于1天表示永不过期)&nbsp;&nbsp;&brvbar;&nbsp;&nbsp;至少有多少积分才可投票：<input name="VoteRequire" id="VoteRequire" class="input2" onBlur="this.className=\'input2\'" onFocus="this.className=\'input1\'" size="5" value="0" /></p><p class="partition"></p><textarea name="voteitems" id="voteitems" rows="10" style="height:100px;width:98%; padding:2px;"></textarea><p class="partition"></p><p class="an"><input id="multivote" type="checkbox" name="multivote" class="smallbox" value="1">&nbsp;<span class="t1">允许多选</span>&nbsp;&nbsp;&nbsp;<input type="button" class="button" value="&nbsp;&nbsp;&nbsp;确定&nbsp;&nbsp;&nbsp;" onClick="insertVote();"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type="button"  class="button" value="&nbsp;&nbsp;&nbsp;取消&nbsp;&nbsp;&nbsp;" onClick="hideDialog();"/></p>');	 
}


function showColorDialog(even)
{
	var colorlist=new Array(40);
	var ocbody = '';
	colorlist[0]="#000000"; colorlist[1]="#993300"; colorlist[2]="#333300"; colorlist[3]="#003300"; 
	colorlist[4]="#003366"; colorlist[5]="#000080"; colorlist[6]="#333399"; colorlist[7]="#333333"; 
	
	colorlist[8]="#800000"; colorlist[9]="#FF6600"; colorlist[10]="#808000";colorlist[11]="#008000"; 
	colorlist[12]="#008080";colorlist[13]="#0000FF";colorlist[14]="#666699";colorlist[15]="#808080"; 
	
	colorlist[16]="#FF0000";colorlist[17]="#FF9900";colorlist[18]="#99CC00";colorlist[19]="#339966"; 
	colorlist[20]="#33CCCC";colorlist[21]="#3366FF";colorlist[22]="#800080";colorlist[23]="#999999"; 
	
	colorlist[24]="#FF00FF";colorlist[25]="#FFCC00";colorlist[26]="#FFFF00";colorlist[27]="#00FF00"; 
	colorlist[28]="#00FFFF";colorlist[29]="#00CCFF";colorlist[30]="#993366";colorlist[31]="#CCCCCC"; 
	
	colorlist[32]="#FF99CC";colorlist[33]="#FFCC99";colorlist[34]="#FFFF99";colorlist[35]="#CCFFCC"; 
	colorlist[36]="#CCFFFF";colorlist[37]="#99CCFF";colorlist[38]="#CC99FF";colorlist[39]="#FFFFFF"; 
	
	ocbody = ""; 
    ocbody += "<table cellspacing='2' cellpadding='1'>"; 
	for(var i=0;i<colorlist.length;i++) { 
		if(i%8==0) 
			ocbody += "<tr>"; 
		ocbody += "<td width=\"12\" height=\"12\" style=\"border:1px solid #FFFFFF;\" onMouseOut=\"colordialogmouseout(this);\" onMouseOver=\"colordialogmouseover(this);\" onMouseDown=\"colordialogmousedown('"+colorlist[i]+"')\" align=\"center\" valign=\"middle\"><table style=\"border:1px solid #808080;\" width=\"12\" height=\"12\" bgcolor=\""+colorlist[i]+"\"><tr><td></td></tr></table></td>"; 
		if(i%8==7) 
			ocbody += "</tr>"; 
	} 
ocbody += "</table>"; 
showDialog(even, 130, 83, ocbody);
}

function showFontSizeDialog(even)
{
	var ocbody = ""; 
     ocbody += "<table  cellspacing='0' cellpadding='3' width='100%' height='230px'>"; 
	for(var i=1;i<8;i++) {  
			ocbody += "<tr>"; 
		ocbody += "<td  onMouseOut=\"colordialogmouseout(this);\" onMouseOver=\"colordialogmouseover(this);\" onMouseDown=\"sizedialogmousedown('"+i+"')\" align=\"center\" valign=\"middle\"><font size='" + i + "'>" + i + "</font></td></tr>"; 
	} 
    ocbody += "</table>"; 
    
showDialog(even, 37, 240, ocbody);
}

function showFontNameDialog(even)
{
	var faces = ['宋体', '黑体','sans-serif','courier new','arial','tahoma','verdana','FixedSys','Impact'];
var ocbody = ""; 
ocbody += "<table cellspacing='0' cellpadding='3' width='100%'>"; 

for(var i=0; i < faces.length; i++)
ocbody += "<tr><td onMouseOut=\"colordialogmouseout(this);\" onMouseOver=\"colordialogmouseover(this);\" onMouseDown=\"fontdialogmousedown('"+faces[i]+"')\" align=\"left\" valign=\"middle\"><font face='" + faces[i] + "'>" + faces[i] + "</font></td></tr>"; 
 
ocbody += "</table>"; 
showDialog(even, 100, 180, ocbody);
}


function colordialogmouseout(obj){ 
obj.style.borderColor="#FFFFFF"; 
obj.bgColor="";
}

function colordialogmouseover(obj){ 
obj.style.borderColor="#333333"; 
obj.bgColor="#FFFFFF"; 
} 
function colordialogmousedown(color){
execmd('forecolor', false, color);
hideDialog();
focuseditor();
} 
function sizedialogmousedown(s){
execmd('fontsize', false, s);
hideDialog();
focuseditor();
} 
function fontdialogmousedown(s){
execmd('fontname', false, s);
hideDialog();
focuseditor();
} 
function insertLink()
{
	var m = $('inputlink').value;
	
	if (mode == 'ubb')	{
		insert('[url=' + m + ']' + m + '[/url]');
	}	
	else{
		insert('<a href="' + m + '">' + m + '</a>');
	}
	hideDialog();
}
function insertEmail()
{
	var m = $('inputemail').value;
	if (m.search(/^[^\s@:"'<>,&]{2,}@[^\[\.]+\.[^\[]{2,}$/gi) < 0)
   	{
		$('inputemail').focus();
	   alert('Email 地址格式不正确');
	   return;
	   
   	}
	if (mode == 'ubb')	{
		insert('[email=' + m + ']' + m + '[/email]');
	}	
	else{
		insert('<a href="mailto:' + m + '">' + m + '</a>');
	}
	hideDialog();
}

function insertImage()
{
	var m = $('inputimage').value;
	 
	if (mode == 'ubb')	{
		insert('[img]' + m + '[/img]');
	}	
	else{
		insert('<img src="' + m + '" />');
	}
	hideDialog();
}
function insertCode()
{
	if (mode == 'ubb')	{
		insert('[code]\n' + $('inputcode').value + '\n[/code]');
	}	
	else{
		insert('[code]\n' + ubb2html($('inputcode').value) + '\n[/code]');
	}
	hideDialog();
}
function insertHide()
{
	insert('[hide]\n' + ubb2html($('inputhide').value) + '\n[/hide]');
	hideDialog();
}
function insertFlash()
{ 
	var w = parseInt($('flashwidth').value);
	var h = parseInt($('flashheight').value);
	
	if(w > 1024)
		w = 600;
	if (h > 768)
		h = 400;
	insert('[flash=' + w + ',' + h + ']\n' + $('flashurl').value + '\n[/flash]'); 
	hideDialog();
}

function insertReal()
{ 
	var w = parseInt($('realwidth').value);
	var h = parseInt($('realheight').value);
	
	if(w > 1024)
		w = 600;
	if (h > 768)
		h = 400;
	insert('[rm=' + w + ',' + h + ']\n' + $('realurl').value + '\n[/rm]'); 
	hideDialog();
}
function insertMedia()
{ 
	var w = parseInt($('mediawidth').value);
	var h = parseInt($('mediaheight').value);
	
	if(w > 1024)
		w = 600;
	if (h > 768)
		h = 400;
	insert('[mp=' + w + ',' + h + ']\n' + $('mediaurl').value + '\n[/mp]'); 
	hideDialog();
}


function hideDialog()
{
	var div = $('inputdialog');
	if (div != null)
		div.parentNode.removeChild(div);
}
function resize(span)
{
	var bh = parseInt(box.style.height);
	var wh = parseInt(win.style.height);
	if (span < 0 && bh <= 100) return;
	if (span > 0 && bh >= 800) return;
	box.style.height = span + bh + 'px'
	win.style.height = span + wh + 'px'
}

function getCurrentContent()
{
	if (mode == 'ubb')
		return gettext();
	if (mode == 'html')
		return html2ubb(gettext());
	return html2ubb(gethtml());
}
function setCurrentContent(content)
{
	if (mode == 'ubb')
		settext(content);
	else
		sethtml(ubb2html(content)); 
}

function stat()
{

	var content = getCurrentContent();
	var l = 5000 - content.length;
	if (l < 0) 
	{
		alert("已经达到限制字符数，系统将自动截断输入内容");
		setCurrentContent(content.substring(0, totals));
	}
	
	$('leftcontentsize').innerHTML = l;
	setTimeout(stat, 500);
}

