1 /*********************************************************************************
2 * SugarCRM Community Edition is a customer relationship management program developed by
3 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Affero General Public License version 3 as published by the
7 * Free Software Foundation with the addition of the following permission added
8 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17 * You should have received a copy of the GNU Affero General Public License along with
18 * this program; if not, see http://www.gnu.org/licenses or write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25 * The interactive user interfaces in modified source and object code versions
26 * of this program must display Appropriate Legal Notices, as required under
27 * Section 5 of the GNU Affero General Public License version 3.
29 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30 * these Appropriate Legal Notices must retain the display of the "Powered by
31 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32 * technical reasons, the Appropriate Legal Notices must display the words
33 * "Powered by SugarCRM".
34 ********************************************************************************/
37 // Class DurationDependency binds start and end datetimecombo fields with duration
39 function DurationDependency(start_field,end_field,duration_field,format){
42 this.start_field = start_field;
43 this.end_field = end_field;
44 this.duration_field = duration_field;
46 this.lock_end_listener = false;
48 var format_parts = this.format.split(" ");
49 this.date_format = format_parts[0];
50 this.time_format = format_parts[1];
51 if(format_parts.length == 3)
52 this.time_format += "" + this.time_format[2];
53 this.date_delimiter = /([-.\\/])/.exec(this.date_format)[0];
54 this.time_delimiter = /([.:])/.exec(this.time_format)[0];
55 this.has_meridiem = /p/i.test(this.format);
57 var date_format_cleaned = this.date_format.replace(/%/g,"").replace(new RegExp(this.date_delimiter, 'g'),"");
58 this.month_pos = date_format_cleaned.search(/m/);
59 this.day_pos = date_format_cleaned.search(/d/);
60 this.year_pos = date_format_cleaned.search(/Y/);
62 if(document.getElementById(end_field).value != "")
63 this.calculate_duration();
65 this.change_duration();
67 this.update_duration_fields();
68 this.set_duration_handler();
72 YAHOO.util.Event.addListener(start_field, "change", function(){
75 YAHOO.util.Event.addListener(end_field, "change", function(){
76 if(self.lock_end_listener)
78 self.calculate_duration();
79 self.update_duration_fields();
80 self.set_duration_handler();
82 if(duration_field != null){
83 YAHOO.util.Event.addListener(duration_field, "change", function(){
84 self.change_duration();
85 self.update_duration_fields();
86 SugarWidgetScheduler.update_time();
91 DurationDependency.prototype.calculate_duration = function(){
93 var start = this.parse_date(document.getElementById(this.start_field).value);
94 var end = this.parse_date(document.getElementById(this.end_field).value);
95 this.duration = (end.getTime() - start.getTime()) / 1000;
101 DurationDependency.prototype.change_start = function(){
102 this.lock_end_listener = true;
104 var start = this.parse_date(document.getElementById(this.start_field).value);
105 var end = new Date(start.getTime() + this.duration * 1000);
106 this.set_date(end,this.end_field);
109 setTimeout(function(){
110 self.lock_end_listener = false;
114 DurationDependency.prototype.change_duration = function(){
115 this.lock_end_listener = true;
117 this.duration = document.getElementById(this.duration_field).value;
118 var start = this.parse_date(document.getElementById(this.start_field).value);
119 var end = new Date(start.getTime() + this.duration * 1000);
120 this.set_date(end,this.end_field);
123 setTimeout(function(){
124 self.lock_end_listener = false;
128 DurationDependency.prototype.update_duration_fields = function(){
129 var minutes = this.duration / 60;
131 var hours_elm = document.getElementById(this.duration_field + "_hours");
132 var minutes_elm = document.getElementById(this.duration_field + "_minutes");
134 hours_elm = document.createElement("input");
135 hours_elm.setAttribute("type","hidden");
136 hours_elm.name = this.duration_field + "_hours";
137 hours_elm.id = hours_elm.name;
138 document.getElementById(this.end_field).parentNode.appendChild(hours_elm);
141 minutes_elm = document.createElement("input");
142 minutes_elm.setAttribute("type","hidden");
143 minutes_elm.name = this.duration_field + "_minutes";
144 minutes_elm.id = minutes_elm.name;
145 document.getElementById(this.end_field).parentNode.appendChild(minutes_elm);
148 hours_elm.value = parseInt(minutes / 60);
149 minutes_elm.value = parseInt(minutes % 60);
152 DurationDependency.prototype.get_duration_text = function(){
153 var minutes = this.duration / 60;
156 var d = parseInt(minutes / 60 / 24);
157 var h = parseInt((minutes / 60) % 24);
158 var m = parseInt(minutes % 60);
159 d = format_part(d,SUGAR.language.get('app_strings', (d > 1)?'LBL_DURATION_DAYS':'LBL_DURATION_DAY'));
160 h = format_part(h,SUGAR.language.get('app_strings', (h > 1)?'LBL_DURATION_HOURS':'LBL_DURATION_HOUR'));
161 m = format_part(m,SUGAR.language.get('app_strings', (m > 1)?'LBL_DURATION_MINUTES':'LBL_DURATION_MINUTE'));
163 function format_part(v,s){
168 v = v + " " + s + " ";
176 DurationDependency.prototype.set_duration_handler = function(){
177 if(this.duration_field == null)
179 var dur_elm = document.getElementById(this.duration_field);
182 if(this.duration >= 0){
183 this.add_custom_duration(dur_elm);
186 dur_elm.value = this.duration;
190 DurationDependency.prototype.add_custom_duration = function(dur_elm){
191 for(var i = 0; i < dur_elm.length; i++){
192 if(dur_elm.options[i].className == 'custom'){
193 var el = dur_elm.options[i];
194 el.parentNode.removeChild(el);
198 var option_exists = false;
200 var pos_found = false;
201 for(var i = 0; i < dur_elm.length; i++){
202 var v = dur_elm.options[i].value;
203 if(v == this.duration){
204 var option_exists = true;
207 if(!pos_found && v > this.duration){
211 if(!pos_found && i == (dur_elm.length - 1)){
219 var option = document.createElement('option');
220 option.value = this.duration;
221 option.className = 'custom';
222 option.innerHTML = this.get_duration_text();
223 var ref = dur_elm.options[pos_index];
224 if(pos_index == dur_elm.length){
225 dur_elm.appendChild(option);
227 dur_elm.insertBefore(option, ref);
232 DurationDependency.prototype.parse_date = function(d){
234 var date_parts = d.split(" ");
235 var date_str = date_parts[0];
236 var time_str = date_parts[1];
237 if(date_parts.length == 3)
238 time_str += date_parts[2];
240 var date_arr = date_str.split(this.date_delimiter);
241 var year = date_arr[this.year_pos];
242 var month = date_arr[this.month_pos];
243 var day = date_arr[this.day_pos];
244 var hour = time_str.substr(0,2);
245 var minute = time_str.substr(3,2);
246 if(this.has_meridiem){
248 if(/pm/i.test(time_str))
250 hour = hour % 12 + (meridiem === "am" ? 0 : 12);
253 var date = new Date(year,month - 1,day,hour,minute);
258 DurationDependency.prototype.set_date = function(date,field){
260 var year = date.getFullYear();
261 var month = date.getMonth() + 1;
262 var day = date.getDate();
263 var hour = date.getHours();
264 var minute = date.getMinutes();
269 if(this.has_meridiem){
271 if(hour * 60 + minute >= 12 * 60)
282 minute = pad(minute);
285 return s < 10 ? "0" + s : s;
289 for(var i = 0; i < 3; i++){
291 date += this.date_delimiter;
292 if(i == this.year_pos)
294 if(i == this.month_pos)
296 if(i == this.day_pos)
300 document.getElementById(field + "_date").value = date;
301 document.getElementById(field + "_hours").value = hour;
302 document.getElementById(field + "_minutes").value = minute;
303 if(this.has_meridiem){
304 var nodes = document.getElementById(field + "_meridiem").childNodes;
305 for(var i = 0; i < nodes.length; i++){
306 if(nodes[i].value == "AM"){
307 meridiem = meridiem.toUpperCase();
311 document.getElementById(field + "_meridiem").value = meridiem;
314 eval("combo_" + field + ".update()");