<!--#include file="QvodCms_Conn.asp"-->
<!--#include file="QvodCms_Check.asp"-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<TITLE>广告管理页面</TITLE>
<link href="skins/css/JaJa_main.css" rel="stylesheet" type="text/css" />
<script language = "javaScript" src = "QvodCms_All.js" type="text/javascript"></script>
<script language = "javaScript" src = "../js/QvodCms.Jquery.js" type="text/javascript"></script>
<script type="text/javascript">
//
function code(id){
	$("#code").slideDown("slow",function (){
		//代码调用
		var content
		content="<";
		content+="script language='javascript' src='@InstallDir@Ad/"+id+".js'></";
		content+="script>";
		$("input").val(content);
		//预览广告
		//$("#skinad").html();
		$("#skinad").html("<iframe src='?action=code&id="+id+"' scrolling='auto' width='100%' height='100%'></iframe>");
	});
	$("button").click(function(){
	    //复制
	    copyToClipboard($("input").val())
		//消失
	    $("#code").fadeOut("slow"); 
	});	
//	
	function copyToClipboard(meintext) {
		if (window.clipboardData) {
			window.clipboardData.setData("Text", meintext);
		}
		else if (window.netscape) { 
			netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
			var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
			if (!clip) return;
			var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
			if (!trans) return;		
			trans.addDataFlavor('text/unicode');
			var str = new Object();
			var len = new Object();		
			var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);		
			var copytext=meintext;
			str.data=copytext;
			trans.setTransferData("text/unicode",str,copytext.length*2);
			var clipid=Components.interfaces.nsIClipboard;
			if (!clip) return false;
			clip.setData(trans,null,clipid.kGlobalClipboard);
		};
		return false;
	}	
}
</script>
</head>
<body> 
<%
Dim Action,Adid,Reurl,Ad_Name,Ad_Intro
Action=Trim(Request.QueryString("action"))
Page=int(request.QueryString ("page"))
If page<=0 Or page="" Then page=1'初始化分页参数
Reurl=Request.ServerVariables("Http_Referer")'来源地址
if Reurl="" then Reurl="QvodCms_Ad.asp"
Ad_id=Clng(Request.QueryString("id"))
Ad_Name=Trim(Request.Form("Ad_Name"))
Ad_Intro=Trim(Request.Form("Ad_Intro"))
Call OpenConn()
If action="add" then
    Call Ad_add()
Elseif action="edit" Then
    Call Ad_edit()
Elseif action="del" then
    Call Ad_del()
Elseif action="delall" Then
    Call Ad_delall()	
Elseif action="add_save" Then
    Call add_save()
Elseif action="edit_save" Then
    Call edit_save()
Elseif action="create" Then
    Call createjs()	
Elseif action="createall" Then
    Call createjsall()		
Elseif action="code" Then
    Call codejs()			
Else 
    Call Ad_main() 
End If

Sub add_save()
    If Ad_Name="" Or Ad_Intro="" Then 
	    Call QvodCms_Err("必要参数不能为空！")
	    Response.End
    End If
	Set rs=Server.CreateObject("adodb.recordset")
	sql="select  * from qvodcms_ad"
	rs.Open sql, Conn, 1, 3
    rs.AddNew
	rs("Ad_id")=Max_id("ad")
	rs("Ad_name")=Ad_name
	rs("Ad_Intro")=Ad_Intro
	rs.update
	rs.Close
	Set rs=nothing 	
    'Conn.Execute("insert into JaJa_ad (Ad_id,Ad_name,Ad_Intro) values ('"&Max_id("ad")&"','"&Ad_name&"','"&Ad_Intro&"')")
	Response.redirect("?")
End Sub

Sub edit_save()
    If Ad_Name="" Or Ad_Intro="" Or Ad_id=0 Then 
	    Call QvodCms_Err("必要参数不能为空！")
	    Response.End
    End If
	Set rs=Server.CreateObject("adodb.recordset")
	sql="select  * from qvodcms_ad where Ad_id="&Ad_id&""
	rs.Open sql, Conn, 1, 3
	If Not (rs.bof And rs.eof) Then
	    rs("Ad_name")=Ad_name
		rs("Ad_Intro")=Ad_Intro
		rs.update
	End If
	rs.Close
	Set rs=nothing 
    'Conn.Execute(" update JaJa_ad set Ad_name='"&Ad_name&"',Ad_Intro='"&Ad_Intro&"' where Ad_id="&Ad_id&" ")
	Response.redirect("?")
End Sub

Sub Ad_del()
    Conn.Execute(" delete from qvodcms_ad where Ad_id="&Ad_id&" ")
	'删除广告文件
	If IsExists("../Ad/"&Ad_id&".js")=True Then DelHtml("../Ad/"&Ad_id&".js")
	Response.redirect("?")
End Sub

Sub Ad_delall()
    Conn.Execute(" delete from qvodcms_ad ")
	'删除广告文件
	Set rs=Conn.Execute("select Ad_id,Ad_name,Ad_intro from qvodcms_ad order by ad_id desc")
	If not(rs.bof and rs.eof) Then
	    Do while not rs.eof
	        If IsExists("../Ad/"&rs("ad_id")&".js")=True Then DelHtml("../Ad/"&rs("ad_id")&".js")
		rs.movenext
		Loop
	End If
	Set rs=nothing
	Response.redirect("?")
End Sub

Sub CreateJs()
    Set rs=Conn.Execute("select Ad_id,Ad_name,Ad_intro from qvodcms_ad where ad_id="&Ad_id&"")
	If Not(rs.bof and rs.eof) Then
		'检测文件夹是否存在如果没有则新建
	    If IsExists("../Ad/")=False Then CreateFolder("../Ad/")
	    Call MakeHtml("../Ad/"&rs("Ad_id")&".js",rs("Ad_intro"))
	Else
	    Call QvodCms_Err("没有数据生成不成功！")
	End If		
	Set rs=nothing
	Response.Redirect(Reurl)
End Sub

Sub CreateJsAll()
    Set rs=Conn.Execute("select Ad_id,Ad_name,Ad_intro from qvodcms_ad order by ad_id desc")
	If Not(rs.bof and rs.eof) Then
		'检测文件夹是否存在如果没有则新建
	    If IsFolder("../Ad/")=False Then CreateFolder("../Ad/")
		Do while not rs.eof
		    Call MakeHtml("../Ad/"&rs("Ad_id")&".js",rs("Ad_intro"))
		rs.movenext
		Loop		    
	End If	
	Set rs=nothing
	Response.Redirect("?")
End Sub

Sub codeJs()
    Response.Write("<div style='text-align:center; background:#FFF;'><br><a href='javascript:this.location.reload();'>刷新广告显示</a><br><br><script language='javascript' src='../Ad/"&Ad_id&".js'></script></div>")
End Sub
%> 
<!--主页面 --> 
<%Sub Ad_main()%> 
<div id="code" style="display:none">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="text-align:center;">
  <tr>
    <th colspan=6 style="text-align:center;"><strong>广告代码预览</strong></th>
  </tr> 
  <tr >
    <td class="td1"><div id="skinad" style="background:#FFFFFF"></div></td>
    </tr>  
  <tr >
    <td class="td1">广告调用代码：<input name="code" type="text" style="width:600px;" value="<script language='javascript' src='{$InstallDir}Ad/<%=Ad_id%>.js'></script>"><button style="border:none; height:21px; margin-left:10px;" onClick="alert('复制成功/请在需要的模板处粘贴！');">复制代码</button></td>
    </tr>
</table>
</div>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <th colspan=7 style="text-align:center;"><strong>网站广告管理系统</strong></th>
  </tr>
  <tr style="text-align:center;">
    <td width="42" class="td2">ID</td>
    <td width="250" class="td2">广告位描述</td>
    <td width="250" class="td2">相关操作</td>
    <td class="td2" style="text-align:left;">生成状态</td>
  </tr> 
<%
Set rs = Server.CreateObject("adodb.recordset")
sql = "select * from qvodcms_ad order by Ad_id desc"
rs.Open sql, Conn, 1, 1
If Not (rs.bof And rs.eof) Then
	i=0
	rs.pagesize=15'每页数据
	rs.AbsolutePage=page
	m=rs.recordcount     '总记录
	n=rs.PageCount       '总页数
	z=rs.pagesize        '每页数量
	do while not rs.eof and i<rs.pagesize
%>
  <tr style="text-align:center;">
    <td class="td1"><%=rs("Ad_id")%>.</td>
    <td class="td1"><%=rs("Ad_name")%></td>
    <td class="td1"><a href="?action=edit&id=<%=rs("Ad_id")%>">编辑</a>　<a href="?action=del&id=<%=rs("Ad_id")%>" onClick="return confirm('请确定要删除吗?');">删除</a>　<a href="?action=create&id=<%=rs("Ad_id")%>">生成JS文件</a>　<a href="#" onClick="code('<%=rs("Ad_id")%>');">调用代码</a></td>
    <td class="td1" style="text-align:left;">
	<%
	If IsExists("../Ad/"&rs("Ad_id")&".js")=True Then
	Response.Write("已生成")
	Else
	Response.Write("<font color='red'>未生成</font>")
	End If
	%>
	</td>
  </tr>
<%
    i=i+1
    rs.movenext  
    loop 
Else
%>
  <tr style="text-align:center;">
    <td colspan="4" class="td1">暂时没有作何广告,请先添加！</td>
  </tr>
<%End If%>
  <tr style="text-align:center;">
    <td colspan="4" class="td1"><%=ShowPage("qvodcms_ad.asp", m, z, page, True, True, "条", True)%></td>
  </tr>
<%  
rs.Close
set rs=nothing
Call CloseConn()'关闭数据库连接
%>
<%End Sub%> 
</table>

<!--新加友情链接 -->
<%Sub Ad_add()%> 
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <form method="post" action="?action=add_save" name="adsave">
  <tr>
    <th colspan=9 style="text-align:center;"><strong>添加广告代码</strong></th>
  </tr> 
  <tr >
    <td colspan="3" class="td1">广告描述：</td>
    <td width="92%" class="td1"><input type="text" name="Ad_Name" style="width:100%;"></td>
  </tr>
  <tr >
    <td colspan="3" class="td1">广告联盟代码：</td>
    <td class="td1"><textarea id="Ad_html" style="width:100%; height:200px;" onpropertychange="change()"></textarea></td>
  </tr> 
  <tr >
    <td colspan="3" class="td1">转化后JS代码：</td>
    <td class="td1"><textarea name="Ad_Intro" id="Ad_Intro" style="width:100%; height:200px;"></textarea></td>
  </tr>   
  <tr >
    <td colspan="4" class="td1" style="text-align:center;"><input type="submit" name="Submit" value="提交保存"> 
    提示：将广告联盟的代码直接粘贴保存即可！</td>
  </tr>
<script>
function change(){
    adsave.Ad_Intro.value="document.writeln(\""+adsave.Ad_html.value.replace(/\\/g,"\\\\").replace(/\//g,"\\/").replace(/\'/g,"\\\'").replace(/\"/g,"\\\"").split('\r\n').join("\");\ndocument.writeln(\"")+"\")"
} 
</script>
  </form>
</table>
<%End Sub%>

<!--编辑友情链接 -->
<%
Sub Ad_edit() 
Set rs = Server.CreateObject("adodb.recordset")
sql = "select * from qvodcms_ad where Ad_id="&Clng(Ad_id)&""
rs.Open sql, Conn, 1, 3
    If not(rs.bof And rs.eof) Then
%>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <form method="post" action="?action=edit_save&id=<%=Ad_id%>">
  <tr>
    <th colspan=9 style="text-align:center;"><strong>编辑广告代码</strong></th>
  </tr> 
  <tr >
    <td colspan="3" class="td1">广告描述：</td>
    <td width="92%" class="td1"><input type="text" name="Ad_Name" style="width:100%;" value="<%=rs("Ad_name")%>"></td>
  </tr>
  <tr >
    <td colspan="3" class="td1">JS代码：</td>
    <td class="td1"><textarea name="Ad_Intro" style="width:100%; height:200px;"><%=rs("Ad_intro")%></textarea></td>
  </tr>
  <tr >
    <td colspan="4" class="td1" style="text-align:center;"><input type="submit" name="Submit" value="提交保存"> 提示：请使用转换工具将广告联盟的代码转换成JS代码后再提交保存！</td>
  </tr>
  </form>
  <tr>
    <th colspan=9 style="text-align:center;"><strong>广告代码转换工具</strong></th>
  </tr> 
<script>
function change(){
    Js.value="document.writeln(\""+html.value.replace(/\\/g,"\\\\").replace(/\//g,"\\/").replace(/\'/g,"\\\'").replace(/\"/g,"\\\"").split('\r\n').join("\");\ndocument.writeln(\"")+"\")"
} 
</script>    
  <tr >
    <td colspan="3" class="td1">广告联盟代码：</td>
    <td class="td1"><textarea id="html" style="width:100%; height:200px;" onpropertychange="change();"></textarea></td>
  </tr>
   <tr >
    <td colspan="3" class="td1">转化后JS代码：</td>
    <td class="td1"><textarea id="Js" style="width:100%; height:200px;" ondblclick='this.select();Js.createTextRange().execCommand("Copy")' title="双击复制"></textarea></td>
  </tr>   
</table> 
<%
    Else
        Response.Write("没有友链！")
    End If	
End Sub
%> 
</body>
</html>