import rt 3.8.10
[freeside.git] / rt / html / NoAuth / js / util.js
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC 
6 %#                                          <jesse@bestpractical.com>
7 %# 
8 %# (Except where explicitly superseded by other copyright notices)
9 %# 
10 %# 
11 %# LICENSE:
12 %# 
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %# 
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %# 
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %# 
29 %# 
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %# 
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %# 
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %# 
47 %# END BPS TAGGED BLOCK }}}
48 /* $(...)
49     Returns DOM node or array of nodes (if more then one argument passed).
50     If argument is node object allready then do nothing.
51     // Stolen from Prototype
52 */
53 function $() {
54     var elements = new Array();
55
56     for (var i = 0; i < arguments.length; i++) {
57         var element = arguments[i];
58         if (typeof element == 'string')
59             element = document.getElementById(element);
60
61         if (arguments.length == 1)
62             return element;
63
64         elements.push(element);
65     }
66
67     return elements;
68 }
69
70 /* Visibility */
71
72 function show(id) { delClass( id, 'hidden' ) }
73 function hide(id) { addClass( id, 'hidden' ) }
74
75 function hideshow(id) { return toggleVisibility( id ) }
76 function toggleVisibility(id) {
77     var e = $(id);
78
79     if ( e.className.match( /\bhidden\b/ ) )
80         show(e);
81     else
82         hide(e);
83
84     return false;
85 }
86
87 function setVisibility(id, visibility) {
88     if ( visibility ) show(id);
89     else hide(id);
90 }
91
92 function switchVisibility(id1, id2) {
93     // Show both and then hide the one we want
94     show(id1);
95     show(id2);
96     hide(id2);
97     return false;
98 }
99
100 /* Classes */
101
102 function addClass(id, value) {
103     var e = $(id);
104     if ( e.className.match( new RegExp('\b'+ value +'\b') ) )
105         return;
106     e.className += e.className? ' '+value : value;
107 }
108
109 function delClass(id, value) {
110     var e = $(id);
111     e.className = e.className.replace( new RegExp('\\s?\\b'+ value +'\\b', 'g'), '' );
112 }
113
114 /* Rollups */
115
116 function rollup(id) {
117     var e   = $(id);
118     var e2  = e.parentNode;
119     
120     if (e.className.match(/\bhidden\b/)) {
121         set_rollup_state(e,e2,'shown');
122         createCookie(id,1,365);
123     }
124     else {
125         set_rollup_state(e,e2,'hidden');
126         createCookie(id,0,365);
127     }
128     return false;
129 }
130
131 function set_rollup_state(e,e2,state) {
132     if (e && e2) {
133         if (state == 'shown') {
134             show(e);
135             delClass( e2, 'rolled-up' );
136         }
137         else if (state == 'hidden') {
138             hide(e);
139             addClass( e2, 'rolled-up' );
140         }
141     }
142 }
143
144
145 /* onload handlers */
146
147 var onLoadStack     = new Array();
148 var onLoadLastStack = new Array();
149 var onLoadExecuted  = 0;
150
151 function onLoadHook(commandStr) {
152     if(typeof(commandStr) == "string") {
153         onLoadStack[ onLoadStack.length ] = commandStr;
154         return true;
155     }
156     return false;
157 }
158
159 // some things *really* need to be done after everything else
160 function onLoadLastHook(commandStr) {
161     if(typeof(commandStr) == "string"){
162         onLoadLastStack[onLoadLastStack.length] = commandStr;
163         return true;
164     }
165     return false;
166 }
167
168 function doOnLoadHooks() {
169     if(onLoadExecuted) return;
170
171     var i;
172     for ( i in onLoadStack ) { 
173         eval( onLoadStack[i] );
174     }
175     for ( i in onLoadLastStack ) { 
176         eval( onLoadLastStack[i] );
177     }
178     onLoadExecuted = 1;
179 }
180
181 window.onload = doOnLoadHooks;
182
183 /* calendar functions */
184
185 function openCalWindow(field) {
186     var objWindow = window.open('<%$RT::WebPath%>/Helpers/CalPopup.html?field='+field, 
187                                 'RT_Calendar', 
188                                 'height=235,width=285,scrollbars=1');
189     objWindow.focus();
190 }
191
192 function createCalendarLink(input) {
193     var e = $(input);
194     if (e) {
195         var link = document.createElement('a');
196         link.setAttribute('href', '#');
197
198         clickevent = function clickevent(e) { openCalWindow(input); return false; };
199         if (! addEvent(link, "click", clickevent)) {
200             return false;
201         }
202         
203         var text = document.createTextNode('<% loc("Choose a date") %>');
204         link.appendChild(text);
205
206         var space = document.createTextNode(' ');
207         
208         e.parentNode.insertBefore(link, e.nextSibling);
209         e.parentNode.insertBefore(space, e.nextSibling);
210
211         return true;
212     }
213     return false;
214 }
215
216 /* other utils */
217
218 function focusElementById(id) {
219     var e = $(id);
220     if (e) e.focus();
221 }
222
223 function updateParentField(field, value) {
224     if (window.opener) {
225         window.opener.$(field).value = value;
226         window.close();
227     }
228 }
229
230 function addEvent(obj, sType, fn) {
231     if (obj.addEventListener) {
232         obj.addEventListener(sType, fn, false);
233     } else if (obj.attachEvent) {
234         var r = obj.attachEvent("on"+sType, fn);
235     } else {
236         return false;
237     }
238     return true;
239 }
240
241 function setCheckbox(form, name, val) {
242     var myfield = form.getElementsByTagName('input');
243     for ( var i = 0; i < myfield.length; i++ ) {
244         if ( name && myfield[i].name != name ) continue;
245         if ( myfield[i].type != 'checkbox' ) continue;
246
247         myfield[i].checked = val;
248     }
249 }
250