<%
Response.Buffer = True
Response.ContentType = "application/x-javascript"
BannerZone = Request("BannerZone")
If BannerZone = "" Then BannerZone = Request("BZ")
	
'***********************************************************************
'   System     :   ASPBanner Unlimited Version 8.0						
'   Author     :   Christopher Williams of CJWSoft www.CJWSoft.com		
'																		
'                         COPYRIGHT NOTICE								
'																		
'   See attached Software License Agreement								
'																		
'   (c) Copyright 2000 - 2005 by CJWSoft.			 All rights reserved
'***********************************************************************
%>
<!--#INCLUDE FILE="config_inc.asp"-->
<%
If IsNull(Application("BannerRedirectURL")) Then
	Response.Write(vbCrLf & "jscode='<font face=arial size=2 color=red>Banner Redirect URL has not been specified in the ASPBanner settings.<br>If this is a new installation the settings must be saved at least once because even though they are pre-populated they must be saved.</font>'")
	Response.End
End If
	
If Application("BannerDatabaseType") <> "MSACCESS" or Application("BannerDatabaseType") <> "SQL" or Application("BannerDatabaseType") <> "MYSQL"  Then
	' Do nothing
Else
	Response.Write(vbCrLf & "<font face=arial size=2 color=red>BannerDatabaseType variable has not been specified correctly.<br>It must be set per the instructions.</font>")
	Response.End
End If
	
	
' Checks the time the banner data was last updated and basically updates it if an hour or more has passed
If Cint(Application("BannersLastUpdated")) <> Cint(Hour(time)) Then %>
<!--#INCLUDE FILE="appinfo_inc.asp"-->
<%
End If
	
	
' Set all variables to empty as this code may be included multiple times in the same page
BannerCycleData = ""
Banner_Array = ""
CurrentBanner = ""
CycleBannerTotal = ""
NewCycleList = ""
NewCycleListArray = ""
Banner_Array2 = ""
Stop_Processing = ""
BR = ""
BN = ""
IIS = ""
IW = ""
IH = ""
IB = ""
IAT = ""
IA = ""
BT = ""
JSCRIPT_Code = ""
	
	If Application("BannerZone" & BannerZone & "_Cycle") <> "" Then
		BannerCycleData = Split(Application("BannerZone" & BannerZone & "_Cycle"),"|")
		Banner_Array = Split(Application("BannerZone" & BannerZone),"|")
		Keep_Processing = True
	Else
		Keep_Processing = False
	End If
	
	
If Keep_Processing = True Then
	
	CurrentBanner = BannerCycleData(0)
	CycleBannerTotal = BannerCycleData(1)
	NewCycleList =  BannerCycleData(2)
	
	NewCycleListArray = Split(NewCycleList,",")
	Banner_Array2 = Split(Banner_Array(NewCycleListArray(CurrentBanner)),vbTab)
	
	
	
	
If Application("DelayedStatsWriting") = "True" Then
	
	Set ObjTempBannerStatsFile = Server.CreateObject("Scripting.FileSystemObject")
	
	If TempStatsLocation = "" Then
		BannerStatsFile = Server.MapPath ("/data/tempstats/" & InternationalDate(DATE) & "_" & Trim(Banner_Array2(0)) & "_impressions.tmp")
	Else
		BannerStatsFile = (TempStatsLocation & "/" & InternationalDate(DATE) & "_" & Trim(Banner_Array2(0)) & "_impressions.tmp")
	End If
	
	Set ReadBannerStatsFile= ObjTempBannerStatsFile.OpenTextFile (BannerStatsFile, 1, True)
	
		If Not ReadBannerStatsFile.AtEndOfStream Then
			BannerImpressions = Trim(ReadBannerStatsFile.ReadLine)
			If BannerImpressions = "" Then BannerImpressions = 0
		Else
			BannerImpressions = 0
		End If
	
	ReadBannerStatsFile.Close
	Set ReadBannerStatsFile = Nothing
	
	BannerImpressions = BannerImpressions + 1
	
	Set WriteBannerStatsFile= ObjTempBannerStatsFile.CreateTextFile (BannerStatsFile, True)
		WriteBannerStatsFile.WriteLine(BannerImpressions)
	WriteBannerStatsFile.Close
	Set WriteBannerStatsFile = Nothing
	
	Set ObjTempBannerStatsFile = Nothing
	
Else
	
		If Application("BannerDatabaseType") = "MYSQL" Then
			
			
			Set ConnBannerSystem = Server.CreateObject("ADODB.Connection")
			ConnBannerSystem.Open Application("BannerConnectionString")
			Set cmdTemp = Server.CreateObject("ADODB.Command")
			Set CmdUpdateStats = Server.CreateObject("ADODB.Recordset")
			cmdTemp.CommandText = "SELECT * FROM Banner_Stats WHERE (Banner_ID = " & Trim(Banner_Array2(0)) & ") AND Banner_Day = '" & InternationalDate(DATE) & "'"
			cmdTemp.CommandType = 1
			Set cmdTemp.ActiveConnection = ConnBannerSystem
			CmdUpdateStats.Open cmdTemp, , 2, 2
				
			If Not CmdUpdateStats.EOF Then
				
				Banner_ImpressionsNew = CmdUpdateStats("Banner_Impressions") + 1
				Set MySQLConn = Server.CreateObject("ADODB.Connection")     
				MySQLConn.Open Application("BannerConnectionString")
				UpdateStatsSQL = "UPDATE Banner_Stats SET Banner_Impressions = '" & Banner_ImpressionsNew &"' WHERE Banner_ID = '" & Trim(Banner_Array2(0)) & "' AND Banner_Day = '" & InternationalDate(DATE) & "';"
				MySQLConn.Execute UpdateStatsSQL
				MySQLConn.Close
				Set MySQLConn = Nothing
				
			Else
				
				Set MySQLConn = Server.CreateObject("ADODB.Connection")     
				MySQLConn.Open Application("BannerConnectionString")
				UpdateStatsSQL = "INSERT INTO Banner_Stats (Stat_ID,Banner_ID,Banner_Day,Banner_Impressions,Banner_Clicks) VALUES ('','" & Trim(Banner_Array2(0)) & "','" & InternationalDate(DATE) & "','1','0');"
				MySQLConn.Execute UpdateStatsSQL
				MySQLConn.Close
				Set MySQLConn = Nothing
				
			End If
			
			ConnBannerSystem.Close
			Set ConnBannerSystem = Nothing
			
			
		Else
			
			
			Set ConnBannerSystem = Server.CreateObject("ADODB.Connection")
			ConnBannerSystem.Open Application("BannerConnectionString")
			Set CmdBannerTemp = Server.CreateObject("ADODB.Command")
			Set CmdUpdateStats = Server.CreateObject("ADODB.Recordset")
			
			If 	Application("BannerDatabaseType") = "SQL" or Application("BannerDatabaseType") = "MYSQL" Then
				CmdBannerTemp.CommandText = "SELECT Banner_Stats.* FROM Banner_Stats WHERE (Banner_ID = " & Trim(Banner_Array2(0)) & ") AND Banner_Day = '" & InternationalDate(DATE) & "'"
			ElseIf Application("BannerDatabaseType") = "MSACCESS" Then
				CmdBannerTemp.CommandText = "SELECT Banner_Stats.* FROM Banner_Stats WHERE (Banner_ID = " & Trim(Banner_Array2(0)) & ") AND Banner_Day = #" & InternationalDate(DATE) & "#"
			End If
			
			CmdBannerTemp.CommandType = 1
			Set CmdBannerTemp.ActiveConnection = ConnBannerSystem
			CmdUpdateStats.Open CmdBannerTemp, , 2, 2
			
			If Not CmdUpdateStats.EOF Then
				CmdUpdateStats.Fields("Banner_Impressions") = (CmdUpdateStats("Banner_Impressions") + 1)
				CmdUpdateStats.Update
				CmdUpdateStats.Close
				Set CmdUpdateStats = Nothing
			Else
				Set CmdBannerTemp = Server.CreateObject("ADODB.Command")
				Set CmdUpdateStats = Server.CreateObject("ADODB.Recordset")
				CmdBannerTemp.CommandText = "SELECT Banner_Stats.* FROM Banner_Stats"
				Set CmdBannerTemp.ActiveConnection = ConnBannerSystem
				CmdUpdateStats.Open CmdBannerTemp, , 2, 2
			
				CmdUpdateStats.AddNew
				CmdUpdateStats.Fields("Banner_ID") = Trim(Banner_Array2(0))
				CmdUpdateStats.Fields("Banner_Day") = InternationalDate(DATE)
				CmdUpdateStats.Fields("Banner_Impressions") = 1
				CmdUpdateStats.Fields("Banner_Clicks") = 0
				CmdUpdateStats.Update
				CmdUpdateStats.Close
				Set CmdUpdateStats = Nothing
			End If
			
			ConnBannerSystem.Close
			Set ConnBannerSystem = Nothing
			
			
		End If
	
End If
	
	
	
	
	JSCRIPT_Code = vbCrLf
	
	
' Banner Type Image Process Like So
If (Banner_Array2(9)) = "Image" Then
	
	If ISNumeric(Banner_Array2(0)) = True Then
		BR = Application("BannerRedirectURL") 
		BN = Trim(Banner_Array2(0)) 
		IIS = Trim(Banner_Array2(1))
		IW = Trim(Banner_Array2(2)) 
		IH = Trim(Banner_Array2(3)) 
		IB = Trim(Banner_Array2(4)) 
		IAT = Trim(Banner_Array2(5))
		IA = Trim(Banner_Array2(6))  
		BT = Trim(Banner_Array2(7)) 
		JSCRIPT_Code = JSCRIPT_Code & "<a href=" & BR & "?Banner_ID=" & BN & " "
		JSCRIPT_Code = JSCRIPT_Code & "target=" & BT & ">"
		JSCRIPT_Code = JSCRIPT_Code & "<img border=" & IB & " "
		JSCRIPT_Code = JSCRIPT_Code & "src=" & IIS & " "
		JSCRIPT_Code = JSCRIPT_Code & "alt=" & Replace(IA," ","&nbsp;") & " "
		JSCRIPT_Code = JSCRIPT_Code & "width=" & IW & " "
		JSCRIPT_Code = JSCRIPT_Code & "height=" & IH & " ></a>" & " "
	End If
	
	If Trim(Banner_Array2(5)) <> "" Then
		JSCRIPT_Code = JSCRIPT_Code & "<br><font size=2 face=Arial>"
		JSCRIPT_Code = JSCRIPT_Code & "<a href=" & BR & "?Banner_ID=" & BN & " " 
		JSCRIPT_Code = JSCRIPT_Code & "target=" & BT & ">"
		JSCRIPT_Code = JSCRIPT_Code & " " & IAT & "</a></font>"
	End If
	
End If
	
	
' Banner Type Code Process Like So
If (Banner_Array2(9)) = "Code" Then
	JSCRIPT_Code = (Replace(base64_decode(Banner_Array2(8)),"<-random->",RndStr(8,"123456789abcdefghijkmnpqrstuvwxyz")))
	'JSCRIPT_Code = (base64_decode(Banner_Array2(8)))
End If
	
JSCRIPT_Code = Replace( JSCRIPT_Code, "'", "\'" )
JSCRIPT_Code = "jscode='" & JSCRIPT_Code & "'"
Response.Write Replace(JSCRIPT_Code, vbCrLf, " ")
	
	
	If Cint(CurrentBanner) = Cint(CycleBannerTotal - 1) Then
		CurrentBanner = 0
	Else
		CurrentBanner = CurrentBanner + 1
	End If
	
	Application.Lock
		Application("BannerZone" & BannerZone & "_Cycle") = CurrentBanner & "|" & CycleBannerTotal & "|" & NewCycleList
	Application.Unlock
	
' This is the End If for the (Keep_Processing = True) line
End If
%>