1 $(document).ready(function() {
3 // get current time from server
4 var today = new Date($('now').val());
6 $("#event-startdate").datepicker({
7 // Don't let the user set a start date < before today
9 onClose: onStartDateSelected
12 $("#event-enddate").datepicker({
14 onClose: onEndDateSelected
17 $("#event-starttime").change(function(e) {
18 var tz = $("#tz").val();
20 var startDate = $("#event-startdate").val();
21 var startTime = $("#event-starttime option:selected").val().replace(/(pm|am)/, ' $1');
22 var startStr = startDate + ' ' + startTime + ' ' + tz;
24 var endDate = $("#event-enddate").val();
25 var endTime = $("#event-endtime option:selected").val();
26 var endStr = endDate + ' ' + endTime.replace(/(pm|am)/, ' $1') + ' ' + tz;
28 // just need to compare hours
29 var start = new Date(startStr);
30 var end = new Date(endStr);
32 updateTimes(startStr, (startDate === endDate), function (data) {
34 $.each(data, function(key, val) {
35 times.push('<option value="' + key + '">' + val + '</option>');
37 $("#event-endtime").html(times.join(''));
40 $("#event-endtime").val(startTime).attr("selected", "selected");
42 $("#event-endtime").val(endTime).attr("selected", "selected");
48 $("#event-endtime").change(function(e) {
49 var HOUR = 60 * 60 * 1000;
50 var tz = $("#tz").val();
51 var startDate = $("#event-startdate").val();
52 var endDate = $("#event-enddate").val();
53 var starttime = $("#event-starttime option:selected").val();
54 var endtime = $("#event-endtime option:selected").val();
55 var endtimeText = $("#event-endtime option:selected").text();
57 // If the end time is in the next day then update the start date
58 if (startDate === endDate) {
59 var startstr = startDate + ' ' + starttime.replace(/(pm|am)/, ' $1') + ' ' + tz;
60 var start = new Date(startstr);
61 var matches = endtimeText.match(/\(.*\)/);
64 hours = matches[0].substr(1).split(' ')[0]; // get x from (x hours)
67 hours = .5; // special case: x == 30 from (30 mins)
69 var end = new Date(start.getTime() + (hours * HOUR));
70 if (end.getDate() > start.getDate()) {
71 $("#event-enddate").datepicker('setDate', end);
72 var endstr = endDate + ' 12:00 am ' + tz;
73 updateTimes(endstr, false, function(data) {
75 $.each(data, function(key, val) {
76 times.push('<option value="' + key + '">' + val + '</option>');
78 $("#event-endtime").html(times.join(''));
81 $("#event-endtime").val(starttime).attr("selected", "selected");
83 $("#event-endtime").val(endtime).attr("selected", "selected");
92 function onStartDateSelected(dateText, inst) {
93 var tz = $("#tz").val();
94 var startTime = $("#event-starttime option:selected").val();
95 var startDateTime = new Date(dateText + ' ' + startTime.replace(/(pm|am)/, ' $1') + ' ' + tz);
97 // When we update the start date and time, we need to update the end date and time
98 // to make sure they are equal or in the future
99 $("#event-enddate").datepicker('option', 'minDate', startDateTime);
104 function onEndDateSelected(dateText, inst) {
108 function recalculateTimes(showDuration) {
109 var tz = $("#tz").val();
111 var startDate = $("#event-startdate").val();
112 var startTime = $("#event-starttime option:selected").val();
113 var startStr = startDate + ' ' + startTime.replace(/(pm|am)/, ' $1') + ' ' + tz;
114 var startDateTime = new Date(startStr);
116 var endDate = $("#event-enddate").val();
117 var endTime = $("#event-endtime option:selected").val();
118 var endDateTime = new Date(endDate + ' ' + endTime.replace(/(pm|am)/, ' $1') + ' ' + tz);
119 var showDuration = true;
121 if (endDateTime.getDate() !== startDateTime.getDate()) {
122 starStr = endDate + ' 12:00 am ' + tz;
123 showDuration = false;
126 updateTimes(startStr, showDuration, function(data) {
128 $.each(data, function(key, val) {
129 times.push('<option value="' + key + '">' + val + '</option>');
131 $("#event-endtime").html(times.join(''));
132 if (startDateTime > endDateTime) {
133 $("#event-endtime").val(startTime).attr("selected", "selected");
135 $("#event-endtime").val(endTime).attr("selected", "selected");
140 function updateTimes(start, duration, onSuccess) {
141 $.getJSON($('#timelist_action_url').val(), {start: start, ajax: true, duration: duration}, onSuccess);