//*****************************界面css风格（start）*******************************
//工具栏风格
function OT_ButtonOver(theTD) {
	theTD.className = "OT_ButtonOver";
}
function OT_ButtonOut(theTD) {
	theTD.className = "OT_ButtonNormal";
}
function OT_ButtonOutK(theTD) {
	theTD.className = "OT_editBox";
}
//function ButtonDown(theTD) {
//	theTD.className = "OT_ButtonDown";
//}
//function ButtonSelect(theTD) {
//	oldClass = (oldClass == "TextEdit_ButtonDown") ? "TextEdit_ButtonNormal" : "TextEdit_ButtonDown";
//	document.body.style.cursor = "default";
//	theTD.className = "TextEdit_ButtonDown";
//}
function OT_TrOver(theTD) {
	theTD.style.backgroundColor='#e2e5ee';
}

function OT_TrOut(theTD) {
	theTD.style.backgroundColor='#FFFFFF';
}

//*****************************界面css风格（end）*********************************


//*****************************公用函数（start）*********************************
//获取对象
function $(ID){
	return document.getElementById(ID);
}

//改变选中元素的样式
function OT_ExeCom(frameID,str,bloon,str2){
	eval(frameID).focus();
//	OTexeCom=OTedit_editBox.document.selection.createRange();
//	OTexeCom.select();
//	OTexeCom.execCommand(str,bloon,str2);
	eval(frameID).document.execCommand(str,bloon,str2);
//	return false
}


//获取元素
function OT_SelectRange(frameID){
	OT_selection	=frameID.document.selection;
	OT_edit			=OT_selection.createRange();	//返回 TextRange/ControlRange 对象
	OT_RangeType	=OT_selection.type;	//获取选中对象的类型。Text:文本，Control:HTML标识控件，None:无
}


//插入代码
function OT_InsertCode(frameID,codeStr){
	frameID.focus();
	OT_SelectRange(frameID);
	OT_edit.pasteHTML(codeStr);
}

//获取当前所在标签
function OT_GetTag(frameID){
	frameID.focus();
	OT_SelectRange(frameID)

	var tagStr = null;
	
	switch(OT_RangeType){
		case 'Control' :
			if (OT_edit.length > 0){tagStr = OT_edit.item(0);}
			break;
		case 'None' :
			tagStr = OT_edit.parentElement();
			break;
		case 'Text' :
			tagStr = OT_edit.parentElement();
			break;
	}
	return tagStr
}

//检查标签
function OT_CheckTag(item,tagName){
	if (item.tagName.search(tagName)!= -1){
		return item;
	}
	if (item.tagName == 'BODY'){
		return false;
	}
	item=item.parentElement;
	return OT_CheckTag(item,tagName);
}

//过滤内容
function OT_FiltData(str){
//	str = str.replace(/<p>/gi,"<p style='margin-top:0px;margin-bottom:0px;'>")
//	str = str.replace(/<p (.*?)>/gi,"<p $1 style=\"margin-top:0px;margin-bottom:0px;\">");
	str = str.replace(/<p>/gi,"<div>")
	str = str.replace(/<p (.*?)>/gi,"<div $1>");
	str = str.replace(/<\/p>/gi,"</div>");
	str = str.replace(/(<(meta|iframe|frame|tbody|layer|form)[^>]*>|<\/(iframe|frame|meta|tbody|layer|form)>)/gi,"");
	str = str.replace(/<\\?\?xml[^>]*>/gi,"");
	str = str.replace(/<\s*(script[^>]*)>(.*)<\/\s*script>/gi,"");
	str = str.replace(/<(\w[^>|\s]*)([^>]*)(on(finish|mouse|Exit|error|click|key|load|change|focus|blur))(.[^>]*)/gi,"<$1");
	str = str.replace(/<(\w[^>|\s]*)([^>]*)(&#|window\.|javascript:|js:|about:|file:|Document\.|vbs:|cookie| name| id)(.[^>]*)/gi,"<$1");
	str = str.replace(/<a (.*?)>(.*?)<\/a>/gi,"<a $1 target='_blank'>$2</a>");
	return str
}
//*****************************公用函数（end）*********************************



//设置字体
function OT_SetFontName(frameID,fontNameID,fontNameKID,newFont){
	OT_ExeCom(frameID,'FontName',false,newFont);
	fontNameID.innerHTML=newFont
	fontNameKID.style.display='none'
}

//设置字号
function OT_SetFontSize(frameID,fontSizeID,fontSizeKID,newSize){
	OT_ExeCom(frameID,'FontSize',false,newSize);
	fontSizeID.innerHTML=newSize;
	fontSizeKID.style.display='none';
}

//打开字体颜色窗口
function OT_OpenFontColor(frameID,fontColorID){
	var arr = showModalDialog("OTtextEditor/selColor.htm","","dialogWidth:20em; dialogHeight:17em; status:0; help:0");
	if (arr != null){
		OT_ExeCom(frameID,'ForeColor',false,arr);
		fontColorID.style.background=arr;
	}
}

//打开字体颜色窗口
function OT_OpenBackColor(frameID,backColorID){
	var arr = showModalDialog("OTtextEditor/selColor.htm","","dialogWidth:20em; dialogHeight:17em; status:0; help:0");
	if (arr != null){
		OT_ExeCom(frameID,'BackColor',false,arr);
		backColorID.style.background=arr;
	}
}

//打开替换窗口
function OT_OpenReplace(frameID){
	var arr = showModalDialog("OTtextEditor/replace.htm?frameID="+ frameID,window,"dialogWidth:18em; dialogHeight:11em; status:0; help:0");
	if (arr != null){
		OT_ExeCom('BackColor',false,arr);
		backColor.style.background=arr;
	}
}

//清理代码
function OT_CleanCode(frameID){
	if (confirm("确定清理代码？")==false){return false}
	var body = frameID.document.body;
	for (var index = 0; index < body.all.length; index++) {
		tag = body.all[index];
		//if (tag.Attribute["className"].indexOf("mso") > -1)
		tag.removeAttribute("className","",0);
		tag.removeAttribute("style","",0);
	}
	var html = frameID.document.body.innerHTML;
	html = html.replace(/\<div>/gi,"[$div]");
	html = html.replace(/\<div (.*?)>/gi,"[$div]");
	html = html.replace(/\<\/div>/gi,"[$\/div]");
	html = html.replace(/\<p\s*[^>]*>/gi,"[$p]");
	html = html.replace(/\<\/p>/gi,"[$\/p]");
	html = html.replace(/\<br>/gi,"[$br]");
	html = html.replace(/\<[^>]*>/g,"");        ///过滤其它所有"<...>"标签
	html = html.replace(/\[\$div\]/gi,"<div>");
	html = html.replace(/\[\$\/div\]/gi,"<\/div>");
	html = html.replace(/\[\$p\]/gi,"<p>");
	html = html.replace(/\[\$\/p\]/gi,"<\/p>");
	html = html.replace(/\[\$br\]/gi,"<br>");
	frameID.document.body.innerHTML = html;
}

//打开插入图片页面
function OT_OpenImage(frameID){
	var arr = showModalDialog("OTtextEditor/image.htm","","dialogWidth:27em; dialogHeight:26em; status:0; help:0");
	if (arr != null){
		OT_InsertCode(frameID,arr);
	}
}

//打开插入栏目框页面
function OT_OpenFieldset(frameID){
	var arr = showModalDialog("OTtextEditor/fieldset.htm","","dialogWidth:22em; dialogHeight:13em; status:0; help:0");
	if (arr != null){
		OT_InsertCode(frameID,arr);
	}
}

//打开插入表格页面
function OT_OpenTable(frameID){
	var arr = showModalDialog("OTtextEditor/table.htm?frameID="+ frameID,window,"dialogWidth:22em; dialogHeight:19em; status:0; help:0");
	if (arr != null){
		frameID=eval(frameID)
		OT_InsertCode(frameID,arr);
	}
}

//打开插入Flash页面
function OT_OpenFlash(frameID){
	var arr = showModalDialog("OTtextEditor/flash.htm",window,"dialogWidth:19em; dialogHeight:13em; status:0; help:0");
	if (arr != null){
		OT_InsertCode(frameID,arr);
	}
}

//打开插入Media页面
function OT_OpenMedia(frameID){
	var arr = showModalDialog("OTtextEditor/media.htm",window,"dialogWidth:19em; dialogHeight:11em; status:0; help:0");
	if (arr != null){
		OT_InsertCode(frameID,arr);
	}
}

//打开插入Real页面
function OT_OpenReal(frameID){
	var arr = showModalDialog("OTtextEditor/real.htm",window,"dialogWidth:19em; dialogHeight:11em; status:0; help:0");
	if (arr != null){
		OT_InsertCode(frameID,arr);
	}
}

//打开字符
function OT_OpenSymbol(frameID){
	var arr = showModalDialog("OTtextEditor/specialchar.htm","","dialogWidth:26em; dialogHeight:17em; status:0; help:0");
	if (arr != null){
		OT_InsertCode(frameID,arr);
	}
}

//打开表情头像页面
function OT_OpenFace(frameID){
	var arr = showModalDialog("OTtextEditor/face.htm","","dialogWidth:23em; dialogHeight:24em; status:0; help:0");
	if (arr != null){
		OT_InsertCode(frameID,arr);
	}
}

//打开插入代码页面
function OT_OpenCode(frameID){
	var arr = showModalDialog("OTtextEditor/code.htm",window,"dialogWidth:31em; dialogHeight:25em; status:0; help:0");
	if (arr != null){
		OT_InsertCode(frameID,arr);
	}
}

//打开帮助页面
function OT_OpenHelp(){
	var arr = showModalDialog("OTtextEditor/help.htm","","dialogWidth:16em; dialogHeight:10em; status:0; help:0");
	if (arr != null){
		OT_InsertCode(arr);
	}
}

//（表格）插入行
function OT_InsertRow(frameID){
	objReference = OT_GetTag(frameID);
	objReference = OT_CheckTag(objReference,'/^(TABLE)|^(TR)|^(TD)|^(TBODY)/');

	switch(objReference.tagName){
		case 'TABLE' :
			var newTable = objReference.cloneNode(true);
			var newRow = newTable.insertRow();
			for(x = 0; x<newTable.rows[0].cells.length; x++){
				var newCell = newRow.insertCell();
			}
			objReference.outerHTML = newTable.outerHTML;
			break;
		case 'TBODY' :
			var newTable = objReference.cloneNode(true);
			var newRow = newTable.insertRow();
			for(x = 0; x<newTable.rows[0].cells.length; x++){
				var newCell = newRow.insertCell();
			}
			objReference.outerHTML=newTable.outerHTML;
			break;
		case 'TR' :
			var rowIndex = objReference.rowIndex;
			var parentTable = objReference.parentElement.parentElement;
			var newTable = parentTable.cloneNode(true);
			var newRow = newTable.insertRow(rowIndex+1);
			for(x = 0; x< newTable.rows[0].cells.length; x++){
				var newCell = newRow.insertCell();
			}
			parentTable.outerHTML = newTable.outerHTML;
			break;
		case 'TD' :
			var parentRow = objReference.parentElement;
			var rowIndex = parentRow.rowIndex;
			var cellIndex = objReference.cellIndex;
			var parentTable = objReference.parentElement.parentElement.parentElement;
			var newTable = parentTable.cloneNode(true);
			var newRow = newTable.insertRow(rowIndex+1);
			for(x = 0; x< newTable.rows[0].cells.length; x++){
				var newCell = newRow.insertCell();
				if (x == cellIndex)newCell.id='ura';
			}
			parentTable.outerHTML = newTable.outerHTML;
			var r = frameID.document.body.createTextRange();
			var item = frameID.document.getElementById('ura');
			item.id = '';
			r.moveToElementText(item);
			r.moveStart('character',r.text.length);
			r.select();
			break;
		default :
			return;
	}
}

//（表格）删除行
function OT_DeleteRow(frameID){
	objReference = OT_GetTag(frameID);
	objReference = OT_CheckTag(objReference,'/^(TABLE)|^(TR)|^(TD)|^(TBODY)/');

	switch(objReference.tagName){
		case 'TR' :
			var rowIndex = objReference.rowIndex;//获得rowindex
			var parentTable=objReference.parentElement.parentElement;
			parentTable.deleteRow(rowIndex);
			break;
		case 'TD' :
			var cellIndex=objReference.cellIndex;
			var parentRow=objReference.parentElement;//获取父行
			var rowIndex = parentRow.rowIndex;//获得rowIndex
			var parentTable=objReference.parentElement.parentElement.parentElement;
			parentTable.deleteRow(rowIndex);
			if (rowIndex>=parentTable.rows.length){
				rowIndex=parentTable.rows.length-1;
			}
			if (rowIndex>=0){
				var r = frameID.document.body.createTextRange();
				r.moveToElementText(parentTable.rows[rowIndex].cells[cellIndex]);
				r.moveStart('character',r.text.length);
				r.select();
			}else{
				parentTable.removeNode(true);
			}
			break;
		default :
			return;
	}
}

//（表格）插入列
function OT_InsertColumn(frameID){//增加了一个新列在表格的右边。
	objReference = OT_GetTag(frameID);
	objReference = OT_CheckTag(objReference,'/^(TABLE)|^(TR)|^(TD)|^(TBODY)/');

	switch(objReference.tagName){
		case 'TABLE' :
			var newTable=objReference.cloneNode(true);
			for(x=0; x<newTable.rows.length; x++){
				var newCell = newTable.rows[x].insertCell();
			}
			newCell.focus();
			objReference.outerHTML=newTable.outerHTML;
			break;
		case 'TBODY' :
			var newTable=objReference.cloneNode(true);
			for(x=0; x<newTable.rows.length; x++){
				var newCell = newTable.rows[x].insertCell();
			}
			objReference.outerHTML=newTable.outerHTML;
			break;
		case 'TR' :
			objReference=objReference.parentElement.parentElement;
			var newTable=objReference.cloneNode(true);
			for(x=0; x<newTable.rows.length; x++){
				var newCell = newTable.rows[x].insertCell();
			}
			objReference.outerHTML=newTable.outerHTML;
			break;
		case 'TD' ://如果光标是在单元里，或某单元被选中,增加一个新列在这单元右边
			var cellIndex = objReference.cellIndex;//获取cellIndex
			var rowIndex=objReference.parentElement.rowIndex;
			var parentTable=objReference.parentElement.parentElement.parentElement;
			var newTable=parentTable.cloneNode(true);
			for(x=0; x<newTable.rows.length; x++){
				var newCell = newTable.rows[x].insertCell(cellIndex+1);
				if (x==rowIndex)newCell.id='ura';
			}
			parentTable.outerHTML=newTable.outerHTML;
			var r = frameID.document.body.createTextRange();
			var item=frameID.document.getElementById('ura');
			item.id='';
			r.moveToElementText(item);
			r.moveStart('character',r.text.length);
			r.select();
			break;
		default :
			return;
	}
}

//（表格）删除列
function OT_DeleteColumn(frameID){
	objReference = OT_GetTag(frameID);
	objReference = OT_CheckTag(objReference,'/^(TABLE)|^(TR)|^(TD)|^(TBODY)/');

	switch(objReference.tagName){
		case 'TD' :
			var rowIndex=objReference.parentElement.rowIndex;
			var cellIndex = objReference.cellIndex;//Get cellIndex
			var parentTable=objReference.parentElement.parentElement.parentElement;
			var newTable=parentTable.cloneNode(true);
			if (newTable.rows[0].cells.length==1){
				parentTable.removeNode(true);
				return;
			}
			for(x=0; x<newTable.rows.length; x++){
				if (newTable.rows[x].cells[cellIndex]=='[object]'){
					newTable.rows[x].deleteCell(cellIndex);
				}
			}
			if (cellIndex>=newTable.rows[0].cells.length){
				cellIndex=newTable.rows[0].cells.length-1;
			}
			if (cellIndex>=0)  newTable.rows[rowIndex].cells[cellIndex].id='ura';
			parentTable.outerHTML=newTable.outerHTML;
			if (cellIndex>=0){
				var r = frameID.document.body.createTextRange();
				var item=frameID.document.getElementById('ura');
				item.id='';
				r.moveToElementText(item);
				r.moveStart('character',r.text.length);
				r.select();
			}
			break;
		default :
			return;
	}
}

//创建超链接(浏览器内置)
function OT_CreateLine(frameID){
	frameID.focus();
	frameID.document.execCommand('CreateLink', true, null);
}

//创建超链接(人工创建)
function OT_OpenLink(frameID){
	frameID.focus();
	OT_SelectRange(frameID);
	var url;
	var selectionType = OT_RangeType.toLowerCase();
	if (selectionType != 'control'){
		if (OT_edit.htmlText.match(/<a .*?>(.*?)<\/a>/gi) != null) {
			url = OT_edit.htmlText.replace(/<a href="(.*?)">.*?<\/a>/gi,"$1");
		}else{
			url = "";
		}
		var arr = showModalDialog("OTtextEditor/link.htm",url,"dialogWidth:18em; dialogHeight:10em; status:0; help:0");
		if (arr != null){
			if (selectionType.toLowerCase() != "none"){
				OT_ExeCom(frameID,"createlink",true,arr);
			}else{
				OT_InsertCode(frameID,"<a href=\""+ arr +"\" target='_blank'>"+ arr +"</a>")
			}
		}
	}else{
		var tag = OT_edit.item(0).tagName.toLowerCase();
		if (tag == "img"){
			var arr = showModalDialog("OTtextEditor/link.htm",url,"dialogWidth:18em; dialogHeight:10em; status:0; help:0");
			OT_ExeCom(frameID,"createlink",false,arr);
		}
	}
}





//保存编辑框内容到textarea
function OT_SaveData(nameID){
	if($(nameID).style.display=="none"){
		$(nameID).value=OT_FiltData(window.frames["OTtextEditor_"+ nameID].document.body.innerHTML);
	}else{
		window.frames["OTtextEditor_"+ nameID].document.body.innerHTML=OT_FiltData($(nameID).value);
	}
}


//模式切换
function OT_ModeChange(nameID,str){
	if ((str == "design" && $("OTtextEditor_"+ nameID).style.display == "") || (str == "html" && $(nameID).style.display == "")){return false}

	OT_SaveData(nameID);

	if (str == "design"){
		$("OTtextEditor_"+ nameID).style.display = "";
		$("mode_design").className = "OT_ModeButtonOn";
	}else{
		$("OTtextEditor_"+ nameID).style.display = "none";
		$("mode_design").className = "OT_ModeButtonOff";
	}

	if (str == "html"){
		$(nameID).style.display = "";
		$("mode_html").className = "OT_ModeButtonOn";
	}else{
		$(nameID).style.display = "none";
		$("mode_html").className = "OT_ModeButtonOff";
	}

}


//设置编辑框的高度
function OT_EditBoxHeight(textEditorID,str){
	var newHeight=0
	if (str=="add"){
		newHeight=parseInt(textEditorID.style.height) + 100

	}else if (str=="cut"){
		newHeight=parseInt(textEditorID.style.height) - 100
		if (newHeight < 200){newHeight=200}
	}

	if (newHeight > 0){
		textEditorID.style.height=newHeight
	}
}

//打开预览窗口
function OT_OpenView(nameID,winStyle){
	OT_SaveData(nameID);

	viewContent=$(nameID).value;
	headContent="<html><head><style type=text/css>body{color:#000000;font-family:宋体; font-size:13px;"+ winStyle +"}</style></head><BODY><font style='color:red;font-size:12px;border:1px red dotted;padding:5px'>提示:预览效果即是过滤掉内容中可能会构成安全危害的代码后的内容（同时过滤界面内容）</font><p>";
	preWin=window.open('preview','','left=0,top=0,width=765,height=400,resizable=1,scrollbars=1, status=1, toolbar=1, menubar=0');
	preWin.document.open();
	preWin.document.write(headContent);
	preWin.document.write(viewContent);
	preWin.document.write("</body></html>");
	preWin.document.close();
	preWin.document.title="预览效果";
//	preWin.document.charset="bg2313";

}
