var path=document.getElementsByTagName('base').item(0).href;
var sRTE;
var siteUrl=path+'include/deditor';
var FacePath = siteUrl + "/images/edit/face/";
var docStyle = "";
var isIE=navigator.appVersion.match(/\bMSIE\b/);
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: "Undo", func: "Undo", id: "a1"},
	{w: 20, x: -25, t: "Redo", func: "Redo", id: "a2"},
	{w: 2, x: 0, id: "a3"},
	{w: 20, x: -45, t: "Cut", func: "Cut", id: "b1"},
	{w: 20, x: -65, t: "Copy", func: "Copy", id: "b2"},
	{w: 20, x: -85, t: "Paste", func: "Paste", id: "b3"},
	{w: 2, x: 0, id: "b4"},
	{w: 20, x: -225, t: "Bold", func: "Bold", id: "c1"},
	{w: 20, x: -245, t: "Italic", func: "Italic", id: "c2"},
	{w: 20, x: -265, t: "Underline", func: "Underline", id: "c3"},
	{w: 2, x: 0, id: "c4"},
	{w: 20, x: -285, t: "justifyleft", func: "justifyleft", id: "d1"},
	{w: 20, x: -305, t: "justifycenter", func: "justifycenter", id: "d2"},
	{w: 20, x: -325, t: "justifyright", func: "justifyright", id: "d3"},
	{w: 20, x: -345, t: "justifyfull", func: "justifyfull", id: "d4"},
	{w: 2, x: 0, id: "d5"},
	{w: 20, x: -145, t: "Add images", func: "img", id: "e1"},
	{w: 20, x: -759, t: "Insert flash", func: "flash", id: "e6"},
	{w: 20, x: -779, t: "Insert video", func: "video", id: "e7"},
	{w: 20, x: -105, t: "Insert link", func: "link", id: "e2"},
	{w: 20, x: -125, t: "Insert table", func: "table", id: "e3"},
	{w: 20, x: -465, t: "Insert UBB face", func: "face", id: "e4"},
	{w: 20, x: -485, t: "Insert textarea", func: "textarea", id: "e5"},
	{w: 20, x: -799, t: "Setting TAG", func: "cclink", id: "e8"},
	{w: 2, x: 0, id: "e4"},
	{w: 20, x: -165, t: "inserthorizontalrule", func: "inserthorizontalrule", id: "f1"},
	{w: 20, x: -185, t: "forecolor", func: "forecolor", id: "f2"},
	{w: 20, x: -205, t: "hilitecolor", func: "hilitecolor", id: "f3"},
	{w: 2, x: 0, id: "f4"},
	{w: 40, x: -505, t: "formatblock", func: "formatblock", id: "g1"},
	{w: 40, x: -545, t: "fontname", func: "fontname", id: "g2"},
	{w: 60, x: -585, t: "fontsize", func: "fontsize", id: "g3"},
	{w: 2, x: 0, id: "g4"},
	{w: 20, x: -365, t: "insertorderedlist", func: "insertorderedlist", id: "h1"},
	{w: 20, x: -385, t: "insertunorderedlist", func: "insertunorderedlist", id: "h2"},
	{w: 20, x: -405, t: "outdent", func: "outdent", id: "h3"},
	{w: 20, x: -425, t: "indent", func: "indent", id: "h4"},
	{w: 2, x: 0, id: "h5"},
	{w: 20, x: -445, t: "editmenu", 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(isIE){
				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(isIE){
				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 (isIE){
			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 = "View source code&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 = "fullEditer&nbsp;&nbsp;";

	oBody["cleanCode"] = document.createElement("label");
	oBody["cleanCode"].style.fontSize = '12px';
	oBody["cleanCode"].onclick = CleanCode;
	oBody["cleanCode"].innerHTML = "Remove format&nbsp;&nbsp;";

	oBody["renew"] = document.createElement("label");
	oBody["renew"].style.fontSize = '12px';
	oBody["renew"].onclick = this.renewcontent;
	oBody["renew"].innerHTML = "Restoration contents";

	oBody["explain"] = document.createElement("label");
	oBody["explain"].style.fontSize = '12px';
	oBody["explain"].innerHTML = "&nbsp;&nbsp;(Wrap with '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(isIE){
				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 = isIE ?  "": "<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: 'arial','helvetica','sans-serif';\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(!navigator.appVersion.match(/\bMSIE\b/)) {
				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("Insert face", 280, 170, siteUrl + "/images/edit/Face.php?siteurl=" + escape(siteUrl));
			break;
		case "link":
			oPenWin("Insert link", 300, 140, siteUrl + "/images/edit/InsertLink.html");
			break;
		case "table":
			oPenWin("Insert table", 300, 240, siteUrl + "/images/edit/InsertTable.html");
			break;
		case "img":
			//oPenWin("请输入图片地址", 380, 400, siteUrl + "../../../?mod=image&item=group&code=insert", 1, 1);
			win_show("Add images", siteUrl + "../../../?mod=image&item=group&code=insert&in_frame=1",380, 400, false);
			break;
		case "flash":
			oPenWin("Insert flash", 300, 160, siteUrl + "/images/edit/InsertFlash.html");
			break;
		case "video":
			oPenWin("Insert video", 300, 160, siteUrl + "/images/edit/InsertVideo.html");
			break;
		case "forecolor":
			oPenWin("Fore color", 140, 162, siteUrl + "/images/edit/ForeColor.html", true);
			break;
		case "hilitecolor":
			oPenWin("Back color", 140, 162, siteUrl + "/images/edit/BackColor.html", true);
			break;
		case "formatblock":
			oPenWin("Format block", 160, 242, siteUrl + "/images/edit/FormatBlock.html", true);
			break;
		case "fontsize":
			oPenWin("Font size", 160, 186, siteUrl + "/images/edit/FontSize.html", true);
			break;
		case "fontname":
			oPenWin("Font name", 160, 280, siteUrl + "/images/edit/FontName.html", true);
			break;
		case "editmenu":
			oPenWin("menu", 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 == "" ? "Please input text in text box" : 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("Please follow the TAG is selected");
		var html = "";
	} else if(rng.text.length > 10) {
		alert("To set up the word lengths to TAG tracking should not exceed 10 bytes");
		var html = rng.text;
	} else {
		_sVal = rng.text;
		var mod=_GET('mod')||'news';
		var html = "<a href=\"index.php?mod=tag&code=list_item&item="+mod+"&tag="+ _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("_") : ['a@true','b@true','c@false','d@true','e@false','f@false','g@true','h@true'];//用户没设置时默认调用 added by xiucai
	}

	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(){
	ajhEditor.save();
}

function insertHtml(html) {
	var oRTE = getFrameNode(sRTE);
	if(!isIE){
		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;
}

//以下部分由江秀才添加
function autoSave()
{
	parentTag('editerToolsBG','form',addSubmitEvent);
	function addSubmitEvent(formObj)
	{
		Event.observe(formObj,'submit',publish_article);
	}
	function parentTag(obj,tagName,callback)
	{
		if(typeof obj=='string')
		{
			var obj=document.getElementById(obj);
		}
		if(obj.tagName.toUpperCase()==tagName.toUpperCase())
		{
			callback.apply(this,[obj]);
		}
		else
		{
			parentTag(obj.parentNode,tagName,callback);
		}
	}
}
function _GET(varName)
{
	var query=window.location.search.substring(1);
	if(query=='')return undefined;
	var query_list=query.split('&');
	for (var i=0;i<query_list.length ;i++ )
	{
		var piece=query_list[i].split('=');
		if(piece[0]==varName)
		{
			return piece[1];
		}
	}
}
