import rt 3.8.7
[freeside.git] / rt / share / 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 /* New code should be using doOnLoad which makes use of prototype
147    instead. See HeaderJavascript.  It works better than clobbering
148    window.onload.  Left around in case other code is using them */
149
150 var onLoadStack     = new Array();
151 var onLoadLastStack = new Array();
152 var onLoadExecuted  = 0;
153
154 function onLoadHook(commandStr) {
155     if(typeof(commandStr) == "string") {
156         onLoadStack[ onLoadStack.length ] = commandStr;
157         return true;
158     }
159     return false;
160 }
161
162 // some things *really* need to be done after everything else
163 function onLoadLastHook(commandStr) {
164     if(typeof(commandStr) == "string"){
165         onLoadLastStack[onLoadLastStack.length] = commandStr;
166         return true;
167     }
168     return false;
169 }
170
171 function doOnLoadHooks() {
172     if(onLoadExecuted) return;
173
174     var i;
175     for ( i in onLoadStack ) { 
176         eval( onLoadStack[i] );
177     }
178     for ( i in onLoadLastStack ) { 
179         eval( onLoadLastStack[i] );
180     }
181     onLoadExecuted = 1;
182 }
183
184 window.onload = doOnLoadHooks;
185
186 /* new onLoad code */
187
188 function doOnLoad(handler) {
189     Event.observe(window, 'load', handler);
190 }
191
192 /* calendar functions */
193
194 function openCalWindow(field) {
195     var objWindow = window.open('<%RT->Config->Get('WebPath')%>/Helpers/CalPopup.html?field='+field, 
196                                 'RT_Calendar', 
197                                 'height=235,width=285,scrollbars=1');
198     objWindow.focus();
199 }
200
201 function createCalendarLink(input) {
202     var e = $(input);
203     if (e) {
204         var link = document.createElement('a');
205         link.setAttribute('href', '#');
206         $(link).observe('click', function(ev) { openCalWindow(input); ev.stop(); });
207         //link.setAttribute('onclick', "openCalWindow('"+input+"'); return false;");
208
209         var text = document.createTextNode('<% loc("Calendar") %>');
210         link.appendChild(text);
211
212         var space = document.createTextNode(' ');
213         
214         e.parentNode.insertBefore(link, e.nextSibling);
215         e.parentNode.insertBefore(space, e.nextSibling);
216
217         return true;
218     }
219     return false;
220 }
221
222 /* other utils */
223
224 function focusElementById(id) {
225     var e = $(id);
226     if (e) e.focus();
227 }
228
229 function updateParentField(field, value) {
230     if (window.opener) {
231         window.opener.$(field).value = value;
232         window.close();
233     }
234 }
235
236 function setCheckbox(form, name, val) {
237     var myfield = form.getElementsByTagName('input');
238     for ( var i = 0; i < myfield.length; i++ ) {
239         if ( name && myfield[i].name != name ) continue;
240         if ( myfield[i].type != 'checkbox' ) continue;
241
242         myfield[i].checked = val;
243     }
244 }
245
246 /* apply callback to nodes or elements */
247
248 function walkChildNodes(parent, callback)
249 {
250         if( !parent || !parent.childNodes ) return;
251         var list = parent.childNodes;
252         for( var i = 0; i < list.length; i++ ) {
253                 callback( list[i] );
254         }
255 }
256
257 function walkChildElements(parent, callback)
258 {
259         walkChildNodes( parent, function(node) {
260                 if( node.nodeType != 1 ) return;
261                 return callback( node );
262         } );
263 }
264
265 /* shredder things */
266
267 function showShredderPluginTab( plugin )
268 {
269         var plugin_tab_id = 'shredder-plugin-'+ plugin +'-tab';
270         var root = $('shredder-plugin-tabs');
271         walkChildElements( root, function(node) {
272                 if( node.id == plugin_tab_id ) {
273                         show( node );
274                 } else {
275                         hide( node );
276                 }
277         } );
278         if( plugin ) {
279                 show('shredder-submit-button');
280         } else {
281                 hide('shredder-submit-button');
282         }
283 }
284
285 function checkAllObjects()
286 {
287         var check = $('shredder-select-all-objects-checkbox').checked;
288         var elements = $('shredder-search-form').elements;
289         for( var i = 0; i < elements.length; i++ ) {
290                 if( elements[i].name != 'WipeoutObject' ) {
291                         continue;
292                 }
293                 if( elements[i].type != 'checkbox' ) {
294                         continue;
295                 }
296                 if( check ) {
297                         elements[i].checked = true;
298                 } else {
299                         elements[i].checked = false;
300                 }
301         }
302 }
303
304 function checkboxToInput(target,checkbox,val){    
305     var tar=$(target);
306     var box = $(checkbox);
307     if(box.checked){
308         if (tar.value==''){
309             tar.value=val;
310         }else{
311             tar.value=val+', '+tar.value;        }
312     }else{
313         tar.value=tar.value.replace(val+', ','');
314         tar.value=tar.value.replace(val,'');
315     }
316 }
317