var sRTE;
var FacePath = siteUrl + "/images/edit/face/";
var docStyle = "";
function word(sNodeBox, sHTML, sMode){
	var oHead = new Object;
	var oBody = new Object;
	var oArea = document.createElement("textarea");
	var posArr = [
	{w: 3, x: -2},
	{w: 20, x: -5, t: "撤銷", func: "Undo", id: "a1"},
	{w: 20, x: -25, t: "重做", func: "Redo", id: "a2"},
	{w: 2, x: 0, id: "a3"},
	{w: 20, x: -45, t: "剪切", func: "Cut", id: "b1"},
	{w: 20, x: -65, t: "複製", func: "Copy", id: "b2"},
	{w: 20, x: -85, t: "粘貼", func: "Paste", id: "b3"},
	{w: 2, x: 0, id: "b4"},
	{w: 20, x: -225, t: "粗體", func: "Bold", id: "c1"},
	{w: 20, x: -245, t: "斜體", func: "Italic", id: "c2"},
	{w: 20, x: -265, t: "下劃線", func: "Underline", id: "c3"},
	{w: 2, x: 0, id: "c4"},
	{w: 20, x: -285, t: "左對齊", func: "justifyleft", id: "d1"},
	{w: 20, x: -305, t: "居中", func: "justifycenter", id: "d2"},
	{w: 20, x: -325, t: "右對齊", func: "justifyright", id: "d3"},
	{w: 20, x: -345, t: "兩端對齊", func: "justifyfull", id: "d4"},
	{w: 2, x: 0, id: "d5"},
	{w: 20, x: -145, t: "添加圖片", func: "img", id: "e1"},
	{w: 20, x: -759, t: "插入Flash", func: "flash", id: "e6"},
	{w: 20, x: -779, t: "插入視頻", func: "video", id: "e7"},
	{w: 20, x: -105, t: "插入鏈接", func: "link", id: "e2"},
	{w: 20, x: -125, t: "插入表格", func: "table", id: "e3"},
	{w: 20, x: -465, t: "插入UBB表情", func: "face", id: "e4"},
	{w: 20, x: -485, t: "插入文字框", func: "textarea", id: "e5"},
	{w: 20, x: -799, t: "設置TAG", func: "cclink", id: "e8"},
	{w: 2, x: 0, id: "e4"},
	{w: 20, x: -165, t: "橫線", func: "inserthorizontalrule", id: "f1"},
	{w: 20, x: -185, t: "文本顏色", func: "forecolor", id: "f2"},
	{w: 20, x: -205, t: "背景色", func: "hilitecolor", id: "f3"},
	{w: 2, x: 0, id: "f4"},
	{w: 40, x: -505, t: "段落樣式", func: "formatblock", id: "g1"},
	{w: 40, x: -545, t: "字體", func: "fontname", id: "g2"},
	{w: 60, x: -585, t: "字體大小", func: "fontsize", id: "g3"},
	{w: 2, x: 0, id: "g4"},
	{w: 20, x: -365, t: "編號列表", func: "insertorderedlist", id: "h1"},
	{w: 20, x: -385, t: "項目符號列表", func: "insertunorderedlist", id: "h2"},
	{w: 20, x: -405, t: "減少縮進", func: "outdent", id: "h3"},
	{w: 20, x: -425, t: "增加縮進", func: "indent", id: "h4"},
	{w: 2, x: 0, id: "h5"},
	{w: 20, x: -445, t: "功能", func: "editmenu"}
	];
	oHead["table"] = document.createElement("table");
	oHead["table"].cellSpacing = "0px";
	oHead["table"].cellPadding = "0px";
	oHead["table"].border = "0px";
	oHead["table"].id = sNodeBox + "Table";
	oHead["table"].className = "editerToolsBox";
	oHead["tbody"] = document.createElement("tbody");
	oHead["tr"] = document.createElement("tr");
	oHead["td"] = document.createElement("td");
	oHead["td"].className = "editerToolsBG";
	oHead["td"].id = "editerToolsBG";
	for(var i = 0; i < posArr.length; i ++) {
		oHead["items"] = document.createElement("img");
		posArr[i]["id"] ? oHead["items"].id = posArr[i]["id"] : function(){};
		oHead["items"].src = siteUrl + "/images/edit/blank.gif";
		oHead["items"].style.margin = "1px";
		oHead["items"].className = "editerToolsIMG";
		oHead["items"].style.width = posArr[i]["w"] + "px";
		oHead["items"].style.backgroundPosition = posArr[i]["x"] + "px 0px";
		if(posArr[i]["func"] != null) {
			oHead["items"].title = posArr[i]["t"];
			oHead["items"]["func"] = posArr[i]["func"];
			oHead["items"].onmouseover = function () {
				var bp = this.style.backgroundPosition;
				this.style.backgroundPosition = bp.split(" ")[0] + " -20px";
			}
			oHead["items"].onmouseout = function () {
				var bp = this.style.backgroundPosition;
				this.style.backgroundPosition = bp.split(" ")[0] + " 0px";
			}
			oHead["items"].onclick = function () {
				wordChiCommand(this["func"]);
			}
		}
		oHead["td"].appendChild(oHead["items"]);
	}
	oHead["tr"].appendChild(oHead["td"]);
	oHead["tbody"].appendChild(oHead["tr"]);
	oHead["table"].appendChild(oHead["tbody"]);
	document.getElementById(sNodeBox).appendChild(oHead["table"]);

	oBody["table"] = document.createElement("table");
	oBody["table"].cellSpacing = "0px";
	oBody["table"].cellPadding = "0px";
	oBody["table"].border = "0px";
	oBody["table"].className = "editerTextBox";
	oBody["table"].id = "editerTextBox";
	oBody["tbody"] = document.createElement("tbody");
	oBody["tr"] = document.createElement("tr");
	oBody["td"] = document.createElement("td");
	oBody["td"].className = "editerTextTopBG";
	oBody["td"].id = "editerTextTopBG";
	oBody["tr"].appendChild(oBody["td"]);
	
	oBody["tbody"].appendChild(oBody["tr"]);
	oBody["tr"] = document.createElement("tr");
	oBody["td"] = document.createElement("td");
	oBody["td"].className = "editerTextBG";
	oBody["td"].id = sNodeBox + "Iframes";
	oBody["tr"].appendChild(oBody["td"]);
	oBody["tbody"].appendChild(oBody["tr"]);

	oBody["tr"] = document.createElement("tr");
	oBody["td"] = document.createElement("td");
	oBody["td"].className = "editerTextBottomBG";
	oBody["input"] = document.createElement("input");
	oBody["input"].type = "checkbox";
	oBody["input"].id = "EditerCMD";
	
	function oInputFunc(){
		var oRTE = getFrameNode(sRTE);
		oRTE.focus();
		var sEditBox = document.getElementById(sNodeBox + "Table");
		var sEditMode = sEditBox.style.display == "none" ? true : false;
		if(sEditMode){
			sEditBox.style.display = "";
			docStyle = oRTE.document.getElementsByTagName("style");
			docStyle[0].disabled = "";
			if(! window.Event){
				var sOutText = escape(oRTE.document.body.innerText);
				sOutText = sOutText.replace("%3CP%3E%0D%0A%3CHR%3E", "%3CHR%3E");
				sOutText = sOutText.replace("%3CHR%3E%0D%0A%3C/P%3E", "%3CHR%3E");
				oRTE.document.body.innerHTML = unescape(sOutText);
			}else{
				var oMozText = oRTE.document.body.ownerDocument.createRange();
				oMozText.selectNodeContents(oRTE.document.body);
				oRTE.document.body.innerHTML = oMozText.toString();
			}
		}else{
			sEditBox.style.display = "none";
			var innerHTML = oRTE.document.body.innerHTML;
			//innerHTML = cleanHTML(innerHTML);	
			//innerHTML = disableNodes(innerHTML);
			docStyle = oRTE.document.getElementsByTagName("style");
			docStyle[0].disabled = "true";
			if(!window.Event){
				oRTE.document.body.innerText = innerHTML;
			}else{
				var oMozText = oRTE.document.createTextNode(innerHTML);
				oRTE.document.body.innerHTML = "";
				oRTE.document.body.appendChild(oMozText);
			}
		}
	}
	function oFullFunc() {
		var fEditMode = document.getElementById("fullEditer").checked;
		var dochtml = document.getElementsByTagName('html')[0];
		if(fEditMode) {
			var dHeight = document.documentElement.clientHeight;
			var dwidth = document.documentElement.clientWidth;
			document.getElementById("fulledit").className = "fullmessage";
			document.body.className = "fullbody";
			dochtml.style.overflow='hidden';
			document.getElementById(sNodeBox + "Table").style.width = document.getElementById(sNodeBox + "Iframes").style.width = document.getElementById(sNodeBox + "_area").style.width = dwidth +"px";
			document.getElementById(sNodeBox + "Iframes").style.height = document.getElementById(sNodeBox + "_area").style.height = dHeight-55 +"px";
		} else {
			document.body.style.cssText = "";
			document.getElementById("fulledit").className = "";
			document.body.className = "";
			dochtml.style.overflow='';
			document.getElementById(sNodeBox + "Iframes").style.height = document.getElementById(sNodeBox + "_area").style.height = "";
			document.getElementById(sNodeBox + "Table").style.width = document.getElementById(sNodeBox + "Iframes").style.width = document.getElementById(sNodeBox + "_area").style.width = document.getElementById("editerToolsBG").style.width = "";
		}
	}

	function CleanCode() {
		var oRTE = getFrameNode(sRTE);
		oRTE.focus();
		if (! window.Event){
			var body = oRTE.document.body;
			for (var index = 0; index < body.all.length; index++) {
				tag = body.all[index];
				tag.removeAttribute("className","",0);
				tag.removeAttribute("style","",0);
			}
			var html = oRTE.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>");
			oRTE.document.body.innerHTML = html;
		}else{
			var oMozText = oRTE.document.body.ownerDocument.createRange();
			oMozText.selectNodeContents(oRTE.document.body);
			oRTE.document.body.innerHTML = oMozText.toString();

		}
	}

	this.renewcontent = function() {
		if(window.confirm('您確定要恢復上次保存?')) {
			var sRTEObj = getFrameNode(sRTE).document.body;
			try {
				var r = sRTEObj.createTextRange();
				r.execCommand("Paste");
			} catch (e) {}
		}
	}
	window.onresize = oFullFunc;
	oBody["input"].onclick = oInputFunc;
	oBody["label"] = document.createElement("label");
	oBody["label"].style.fontSize = '12px';
	oBody["label"].htmlFor = "EditerCMD"
	oBody["label"].innerHTML = "顯示源代碼&nbsp;&nbsp;";
	
	oBody["fullEditer"] = document.createElement("input");
	oBody["fullEditer"].type = "checkbox";
	oBody["fullEditer"].id = "fullEditer";
	oBody["fullEditer"].onclick = oFullFunc;

	oBody["fulllabel"] = document.createElement("label");
	oBody["fulllabel"].style.fontSize = '12px';
	oBody["fulllabel"].htmlFor = "fullEditer"
	oBody["fulllabel"].innerHTML = "全屏&nbsp;&nbsp;";

	oBody["cleanCode"] = document.createElement("label");
	oBody["cleanCode"].style.fontSize = '12px';
	oBody["cleanCode"].onclick = CleanCode;
	oBody["cleanCode"].innerHTML = "清除格式&nbsp;&nbsp;";

	oBody["renew"] = document.createElement("label");
	oBody["renew"].style.fontSize = '12px';
	oBody["renew"].onclick = this.renewcontent;
	oBody["renew"].innerHTML = "恢復內容";

	oBody["explain"] = document.createElement("label");
	oBody["explain"].style.fontSize = '12px';
	oBody["explain"].innerHTML = "&nbsp;&nbsp;(換行請用 Shift+Enter 鍵)";
	
	oBody["td"].appendChild(oBody["input"]);
	oBody["td"].appendChild(oBody["label"]);
	oBody["td"].appendChild(oBody["fullEditer"]);
	oBody["td"].appendChild(oBody["fulllabel"]);
	oBody["td"].appendChild(oBody["cleanCode"]);
	oBody["td"].appendChild(oBody["renew"]);
	oBody["td"].appendChild(oBody["explain"]);
	
	oBody["tr"].appendChild(oBody["td"]);
	oBody["tbody"].appendChild(oBody["tr"]);

	oBody["table"].appendChild(oBody["tbody"]);
	
	document.getElementById(sNodeBox).appendChild(oBody["table"]);
	sRTE = sNodeBox + "_area";
	document.getElementById(sNodeBox + "Iframes").innerHTML = "<iframe frameborder='0' scrolling='yes' id=" + sRTE + " class='editerTextArea'></iframe>";

	oArea.name = sNodeBox;
	oArea.id = sNodeBox + "_textarea";
	oArea.style.display = "none";
	oArea.value = sHTML;
	document.getElementById(sNodeBox).appendChild(oArea);
	this.copycontent = function() {
		var sRTEObj = getFrameNode(sRTE).document.body;
		var sHTML = sRTEObj.innerHTML;
		var textareavalue = sHTML;
		if (textareavalue.length > 0) {
			try {
				var r = sRTEObj.createTextRange();
				r.execCommand("Copy");
			} catch (e) {}
		}
	}
	this.save = function(){
		this.copycontent;
		if(oBody["input"].checked == true) {
			oInputFunc();
			oBody["input"].checked = false;
		}
		var oRTE = getFrameNode(sRTE);
		oRTE.focus();
		var sEditBox = document.getElementById(sNodeBox + "Table");
		var sEditMode = sEditBox.style.display == "none" ? true : false;
		if(sEditMode){
			if(!indow.Event){
				oArea.value = oRTE.document.body.innerText;
			}else{
				var oMozText = oRTE.document.body.ownerDocument.createRange();
				oMozText.selectNodeContents(oRTE.document.body);
				oArea.value = oMozText.toString();
			}
		}else{
			oArea.value = oRTE.document.body.innerHTML;
		}
	}
	
	window.onunload = this.copycontent;
	function writeDesignMode(sNodeBox, sHTML, sMode) {
		enableDesignMode(sNodeBox, "on");
		var sFix = window.Event ? "<div></div>" : "";
		if(sMode == 1) {
			var frameHTML = sHTML + "\n\
			" + sFix;
		} else {
			var frameHTML = "\
			<html>\n\
			<head>\n\
			<style>\n\
			body {\n\
				background: #ffffff;\n\
				margin:0px;\n\
				padding:0px;\n\
				font-size:12px;\n\
				overflow:auto;\n\
				scrollbar-face-color:#fff;\n\
				scrollbar-highlight-color:#c1c1bb;\n\
				scrollbar-shadow-color:#c1c1bb;\n\
				scrollbar-3dlight-color:#ebebe4;\n\
				scrollbar-arrow-color:#cacab7;\n\
				scrollbar-track-color:#f4f4f0;\n\
				scrollbar-darkshadow-color:#ebebe4;\n\
				word-wrap: break-word;\n\
				font-family: 'Courier New', 'Tahoma', 'Verdana', 'Courier', '細明體';\n\
			}\n\
			</style>\n\
			</head>\n\
			<body>\n\
			" + sHTML + "\n\
			" + sFix + "\n\
			</body>\n\
			</html>";
		}
	
		var oRTE = getFrameNode(sNodeBox).document;
		oRTE.open();
		oRTE.write(frameHTML);
		oRTE.close();
	}
	
	writeDesignMode(sRTE, sHTML, sMode);
	saveWordStatus();
}

function getFrameNode(sNode){
	return document.frames ? document.frames[sNode] : document.getElementById(sNode).contentWindow;
}
	
function enableDesignMode(sNodeBox, sMode){
	document.frames ? document.frames[sNodeBox].document.designMode = sMode : document.getElementById(sNodeBox).contentDocument.designMode = sMode;
}

function wordChiCommand(_sCmd) {
	var oRTE = document.frames ? document.frames[sRTE] : document.getElementById(sRTE).contentWindow;
	function oPenWin(_sTitle, _sWidth, _sHeight, _sUrl, _bDialog, _open){
		xposition=0; yposition=0;
		if ((parseInt(navigator.appVersion) >= 4 )) {
			xposition = (screen.width - _sWidth) / 2;
			yposition = (screen.height - _sHeight) / 2;
		}
		if(_open) {
			window.open(_sUrl,"win","menubar=no,location=no,resizable=no,scrollbars=no,status=no,left="+xposition+",top="+yposition+",width="+_sWidth+",height="+_sHeight);
		} else {
			if(window.Event) {
				window.open(_sUrl,"win","menubar=no,location=no,resizable=no,scrollbars=no,status=no,left="+xposition+",top="+yposition+",innerWidth="+_sWidth+",innerHeight="+_sHeight);
			} else {
				if(_bDialog == true) {
					showModelessDialog(_sUrl, window, "dialogHeight:"+(_sHeight+20)+"px;dialogWidth:"+_sWidth+"px;status:no;help:no;resizable:yes;status:no;tustatus:no;");
				} else {
					showModalDialog(_sUrl, window, "dialogHeight:"+(_sHeight+20)+"px;dialogWidth:"+_sWidth+"px;status:no;help:no;resizable:yes;status:no;tustatus:no;");
				}
			}
		}
	}
	switch(_sCmd){
		
		case "":
			break;
		case "face":
			oPenWin("插入表情圖標", 280, 170, siteUrl + "/images/edit/Face.php?siteurl=" + escape(siteUrl));
			break;
		case "link":
			oPenWin("請輸入網頁地址", 300, 140, siteUrl + "/images/edit/InsertLink.html");
			break;
		case "table":
			oPenWin("請選擇表格屬性", 300, 240, siteUrl + "/images/edit/InsertTable.html");
			break;
		case "img":
			oPenWin("請輸入圖片地址", 380, 400, siteUrl + "/batch.insertimage.php", 1, 1);
			break;
		case "flash":
			oPenWin("請輸入圖片地址", 300, 160, siteUrl + "/images/edit/InsertFlash.html");
			break;
		case "video":
			oPenWin("請輸入圖片地址", 300, 160, siteUrl + "/images/edit/InsertVideo.html");
			break;
		case "forecolor":
			oPenWin("請選擇文本顏色", 140, 162, siteUrl + "/images/edit/ForeColor.html", true);
			break;
		case "hilitecolor":
			oPenWin("請選擇背景顏色", 140, 162, siteUrl + "/images/edit/BackColor.html", true);
			break;
		case "formatblock":
			oPenWin("請選擇段落風格", 160, 242, siteUrl + "/images/edit/FormatBlock.html", true);
			break;
		case "fontsize":
			oPenWin("請選擇字體大小", 160, 186, siteUrl + "/images/edit/FontSize.html", true);
			break;
		case "fontname":
			oPenWin("請選擇字體樣式", 160, 280, siteUrl + "/images/edit/FontName.html", true);
			break;
		case "editmenu":
			oPenWin("開啟功能列表", 156, 250, siteUrl + "/images/edit/menu.html");
			break;
		case "textarea":
			insetQUER();
			break;
		case "cclink":
			insetCCLink();
			break;
		case "CleanCode":
			CleanCode();
			break;
		default:
			oRTE.focus();
			oRTE.document.execCommand(_sCmd, false, null);
			oRTE.focus();
			break;
	}
}


function insetQUER() {
	var _sVal;
	var rng = {};
	var oRTE = getFrameNode(sRTE);
	if (document.all) {
		var selection = oRTE.document.selection; 
		if (selection != null) {
			rng = selection.createRange();
		}
	} else {
		var selection = oRTE.getSelection();
		
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
		rng.text = rng.toString();
	}
	_sVal = rng.text == "" ? "請在文本框輸入文字" : rng.text;
	var html = "<table style='border:1px solid #999;width:80%;font-size:12px;' align='center'><tr><td>"+ _sVal +"</td></tr></table>";
	
	insertHtml(html);
}
function insetCCLink() {
	var _sVal;
	var rng = {};
	var oRTE = getFrameNode(sRTE);
	if (document.all) {
		var selection = oRTE.document.selection; 
		if (selection != null) {
			rng = selection.createRange();
		}
	} else {
		var selection = oRTE.getSelection();
		
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
		rng.text = rng.toString();
	}
	if(rng.text == "") {
		alert("請選中要設置為TAG跟蹤的文字");
		var html = "";
	} else if(rng.text.length > 10) {
		alert("要設置為TAG跟蹤的文字長度不能超過10個字節");
		var html = rng.text;
	} else {
		_sVal = rng.text;
		var html = "<a href=\"" + siteUrl + "/tag.php?k="+ (_sVal)+"\" target='_blank'>"+ _sVal +"</a>";
	}
	insertHtml(html);
}
function getStatus(){
	var sFunc = getCookie("supeSpaceWordFunc") ? getCookie("supeSpaceWordFunc").split("_") : [];
	for(var i = 0; i < sFunc.length; i++){
		hideList(sFunc[i]);
	}
}
function getCookie(name){
	var result = null; 
	var myCookie = document.cookie + ";"; 
	var searchName = name + "="; 
	var startOfCookie = myCookie.indexOf(searchName); 
	var endOfCookie; 
	if (startOfCookie != -1)
	{ 
		startOfCookie += searchName.length; 
		endOfCookie = myCookie.indexOf(";", startOfCookie); 
		result = unescape(myCookie.substring(startOfCookie, endOfCookie)); 
	} 
	return result; 
}
function setCookie(name, value, expires, path, domain, secure){
	var expDays = expires * 24 * 60 * 60 * 1000;
	var expDate = new Date(); 
	expDate.setTime(expDate.getTime() + expDays); 
	var expString = ((expires == null) ? "" : (";expires=" + expDate.toGMTString())) 
	var pathString = ((path == null) ? "" : (";path=" + path)) 
	var domainString = ((domain == null) ? "" : (";domain=" + domain)) 
	var secureString = ((secure == true) ? ";secure" : "" )
	document.cookie = name + "=" + escape(value) + expString + pathString + domainString + secureString;
}
function hide(_sId){
	document.getElementById(_sId).style.display = document.getElementById(_sId).style.display == "none" ? "" : "none" ;
}
function swap(s,a,b,c){
	document.getElementById(s)[a]=document.getElementById(s)[a]==b?c:b;
}
function saveWordStatus(_sFunc){
	var i = 1;
	var sFuncId = [];

	if(!_sFunc){
		_sFunc = getCookie("supeSpaceWordFunc") != null ? getCookie("supeSpaceWordFunc").split("_") : [];
	}

	for(var j = 0;j < _sFunc.length; j++){
		i = 1;
		sFuncId = _sFunc[j].split("@");
		while(document.getElementById(sFuncId[0] + i)){
			document.getElementById(sFuncId[0] + i).style.display = sFuncId[1] == "true" ? "none" : "";
			i++;
		}
		sFuncId = [];
	}

	setCookie("supeSpaceWordFunc", _sFunc.join("_"), 7);
}

function publish_article(){
	et.save();
}

function insertHtml(html) {
	var oRTE = getFrameNode(sRTE);
	if(window.Event){
		oRTE.document.execCommand('insertHTML', false, html);
	} else {
		oRTE.focus();
		var oRng = oRTE.document.selection.createRange();
		oRng.pasteHTML(html);
		oRng.collapse(false);
		oRng.select();
	}
}

function ieGetParent() {
  var node = null;
  var oRTE = getFrameNode(sRTE);
  oRTE.focus();
  var range = oRTE.document.selection.createRange();
  node = range.parentElement();
  while (node) {
    if (node.nodeType == 1 && 
        node.nodeName != 'P' && 
        node.nodeName != 'SPAN' && 
        node.nodeName != 'A') {
        return(node);
    }
    if (node.parentNode) {
      node = node.parentNode;
    } else {
      return null;
    }
  }
}

function cleanHTML(s) {
  // 把標記名統一轉換為小寫
  s = cleanNodes(s);
  // 使一些標記兼容XHTML標準
  s = s.replace(/<(hr|br)>/gi, '<$1 \/>\n');
  // 使圖片標記兼容XHTML標準
  s = s.replace(/<(img [^>]+)>/gi, '<$1 \/>');
  return s; 
}

function cleanNodes(s) { 
  
  // 獲取標記的開始位置
  var htmlPattern = new RegExp('<[ ]*([\\w]+).*?>', 'gi');
  s = s.replace(htmlPattern, function(ref) {
    
    var cleanStartTag = ""; 
    
    // 分離標記和該標記的屬性
    var ref = ref.replace('^<[ ]*', '<'); 
    var ndx = ref.search(/\s/);
    var tagname = ref.substring(0 ,ndx);
    var attributes = ref.substring(ndx, ref.length);
    
    // 把標記名統一轉換為小寫
    if (ndx == -1) return ref.toLowerCase(); // 簡單標記
    cleanStartTag += tagname.toLowerCase();
    
    // 成對分割標記屬性    
    var pairs = attributes.match(/[\w]+\s*=\s*("[^"]*"|[^">\s]*)/gi);
    if (pairs) {
      for (var t = 0; t < pairs.length; t++) {
        var pair = pairs[t];
        var ndx = pair.search(/=/);
        
        // 把屬性名轉換為小寫
        var attrname = pair.substring(0, ndx).toLowerCase();
        
        if (attrname.match(/on\w+/i)) {
          continue;
        }
        
        // 為屬性值加上雙引號
        var attrval = pair.substring(ndx, pair.length);
        var wellFormed = new RegExp('=[ ]*"[^"]*"', "g");
        if (!wellFormed.test(attrval)) {
          var attrvalPattern = new RegExp('=(.*?)', 'g');
          attrval = attrval.replace(attrvalPattern, '=\"$1');
          attrval += '"';
        }
        var attr = attrname + attrval;
        cleanStartTag += " " + attr;
      }
    } 
    cleanStartTag += ">";

    return cleanStartTag;
  });

  // 把結束標記轉換為小寫
  s = s.replace(/<\/\s*[\w]*\s*>/g, function (ref) {return ref.toLowerCase();});

  return s;
}

function disableNodes(s) {
  // 禁用script標記(包括在屬性中設置事件屬性)
  var scriptPattern = new RegExp('<script[^>]*>.*<\/script\s*>', 'gi');
  s = s.replace(scriptPattern, '');
  
  //禁用frame(iframe)標記
  var framePattern = new RegExp('<i?frame[^>]*>.*<\/i?frame\s*>', 'gi');
  s = s.replace(framePattern, '');  
  
  var formPattern = new RegExp('<form[^>]*>.*</form\s*>', 'gi');
  s = s.replace(formPattern, ''); 
  
  return s;
}