<%@ include file="/includes.jsp" %>
<%@ taglib prefix="smvc" uri="http://www.springframework.org/tags/form"%>
<script src="../datePicker.js" type="text/javascript"></script>
<script>
//<!--
	var recurElements = new Array("recurFrequency", "recurInterval", "recurEndNone" ,"recurEndCount", "recurEndDate", "recurEndCountValue");
	
	function enableRecurrence(mode) {
		for(var i = 0; i < recurElements.length; i++) {
			document.getElementById(recurElements[i]).disabled = !mode;
		}
	}
	
	function initRecurrence() {
		enableRecurrence(document.getElementById("recurEnabled").checked);
		window.focus()
	}
	
	function update() {
		dispatch("update");
		document.getElementById("form").submit();
	}
	function checkAndSubmit(myform){
	var startDate = document.getElementById("startDate");
		if(startDate && !isDate( startDate.value, 'Start') ){
			startDate.focus();
			return false;
		}
		
	var endDate = document.getElementById("endDate");
		if(endDate && !isDate( endDate.value, 'End') ){
			endDate.focus();
			return false;
		}
		
	var reminderDate = document.getElementById("reminderDate");
		if (reminderDate.value != ''){
			if(reminderDate && !isDate( reminderDate.value, 'Reminder') ){
				reminderDate.focus();
				return false;
			}
		}
		
	var recurUntil = document.getElementById("recurUntil");
		if (recurUntil.value != ''){
			if(recurUntil && !isDate( recurUntil.value, 'On') ){
				recurUntil.focus();
				return false;
			}
		}
	}
//-->
</script>
<smvc:form modelAttribute="occurenceForm" id="form" action="../calendar/eventActions.form" onsubmit="return checkAndSubmit(this);">
	<input type="hidden" name="redirectParams" value="<c:out value="${requestScope['javax.servlet.forward.query_string']}" />" />
	<input type="hidden" name="method" value="save" id="dispatchMethod"/>
	<input type="hidden" name="forward" value="${param.forward }"/>
	<table>
		<tr>
			<td>
				<tags:dialogBox title="Edit Event">
					<table>
						<c:if test="${empty occurenceForm.occurence.id}">
							<tr>
								<td class="label">Resource</td>
								<td class="input">
									<smvc:select path="occurence.resourceId">
										<c:forEach var="subscription" items="${calendar.userCalendar.subscriptions}">
											<c:if test="${subscription.writable }">
												<smvc:option value="${subscription.resourceId}">${subscription.name}</smvc:option>
											</c:if>
										</c:forEach>
									</smvc:select>
								</td>
							</tr>
						</c:if>
						<tr>
							<td class="label">Title</td>
							<td class="input"><smvc:input size="35" maxlength="255" path="occurence.title"/></td>
						</tr>
						<tr>
							<td class="label">Description</td>
							<td class="input"><smvc:textarea cols="30" rows="3" path="occurence.description"/></td>
						</tr>
						<tr>
							<td class="label">Category</td>
							<td class="input"><webapp:selectAndEditList path="occurence.category" domain="webapp" list="category" includeEmpty="true"/></td>
						</tr>
						<tr>
							<td class="label">Location</td>
							<td class="input"><webapp:selectAndEditList path="occurence.location" domain="webapp" list="location" includeEmpty="true"/></td>
						</tr>
						<tr>
							<td class="label">Start</td>
							<td class="input">Time:<smvc:input size="5" path="startTime" /> Date:<webapp:date property="startDate" styleId="startDate" value="${occurenceForm.startDate}"/></td>
						</tr>
						<tr>
							<td class="label">End</td>
							<td class="input">Time:<smvc:input size="5" path="endTime" /> Date:<webapp:date property="endDate" styleId="endDate" value="${occurenceForm.endDate}"/></td>
						</tr>	
						<tr>
							<td class="label">Reminder</td>
							<td class="input">Time:<smvc:input size="5" path="reminderTime" /> Date:<webapp:date property="reminderDate" styleId="reminderDate"  value="${occurenceForm.reminderDate}"/></td>
						</tr>
						<tr>
							<td class="label">Status</td>
							<td class="input">
								<smvc:select path="occurence.status">
									<smvc:option value="busy">Busy</smvc:option>
									<smvc:option value="tentative">Tentative</smvc:option>
									<smvc:option value="free">Free</smvc:option>
									<smvc:option value="outofoffice">Out of Office</smvc:option>
								</smvc:select>
							</td>
						</tr>
						<tr>
							<td class="label">Access</td>
							<td class="input">
								<span title="The event will only be visible by the owner">Private <smvc:radiobutton path="occurence.accessibility" value="0"/></span>
								<span title="Only the time of this event will be visible by others (if subscribed to your diary)">Protected <smvc:radiobutton path="occurence.accessibility" value="1"/></span>
								<span title="All users will be able to see this event (if subscribed to yor diary)">Public <smvc:radiobutton path="occurence.accessibility" value="2"/></span>
							</td>
						</tr>
						<tr>
							<td class="label">Enable&nbsp;recurrence</td>
							<td class="input"><smvc:radiobutton styleId="recurEnabled" onclick="enableRecurrence(true)" path="occurence.recurrenceEnabled" value="true"/>Yes<smvc:radiobutton onclick="enableRecurrence(false)" path="occurence.recurrenceEnabled" value="false"/>No</td>
						</tr>
						<tr>
							<td colspan="2">
								<table>
									<tr>
										<td class="label">Frequency</td>
										<td class="input">
											<smvc:select path="occurence.recurrence.frequency" styleId="recurFrequency">
												<smvc:option value="DAILY">Daily</smvc:option>
												<smvc:option value="WEEKLY">Weekly</smvc:option>
												<smvc:option value="MONTHLY">Monthly</smvc:option>
												<smvc:option value="YEARLY">Yearly</smvc:option>
											</smvc:select>
										</td>
									</tr>
									<tr >
										<td class="label">Interval</td>
										<td class="input"><smvc:input size="3" path="occurence.recurrence.interval" styleId="recurInterval"/></td>
									</tr>
									<!-- Temporarily disabled -->
									<!--<tr id="DAILYvalues"> 
										<td class="label">Only on days:</td>
										<td class="input">
											<smvc:checkbox path="selectedRecurDays" value="MO"/>Mon 
											<smvc:checkbox path="selectedRecurDays" value="TU"/>Tue 
											<smvc:checkbox path="selectedRecurDays" value="WE"/>Wed 
											<smvc:checkbox path="selectedRecurDays" value="TH"/>Thu<br/>
											<smvc:checkbox path="selectedRecurDays" value="FR"/>Fri
											<smvc:checkbox path="selectedRecurDays" value="SA"/>Sat 
											<smvc:checkbox path="selectedRecurDays" value="SU"/>Sun
										</td>
									</tr>-->
									<tr>
										<td class="label" valign="top">Ends</td>
										<td class="input">
											<smvc:radiobutton path="occurence.recurrence.endType" value="none" styleId="recurEndNone"/>Never<br/>
											<smvc:radiobutton path="occurence.recurrence.endType" value="count" styleId="recurEndCount"/> After <smvc:input size="3" path="occurence.recurrence.count" styleId="recurEndCountValue"/> occurrences<br/>
											<smvc:radiobutton path="occurence.recurrence.endType" value="date" styleId="recurEndDate"/>On date <webapp:date property="occurence.recurrence.until" styleId="recurUntil"/>
										</td>
									</tr>
								</table>
							</td>
						</tr>
						<tr>
							<td class="input" colspan="2" align="right">&nbsp;<input  type="submit" value="Save"/>&nbsp;
							<c:if test="${occurenceForm.occurence.id != null }">
								<input  type="submit" value="Delete" onclick="return confirmAndDispatch('delete', 'Are you sure you wish to delete this event?')"/>
							</c:if></td>
						</tr>
					</table>
				</tags:dialogBox>
			</td>
		</tr>
	</table>
</smvc:form>
