]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js
Merge branch 'master' of git.evergreen-ils.org:Evergreen into template-toolkit-opac
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / OpenILS / global_util.js
1     function $(id) { return document.getElementById(id); }
2
3     function oils_unsaved_data_V() {
4         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
5         data.stash_retrieve();
6         if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
7         data.unsaved_data++;
8         window.oils_lock++;
9         data.stash('unsaved_data');
10         dump('\n=-=-=-=-=\n');
11         dump('oils_unsaved_data_V for ' + location.href + '\n');
12         dump('incrementing window.oils_lock\n');
13         dump('incrementing data.unsaved_data\n');
14         dump('\twindow.oils_lock == ' + window.oils_lock + '\n');
15         dump('\tdata.unsaved_data == ' + data.unsaved_data + '\n');
16     }
17
18     function oils_unsaved_data_P(count) {
19         dump('\n=-=-=-=-=\n');
20         dump('oils_unsaved_data_P for ' + location.href + '\n');
21         if (!count) { count = 1; }
22         dump('decrementing window.oils_lock by ' + count + '\n');
23         window.oils_lock -= count;
24         if (window.oils_lock < 0) { window.oils_lock = 0; }
25         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
26         data.stash_retrieve();
27         if (typeof data.unsaved_data == 'undefined') { data.unsaved_data = 0; }
28         dump('decrementing data.unsaved_data by ' + count + '\n');
29         data.unsaved_data -= count;
30         if (data.unsaved_data < 0) { data.unsaved_data = 0; }
31         data.stash('unsaved_data');
32         dump('\twindow.oils_lock == ' + window.oils_lock + '\n');
33         dump('\tdata.unsaved_data == ' + data.unsaved_data + '\n');
34     }
35
36     function oils_lock_page(params) {
37         dump('\n=-=-=-=-=\n');
38         dump('oils_lock_page for ' + location.href + '\n');
39         if (!params) { params = {}; }
40         if (window.oils_lock > 0) {
41             if (!params.allow_multiple_locks) {
42                 return window.oils_lock;
43             }
44         }
45         if (typeof xulG != 'undefined') {
46             if (typeof xulG.unlock_tab == 'function') {
47                 dump('\twith xulG.lock_tab\n');
48                 xulG.lock_tab();
49                 window.oils_lock++; // different window scope than the chrome of xulG.lock_tab
50             } else {
51                 dump('\twithout xulG.lock_tab\n');
52                 oils_unsaved_data_V();
53             }
54         } else {
55             dump('\twithout xulG.lock_tab\n');
56             oils_unsaved_data_V();
57         }
58         return window.oils_lock;
59     }
60
61     function oils_unlock_page(params) {
62         dump('\n=-=-=-=-=\n');
63         dump('oils_unlock_page for ' + location.href + '\n');
64         if (typeof xulG != 'undefined') {
65             if (typeof xulG.unlock_tab == 'function') {
66                 dump('\twith xulG.unlock_tab\n');
67                 xulG.unlock_tab();
68                 window.oils_lock--; // different window scope than the chrome of xulG.unlock_tab
69                 if (window.oils_lock < 0) { window.oils_lock = 0; }
70             } else {
71                 dump('\twithout xulG.unlock_tab\n');
72                 oils_unsaved_data_P();
73             }
74         } else {
75             dump('\twithout xulG.unlock_tab\n');
76             oils_unsaved_data_P();
77         }
78         return window.oils_lock;
79     }
80
81     window.oils_lock = 0;
82     dump('\n=-=-=-=-=\n');
83     dump('init window.oils_lock == ' + window.oils_lock + ' for ' + location.href + '\n');
84     window.addEventListener(
85         'close',
86         function(ev) {
87             try {
88                 dump('\n=-=-=-=-=\n');
89                 dump('oils_lock_page/oils_unlock_page onclose handler for ' + location.href + '\n');
90                 if (window.oils_lock > 0) {
91                     var confirmation = window.confirm($('offlineStrings').getString('menu.close_window.unsaved_data_warning'));
92                     if (!confirmation) {
93                         ev.preventDefault();
94                         return false;
95                     }
96                 }
97
98                 if (typeof xulG != 'undefined') {
99                     if (typeof xulG.unlock_tab == 'function') {
100                         xulG.unlock_tab();
101                     } else {
102                         oils_unsaved_data_P( window.oils_lock );
103                     }
104                 } else {
105                     oils_unsaved_data_P( window.oils_lock );
106                 }
107                 window.oils_lock = 0;
108                 dump('forcing window.oils_lock == ' + window.oils_lock + '\n');
109
110                 // Dispatching the window close event doesn't always close the window, even though the event does happen
111                 setTimeout(
112                     function() {
113                         try {
114                             window.close();
115                         } catch(E) {
116                             dump('Error inside global_util.js, onclose handler, setTimeout window.close KLUDGE: ' + E + '\n');
117                         }
118                     }, 0
119                 );
120
121                 return true;
122             } catch(E) {
123                 dump('Error inside global_util.js, onclose handler: ' + E + '\n');
124                 return true;
125             }
126         },
127         false
128     );
129
130     function ses(a,params) {
131         try {
132             if (!params) params = {};
133             var data;
134             if (params.data) {
135                 data = params.data; data.stash_retrieve();
136             } else {
137                 // This has been breaking in certain contexts, with an internal instantiation of util.error failing because of util.error being an object instead of the constructor function it should be
138                 JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
139             }
140
141             switch(a) {
142                 case 'staff' : return data.list.au[0]; break;
143                 case 'staff_id' : return data.list.au[0].id(); break;
144                 case 'staff_usrname' : return data.list.au[0].usrname(); break;
145                 case 'ws_ou' :
146                     return data.list.au[0].ws_ou();
147                 break;
148                 case 'ws_ou_shortname' :
149                     return data.hash.aou[ data.list.au[0].ws_ou() ].shortname();
150                 break;
151                 case 'authtime' :
152                     return data.session.authtime;
153                 break;
154                 case 'key':
155                 default:
156                     return data.session.key;
157                 break;
158             }
159         } catch(E) {
160             alert(location.href + '\nError in global_utils.js, ses(): ' + E);
161             throw(E);
162         }
163     }
164
165     function font_helper() {
166         try {
167             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
168             removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
169             removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
170             removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
171             removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
172             removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
173             removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
174             removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
175             removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
176             removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
177             addCSSClass(document.documentElement,data.global_font_adjust);
178         } catch(E) {
179             var Strings = $('offlineStrings') || $('commonStrings');
180             alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
181         }
182     }
183
184     function oils_persist(e,cancelable) {
185         try {
186             if (!e) { return; }
187             if (typeof cancelable == 'undefined') { cancelable = false; } 
188             var evt = document.createEvent("Events");
189             evt.initEvent( 'oils_persist', false, cancelable ); // event name, bubbles, cancelable
190             e.dispatchEvent(evt);
191         } catch(E) {
192             alert('Error with oils_persist():' + E);
193         }
194     }
195
196     function persist_helper(base_key_suffix) {
197         try {
198             if (base_key_suffix) {
199                 base_key_suffix = base_key_suffix.replace(/[^A-Za-z]/g,'_') + '_';
200             } else {
201                 base_key_suffix = '';
202             }
203
204             function gen_event_handler(etype,node) {
205                 return function(ev) {
206                     try {
207                         oils_persist(ev.target);
208                     } catch(E) {
209                         alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
210                     }
211                 };
212             };
213
214             function gen_oils_persist_handler(bk,node) {
215                 return function(ev) {
216                     try {
217                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
218                         var target;
219                         if (ev.target.nodeName == 'command') {
220                             target = node;
221                             if (ev.explicitOriginalTarget != node) return;
222                         } else {
223                             target = ev.target;
224                             if (target == window) {
225                                 target = window.document.documentElement;
226                             }
227                         }
228                         var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
229                         var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
230                         var attribute_list = target.getAttribute('oils_persist').split(' ');
231                         dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
232                         for (var j = 0; j < attribute_list.length; j++) {
233                             var key = base_key + attribute_list[j];
234                             var value;
235                             try {
236                                 value = encodeURI(target.getAttribute( attribute_list[j] ));
237                             } catch(E) {
238                                 dump('Error in persist_helper with encodeURI: ' + E + '\n');
239                                 value = target.getAttribute( attribute_list[j] );
240                             }
241                             if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
242                                 value = target.checked;
243                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
244                             } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( target.nodeName ) > -1 ) {
245                                 value = target.value;
246                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
247                             } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
248                                 value = target.value;
249                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
250                             } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( target.nodeName ) > -1 ) {
251                                 value = window.windowState;
252                                 dump('\t' + value + ' <== window.windowState, @' + attribute_list[j] + '\n');
253                             } else if ( attribute_list[j] == 'height' && ['window'].indexOf( target.nodeName ) > -1 ) {
254                                 value = window.outerHeight;
255                                 dump('\t' + value + ' <== window.outerHeight, @' + attribute_list[j] + '\n');
256                             } else if ( attribute_list[j] == 'width' && ['window'].indexOf( target.nodeName ) > -1 ) {
257                                 value = window.outerWidth;
258                                 dump('\t' + value + ' <== window.outerWidth, @' + attribute_list[j] + '\n');
259                             } else {
260                                 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
261                             }
262                             prefs.setCharPref( key, value );
263                             // TODO: Need to add logic for splitter repositioning, grippy state, etc.
264                             // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
265                         }
266                         if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
267                             var peer_list = target.getAttribute('oils_persist_peers').split(' ');
268                             for (var j = 0; j < peer_list.length; j++) {
269                                 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
270                                 oils_persist( document.getElementById( peer_list[j] ), true );
271                             } 
272                         }
273                     } catch(E) {
274                         alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
275                     }
276                 };
277             }
278
279             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
280             var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
281             var nodes = document.getElementsByAttribute('oils_persist','*');
282             for (var i = 0; i < nodes.length; i++) {
283                 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
284                 var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
285                 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
286                 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
287                 for (var j = 0; j < attribute_list.length; j++) {
288                     var key = base_key + attribute_list[j];
289                     var has_key = prefs.prefHasUserValue(key);
290                     var value;
291                     try {
292                         value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
293                     } catch(E) {
294                         dump('Error in persist_helper with decodeURI: ' + E + '\n');
295                         value = has_key ? prefs.getCharPref(key) : null;
296                     }
297                     if (value == 'true') { value = true; }
298                     if (value == 'false') { value = false; }
299                     if (has_key) {
300                         if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
301                             nodes[i].checked = value; 
302                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
303                             if (!value) {
304                                 nodes[i].removeAttribute('checked');
305                                 dump('\tremoving @checked\n');
306                             }
307                         } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
308                             nodes[i].value = value;
309                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
310                         } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( nodes[i].nodeName ) > -1 ) {
311                             nodes[i].value = value;
312                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');       
313                         } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
314                             switch(value) {
315                                 case window.STATE_MAXIMIZED:
316                                     window.maximize();
317                                     break;
318                                 case window.STATE_MINIMIZED:
319                                     window.minimize();
320                                     break;
321                             };
322                             dump('\t' + value + ' ==> window.windowState, @' + attribute_list[j] + '\n');
323                         } else if ( attribute_list[j] == 'height' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
324                             window.outerHeight = value;
325                             dump('\t' + value + ' ==> window.outerHeight, @' + attribute_list[j] + '\n');
326                         } else if ( attribute_list[j] == 'width' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
327                             window.outerWidth = value;
328                             dump('\t' + value + ' ==> window.outerWidth, @' + attribute_list[j] + '\n');
329                         } else {
330                             nodes[i].setAttribute( attribute_list[j], value);
331                             dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
332                         }
333                     }
334                 }
335                 var cmd = nodes[i].getAttribute('command');
336                 var cmd_el = document.getElementById(cmd);
337                 if (nodes[i].disabled == false && nodes[i].hidden == false) {
338                     var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
339                     if (no_poke && no_poke == 'true') {
340                         // Timing issue for some checkboxes; don't poke them with an event
341                         dump('\tnot poking\n');
342                     } else {
343                         if (cmd_el) {
344                             dump('\tpoking @command\n');
345                             var evt = document.createEvent("Events");
346                             evt.initEvent( 'command', true, true );
347                             cmd_el.dispatchEvent(evt);
348                         } else {
349                             dump('\tpoking\n');
350                             var evt = document.createEvent("Events");
351                             evt.initEvent( 'command', true, true );
352                             nodes[i].dispatchEvent(evt);
353                         }
354                     }
355                 }
356                 if (cmd_el) {
357                     cmd_el.addEventListener(
358                         'command',
359                         gen_event_handler('command',cmd_el),
360                         false
361                     );
362                     cmd_el.addEventListener(
363                         'oils_persist',
364                         gen_oils_persist_handler( base_key, nodes[i] ),
365                         false
366                     );
367                 } else {
368                     var node = nodes[i];
369                     var event_types = [];
370                     if (node.hasAttribute('oils_persist_events')) {
371                         var event_type_list = node.getAttribute('oils_persist_events').split(' ');
372                         for (var j = 0; j < event_type_list.length; j++) {
373                             event_types.push( event_type_list[j] );
374                         }
375                     } else {
376                         if (node.nodeName == 'textbox') { 
377                             event_types.push('change');
378                         } else if (node.nodeName == 'menulist') { 
379                             event_types.push('select');  
380                         } else if (node.nodeName == 'window') {
381                             event_types.push('resize'); 
382                             node = window; // xul window is an element of window.document
383                         } else {
384                             event_types.push('command'); 
385                         }
386                     }
387                     for (var j = 0; j < event_types.length; j++) {
388                         node.addEventListener(
389                             event_types[j],
390                             gen_event_handler(event_types[j],node),
391                             false
392                         );
393                     }
394                     node.addEventListener(
395                         'oils_persist',
396                         gen_oils_persist_handler( base_key, node ),
397                         false
398                     );
399                 }
400             }
401         } catch(E) {
402             alert('Error in persist_helper(): ' + E);
403         }
404     }
405
406     function getKeys(o) {
407         var keys = [];
408         for (var k in o) keys.push(k);
409         return keys;
410     }
411
412     function get_contentWindow(frame) {
413         try {
414             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
415             if (frame && frame.contentWindow) {
416                 try {
417                     if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
418                                      return frame.contentWindow.wrappedJSObject;
419                           }
420                 } catch(E) {
421                     var Strings = $('offlineStrings') || $('commonStrings');
422                     alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
423                 }
424                 return frame.contentWindow;
425             } else {
426                 return null;
427             }
428         } catch(E) {
429             var Strings = $('offlineStrings') || $('commonStrings');
430             alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
431         }
432     }
433
434     function update_modal_xulG(v) {
435         try {
436             if (typeof xulG != "undefined" && xulG.not_modal) {
437                 xulG = v;
438                 xulG.not_modal = true;
439                 return;
440             }
441
442             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
443             var key = location.pathname + location.search + location.hash;
444             if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
445                 data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ] = v;
446                 data.stash('modal_xulG_stack');
447             }
448         } catch(E) {
449             alert('FIXME: update_modal_xulG => ' + E);
450         }
451     }
452
453     function xul_param(param_name,_params) {
454         /* By default, this function looks for a CGI-style query param identified by param_name.  If one isn't found, it then looks in xulG.  If one still isn't found, and _params.stash_name is true, it looks in the global xpcom stash for the field identified by stash_name.  If _params.concat is true, then it looks in all these places and concatenates the results.  There are also options for converting JSON to javascript objects, and clearing the xpcom stash_name field after retrieval.  Also added, ability to search a specific spot in the xpcom stash that implements a stack to hold xulG's for modal windows */
455         try {
456             //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
457             var value = undefined; if (!_params) _params = {};
458             if (typeof _params.no_cgi == 'undefined') {
459                 var cgi = new CGI();
460                 if (cgi.param(param_name)) {
461                     var x = cgi.param(param_name);
462                     //dump('\tfound via location.href = ' + x + '\n');
463                     if (typeof _params.JSON2js_if_cgi != 'undefined') {
464                         x = JSON2js( x );
465                         //dump('\tJSON2js = ' + x + '\n');
466                     }
467                     if (typeof _params.concat == 'undefined') {
468                         //alert(param_name + ' x = ' + x);
469                         return x; // value
470                     } else {
471                         if (value) {
472                             if (value.constructor != Array) value = [ value ];
473                             value = value.concat(x);
474                         } else {
475                             value = x;
476                         }
477                     }
478                 }
479             }
480             if (typeof _params.no_xulG == 'undefined') {
481                 if (typeof _params.modal_xulG != 'undefined') {
482                     if (typeof xulG != 'undefined' && xulG.not_modal) {
483                         // for interfaces that used to be modal but aren't now, do nothing
484                     } else {
485                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
486                         var key = location.pathname + location.search + location.hash;
487                         //dump('xul_param, considering modal key = ' + key + '\n');
488                         if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
489                             xulG = data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ];
490                         }
491                     }
492                 }
493                 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
494                     var x = xulG[ param_name ];
495                     //dump('\tfound via xulG = ' + x + '\n');
496                     if (typeof _params.JSON2js_if_xulG != 'undefined') {
497                         x = JSON2js( x );
498                         //dump('\tJSON2js = ' + x + '\n');
499                     }
500                     if (typeof _params.concat == 'undefined') {
501                         //alert(param_name + ' x = ' + x);
502                         return x; // value
503                     } else {
504                         if (value) {
505                             if (value.constructor != Array) value = [ value ];
506                             value = value.concat(x);
507                         } else {
508                             value = x;
509                         }
510                     }
511                 }
512             }
513             if (typeof _params.no_xpcom == 'undefined') {
514                 /* the field names used for temp variables in the global stash tend to be more unique than xuLG or CGI param names, to avoid collisions */
515                 if (typeof _params.stash_name != 'undefined') { 
516                     JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
517                     if (typeof data[ _params.stash_name ] != 'undefined') {
518                         var x = data[ _params.stash_name ];
519                         //dump('\tfound via xpcom = ' + x + '\n');
520                         if (typeof _params.JSON2js_if_xpcom != 'undefined') {
521                             x = JSON2js( x );
522                             //dump('\tJSON2js = ' + x + '\n');
523                         }
524                         if (_params.clear_xpcom) { 
525                             data[ _params.stash_name ] = undefined; data.stash( _params.stash_name ); 
526                         }
527                         if (typeof _params.concat == 'undefined') {
528                             //alert(param_name + ' x = ' + x);
529                             return x; // value
530                         } else {
531                             if (value) {
532                                 if (value.constructor != Array) value = [ value ];
533                                 value = value.concat(x);
534                             } else {
535                                 value = x;
536                             }
537                         }
538                     }
539                 }
540             }
541             //alert(param_name + ' value = ' + value);
542             return value;
543         } catch(E) {
544             dump('xul_param error: ' + E + '\n');
545         }
546     }
547
548     function get_bool(a) {
549         // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
550         // So false includes 'f', '', '0', 0, null, and undefined
551         if (a == 'f') return false;
552         if (a == 'F') return false;
553         if (a == '0') return false;
554         if (a) return true; else return false;
555     }
556
557     function get_localized_bool(a) {
558         var Strings = $('offlineStrings') || $('commonStrings');
559         return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
560     }
561
562     function get_db_true() {
563         return 't';
564     }
565
566     function get_db_false() {
567         return 'f';
568     }
569
570     function copy_to_clipboard(ev) {
571         try {
572             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
573             var text;
574             if (typeof ev == 'object') {
575                 if (typeof ev.target != 'undefined') {
576                     if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
577                     if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
578                 }
579             } else if (typeof ev == 'string') {
580                 text = ev;
581             }
582             const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
583                 .getService(Components.interfaces.nsIClipboardHelper);
584             gClipboardHelper.copyString(text);
585             var Strings = $('offlineStrings') || $('commonStrings');
586             alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
587         } catch(E) {
588             var Strings = $('offlineStrings') || $('commonStrings');
589             alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));    
590         }
591     }
592
593     function clear_the_cache() {
594         try {
595             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
596             var cacheClass         = Components.classes["@mozilla.org/network/cache-service;1"];
597             var cacheService    = cacheClass.getService(Components.interfaces.nsICacheService);
598             cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
599             cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
600         } catch(E) {
601             var Strings = $('offlineStrings') || $('commonStrings');
602             alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
603         }
604     }
605
606     function toOpenWindowByType(inType, uri) {
607         var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
608         window.open(uri, "_blank", winopts);
609     }
610
611     function url_prefix(url) {
612         if (url.match(/^\//)) url = urls.remote + url;
613         if (! url.match(/^(http|chrome):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
614         dump('url_prefix = ' + url + '\n');
615         return url;
616     }
617
618     function widget_prompt(node,args) {
619         // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method
620         // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node
621         // if 'method', then the method key will reference a function that returns the value
622         try {
623             if (!node) { return false; }
624             if (!args) { args = {}; }
625             args[ 'widget' ] = node;
626
627             var url = location.protocol == 'chrome'
628                 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
629                 : '/xul/server/util/widget_prompt.xul';
630
631             JSAN.use('util.window'); var win = new util.window();
632             var my_xulG = win.open(
633                 url,
634                 args.title || 'widget_prompt',
635                 'chrome,modal',
636                 args
637             );
638
639             if (my_xulG.status == 'incomplete') {
640                 return false;
641             } else {
642                 return my_xulG.value;
643             }
644         } catch(E) {
645             alert('Error in global_utils.js, widget_prompt(): ' + E);
646         }
647     }