// JavaScript Document

var txtLeft,txtRight,reVal;
function ubbApply(type,thisForm){
	with(thisForm){
	switch(type){
		case 1 :
			insertAtCaret(ubbcode,joinUbb(txtLeft,txtRight));
			//ubbcode.value=txtLeft+ubbcode.value+txtRight;
			ubbcode.focus();
			break;
		case 2 :
			insertAtCaret(ubbcode,txtLeft + reVal + txtRight);
			//ubbcode.value += txtLeft + reVal + txtRight;
			ubbcode.focus();
			break;
		case 3 :
			insertAtCaret(ubbcode,reVal);
			ubbcode.focus();
			break;
	}
	}
}

function addImg(thisForm) {
	reVal = prompt("请输入图片链接地址", "http://");
//	alert(reVal);
	if(reVal!=null&&reVal!="http://"){
		txtLeft="[IMG]";
		txtRight="[/IMG]";
		ubbApply(2,thisForm);
	}
}

function addBold(thisForm){
	txtLeft="[B]";
	txtRight="[/B]";
	ubbApply(1,thisForm);
}

function addColor(thisForm){
	var reVal = showModalDialog(mainpath+"/system/editor/palette.html", "", "dialogWidth:350px; dialogHeight:280px;status:0;help:0;scroll:0");
	if(reVal!=null){
		txtLeft="[COLOR="+reVal+"]";
		txtRight="[/COLOR]";
		ubbApply(1,thisForm);
	}
}
function addUrl(thisForm){
	var reVal= prompt("请输入连接网址","http://");
	if(reVal!=null&&reVal!="http://"){
		txtLeft="[URL="+reVal+"]";
		txtRight="[/URL]";
		ubbApply(1,thisForm);
	}
}

function addMail(thisForm){
	reVal= prompt("请输入电子邮件地址","");
	if(reVal!=null&&reVal!=""){
		txtLeft="[MAIL]";
		txtRight="[/MAIL]";
		ubbApply(2,thisForm);
	}
}
function addFace(inVal,thisForm){
	reVal="[FC"+inVal+"]";
	ubbApply(3,thisForm);
	smileBox.style.display="none";
}
function addMovie(thisForm){
	reVal= prompt("请输入Media/Real影片网址","http://");
	if(reVal!=null&&reVal!="http://"){
		txtLeft="[MOVIE]";
		txtRight="[/MOVIE]";
		ubbApply(2,thisForm);
	}
}

function addSwf(thisForm){
	reVal= prompt("请输入Flash动画网址","http://");
	if(reVal!=null&&reVal!="http://"){
		txtLeft="[FLASH]";
		txtRight="[/FLASH]";
		ubbApply(2,thisForm);
	}
}
function toUbb(str) {
	str = str.replace(/\r/g, "");
	str = str.replace(/on(load|click|dbclick|mouseover|mousedown|mouseup)="[^"]+"/ig, "");
	str = str.replace(/<"+"script[^>]*?>([\w\W]*?)<\/script>/ig, "");
	str = str.replace(/(<a[\s]*.*href=["|']?)([^"|^'|\s]*)([^>]*>)(.*)(<[\s]*\/[^>]*>)/ig,"\n[URL=$2]$4[/URL]\n");
	str = str.replace(/(<font[\s]*.*color=["|']?)([^"|^'|\s]*)([^>]*>)(.*)(<[\s]*\/[^>]*>)/ig,"\n[COLOR=$2]$4[/COLOR]\n");
	str = str.replace(/<param name=["|']?movie["|']? value=["|']?([^"|']+)["|']?[^>]*>/ig, "\n[FLASH]$1[/FLASH]\n");
	str = str.replace(/<param name=["|']?filename["|']? value=["|']?([^"|']+)["|']?[^>]*>/ig, "\n[MOVIE]$1[/MOVIE]\n");
	str = str.replace(/<img[\s]*.*src[\s]*=[\s]*["|']?([^"|^'|\s]*)[^>]*>/ig, "\n[IMG]$1[/IMG]\n");
	str = str.replace(/<(\/?)\s*(b)>/ig, "[$1B]");
	str = str.replace(/&nbsp;/g, " ");
	str = str.replace(/&amp;/g, "&");
	str = str.replace(/&quot;/g, "\"");
	str = str.replace(/&lt;/g, "<");
	str = str.replace(/&gt;/g, ">");
	
	str = str.replace(/<br>/ig, "\n");
	str = str.replace(/<[^>]*?>/g, "");
	str = str.replace(/\[url=([^\]]+)\]\n(\[img\]\1\[\/img\])\n\[\/url\]/g,"$2");
	str = str.replace(/\n+/g, "\n");
	return str;
}
function toHtml(str){
	str= str.replace(/&/g,"&amp;");
	str= str.replace(/ /g,"&nbsp;");
	str= str.replace(/"/g,"&quot;");
	str= str.replace(/</g,"&lt;");
	str= str.replace(/>/g,"&gt;");
	str= str.replace(/\n|\r/g,"<br>");
	str= str.replace(/\[B\](.*?)\[\/B\]/ig,"<b>$1</b>");
	str= str.replace(/\[IMG=(.*?)\](.*?)\[\/IMG\]/ig,"<img style=\"$1\" src=\"$2\" border=\"0\" \/>");
	str= str.replace(/\[IMG\](.*?)\[\/IMG\]/ig,"<img src=\"$1\" border=\"0\" \/>");
	
	//======================================================================================================
	str= str.replace(/\[FONT=(.*?)\](.*?)\[\/FONT\]/ig,"<font style=\"$1\">$2</font>");
	str= str.replace(/\[MARQUEE\](.*?)\[\/MARQUEE\]/ig,"<marquee behavior=\"alternate\" scrollamount=\"3\" onmouseover=\"this.stop();\" onmouseout=\"this.start();\">$1</marquee>");
	//======================================================================================================
	str= str.replace(/\[URL=(.*?)\](.*?)\[\/URL\]/ig,"<a href=\"$1\" class=\"Link\" target=\"_blank\">$2</a>");
	str= str.replace(/\[COLOR=(.*?)\](.*?)\[\/COLOR\]/ig,"<font color=\"$1\">$2</font>");
	str= str.replace(/\[MAIL\](.*?)\[\/MAIL\]/ig,"<a href=\"mailto:$1\">$1</a>");
	str= str.replace(/\[FC([^\]]*?)\]/ig,"<img src=\""+mainpath+"/system/ubb/img/face/$1.gif\" border=\"0\" \/>");
	//=======================================================================================================
	str= str.replace(/\[FLASH=(.*?)\](.*?)\[\/FLASH\]/ig,"<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"768\" height=\"120\" style=\"$1\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\"><param name=\"movie\" value=\"$2\" /><param name=\"quality\" value=\"high\" /><embed src=\"$2\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\"></embed></object>");
	str= str.replace(/\[FLASH\](.*?)\[\/FLASH\]/ig,"<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\"768\" height=\"120\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\"><param name=\"movie\" value=\"$1\" /><param name=\"quality\" value=\"high\" /><embed src=\"$1\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\"></embed></object>");
	//=======================================================================================================
	str= str.replace(/\[MOVIE=(.*?)\](.*?)\[\/MOVIE\]/ig,"<object classid=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" style=\"$1\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715\" align=\"baseline\" border=\"0\" standby=\"Loading Microsoft Windows Media Player components...\" type=\"application/x-oleobject\">\n<param name=\"FileName\" value=\"$2\" />\n<param name=\"ShowPositionControls\" value=\"0\" />\n</object>\n");
	str= str.replace(/\[MOVIE\](.*?)\[\/MOVIE\]/ig,"<object classid=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715\" align=\"baseline\" border=\"0\" standby=\"Loading Microsoft Windows Media Player components...\" type=\"application/x-oleobject\">\n<param name=\"FileName\" value=\"$1\" />\n<param name=\"ShowPositionControls\" value=\"0\" />\n</object>\n");
	//=======================================================================================================
	str= str.replace(/\[QUOTE\](.*?)\[\/QUOTE\]/ig,"<div class=\"re_quote\">$1</div>\n");
	str= str.replace(/\[ANN=(.*?)\](.*?)\[\/ANN\]/ig,"<a href=\"$1\" class=\"AnnexLink\" target=\"_blank\">$2</a>");
	str= str.replace(/\[CODE\](.*?)\[\/CODE\]/ig,"<div class=\"ubbcode\">$1</div>");
    return str;
}

function showSmile(obj){
	if(smileBox.innerHTML==""){
		var boxX=parseInt(getLeft(obj))+20;
		var boxY=parseInt(getTop(obj))+20;
		smileBox.innerHTML="<div style='position:absolute;z-index:999; left:"+boxX+"; top:"+boxY+";'>"+facecode+"</div>";
	}else{
		if(smileBox.style.display=="none"){smileBox.style.display="";}
		else{smileBox.style.display="none";}
	}
}

//=================Code Begin======================
//此处为笑咪咪添加		2006-8-11
function joinUbb(left,right)
{
	if(typeof(document.selection.createRange().text)!="undefined")
	{
		return left+document.selection.createRange().text+right;
	}
	else
	{
		return left+right;	
	}
}

function setCaret(AreaObj) 
{ 
	if (AreaObj.createTextRange)
		AreaObj.caretPos = document.selection.createRange().duplicate(); 
} 

function insertAtCaret (AreaObj, text) 
{ 
	if (AreaObj.createTextRange && AreaObj.caretPos) 
	{
		var caretPos = AreaObj.caretPos; 
		caretPos.text =caretPos.text.charAt(caretPos.text.length - 1)==''?text+'':text; 
	}
	else
	{
		AreaObj.value = text;
	}
}
//=================Code End======================