
<script>

	if (!String.prototype.format) {
		String.prototype.format = function () {
			var args = arguments;
			return this.replace(/{(\d+)}/g, function (match, number) {
				return typeof args[number] != 'undefined'
					? args[number]
					: match;
			});
		};
	}

	function translateWeekDayCodeToWeekDays(weekDayCode) {
		switch (weekDayCode) {
			case 1: return "SUN";
			case 2: return "MON";
			case 4: return "TUE";
			case 8: return "WED";
			case 16: return "THU";
			case 32: return "FRI";
			case 64: return "SAT";
			case 62: return "MON,TUE,WED,THU,FRI";
			case 65: return "SAT,SUN";
			case 127: return "MON,TUE,WED,THU,FRI,SAT,SUN";
			default: return "*;"
		}
	}

	function generateCronExpression(baseName) {
		var controls = ASPxClientControl.GetControlCollection();
		var radioButtonList = ASPxClientRadioButtonList.Cast(controls.GetByName(baseName + "_AptRecCtl_TypeEdt"));
		var checkRecurrenceType = radioButtonList.GetValue();
		var timeEditDate = ASPxClientTimeEdit.Cast(devTimeEditCron).GetDate();
		var time = timeEditDate.getSeconds() + " " + timeEditDate.getMinutes() + " " + timeEditDate.getHours();
		var result = "* * * * * *";
		switch (checkRecurrenceType) {
			case 0:
				var repeatNDaysCheckBox = ASPxClientCheckBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Daily_RbDay"));
				var repeatNDaysSpinEdit = ASPxClientSpinEdit.Cast(controls.GetByName(baseName + "_AptRecCtl_Daily_SpnDayCnt"));
				var repeatEveryDayCheckBox = ASPxClientCheckBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Daily_RbEvrDay"));
				if (repeatEveryDayCheckBox.GetChecked()) {
					result = "{0} ? * MON-FRI *".format(time);
				} else {
					result = "{0} 1/{1} * ? *".format(time, repeatNDaysSpinEdit.GetValue());
				}
				break;
			case 1:
				var repeatNWeeksSpinEdit = ASPxClientSpinEdit.Cast(controls.GetByName(baseName + "_AptRecCtl_Weekly_SpnWeekCnt"));
				var repeatWeekDaysMondayCheckBox = ASPxClientCheckBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Weekly_WeekDaysEdt_Monday0"));
				var repeatWeekDaysTuesdayCheckBox = ASPxClientCheckBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Weekly_WeekDaysEdt_Tuesday1"));
				var repeatWeekDaysWednesdayCheckBox = ASPxClientCheckBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Weekly_WeekDaysEdt_Wednesday2"));
				var repeatWeekDaysThursdayCheckBox = ASPxClientCheckBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Weekly_WeekDaysEdt_Thursday3"));
				var repeatWeekDaysFridayCheckBox = ASPxClientCheckBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Weekly_WeekDaysEdt_Friday4"));
				var repeatWeekDaysSaturdayCheckBox = ASPxClientCheckBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Weekly_WeekDaysEdt_Saturday5"));
				var repeatWeekDaysSundayCheckBox = ASPxClientCheckBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Weekly_WeekDaysEdt_Sunday6"));
				var weekValue = repeatNWeeksSpinEdit.GetValue();
				if (weekValue == 1) {
					var weekDays = [];
					if (repeatWeekDaysMondayCheckBox.GetChecked()) { weekDays.push("MON"); }
					if (repeatWeekDaysTuesdayCheckBox.GetChecked()) { weekDays.push("TUE"); }
					if (repeatWeekDaysWednesdayCheckBox.GetChecked()) { weekDays.push("WED"); }
					if (repeatWeekDaysThursdayCheckBox.GetChecked()) { weekDays.push("THU"); }
					if (repeatWeekDaysFridayCheckBox.GetChecked()) { weekDays.push("FRI"); }
					if (repeatWeekDaysSaturdayCheckBox.GetChecked()) { weekDays.push("SAT"); }
					if (repeatWeekDaysSundayCheckBox.GetChecked()) { weekDays.push("SUN"); }
					result = "{0} ? * {1} *".format(time, weekDays.join(","));
				} else {
					result = "{0} 1/{1} * ? *".format(time, weekValue * 7);
				}
				break;
			case 2:
				var repeatNDayOfMonthRadioButton = ASPxClientRadioButton.Cast(controls.GetByName(baseName + "_AptRecCtl_Monthly_RbDay"));
				var repeatNDayOfMonthSpinEdit = ASPxClientSpinEdit.Cast(controls.GetByName(baseName + "_AptRecCtl_Monthly_SpnMnthDay"));
				var repeatNMonthsForDaySpinEdit = ASPxClientSpinEdit.Cast(controls.GetByName(baseName + "_AptRecCtl_Monthly_SpnDayMnthCnt"));

				var repeatNWeeksOfMonthRadioButton = ASPxClientRadioButton.Cast(controls.GetByName(baseName + "_AptRecCtl_Monthly_RbWeekDays"));
				var repeatNWeekDayNumberOfMonthComboBox = ASPxClientComboBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Monthly_WmeWeekOfMnth"));
				var repeatNWeekDayOfMonthComboBox = ASPxClientComboBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Monthly_WdeWeekDays"));
				var repeatNMonthsForWeekSpinEdit = ASPxClientSpinEdit.Cast(controls.GetByName(baseName + "_AptRecCtl_Monthly_SpinWeekDaysMnthCnt"));
				var repeatNWeekDayNumberOfMonth = repeatNWeekDayNumberOfMonthComboBox.GetValue();
				var repeatNWeekDayOfMonth = repeatNWeekDayOfMonthComboBox.GetValue();
				var repeatNMonthsForWeek = repeatNMonthsForWeekSpinEdit.GetValue();
				if (repeatNDayOfMonthRadioButton.GetChecked()) {
					result = "{0} {1} 1/{2} ? *".format(time, repeatNDayOfMonthSpinEdit.GetValue(), repeatNMonthsForDaySpinEdit.GetValue());
				} else {
					result = "{0} ? 1/{1} {2}#{3} *".format(time, repeatNMonthsForWeek,
						translateWeekDayCodeToWeekDays(repeatNWeekDayOfMonth),
						(repeatNWeekDayNumberOfMonth == 5 ? 4 : repeatNWeekDayNumberOfMonth));
				}
				break;
			case 3:
				var repeatNDayOfMonthByYearRadioButton = ASPxClientRadioButton.Cast(controls.GetByName(baseName + "_AptRecCtl_Yearly_RbDay"));
				var repeatNMonthByYearComboBox = ASPxClientComboBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Yearly_MeDayMnth"));
				var repeatNDayOfMonthByYearSpinEdit = ASPxClientSpinEdit.Cast(controls.GetByName(baseName + "_AptRecCtl_Yearly_SpnDayNo"));

				var repeatNWeekDayOfMonthByYearRadioButton = ASPxClientRadioButton.Cast(controls.GetByName(baseName + "_AptRecCtl_Yearly_RbWeekOfMnth"));
				var repeatNWeekDayNumberOfMonthByYearComboBox = ASPxClientComboBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Yearly_WmeWeekOfMnth"));
				var repeatNWeekDayOfMonthByYearComboBox = ASPxClientComboBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Yearly_WdeWeekDays"));
				var repeatNMonthOfYearComboBox = ASPxClientComboBox.Cast(controls.GetByName(baseName + "_AptRecCtl_Yearly_MeWeekDaysMnth"));
				var repeatNWeekDayNumberOfMonthByYear = repeatNWeekDayNumberOfMonthByYearComboBox.GetValue();
				var repeatNWeekDayOfMonthByYear = repeatNWeekDayOfMonthByYearComboBox.GetValue();
				if (repeatNDayOfMonthByYearRadioButton.GetChecked()) {
					result = "{0} {1} {2} ? *".format(time, repeatNDayOfMonthByYearSpinEdit.GetValue(), repeatNMonthByYearComboBox.GetValue());
				} else {
					result = "{0} ? {1} {2}#{3} *".format(time, repeatNMonthOfYearComboBox.GetValue(),
						translateWeekDayCodeToWeekDays(repeatNWeekDayOfMonthByYear),
						(repeatNWeekDayNumberOfMonthByYear == 5 ? 4 : repeatNWeekDayNumberOfMonthByYear))
				}
				break;
		}
		return result;
	}

</script>

<style>
	.recurrenceRange {
		display: none !important;
	}
</style>

@Html.DevExpress().TimeEdit(t =>
{
	t.Name = "devTimeEditCron";
	t.DateTime = new DateTime(2000, 1, 1, 6, 0, 0);
	t.Properties.DisplayFormatString = "HH:mm \"Uhr\"";
}).GetHtml()

@Html.DevExpress().AppointmentRecurrenceForm(r =>
{
	r.Name = "devRecurrenceFormCron";
	r.IsRecurring = true;
	r.PreRender = (sender, e) =>
	{
		var control = sender as DevExpress.Web.ASPxScheduler.Controls.AppointmentRecurrenceForm;
		var controls = GreenTree.Nachtragsmanagement.Web.Extensions.ControlHelper.GetAllSubControlsOfTypes(
			control,
			typeof(ASPxCheckBox),
			typeof(DevExpress.Web.ASPxScheduler.Controls.RecurrenceRangeControl));

		foreach (var subControl in controls)
		{
			if (subControl.ID == "ChkRecurrence")
				((ASPxEdit)subControl).ClientVisible = false;

			if (subControl.ID == "RangeCtl")
				((DevExpress.Web.ASPxScheduler.Controls.RecurrenceRangeControl)subControl).MainDiv.CssClass = "recurrenceRange";
		}
	};
}).GetHtml()