<%
Function GetDaysInMonth(iMonth, iYear)
	Select Case iMonth
		Case 1, 3, 5, 7, 8, 10, 12
			GetDaysInMonth = 31
		Case 4, 6, 9, 11
			GetDaysInMonth = 30
		Case 2
			If IsDate("February 29, " & iYear) Then
				GetDaysInMonth = 29
			Else
				GetDaysInMonth = 28
			End If
	End Select
End Function

Function GetWeekdayMonthStartsOn(iMonth, iYear)
	GetWeekdayMonthStartsOn = WeekDay(CDate(iMonth & "/1/" & iYear))
End Function

Function SubtractOneMonth(dDate)
Dim iDay, iMonth, iYear	
	iDay = Day(dDate)
	iMonth = Month(dDate)
	iYear = Year(dDate)
	If iMonth = 1 Then
		iMonth = 12
		iYear = iYear - 1
	Else
		iMonth = iMonth - 1
	End If
	If iDay > GetDaysInMonth(iMonth, iYear) Then iDay = GetDaysInMonth(iMonth, iYear)
	SubtractOneMonth = CDate(iMonth & "-" & iDay & "-" & iYear)
End Function

Function AddOneMonth(dDate)
	Dim iDay, iMonth, iYear	
	iDay = Day(dDate)
	iMonth = Month(dDate)
	iYear = Year(dDate)
	If iMonth = 12 Then
		iMonth = 1
		iYear = iYear + 1
	Else
		iMonth = iMonth + 1
	End If
	If iDay > GetDaysInMonth(iMonth, iYear) Then iDay = GetDaysInMonth(iMonth, iYear)
	AddOneMonth = CDate(iMonth & "-" & iDay & "-" & iYear)
End Function

Dim dDate
Dim iDIM
Dim iDOW
Dim iCurrent
Dim iPosition
If IsDate(Request.QueryString("date")) Then
	dDate = CDate(Request.QueryString("date"))
Else
	If IsDate(Request.QueryString("month") & "-" & Request.QueryString("day") & "-" & Request.QueryString("year")) Then
		dDate = CDate(Request.QueryString("month") & "-" & Request.QueryString("day") & "-" & Request.QueryString("year"))
	Else
		dDate = Date()
	End If
End If
iDIM = GetDaysInMonth(Month(dDate), Year(dDate))
iDOW = GetWeekdayMonthStartsOn(Month(dDate), Year(dDate))

set objRecordset = Server.CreateObject("ADODB.Recordset")
objRecordset.ActiveConnection = MM_ConnIntra_STRING
objRecordset.Source = "SELECT * FROM calendar WHERE Month = " & Month(dDate) & " AND Year = " & Year(dDate) & " ORDER BY Day ASC"
objRecordset.CursorType = 0
objRecordset.CursorLocation = 2
objRecordset.LockType = 3
objRecordset.Open()
objRecordset_numRows = 0

dim arrMonth(6,5,3)
g = 0
h = 0
for g = 0 to 5
	for h = 0 to 6
		if g = 0 AND h = (iDOW-1) then
			arrMonth(h,g,0) = 1
		end if
		if h > 0 then
			if arrMonth(h-1,g,0) <> "" and arrMonth(h-1,g,0) < iDIM then
				arrMonth(h,g,0) = arrMonth(h-1,g,0) + 1
			end if
		end if
		if h = 6 AND arrMonth(h,g,0) <> "" AND arrMonth(h,g,0) < iDIM then
			arrMonth(0,g+1,0) = arrMonth(h,g,0) + 1
		end if
	next
	h = 0
next
h = 0
g = 0
do while not objRecordset.EOF
	for g = 0 to 5
		for h = 0 to 6
			if not objRecordset.EOF then
			if objRecordset("Day") = arrMonth(h,g,0) then
				currDay = objRecordset("Day")
				arrMonth(h,g,1) = objRecordset("Subject")
				arrMonth(h,g,2) = objRecordset("ID")
				arrMonth(h,g,3) = objRecordset("AddedBy")
				objRecordset.MoveNext
				if not objRecordset.EOF then
				do while currDay = objRecordset("Day")
					currDay = objRecordset("Day")
					arrMonth(h,g,1) = arrMonth(h,g,1) & "nxtitm" & objRecordset("Subject")
					arrMonth(h,g,2) = arrMonth(h,g,2) & "nxtitm" & objRecordset("ID")
					arrMonth(h,g,3) = arrMonth(h,g,3) & "nxtitm" & objRecordset("AddedBy")
					objRecordset.MoveNext
					if objRecordset.EOF then
						exit do
					end if
				loop
				end if
			end if
			end if
		next
		h = 0
	next
	if not objRecordset.EOF then
		objRecordset.MoveNext
	end if
loop
objRecordset.ActiveConnection.Close 
set objRecordset = nothing

strSQL = "SELECT * FROM calendar WHERE Recurring = 'Yes' AND StartDate <= #" & Month(dDate) & "/" & iDIM & "/" & Year(dDate) & " 23:59:59# AND EndDate >= #" & Month(dDate) & "/1/" & Year(dDate) & " 00:00:00#"
set objRecordset = Server.CreateObject("ADODB.Recordset")
objRecordset.ActiveConnection = MM_ConnIntra_STRING
objRecordset.Source = strSQL
objRecordset.CursorType = 0
objRecordset.CursorLocation = 2
objRecordset.LockType = 3
objRecordset.Open()
objRecordset_numRows = 0
h = 0
g = 0
do while not objRecordset.EOF
	Select Case objRecordset("Freq")
		Case "Weekly"
			g = 0
			for g = 0 to 5
				if arrMonth(objRecordset("FreqOpt"),g,0) <> "" then
					if CDate(Month(dDate) & "/" & arrMonth(objRecordset("FreqOpt"),g,0) & "/" & Year(dDate)) >= objRecordset("StartDate") AND CDate(Month(dDate) & "/" & arrMonth(objRecordset("FreqOpt"),g,0) & "/" & Year(dDate)) <= objRecordset("EndDate") then
						if arrMonth(objRecordset("FreqOpt"),g,1) <> "" then
							arrMonth(objRecordset("FreqOpt"),g,1) = arrMonth(objRecordset("FreqOpt"),g,1) & "nxtitm" & objRecordset("Subject")
							arrMonth(objRecordset("FreqOpt"),g,2) = arrMonth(objRecordset("FreqOpt"),g,2) & "nxtitm" & objRecordset("ID")
							arrMonth(objRecordset("FreqOpt"),g,3) = arrMonth(objRecordset("FreqOpt"),g,3) & "nxtitm" & objRecordset("AddedBy")
						else
							arrMonth(objRecordset("FreqOpt"),g,1) = objRecordset("Subject")
							arrMonth(objRecordset("FreqOpt"),g,2) = objRecordset("ID")
							arrMonth(objRecordset("FreqOpt"),g,3) = objRecordset("AddedBy")
						end if
					end if
				end if
			next
			
		Case "Daily"
			g = 0
			h = 0
			for g = 0 to 5
				for h = 0 to 6
				if arrMonth(h,g,0) <> "" then
					if CDate(Month(dDate) & "/" & arrMonth(h,g,0) & "/" & Year(dDate)) >= objRecordset("StartDate") AND CDate(Month(dDate) & "/" & arrMonth(h,g,0) & "/" & Year(dDate)) <= objRecordset("EndDate") then
						strOK = 3
						'if objRecordset("NoWeekends") = "Yes" then
						'	if h = 0 or h = 6 then
						'		strOK = 4
						'	end if
						'end if
						if strOK = 3 then
							if arrMonth(h,g,1) <> "" then
								arrMonth(h,g,1) = arrMonth(h,g,1) & "nxtitm" & objRecordset("Subject")
								arrMonth(h,g,2) = arrMonth(h,g,2) & "nxtitm" & objRecordset("ID")
								arrMonth(h,g,3) = arrMonth(h,g,3) & "nxtitm" & objRecordset("AddedBy")
							else
								arrMonth(h,g,1) = objRecordset("Subject")
								arrMonth(h,g,2) = objRecordset("ID")
								arrMonth(h,g,3) = objRecordset("AddedBy")
							end if
						end if
					end if
				end if
				next
				h = 0
			next
			
		Case "Monthly"
			g = 0
			h = 0
			if len(objRecordset("FreqOpt")) < 3 then
				for g = 0 to 5
					for h = 0 to 6
					if arrMonth(h,g,0) = CInt(objRecordset("FreqOpt")) then
					if CDate(Month(dDate) & "/" & arrMonth(h,g,0) & "/" & Year(dDate)) >= objRecordset("StartDate") AND CDate(Month(dDate) & "/" & arrMonth(h,g,0) & "/" & Year(dDate)) <= objRecordset("EndDate") then
						if arrMonth(h,g,1) <> "" then
							arrMonth(h,g,1) = arrMonth(h,g,1) & "nxtitm" & objRecordset("Subject")
							arrMonth(h,g,2) = arrMonth(h,g,2) & "nxtitm" & objRecordset("ID")
							arrMonth(h,g,3) = arrMonth(h,g,3) & "nxtitm" & objRecordset("AddedBy")
						else
							arrMonth(h,g,1) = objRecordset("Subject")
							arrMonth(h,g,2) = objRecordset("ID")
							arrMonth(h,g,3) = objRecordset("AddedBy")
						end if
					end if
					end if
					next
					h = 0
				next
			else
				wCount = 0
				arrMonthOpt = split(objRecordset("FreqOpt")," ")
				if CInt(arrMonthOpt(0)) = 5 then
					if arrMonth(arrMonthOpt(1),5,0) <> "" then
					if CDate(Month(dDate) & "/" & arrMonth(arrMonthOpt(1),5,0) & "/" & Year(dDate)) >= objRecordset("StartDate") AND CDate(Month(dDate) & "/" & arrMonth(arrMonthOpt(1),5,0) & "/" & Year(dDate)) <= objRecordset("EndDate") then
						if arrMonth(arrMonthOpt(1),5,1) <> "" then
							arrMonth(arrMonthOpt(1),5,1) = arrMonth(arrMonthOpt(1),5,1) & "nxtitm" & objRecordset("Subject")
							arrMonth(arrMonthOpt(1),5,2) = arrMonth(arrMonthOpt(1),5,2) & "nxtitm" & objRecordset("ID")
							arrMonth(arrMonthOpt(1),5,3) = arrMonth(arrMonthOpt(1),5,3) & "nxtitm" & objRecordset("AddedBy")
						else
							arrMonth(arrMonthOpt(1),5,1) = objRecordset("Subject")
							arrMonth(arrMonthOpt(1),5,2) = objRecordset("ID")
							arrMonth(arrMonthOpt(1),5,3) = objRecordset("AddedBy")
						end if
					end if
					else
					if CDate(Month(dDate) & "/" & arrMonth(arrMonthOpt(1),4,0) & "/" & Year(dDate)) >= objRecordset("StartDate") AND CDate(Month(dDate) & "/" & arrMonth(arrMonthOpt(1),4,0) & "/" & Year(dDate)) <= objRecordset("EndDate") then
						if arrMonth(arrMonthOpt(1),4,1) <> "" then
							arrMonth(arrMonthOpt(1),4,1) = arrMonth(arrMonthOpt(1),4,1) & "nxtitm" & objRecordset("Subject")
							arrMonth(arrMonthOpt(1),4,2) = arrMonth(arrMonthOpt(1),4,2) & "nxtitm" & objRecordset("ID")
							arrMonth(arrMonthOpt(1),4,3) = arrMonth(arrMonthOpt(1),4,3) & "nxtitm" & objRecordset("AddedBy")
						else
							arrMonth(arrMonthOpt(1),4,1) = objRecordset("Subject")
							arrMonth(arrMonthOpt(1),4,2) = objRecordset("ID")
							arrMonth(arrMonthOpt(1),4,3) = objRecordset("AddedBy")
						end if
					end if
					end if
				else
					g = 0
					do while wCount < CInt(arrMonthOpt(0))
						if arrMonth(arrMonthOpt(1),g,0) <> "" then
							wCount = wCount + 1
						end if
						g = g + 1
					loop
					g = g - 1
					if CDate(Month(dDate) & "/" & arrMonth(arrMonthOpt(1),g,0) & "/" & Year(dDate)) >= objRecordset("StartDate") AND CDate(Month(dDate) & "/" & arrMonth(arrMonthOpt(1),g,0) & "/" & Year(dDate)) <= objRecordset("EndDate") then
					if arrMonth(arrMonthOpt(1),g,1) <> "" then
						arrMonth(arrMonthOpt(1),g,1) = arrMonth(arrMonthOpt(1),g,1) & "nxtitm" & objRecordset("Subject")
						arrMonth(arrMonthOpt(1),g,2) = arrMonth(arrMonthOpt(1),g,2) & "nxtitm" & objRecordset("ID")
						arrMonth(arrMonthOpt(1),g,3) = arrMonth(arrMonthOpt(1),g,3) & "nxtitm" & objRecordset("AddedBy")
					else
						arrMonth(arrMonthOpt(1),g,1) = objRecordset("Subject")
						arrMonth(arrMonthOpt(1),g,2) = objRecordset("ID")
						arrMonth(arrMonthOpt(1),g,3) = objRecordset("AddedBy")
					end if
					end if
				end if
			end if

		Case "Yearly"
			arrYr = split(objRecordset("FreqOpt")," ")
			if CInt(arrYr(0)) = Month(dDate) then
				g = 0
				h = 0
				for g = 0 to 5
					for h = 0 to 6
						if arrMonth(h,g,0) = CInt(arrYr(1)) then
							if CDate(Month(dDate) & "/" & arrMonth(h,g,0) & "/" & Year(dDate)) >= objRecordset("StartDate") AND CDate(Month(dDate) & "/" & arrMonth(h,g,0) & "/" & Year(dDate)) <= objRecordset("EndDate") then
								if arrMonth(h,g,1) <> "" then
									arrMonth(h,g,1) = arrMonth(h,g,1) & "nxtitm" & objRecordset("Subject")
									arrMonth(h,g,2) = arrMonth(h,g,2) & "nxtitm" & objRecordset("ID")
									arrMonth(h,g,3) = arrMonth(h,g,3) & "nxtitm" & objRecordset("AddedBy")
								else
									arrMonth(h,g,1) = objRecordset("Subject")
									arrMonth(h,g,2) = objRecordset("ID")
									arrMonth(h,g,3) = objRecordset("AddedBy")
								end if
							end if
						end if
					next
					h = 0
				next
			end if
		End Select
	objRecordset.MoveNext			
loop
objRecordset.ActiveConnection.Close 
set objRecordset = nothing
%>
