#include <Date.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include<WindowsConstants.au3>
Opt('GUIOnEventMode', 1)
Opt('MustDeclareVars', 1)

;===============================================================================
;Function Name..: _Easter($iYear [,$bQuiet] [, $sTitle] [,$hParent] [, $sEditStyle] [, $iWidth] [, $iHeight] [, $iLeft] [, $iTop])
;Description....:	errechnet das Datum von Ostern für jedes übergebene Jahr
;Parameter(s)...: 	$iYear		= das Jahr für das Ostern errechnet werden soll
;				  	$bQuiet		= Schalter für "stumme Ausgabe" True es wird nur gerechnet aber nicht angezeigt
;					$sTitle     = Fenstertitel 					(optional)
;					$hParent	= ID des aufrufenden Fensters	(optional, Stabdard = 0)
;								wenn dieser Parameter übergeben wird verhält wird das Anzeigefenster
;								Modal angezeigt, d.h. es ist automatisch im Vordergrund
;								vor dem aufrufenden Fenster (verliert aber leider den Focus, beim Wechsel
;								zu anderer Anwendung und wieder zurück, d.h man muß einmal in das Fenster klicken)
;					$sEditStyle = Edit-Style für GUICtrl		(optional)
;					$iWidth     = Breite des Fensters			(optional, Standard = 400 Pixel)
;					$iHeight    = Höhe des Fensters   			(optional, Standard = 300 Pixel)
;					$iLeft      = Abstand von links   			(optional, Standard = zentriert)
;					$iTop       = Abstand von oben    			(optional, Standard = zentriert)
;Return Value...:	Ostersonntag
;Author.........:  (Auto)Bert Osterberechung Formel auf http://www.nabkal.de/gauss2.html gefunden
;								"		GUI-Darstellung Oscar's (www.autoit.de) _StringDisplay
;										in Anfängerfragen zur GUI(Checkbox,Inputbox und welches Gui Konzept) Beitrag #5
;===============================================================================
Func _Easter($iYear, $bQuiet = False, $sTitle = 'Ostern  ', $hParent = 0, $EditStyle = -1, $iWidth = 250, $iHeight = 200, $iLeft = -1, $iTop = -1)
	Local $iDay, $iMonth, $sHT, $hGui
	Local $iA = Mod($iYear, 19) ;Gausssche Zykluszahl
	;a = Jahr mod 19
	;;ConsoleWrite("A: " & $iYear & " mod 19 =" &$iA & @crlf)	;2009 mod 19 = 14 R
	Local $iB = Mod($iYear, 4)
	;b = Jahr mod 4
	;;ConsoleWrite("B: " & $iYear & " mod 4 =" &$iB & @crlf)	;2009 mod 4 = 1 R
	Local $iC = Mod($iYear, 7)
	;c = Jahr mod 7
	;;ConsoleWrite("C: " & $iYear & " mod 7 =" &$iC & @crlf)				;2009 mod 7 = 0 R
	Local $iH1 = Floor(Int($iYear / 100))
	;H1 = Jahr div 100
	;;ConsoleWrite("H1 " & $iYear & " div 100 = " & $iH1 & @crlf)		;2009 div 100 = 20 R
	Local $iH2 = Floor(Int($iYear / 400))
	;H2 = Jahr div 400
	;;ConsoleWrite("H2 " & $iYear &  " div 400 = " & $iH2 & @crlf)		;2009 div 100 = 5 R
	;Local $iAS = 15 + int($iYear/100) - int($iYear/400) -2	;aequatio solaris
	;Local $iAL = int($iYear/300) -2						;aequtio lunaris
	Local $iN = 4 + $iH1 - $iH2 ; 4+20-5 = 19
	;N = 4 + H1 - H2
	;;ConsoleWrite("N: 4 +" & $iH1 & "-" & $iH2 &"=" & $iN & @crlf)
	Local $iM = 15 + $iH1 - $iH2 - (Floor(Int(8 * $iH1 + 13) / 25)) ;15+20-5-((8*20+13) div 25] = 24
	;M = 15 + H1 - H2- [(8 * H1 + 13) div 25]
	;;ConsoleWrite("M: 15 + "  & $iH1 & "-" & $iH2 & "-[8*"& $iH1 & " + 13) div 25] =" &$iM  & @crlf)
	Local $iD = Mod((19 * $iA + $iM), 30) ;(19*14 +24) mod 30 = 20
	;d = (19 * a + M) mod 30
	;;ConsoleWrite("(D: 19 * " & $iA & " + " & $iM & ") mod 30 = " & $iD & @CRLF)
	Local $iE = Mod(2 * $iB + 4 * $iC + 6 * $iD + $iN, 7)
	;e = (2 * b + 4 * c + 6 * d + N) mod 7
	;;ConsoleWrite("(E: 2*" & $ib & " + 4* " &  $iC & " + 6*" & $iD & " + " & $iN & " Mod 7) = " & $iE & @CRLF)

	Local $iOS = $iD + $iE ;das sind die Anzahl der Tage nach dem 22. März
	;;ConsoleWrite(@CRLF & "OS: 22 + " & $iD & "  + " & $iE & " = " & $iOS & "Tage nach dem 22. März" & @CRLF)
	$iOS = $iOS + 22 ;22 Tage vom März
	If $iOS = 57 Then $iOS = 50
	If ($iD = 28) And ($iE = 6) And ($iA > 10) Then $iOS = 49
	$iOS = $iOS + 59 ;jetzt noch 1. Januar bis 28. Februar hinzuzählen
	If _DateIsLeapYear($iYear) Then $iOS = $iOS + 1 ;im Schaltjahr auch noch den 29. Februar
	If $bQuiet Then Return $iOS
	#cs
		a = Jahr mod 19
		b = Jahr mod 4
		c = Jahr mod 7
		H1 = Jahr div 100
		H2 = Jahr div 400
		N = 4 + H1 - H2
		M = 15 + H1 - H2- [(8 * H1 + 13) div 25]
		d = (19 * a + M) mod 30
		e = (2 * b + 4 * c + 6 * d + N) mod 7z
		Ostern = 22 + d + e                     Tage nach dem 2. März
		wenn: (ostern = 57)                     dann: (ostern = 50)
		wenn: (d = 28) und (e = 6) und (a > 10) dann: (ostern = 49)
		ostern: Ostersonntag als Märzdatum
	#ce
	;ab hier aus Beispiel von Oskar
	If Not IsDeclared('BS_DEFPUSHBUTTON') Then Local Const $BS_DEFPUSHBUTTON = 0x00000001
	If Not IsDeclared('GUI_EVENT_CLOSE') Then Local Const $GUI_EVENT_CLOSE = 0xFFFFFFFD
	If Not IsDeclared('WS_EX_COMPOSITED') Then Local Const $WS_EX_COMPOSITED = 0x02000000
	If Not IsDeclared('WS_MAXIMIZEBOX') Then Local Const $WS_MAXIMIZEBOX = 0x00010000
	If Not IsDeclared('WS_MINIMIZEBOX') Then Local Const $WS_MINIMIZEBOX = 0x00020000
	If Not IsDeclared('WS_SIZEBOX') Then Local Const $WS_SIZEBOX = 0x00040000
	Local $iEventMode = Opt('GUIOnEventMode', 0)
	If $hParent = 0 Then ;Oscar's Beispiel
		$hGui = GUICreate($sTitle, $iWidth, $iHeight, $iLeft, $iTop, BitOR($WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SIZEBOX), $WS_EX_COMPOSITED)
	Else ;damit sich das Fenster wie ein Toolwindow verhält
		$hGui = GUICreate($sTitle, $iWidth, $iHeight, $iLeft, $iTop, BitOR($DS_MODALFRAME + $DS_SETFOREGROUND, $WS_CAPTION + $WS_SYSMENU + $WS_SIZEBOX), $WS_EX_TOOLWINDOW, $hParent)
		GUISetState(@SW_DISABLE, $hParent)
	EndIf
	GUISwitch($hGui)
	GUISetState(@SW_HIDE, $hGui)
	;das war ich wieder
	If $iYear < 1583 Then
		Local $hlblHinweis = GUICtrlCreateLabel("für Jahre vor 1583 keine Berechnung möglich!", 5, 5, $iWidth - 10, $iHeight - 35);, $SS_CENTER)
		GUICtrlSetResizing(-1, $GUI_DOCKHEIGHT + $GUI_DOCKLEFT + $GUI_DOCKTOP + $GUI_DOCKRIGHT)
	Else
		Local $hlstvwHoly = GUICtrlCreateListView("Feiertag                    | Datum             ", 5, 5, $iWidth - 10, $iHeight - 35);, BitOr($EditStyle,$LVS_EX_GRIDLINES))
		GUICtrlSetResizing(-1, $GUI_DOCKBORDERS)
		;GUICtrlSetResizing(-1, 2 + 4 + 32 + 64)
		GUICtrlCreateListViewItem(_getHolyday("Weiberfasnacht:|", $iOS - 52, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Rosenmontag:|", $iOS - 48, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Fasnachtsdienstag:|", $iOS - 47, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Aschwermittwoch:|", $iOS - 46, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Gründonnerstag:|", $iOS - 3, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Karfreitag:|", $iOS - 2, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Ostersamstag:|", $iOS - 1, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Ostersontag:|", $iOS, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Ostermontag:|", $iOS + 1, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Christi Himmelfahrt:|", $iOS + 39, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Pfingstsonntag:|", $iOS + 49, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Pfingstmontag:|", $iOS + 50, $iYear), $hlstvwHoly)
		GUICtrlCreateListViewItem(_getHolyday("Fronleichnam:|", $iOS + 60, $iYear), $hlstvwHoly)
		;Local $hEdit = GUICtrlCreateEdit($sText, 5, 5, $iWidth - 10, $iHeight - 65, $EditStyle)
	EndIf
	Local $hClose = GUICtrlCreateButton('&Schliessen', $iWidth / 2 - 35, $iHeight - 25, 70, 25, $BS_DEFPUSHBUTTON)
	GUICtrlSetResizing(-1, $GUI_DOCKHEIGHT + $GUI_DOCKLEFT + $GUI_DOCKBOTTOM + $GUI_DOCKRIGHT)
	ControlFocus($hGui, '', $hClose)
	GUISetState(@SW_SHOW, $hGui)
	While True
		Switch GUIGetMsg()
			Case $hClose, $GUI_EVENT_CLOSE
				ExitLoop
				;Case $GUI_EVENT_RESTORE
				;GuiSetState(@SW_RESTORE,$hGui)
				;;ConsoleWrite("Bin wieder da")
		EndSwitch
		;Sleep(125)
	WEnd
	;ControlFocus($hGui, '', $hEdit)
	ControlFocus($hGui, '', $hlstvwHoly)
	;Local $sSelectedText = ControlCommand($hGui, '', $hEdit, 'GetSelected', '')
	;If @error Then $sSelectedText = ''
	If $hParent <> 0 Then
		GUISetState(@SW_ENABLE, $hParent)
		GUISetState(@SW_SHOWNORMAL, $hParent)
	EndIf
	GUIDelete($hGui)
	Opt('GUIOnEventMode', $iEventMode)
	Return $iOS
EndFunc   ;==>_Easter

;===============================================================================
;Function Name..: _getHolyday($sHT,$iHDay,$iYear)
;Description....:	ermittelt das Datum des übergegenen x. Tages des Jahres und ertstellt
;					daraus zusammen mit der Bez. einen String zum Eintragen in eine 2 Spaltige Listview
;Parameter(s)...: 	$sHT		= der String mit der Bezeichnung ( Wenn kein Datensparatorzeichen am Schluß
;								  wird einer angehängt
;				  	$bHDay		= der x. Tag des Jahres (1. Januar = 1)
;					$iYear		= Jahresdatum, muß größer 1000 sein, da _DateDays in Month bei klieneren Zahlen keinen Wert zurückliefert
;Return Value...:	String für Eintrag in ListView
;Author.........:  (Auto)Bert
;===============================================================================
Func _getHolyday($sHT, $iHDay, $iYear)
	Local $iDay, $iMonth, $i, $j, $sSep
	$sSep = Opt('GUIDataSeparatorChar')
	If StringRight($sHT, 1) <> $sSep Then $sHT = $sHT & $sSep
	;ConsoleWrite("getHolyDay " & $iHDay & @CRLF)
	For $i = 1 To 12
		$j = _DateDaysInMonth($iYear, $i)
		If $j >= $iHDay Then
			$iDay = $iHDay
			$iMonth = $i
		EndIf
		$iHDay = $iHDay - $j
		If $iHDay <= 0 Then ExitLoop
		;ConsoleWrite($i & " : " & $j & ": getHolyDay " & $sHT & "  " & $iHDay & @CRLF)
	Next
	If $iDay < 10 Then $sHT = $sHT & '0'
	$sHT = $sHT & $iDay
	$sHT = $sHT & "."
	If $iMonth < 10 Then $sHT = $sHT & '0'
	$sHT = $sHT & $iMonth
	$sHT = $sHT & "." & $iYear
	;ConsoleWrite("Return getHolyDay " & $sHT & @CRLF)
	Return $sHT
EndFunc   ;==>_getHolyday
