1 /*********************************************************************************
2 * SugarCRM Community Edition is a customer relationship management program developed by
3 * SugarCRM, Inc. Copyright (C) 2004-2011 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 Calendar = function() {};
39 Calendar.getHighestZIndex = function (containerEl)
45 els = containerEl ? containerEl.getElementsByTagName('*') : document.getElementsByTagName('*');
47 for(var i=0; i < els.length; i++)
49 currentIndex = YAHOO.util.Dom.getStyle(els[i], "zIndex");
50 if(!isNaN(currentIndex) && currentIndex > highestIndex)
52 highestIndex = parseInt(currentIndex);
56 return (highestIndex == Number.MAX_VALUE) ? Number.MAX_VALUE : highestIndex+1;
59 Calendar.setup = function (params) {
61 YAHOO.util.Event.onDOMReady(function(){
63 var Event = YAHOO.util.Event;
64 var Dom = YAHOO.util.Dom;
67 var showButton = params.button ? params.button : params.buttonObj;
68 var userDateFormat = params.ifFormat ? params.ifFormat : (params.daFormat ? params.daFormat : "m/d/Y");
69 var inputField = params.inputField ? params.inputField : params.inputFieldObj;
70 var startWeekday = params.startWeekday ? params.startWeekday : 0;
71 var dateFormat = userDateFormat.substr(0,10);
72 var date_field_delimiter = /([-.\\/])/.exec(dateFormat)[0];
73 dateFormat = dateFormat.replace(/[^a-zA-Z]/g,'');
75 var monthPos = dateFormat.search(/m/);
76 var dayPos = dateFormat.search(/d/);
77 var yearPos = dateFormat.search(/Y/);
79 Event.on(Dom.get(showButton), "click", function() {
83 dialog = new YAHOO.widget.SimpleDialog("container_" + showButton, {
85 context:[showButton, "tl", "bl", null, [-175,5]],
89 zIndex: Calendar.getHighestZIndex(document.body)
92 dialog.setHeader(SUGAR.language.get('app_strings', 'LBL_MASSUPDATE_DATE'));
93 var dialogBody = '<p class="callnav_today"><a href="javascript:void(0)" id="callnav_today">' + SUGAR.language.get('app_strings', 'LBL_EMAIL_DATE_TODAY') + '</a></p><div id="' + showButton + '_div"></div>';
94 dialog.setBody(dialogBody);
95 dialog.render(document.body);
97 //Since the cal div name is dynamic we need to add a custom class to override some default yui css styles
98 Dom.addClass("container_" + showButton, "cal_panel");
100 //Clear the date selection if the user clicks on today.
101 Event.addListener("callnav_today", "click", function(){
103 var now = new Date();
104 //Reset the input field value
105 Dom.get(inputField).value = formatSelectedDate(now);
107 var cellIndex = calendar.getCellIndex(now);
110 var cell = calendar.cells[cellIndex];
111 Dom.addClass(cell, calendar.Style.CSS_CELL_SELECTED);
113 //Must return false to prevent onbeforeunload from firing in IE8
117 dialog.showEvent.subscribe(function() {
118 if (YAHOO.env.ua.ie) {
119 // Since we're hiding the table using yui-overlay-hidden, we
120 // want to let the dialog know that the content size has changed, when
122 dialog.fireEvent("changeContent");
126 // Hide Calendar if we click anywhere in the document other than the calendar
127 Event.on(document, "click", function(e) {
134 var el = Event.getTarget(e);
135 var dialogEl = dialog.element;
136 if (el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != Dom.get(showButton) && !Dom.isAncestor(Dom.get(showButton), el)) {
142 // Lazy Calendar Creation - Wait to create the Calendar until the first time the button is clicked.
146 month: SUGAR.language.get('app_strings', 'LBL_CHOOSE_MONTH'),
147 year: SUGAR.language.get('app_strings', 'LBL_ENTER_YEAR'),
148 submit: SUGAR.language.get('app_strings', 'LBL_EMAIL_OK'),
149 cancel: SUGAR.language.get('app_strings', 'LBL_CANCEL_BUTTON_LABEL'),
150 invalidYear: SUGAR.language.get('app_strings', 'LBL_ENTER_VALID_YEAR')
152 monthFormat: YAHOO.widget.Calendar.SHORT,
156 calendar = new YAHOO.widget.Calendar(showButton + '_div', {
158 hide_blank_weeks:true,
162 calendar.cfg.setProperty('DATE_FIELD_DELIMITER', date_field_delimiter);
163 calendar.cfg.setProperty('MDY_DAY_POSITION', dayPos+1);
164 calendar.cfg.setProperty('MDY_MONTH_POSITION', monthPos+1);
165 calendar.cfg.setProperty('MDY_YEAR_POSITION', yearPos+1);
166 calendar.cfg.setProperty('START_WEEKDAY', startWeekday);
168 //Configure the month and days label with localization support where defined
169 if(typeof SUGAR.language.languages['app_list_strings'] != 'undefined' && SUGAR.language.languages['app_list_strings']['dom_cal_month_long'] != 'undefined')
171 if(SUGAR.language.languages['app_list_strings']['dom_cal_month_long'].length == 13)
173 SUGAR.language.languages['app_list_strings']['dom_cal_month_long'].shift();
175 calendar.cfg.setProperty('MONTHS_LONG', SUGAR.language.languages['app_list_strings']['dom_cal_month_long']);
178 if(typeof SUGAR.language.languages['app_list_strings'] != 'undefined' && typeof SUGAR.language.languages['app_list_strings']['dom_cal_day_short'] != 'undefined')
180 if(SUGAR.language.languages['app_list_strings']['dom_cal_day_short'].length == 8)
182 SUGAR.language.languages['app_list_strings']['dom_cal_day_short'].shift();
184 calendar.cfg.setProperty('WEEKDAYS_SHORT', SUGAR.language.languages['app_list_strings']['dom_cal_day_short']);
187 var formatSelectedDate = function(selDate)
189 var monthVal = selDate.getMonth() + 1; //Add one for month value
192 monthVal = '0' + monthVal;
195 var dateVal = selDate.getDate();
199 dateVal = '0' + dateVal;
202 var yearVal = selDate.getFullYear();
220 selDate += date_field_delimiter + monthVal;
224 selDate += date_field_delimiter + dateVal;
228 selDate += date_field_delimiter + yearVal;
233 selDate += date_field_delimiter + monthVal;
237 selDate += date_field_delimiter + dateVal;
241 selDate += date_field_delimiter + yearVal;
247 calendar.selectEvent.subscribe(function() {
248 var input = Dom.get(inputField);
249 if (calendar.getSelectedDates().length > 0) {
251 input.value = formatSelectedDate(calendar.getSelectedDates()[0]);
253 if(params.comboObject)
255 params.comboObject.update();
262 //does not trigger onchange event
269 //Fire any on-change events for this input field
270 SUGAR.util.callOnChangeListers(input);
273 calendar.renderEvent.subscribe(function() {
274 // Tell Dialog it's contents have changed, which allows
275 // container to redraw the underlay (for IE6/Safari2)
276 dialog.fireEvent("changeContent");
281 var seldate = calendar.getSelectedDates();
282 if (Dom.get(inputField).value.length > 0) {
283 val = new Date(Dom.get(inputField).value);
284 if(!isNaN(val.getTime()))
286 calendar.cfg.setProperty("selected", Dom.get(inputField).value);
287 seldate = Dom.get(inputField).value.split(date_field_delimiter);
288 calendar.cfg.setProperty("pagedate", seldate[monthPos] + calendar.cfg.getProperty("DATE_FIELD_DELIMITER") + seldate[yearPos]);
290 } else if (seldate.length > 0) {
291 // Set the pagedate to show the selected date if it exists
292 calendar.cfg.setProperty("selected", seldate[0]);
293 var month = seldate[0].getMonth() + 1;
294 var year = seldate[0].getFullYear();
295 calendar.cfg.setProperty("pagedate", month + calendar.cfg.getProperty("DATE_FIELD_DELIMITER") + year);