<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
option explicit
response.buffer=true
Const ChannelID=0
%>
<!--#include file="../Conn.asp"-->
<!--#include file="../inc/Cl_ClsSysTem.asp"-->
<%
if Not Cl.ChkAdminLogin then
	Response.write "<li>您未登录或者您无此操作权限！</li>"
	Response.end
end if
Dim Rs,StyleID,CssID,Module,TemplateStr,TemplateNum,txtRun,TempContent
Dim sHTML,sInsTallDir,MainStr
Cl.GetWeb_Setting()
StyleID		= Cl.ChkClng(Request("StyleID"))
Module		= Cl.CheckStr(request("module"))
txtRun		= Cl.ChkClng(Request("txtRun"))
sInsTallDir	= "http://"&Request.servervariables("Server_name") & Cl.WebDir
'从数据库中取出对应项目内容
if Lcase(Module)<>"main_style" then
	Set Rs=Cl.Execute("Select ID,StyleName,Main_Style,"&Module&" From Cl_Style Where id="&StyleID)
	MainStr		= Split(Rs(2),"@@@")
	TempStr		= Split(Rs(3),"@@@")
else
	Set Rs=Cl.Execute("Select ID,StyleName,"&Module&" From Cl_Style Where id="&StyleID)
	MainStr		= Split(Rs(2),"@@@")
	TempStr		= MainStr
end if
Set Rs=Nothing
TemplateStr		= Split(TempStr(0),"|||")
TemplateNum		= Ubound(TemplateStr)
if txtRun>TemplateNum then txtRun=TemplateNum
MainStr(0)		= Split(MainStr(0),"|||")
MainStr(0)(0)	= Split(MainStr(0)(0),"||")
CssID			= Cl.ChkCLng(MainStr(0)(0)(1))
Cl.StyleID		= StyleID
Cl.CssID		= CssID
Cl.LoadTemplates("")

If TemplateStr(Ubound(TemplateStr))="" Then TemplateStr(Ubound(TemplateStr))="del"
TempContent = TemplateStr(txtRun)
TempContent = Replace(TempContent,"{%showhead%}","<" & "!--clhead-->" & Cl.Head & "<" & "!--clheadend--" & ">")
TempContent = Replace(TempContent,"{%showfooter%}","<" & "!--clfooter-->" & Cl.Bottom & "<" & "!--clfooterend--" & ">")
TempContent = Replace(TempContent,"</body>","<" & "!--/body-->")
TempContent = Replace(TempContent,"</html>","<" & "!--/html-->")
TempContent = Replace(TempContent,"</textarea>","[/textarea]")
%>
<html><head>
<title>创力网站管理系统模版编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="../Skins/Admin_Style.Css">
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" class="border">
<form name="FormEdit">
<tr><td align="center" class="title" Colspan=4><b>创 力 网 站 管 理 系 统 模 板 编 辑 窗 口</b></td></tr>
<tr><td align="center" width="100%" Colspan=4>
	<TEXTAREA id="Content" name="Content" ROWS="30" style="width:100%" onmouseout="setContent('get');" onMouseUp=""><%=Server.HtmlEnCode(TempContent)%></TEXTAREA>
	</td></tr>
<tr><td align="center" width="100%" Colspan=4>
	<TEXTAREA id="EditorTemp" name="EditorTemp" style="display:none"></TEXTAREA>
	</td></tr>
<tr><td align="center" width="100%" Colspan=4>
	<IFRAME ID="editor" src="../Editor/Editor.asp?id=EditorTemp&style=Template&cid=0&CssID=<%=CssID%>" frameborder="0" scrolling="no" width="100%" height="1"></IFRAME>
	</td></tr>
<tr>
	<td height="22" width="30%"><Input id=isshowcode name=isshowcode type=Checkbox value=1 onclick="showcode()"  checked><span id=showc>关闭代码</span>&nbsp;&nbsp;<Input id=isshowedit name=isshowedit type=Checkbox value=1 onclick="showedit()"><span id=showe>可视编辑</span></td>
	<td height="22" align=center width="40%">
	<input type="button" name="editbutton" value="&nbsp;[保存修改]&nbsp;" onClick="SaveContent();selfedit()">
	<input type="button" name="close" value="&nbsp;[关  闭]&nbsp;" onClick="window.close()">
	</td>
	<td height="22" align=right width="30%"><a href="javascript:sizeChange('-')"><img src="images/minus.gif" unselectable="on" border=0></a> <a href="javascript:sizeChange('+')"><img src="images/plus.gif" unselectable="on" border=0></a>&nbsp;&nbsp;&nbsp;&nbsp;</td>
	</tr>
</form></table>
<script language="JavaScript" type="text/javascript">
<!--
function selfedit(){
if (editor.ModeEdit.value!='EDIT'){
alert('请先转到编辑模式！');
return false;
}
var pp=self.opener.document.getElementById('t<%=txtRun%>');
//pp.value=editor.Editor.document.body.innerHTML;
pp.value=document.FormEdit.Content.value
self.close();
}
function showcode(){
	if (document.getElementById('isshowcode').checked == true) {
		document.FormEdit.Content.style.display='';
		document.getElementById('showc').innerText='关闭代码';
	}else{
		document.FormEdit.Content.style.display='none';
		document.getElementById('showc').innerText='显示代码';
		document.getElementById('isshowedit').checked = true;
		document.getElementById('editor').style.height=520;
		document.getElementById('showe').innerText='关闭编辑';
	}
}
function showedit(){
	if (document.getElementById('isshowedit').checked == true) {
		document.getElementById('editor').style.height=520;
		document.getElementById('showe').innerText='关闭编辑';
	}else{
		document.getElementById('editor').style.height=0;
		document.getElementById('showe').innerText='可视编辑';
		document.getElementById('isshowcode').checked = true;
		document.FormEdit.Content.style.display='';
		document.getElementById('showc').innerText='关闭代码';
	}
}
function sizeChange(st){
	if (document.getElementById('isshowedit').checked == true) {
		var obj=document.getElementById('editor');
		var sh=parseInt(obj.style.height);
		if (st=="+"){
		obj.style.height=sh+100;
		}
		else if (sh>200) {
		obj.style.height=sh-100;
		}
	}else if (document.getElementById('isshowcode').checked == true){
		var obj=document.FormEdit.Content
		var sc=parseInt(obj.rows);
		if (st=="+"){
		obj.rows=sc+10;
		}
		else if (sc>15) {
		obj.rows=sc-10;
		}
	}
}
//-->
</script>
<script language="VBScript" type="text/vbscript">
    Dim regEx, Match, Matches, StrBody,strTemp,strTemp2,strMatch,arrMatch,i
    Dim Content1,Content2,TemplateContent,TemplateContent2,arrContent
	setContent("editortemp")
    Function setContent(sType)
	Set regEx = New RegExp
    regEx.IgnoreCase = True
    regEx.Global = True
    if sType="set" then
		TemplateContent = document.FormEdit.Content.value
		TemplateContent2 = editor.Editor.document.body.innerHTML
        if TemplateContent2="" then Exit function
        regEx.Pattern = "\<IMG(.[^\<]*)\$\>"
        Set Matches = regEx.Execute(TemplateContent2)
        For Each Match In Matches
            regEx.Pattern = "\#(.*)\#"
            Set strTemp = regEx.Execute(Match.Value)
            For Each Match2 In strTemp
                strTemp2 = Replace(Match2.Value, "&amp;", "&")
                strTemp2 = Replace(strTemp2, "#", "")
                'strTemp2 = Replace(strTemp2, "$}", "%}")
                strTemp2 = Replace(strTemp2,"&13;&10;",vbCrLf)
                strTemp2 = Replace(strTemp2,"&9;",vbTab)
                strTemp2 = Replace(strTemp2, "[!", "<")
                strTemp2 = Replace(strTemp2, "!]", ">")
                TemplateContent2=Replace(TemplateContent2, Match.Value, strTemp2)
            Next
        Next
        TemplateContent2=Replace(TemplateContent2, "$}", "%}")
        TemplateContent2=Replace(TemplateContent2,"width=<%=Cl.mainsetting(0)%>","width={%width%}")
        TemplateContent2=Replace(TemplateContent2,"<%=sInsTallDir & Cl.Web_PicUrl%>","{%picurl%}")
        TemplateContent2=Replace(TemplateContent2, "<%=sInsTallDir%>","{%webdir%}")
        TemplateContent2=Replace(TemplateContent2,"[$uploaddir$]","{%uploaddir%}")
        TemplateContent2=Replace(TemplateContent2,"[$channelid$]","{%channelid%}")
        TemplateContent2=Replace(TemplateContent2,"[$classid$]","{%classid%}")
        TemplateContent2=Replace(TemplateContent2,"{%webdir%}Editor/","")
        TemplateContent2=Replace(TemplateContent2,"[/textarea]","</textarea>")
        TemplateContent2=Resumeblank(TemplateContent2)
		if TemplateContent="" then document.FormEdit.Content.value=TemplateContent2 : Exit Function
        if Instr(TemplateContent,"<body>")=0 then
            regEx.Pattern = "(\<body)(.[^\<]*)(\>)"
            Set Matches = regEx.Execute(TemplateContent)
            For Each Match In Matches
                StrBody = Match.Value
            Next
            If StrBody = ""  Then
				document.FormEdit.Content.value=TemplateContent2
				Exit Function
			else
				arrContent = Split(TemplateContent, StrBody)
				Content1 = arrContent(0) & StrBody
				Content2 = arrContent(1)
            End If 
        Else
			arrContent = Split(TemplateContent, "<body>")
			Content1 = arrContent(0) & "<body>"
			Content2 = arrContent(1)
        End if
        document.FormEdit.Content.value=Content1& vbCrLf &TemplateContent2
    Else
		TemplateContent= document.FormEdit.Content.value
        if TemplateContent="" then Exit function
        if Instr(TemplateContent,"<body>")=0 then
			StrBody = ""
            regEx.Pattern = "(\<body)(.[^\<]*)(\>)"
            Set Matches = regEx.Execute(TemplateContent)
            For Each Match In Matches
                StrBody = Match.Value
            Next
            If StrBody = ""  Then
                Content1 = ""
				Content2 = TemplateContent
			else
				arrContent = Split(TemplateContent, StrBody)
				Content1 = arrContent(0) & StrBody
				Content2 = arrContent(1)
            End If 
        Else
			arrContent = Split(TemplateContent, "<body>")
			Content1 = arrContent(0) & "<body>"
			Content2 = arrContent(1)
        End if
        Content2 = Replace(Content2,"{%webdir%}","<%=sInsTallDir%>")
        Content2 = Replace(Content2,"{%uploaddir%}","[$uploaddir$]")
        Content2 = Replace(Content2,"{%width%}","<%=Cl.mainsetting(0)%>")
        Content2 = Replace(Content2,"{%picurl%}","<%=sInsTallDir & Cl.Web_PicUrl%>")
        Content2 = Replace(Content2,"{%channelid%}","[$channelid$]")
        Content2 = Replace(Content2,"{%classid%}","[$classid$]")
        Content2 = Replace(Content2,"</textarea>","[/textarea]")
        '图片替换JS
        regEx.Pattern = "(\<" & "Script)(.[^\<]*)(\<\/Script\>)"
        Set Matches = regEx.Execute(Content2)
        For Each Match In Matches
            strTemp = Replace(Match.Value, "<", "[!")
            strTemp = Replace(strTemp, ">", "!]")
            strTemp = Replace(strTemp, "'", """")
            strTemp = "<IMG alt='#" & strTemp & "#' src=""<%=Cl.WebDir%>Editor/Images/Template/Js.gif"" border=0 $>"
            Content2 = Replace(Content2, Match.Value, strTemp)
        Next
        '图片替换超级标签
        regEx.Pattern = "(\{\%ShowArticle|\{\%ShowSoft|\{\%ShowPhoto|\{\%ShowMovie|\{\%ShowProduct|\{\%ShowPicArticle|\{\%ShowPicSoft|\{\%ShowPicPhoto|\{\%ShowPicMovie|\{\%ShowPicProduct)\((.[^(\{\%)]*)\)\%}"
        Content2 = regEx.Replace(Content2, "<IMG alt='#$1($2)$}#' src=""<%=Cl.WebDir%>Editor/Images/Template/Label.gif"" border=0 $>")
        '图片替换其它标签(带参数的)
        'regEx.Pattern = "(\{\%([^{%}])*)%}"
        regEx.Pattern = "(\{%)(.[^\{]*)(\%\})"
        Set Matches = regEx.Execute(Content2)
        For Each Match In Matches
			if Instr(Match.Value,"(")>0 and Instr(Match.Value,")")>0 then
			strTemp = Replace(Match.Value, "<", "[!")
            strTemp = Replace(strTemp, ">", "!]")
            strTemp = "<IMG alt='#" & Match.Value & "#' src=""<%=Cl.WebDir%>Editor/Images/Template/Label2.gif"" border=0 $>"
            Content2 = Replace(Content2, Match.Value, strTemp)
			end if
        Next
		if sType="editortemp" then
        document.FormEdit.EditorTemp.value=Content2
		else
        editor.Editor.document.body.innerHTML=Content2
		editor.showBorders()
		end if
    End if
    End function
	
    Function SaveContent()
		Dim TContent
		Content = document.FormEdit.Content.value
        if Content="" then Exit function
        Content = Replace(Content,"[/textarea]","</textarea>")
		Content = Replace(Content,"<!--/body-->","</body>")
		Content = Replace(Content,"<!--/html-->","</html>")
		if Instr(Content,"<!--clhead-->")>0 and Instr(Content,"<!--clheadend-->")>0 then
			TContent=Split(Content,"<!--clheadend-->")
			if Ubound(TContent) > 0 then
				Content="{%showhead%}" & TContent(1)
			end if
		end if
		if Instr(Content,"<!--clfooter-->")>0 and Instr(Content,"<!--clfooterend-->")>0 then
			TContent=Split(Content,"<!--clfooter-->")
			if Ubound(TContent) > 0 then
				Content=TContent(0) & "{%showfooter%}"
			end if
		elseif Instr(Content,"{%showfooter%}")=0 and (Instr(Content,"{%showhead%}")>0 or Instr(Lcase(Content),"<html>")>0) then
			<%if Lcase(Module)<>"main_style" then%>
				Content=Replace(Content,"</body>","")
				Content=Replace(Content,"</html>","")
				Content=Content & Vbcrlf & "</body>" & Vbcrlf & "</html>"
			<%end if%>
		end if
        document.FormEdit.Content.value=Content
    End Function

	Function  Resumeblank(byval Content)
		Dim strHtml,strHtml2,Num,Numtemp,Strtemp
		strHtml=Replace(Content, "<A", "<a")
		strHtml=Replace(strHtml, "</A>", "</a>")
		strHtml=Replace(strHtml, "<IMG", "<img")
		strHtml=Replace(strHtml, "<FONT", "<font")
		strHtml=Replace(strHtml, "</FONT>", "</font>")
		strHtml=Replace(strHtml, "<STRONG", "<strong")
		strHtml=Replace(strHtml, "</STRONG>", "</strong>")
		strHtml=Replace(strHtml, "<DIV", "<div")
		strHtml=Replace(strHtml, "</DIV>", "</div>")
		strHtml=Replace(strHtml, "<TABLE", "<table")
		strHtml=Replace(strHtml, "</TABLE>", vbCrLf & "</table>"& vbCrLf)
		strHtml=Replace(strHtml, "<TBODY>", "")
		strHtml=Replace(strHtml, "</TBODY>","" & vbCrLf)
		strHtml=Replace(strHtml, "<TR", "<tr")
		strHtml=Replace(strHtml, "</TR>", vbCrLf & "</tr>"& vbCrLf)
		strHtml=Replace(strHtml, "<TD", "<td")
		strHtml=Replace(strHtml, "</TD>", "</td>")
		strHtml=Replace(strHtml, "<" & "!--", vbCrLf & "<" & "!--")
		strHtml=Replace(strHtml, "<SELECT",vbCrLf & "<Select")
		strHtml=Replace(strHtml, "</SELECT>",vbCrLf & "</Select>")
		strHtml=Replace(strHtml, "<OPTION",vbCrLf & "  <Option")
		strHtml=Replace(strHtml, "</OPTION>","</Option>")
		strHtml=Replace(strHtml, "<INPUT",vbCrLf & "  <Input")
		strHtml=Replace(strHtml, "<" & "SCRIPT",vbCrLf & "<" & "Script")
		strHtml=Replace(strHtml, "</" & "SCRIPT>", "</" & "Script>")
		strHtml=Replace(strHtml, "&amp;","&")
		arrContent = Split(strHtml,vbCrLf)
		For i = 0 To UBound(arrContent)
			Numtemp=false
			if Instr(arrContent(i),"<table")>0 then
				Numtemp=True
				if Strtemp<>"<table" and Strtemp <>"</table>" then
				  Num=Num+2
				End if 
				Strtemp="<table"
			elseif Instr(arrContent(i),"<tr")>0 then
				Numtemp=True
				if Strtemp<>"<tr" and Strtemp<>"</tr>" then
				  Num=Num+2
				End if 
				Strtemp="<tr"
			elseif Instr(arrContent(i),"<td")>0 then
				Numtemp=True
				if Strtemp<>"<td" and Strtemp<>"</td>" then
				  Num=Num+2
				End if 
				Strtemp="<td"
			elseif Instr(arrContent(i),"</table>")>0 then
				Numtemp=True
				if Strtemp<>"</table>" and Strtemp<>"<table" then
				  Num=Num-2
				End if 
				Strtemp="</table>"
			elseif Instr(arrContent(i),"</tr>")>0 then
				Numtemp=True
				if Strtemp<>"</tr>" and Strtemp<>"<tr" then
				  Num=Num-2
				End if 
				Strtemp="</tr>"
			elseif Instr(arrContent(i),"</td>")>0 then
				Numtemp=True
				if Strtemp<>"</td>" and Strtemp<>"<td" then
				  Num=Num-2
				End if 
				Strtemp="</td>"
			elseif Instr(arrContent(i),"<" & "!--")>0 then
				Numtemp=True
			End if
			if Num< 0 then Num = 0
			if trim(arrContent(i))<>"" then
				if i=0 then
					strHtml2= string(Num," ") & arrContent(i) 
				elseif Numtemp=True then
					strHtml2= strHtml2 & vbCrLf & string(Num," ") & arrContent(i) 
				else
					strHtml2= strHtml2 & vbCrLf & arrContent(i) 
				end if
			end if
		Next
		Resumeblank=strHtml2
	End function
</script>
</body>
</html>