var gtxtEDInput = null;
var goptHtml = null;
var gEDPreview = null;
var gPostType = 0
var uimgPath = './inc/images/'
var gedRGBWho = 0;
var gedLastFace = null;

var divEDPopup = null;
var objEDFocus = null;

var gedRGBBuf = new Array();

var gedHtml = 0;
var gbNew = 0;

//
function GetAString(s){
	var c,n,i;
	c = '';
	n = s.length;
	for (i=0;i<n;i++)
	{
		if (s.charCodeAt(i) == 0) return c;
		c += s.charAt(i);
	}
	return c;
}

function GetInput(info,d){
	var s
	s = prompt(info,d);
	if (s)
		return GetAString(s);
	else
		return '';
}

function AddText(b,e){
	if (null == gtxtEDInput) return;
	gtxtEDInput.focus();
	if ((document.selection)&&(document.selection.type == "Text")) {
		var range = document.selection.createRange();
		var ch_text = range.text;
		range.text = b + ch_text + e;
		gtxtEDInput.focus();
	} else {
		gtxtEDInput.value = b + gtxtEDInput.value + e;
		gtxtEDInput.focus();
	}
}

function at1(t1){ 
	AddText('[' + t1 + ']','[/' + t1 + ']'); 
}
function at2(t1,t2){ 
	AddText('[' + t1 + '=' + t2 + ']','[/' + t1 + ']'); 
}

function at2a(t1,tt,td){ 
	if (null == gtxtEDInput) return;
	var v = GetInput(tt,td);
	if (v){
		if (v == 'http://') return;
		gtxtEDInput.value += '[' + t1 + ']' + v + '[/' + t1 + ']';
	}
}

// 设置内容
function edSetB()						{ at1('b'); }
function edSetI()						{ at1('i'); }
function edSetU()						{	at1('u'); }

function edSetFontSize(fs)	{ at2('size',fs); }
function edSetFontFace(ff)	{ at2('font',ff); }
function edSetFontColor(fc)	{ at2('color',fc); }
function edSetBGColor(bc)		{ at2('bg',bc); }

function edSetLeft()				{ at1('left'); }
function edSetCenter()			{ at1('center'); }
function edSetRight()				{ at1('right');	}

function edSetUrl()					{ at2a('url','请输入主页地址', 'http://'); }
function edSetEmail()				{ at2a('email','请输入邮件地址', ''); }
function edSetImage()				{ at2a('img','请输入图片地址', 'http://'); }
function edSetFlash()				{ at2a('flash','请输入FLASH地址', 'http://'); }
function edSetRm()					{ at2a('rm','请输入RealPlayer文件地址', 'http://'); }
function edSetMp()					{ at2a('mp','请输入Media Player文件地址', 'http://'); }

function edSetQuote()				{ at1('quote'); }
function edSetCode()				{ at1('code'); }

function edSetColor(obj,t){
	var c = obj.style.backgroundColor;
	if ('' == c) c = '#000000';
	if (1 == gedRGBWho)
		edSetFontColor(c);
	else
		edSetBGColor(c);
	hidePopup();
}

function edSetType(obj, t) {
	//edTxtTitle.style.backgroundImage = 'url(' + obj.src + ')';
	//obj.parentElement.style.backgroundImage = 'url(' + obj.src + ')';
	if (gedLastFace != null)
		gedLastFace.style.border = '2px solid #ffffff';
	obj.style.border = '2px outset #122D98';
	gedLastFace = obj;
	document.frmEditor.iNoteType.value = t;
	//alert(document.frmEditor.notetype.value);
	
	if ((t & 15) == 15) 
		edTdMore.style.display = 'block';
	else
		edTdMore.style.display = 'none';
}

function edSetEmot(obj,t)
{
	AddText('','[em' + t + ']'); 
}

// 函数部分
var base_hexa = "0123456789ABCDEF";
function dec2Hexa(number)
{
   return base_hexa.charAt(Math.floor(number / 16)) + base_hexa.charAt(number % 16);
}
function RGB2Hexa(r,g,b)
{
  return "#" + dec2Hexa(r) + dec2Hexa(g) + dec2Hexa(b);
}

function _fcOver(obj)	// 根据当前底色更改容器底色
{
	//obj.parentElement.style.backgroundColor = obj.style.backgroundColor;
	//edTxtTitle.value = event.offsetY;
	obj.parentElement.style.backgroundColor = gedRGBBuf[event.offsetY];
}

function edSubmit()
{
	if (gbNew){
		var nt = document.frmEditor.tNoteTitle;
		if (nt.value.length < 8){
			nt.focus();
			alert("文章标题不能为空或太短！");
			return false;
		}
	}
	gtxtEDInput.focus();
	var MessageLength = gtxtEDInput.value.length;
	if( MessageLength < 2 ){ alert("文章内容不能小于2个字符！");return false;}
	return confirm('您确定已写好并提交内容？');
}

function edCancel()
{
	gtxtEDInput.focus();
	if (confirm('您确定要撤销所有输入重写？'))
	{
		return true;
	}
	else
		return false;
}

function edPreview()
{
	var v = gtxtEDInput.value;
	var h = goptHtml.value;

	switch(parseInt(h)){
		case 0:
			gEDPreview.innerHTML = removeHtml(v);
			break;
		case 1:
			v = UBBCode(v,0);
			gEDPreview.innerHTML = v.replace(/\r\n/ig,'<br>');
			break;
		case 2:
			gEDPreview.innerHTML = v.replace(/\r\n/ig,'<br>');
			break;
		case 3:
			v = UBBCode(v,1);
			gEDPreview.innerHTML = v.replace(/\r\n/ig,'<br>');
			break;
		default : return;
	}
	if (gEDPreview.innerHTML == '') gEDPreview.innerHTML = '<b>当前无预览内容</b>';
	window.focus();
	gEDPreview.style.display = 'block';
}

function edShowPreview(chk)
{
	window.focus();
	if (chk.checked)
		edPreview();
	else
		gEDPreview.style.display = 'none';
	return;

	window.focus();
	if (gEDPreview.style.display == 'block')
		gEDPreview.style.display = 'none';
	else
		edPreview();
}

function edRefreshPreview()
{
	if (gEDPreview.style.display == 'block')
		edPreview();
}

function edShowEmot()
{
	var i,s;
	window.focus();
	if (edTdEmot.style.display == 'block')
		edTdEmot.style.display = 'none';
	else {
		edTdEmot.style.display = 'block';
	}
	if (edTdEmot.innerHTML != '') return;
	s = '';
	for (i=1;i<20;i++){
		s += '<img border="0" src="'+uimgPath+'emot/em'+i+'.gif" onclick="edSetEmot(this,'+i+')">';
	} 
	edTdEmot.innerHTML = s;
}



// 基本显示函数
function edIcon(gif,cmd,alt){
	return '<a class="icon" href="javascript:" onclick="' + cmd + ';return false;"><img border="0" src="' + uimgPath + 'ubb/' + gif + '.gif" alt="' + alt + '" title="'+alt+'"></a>';
}

function edFS(n)	// font size
{
	return '<a href="javascript:edSetFontSize('+n+');" onclick="return false;"><font size="' + n + '">' + n + '</font></a><br>'
}

function edFF(f)	// font face
{
	return '<a href="javascript:edSetFontFace(\''+f+'\');" onclick="return false;"><font face="' + f + '" title="'+f+'">' + f + '</font></a><br>'
}

function edFontSize(){
	var s = edFS(1) + edFS(2)	+ edFS(3) + edFS(4) + edFS(5) + edFS(6) + edFS(7);
	return s;
}

function edFontFace(){
	var ff = 'Arial,Arial Black,Verdana,Times New Roman,Garamond,Courier New,Webdings,Wingdings,'
		+ '方正舒体,方正姚体,仿宋_GB2312,黑体,华文彩云,华文细黑,华文新魏,华文行楷,华文中宋,楷体_GB2312,隶书,宋体,新宋体,幼圆';
	var i,n,v;
	v = ff.split(',');
	n = v.length;
	s = '';
	for (i=0;i<n;i++) s += edFF(v[i]);
	return s;
}

col = new Array;
col[0] = new Array(255,0,255,-1,255,-1);
col[1] = new Array(255,0,0,1,0,0);
col[2] = new Array(255,-1,255,0,0,0);
col[3] = new Array(0,0,255,0,0,1);
col[4] = new Array(0,0,255,-1,255,0);
col[5] = new Array(0,1,0,0,255,0);
col[6] = new Array(255,-1,0,0,255,-1);

function rgb(pas,w,h){
	var s = '';

	s = '<table cellSpacing="0" cellPadding="0" align="left" border="0">'
		+ '<tbody>'
		+ '<tr><td id=ColorUsed onclick="if(this.bgColor.length > 0) insertTag(this.bgColor)" vAlign=center align=middle>'
		+ '<img height=10 width=20 border=1></td></td>';
	
	for (j=0;j<6+1;j++){
		for (i=0;i<pas+1;i++){
			r = Math.floor(col[j][0]+col[j][1]*i*(255)/pas);
			g = Math.floor(col[j][2]+col[j][3]*i*(255)/pas);
			b = Math.floor(col[j][4]+col[j][5]*i*(255)/pas);
		  codehex = r + '' + g + '' + b;
		  s += '<td bgColor=\"' + RGB2Hexa(r,g,b) + '\"onmouseover="_fcOver(this)" onClick=\"insertTag(this.bgColor)\" width=\"'+w+'\" height=\"'+h+'\"><img height='+h+' width='+w+' border=0 title=\"字体颜色："></td>\n';
		}
	}
	s += '</tr></tbody></table>';
	return s;
}


function edRGB(pas){	// 颜色
	var s = '';
	var c,e;
	e = '" onmouseover="_fcOver(this)" onmousedown="edSetColor(this)"></div>';
	s = '<div class="show" onclick="edSetColor(this.parentElement)"></div>'
	for (j=0;j<6+1;j++){
		for (i=0;i<pas+1;i++){
			r = Math.floor(col[j][0]+col[j][1]*i*(255)/pas);
			g = Math.floor(col[j][2]+col[j][3]*i*(255)/pas);
			b = Math.floor(col[j][4]+col[j][5]*i*(255)/pas);
			c = RGB2Hexa(r,g,b);
			s += '<div style="background-color:' + c + e;
		}
	}
	return s;
}

function edSetRGB(pas){	// 颜色
	var i,j,r,g,b;
	for (j=0;j<6+1;j++){
		for (i=0;i<pas+1;i++){
			r = Math.floor(col[j][0]+col[j][1]*i*(255)/pas);
			g = Math.floor(col[j][2]+col[j][3]*i*(255)/pas);
			b = Math.floor(col[j][4]+col[j][5]*i*(255)/pas);
			gedRGBBuf.push(RGB2Hexa(r,g,b));
		}
	}
	gedRGBBuf.push('#000000');
	gedRGBBuf.push('#000000');
}

edSetRGB(33);
//gedRGBBuf = edRGB(33);
//gedRGBBuf = rgb(65,1,10);

function hidePopup()
{
	if (divEDPopup != null)	
		divEDPopup.removeNode(true);
	divEDPopup = null;
}

function autoHidePopup()
{
	if (divEDPopup == null) return;
	var b;
	if (objEDFocus.tagName == 'BODY')
		b = objEDFocus == document.activeElement;
	else
		b = objEDFocus.contains(document.activeElement);

	//b = objEDFocus.contains(document.activeElement);
	if (b)
		setTimeout("autoHidePopup()",250);
	else{
		hidePopup();
	}
}

function actionPopup(css,html)
{
	if (divEDPopup != null)	divEDPopup.removeNode(true);
	divEDPopup = document.createElement('DIV');
	divEDPopup.className = css;
	divEDPopup.innerHTML = html;
	document.body.insertBefore(divEDPopup,document.body.firstChild);
	
	var l = event.clientX+document.body.scrollLeft;
	var t = event.clientY+document.body.scrollTop;
	var l1 = document.body.clientWidth + document.body.scrollLeft - 10;
	var t1 = document.body.clientHeight + document.body.scrollTop - 10;
	if (l + divEDPopup.scrollWidth > l1) l = l1 - divEDPopup.scrollWidth;
	if (t + divEDPopup.scrollHeight > t1) t = t1 - divEDPopup.scrollHeight;
	with (divEDPopup.style){
		pixelLeft = l;
		pixelTop = t;
	}
	divEDPopup.focus();
	objEDFocus = document.activeElement;
	setTimeout("autoHidePopup()",500);
	//divEDPopup.onblur = hidePopup;
	//divEDPopup.onclick = hidePopup;
}

function showFace(obj)
{
	actionPopup('EDFontFace',edFontFace());
}

function showSize(obj)
{
	actionPopup('EDFontSize',edFontSize());
}

function showColor(obj, t)
{
	//actionPopup('EDFontColor',edRGB(65,t));
	var img;
	gedRGBWho = t;
	img = '<img border="0" src="'+uimgPath+'/note/color33.jpg" onmousemove="_fcOver(this)" onmousedown="edSetColor(this.parentElement)" />'
	actionPopup('EDFontColor','<div class="show" onclick="edSetColor(this.parentElement)"></div>'+img);
}

function SetEDIcon(html){
	if (0 == html)
	{
		dw('&nbsp;');
		return;
	}
	dw('<div class="EDIcon" oncontextmenu="return false" ondragstart="return false" onselectstart="return false">'
		,edIcon('bold','edSetB(this)','粗体')
		,edIcon('italicize','edSetI(this)','斜体')
		,edIcon('underline','edSetU(this)','下划线')
//		,edIcon('fontsize','showSize(this)','字体大小')
		,edIcon('fontface','showFace(this)','字体')
		,edIcon('fontcolor','showColor(this,1)','字体颜色')
		,edIcon('fontbg','showColor(this,2)','字体底色')
		,edIcon('jleft','edSetLeft(this)','居左')
		,edIcon('jcenter','edSetCenter(this)','居中')
		,edIcon('jright','edSetRight(this)','居右')
		,edIcon('url','edSetUrl(this)','插入超链接')
		,edIcon('email','edSetEmail(this)','插入电子邮件地址')
		,edIcon('image','edSetImage(this)','插入图片')
		,edIcon('flash','edSetFlash(this)','插入FLASH文件')
		,edIcon('rm','edSetRm(this)','插入RealPlayer文件')
		,edIcon('mp','edSetMp(this)','插入Media Player文件')
		,edIcon('quote','edSetQuote(this)','引用')
		,edIcon('code','edSetCode(this)','代码')
		,'</div>'
	);
}

function _oneType(id,gif,hint)
{
	dw('<img border="0" src="',uimgPath,'note/',gif,'" title="',hint,'">')
}

function SetEDType(){
	var ff = '0,无类型,1,纯粹灌水,2,转载文章或新闻,3,求助贴,4,二手转卖或赠送,5,心得与经验,6,活动召集,14,广告贴,15,资料收集';
	var i,n,v,k;
	v = ff.split(',');
	n = v.length / 2;

	dw(	'<table width="100%"><tr><td width="60" align="right">',
		'类型：</td><td>',
		'<input type="hidden" name="iNoteType" value="0" id="edHideNoteType">'
		);

	for (i=0;i<n;i++){
		k = - v[i*2] * 25;
		k = 'background-position: ' + k + 'px 0px;';
		dw('<div style="',k,'" title="', v[i*2+1] ,'" onclick="edSetType(this,',v[i*2],')" id="edNoteType',v[i*2],'"></div>');
	}
	dw('</td></tr></table>');
}

function SetEDCmds(html){
	var s;
	if (html > 1){
		s = '<option value="2">启用HTML</option><option value="3">HTML+UBB</option>';
	}
	else
		s = ''; 
	dw( '<input type="submit" value=" 发 表 ">'
		, '<input type="reset"  value=" 重 写 " onclick="return edCancel();">'
		);
	if (html == 0) return;
	dw( '<input type="button" value=" 图 标 " onclick="edShowEmot();">　'
		, '<select name="iHTML">'
		, '<option value="1">UBB转换</option>'
		, '<option value="0">原样显示</option>'
		, s
		, '</select>'
		, '<input type="checkbox" onclick="edShowPreview(this);">'
		, '<input type="button" value=" 预 览 " onclick="edRefreshPreview();">'
	);
}


function edSetTopicA(chk,n,t){
	if (n & t)
		chk.style.backgroundPosition = '0px -24px';
	else
		chk.style.backgroundPosition = '0px 0px';
}

function edSetTopic(obj,t){
	var nt = document.frmEditor.tNoteTopic;
	var n = nt.value;
	n = parseInt(n);
	if (obj != null){
		if (n & t)
		{
			n = (n | t) - t;
			obj.style.backgroundPosition = '0px 0px';
		} else {
			n |= t;
			obj.style.backgroundPosition = '0px -24px';
		}
	}
	nt.value = n;

	edSetTopicA(edT1,n,1);	
	edSetTopicA(edT2,n,2);	
	edSetTopicA(edT4,n,4);	
	edSetTopicA(edT8,n,8);	
	edSetTopicA(edT100,n,0x100);	
	edSetTopicA(edT200,n,0x200);	
	edSetTopicA(edT400,n,0x400);	
	edSetTopicA(edT800,n,0x800);	
	edSetTopicA(edT1000,n,0x1000);	
	edSetTopicA(edT2000,n,0x2000);	

	edSetTopicA(edChk0,n,0x00f0);	
	edSetTopicA(edChk1,n,0x0010);	
	edSetTopicA(edChk2,n,0x0020);	
	edSetTopicA(edChk3,n,0x0040);	
	edSetTopicA(edChk4,n,0x0080);	
}

function SetEDMore(){
	dw(
		'<table width="100%" class="More"><tr><td width="60" align="right">所属：</td><td oncontextmenu="return false" ondragstart="return false" onselectstart="return false">',
		'<input type="hidden" name="tNoteTopic" id="edHideNoteTopic" value="0">',
		'<div class="CheckBox" onclick="edSetTopic(this,1)" id="edT1">孕前</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,2)" id="edT2">孕期</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,4)" id="edT4">婴幼儿</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,8)" id="edT8">学前期</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x00f0)" id="edChk0">学龄期</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x0100)" id="edT100">教育相关</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x0200)" id="edT200">营养健康</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x0400)" id="edT400">意外处理</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x0800)" id="edT800">小常识</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x1000)" id="edT1000">常见疾病</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x2000)" id="edT2000">其他疾病</div>',
		'</td></tr><tr><td align="right">学龄期：</td><td oncontextmenu="return false" ondragstart="return false" onselectstart="return false">',
		//'<br clear="all" />',
		'<div class="CheckBox" onclick="edSetTopic(this,0x0010)" id="edChk1">幼儿园</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x0020)" id="edChk2">小学</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x0040)" id="edChk3">初中</div>',
		'<div class="CheckBox" onclick="edSetTopic(this,0x0080)" id="edChk4">高中</div>',
		'</td></tr>',
		'</td></tr><tr><td align="right">链接：</td><td>',
		'<input type="text" name="tNoteUrl" maxlength="127" class="Title">',
		'</td></tr><tr><td align="right">原作者：</td><td>',
		'<input type="text" name="tNoteFrom" maxlength="127" class="Title">',
		'</td></tr>',
		'</table>'
	);
}

function EditorForm(bNew, url, html){
	var s;
	gedHtml = html;
	gbNew = bNew;

	if (bNew)
		s = '<div id="edTitle">发表新文章</div>';
	else
		s = '<div id="edTitle">回复贴子</div>';                                          
	dw(
		'<form name="frmEditor" method="post" action="',url,'" onsubmit="return edSubmit()" target="_self">',
		'<input type="hidden" name="editerPost" value="1">',
		'<table cellspacing="0" cellpadding="0" class="Editor"><tr><td class="Title">',
		s,
		'</td></tr>'
	);
	if (bNew){
		dw('<tr><td>',
			'<table width="100%"><tr><td width="60" align="right">',
			'标题：</td><td><input type="text" name="tNoteTitle" maxlength="127" class="Title">',
			'</td></tr></table>',
			'</td></tr>',
			'<tr><td class="Type">');
		SetEDType();
		dw(
			'</td></tr>',
			'<tr><td class="More" id="edTdMore">'
			);
		SetEDMore();
		dw('</td></tr>');
	}
	dw('<tr><td class="Icon">');
	SetEDIcon(html);
	dw(
		'<tr><td class="Data">',
		'<textarea name="tNoteData" rows="10" cols="20" id="edTxtInput"></textarea>',
		'</td></tr>',
		'<tr><td class="Cmds">');
	SetEDCmds(html);
	dw(
		'</td></tr>',
		'<tr><td class="Emot" id="edTdEmot">',
		'</td></tr>',
		'<tr><td class="Preview" id="edTdPreview">',
		'</td></tr>',
		'</form></table>'
	);
	gtxtEDInput = document.frmEditor.tNoteData;
	if (html) goptHtml = document.frmEditor.iHTML;

	gEDPreview = eval('edTdPreview');
}

function EditorSetData(nt,nty,nto,nu,nf,nd,ih)
{
	if (nt == '')
		edTitle.innerText = '修改回复';
	else{
		edTitle.innerText = '修改主题贴';
		document.frmEditor.tNoteTitle.value = nt;
	}
	var obj = eval('edNoteType'	+ nty);
	edSetType(obj,nty);
	document.frmEditor.tNoteTopic.value = nto;
	edSetTopic(null,nto);
	if (nu != '') document.frmEditor.tNoteUrl.value = nu;
	if (nf != '') document.frmEditor.tNoteFrom.value = nf;
	gtxtEDInput.value = nd;
	document.frmEditor.iHTML.value = ih;
}


