]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/global_util.js
Merge remote branch 'working/user/dbwells/shared_siss_editor'
[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_name':
146                     return data.ws_name;
147                 break;
148                 case 'ws_id' :
149                     return data.list.au[0].wsid();
150                 break;
151                 case 'ws_ou' :
152                     return data.list.au[0].ws_ou();
153                 break;
154                 case 'ws_ou_shortname' :
155                     return data.hash.aou[ data.list.au[0].ws_ou() ].shortname();
156                 break;
157                 case 'authtime' :
158                     return data.session.authtime;
159                 break;
160                 case 'key':
161                 default:
162                     return data.session.key;
163                 break;
164             }
165         } catch(E) {
166             alert(location.href + '\nError in global_utils.js, ses(): ' + E);
167             throw(E);
168         }
169     }
170
171     function font_helper() {
172         try {
173             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
174             removeCSSClass(document.documentElement,'ALL_FONTS_LARGER');
175             removeCSSClass(document.documentElement,'ALL_FONTS_SMALLER');
176             removeCSSClass(document.documentElement,'ALL_FONTS_XX_SMALL');
177             removeCSSClass(document.documentElement,'ALL_FONTS_X_SMALL');
178             removeCSSClass(document.documentElement,'ALL_FONTS_SMALL');
179             removeCSSClass(document.documentElement,'ALL_FONTS_MEDIUM');
180             removeCSSClass(document.documentElement,'ALL_FONTS_LARGE');
181             removeCSSClass(document.documentElement,'ALL_FONTS_X_LARGE');
182             removeCSSClass(document.documentElement,'ALL_FONTS_XX_LARGE');
183             addCSSClass(document.documentElement,data.global_font_adjust);
184         } catch(E) {
185             var Strings = $('offlineStrings') || $('commonStrings');
186             alert(Strings.getFormattedString('openils.global_util.font_size.error', [E]));
187         }
188     }
189
190     function oils_persist(e,cancelable) {
191         try {
192             if (!e) { return; }
193             if (typeof cancelable == 'undefined') { cancelable = false; } 
194             var evt = document.createEvent("Events");
195             evt.initEvent( 'oils_persist', false, cancelable ); // event name, bubbles, cancelable
196             e.dispatchEvent(evt);
197         } catch(E) {
198             alert('Error with oils_persist():' + E);
199         }
200     }
201
202     function persist_helper(base_key_suffix) {
203         try {
204             if (base_key_suffix) {
205                 base_key_suffix = base_key_suffix.replace(/[^A-Za-z]/g,'_') + '_';
206             } else {
207                 base_key_suffix = '';
208             }
209
210             function gen_event_handler(etype,node) {
211                 return function(ev) {
212                     try {
213                         oils_persist(ev.target);
214                     } catch(E) {
215                         alert('Error in persist_helper, firing virtual event oils_persist after ' + etype + ' event on ' + node.nodeName + '.id = ' + node.id + ': ' + E);
216                     }
217                 };
218             };
219
220             function gen_oils_persist_handler(bk,node) {
221                 return function(ev) {
222                     try {
223                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
224                         var target;
225                         if (ev.target.nodeName == 'command') {
226                             target = node;
227                             if (ev.explicitOriginalTarget != node) return;
228                         } else {
229                             target = ev.target;
230                             if (target == window) {
231                                 target = window.document.documentElement;
232                             }
233                         }
234                         var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
235                         var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + target.getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
236                         var attribute_list = target.getAttribute('oils_persist').split(' ');
237                         dump('on_oils_persist: <<< ' + target.nodeName + '.id = ' + target.id + '\t' + bk + '\n');
238                         for (var j = 0; j < attribute_list.length; j++) {
239                             var key = base_key + attribute_list[j];
240                             var value;
241                             try {
242                                 value = encodeURI(target.getAttribute( attribute_list[j] ));
243                             } catch(E) {
244                                 dump('Error in persist_helper with encodeURI: ' + E + '\n');
245                                 value = target.getAttribute( attribute_list[j] );
246                             }
247                             if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( target.nodeName ) > -1 ) {
248                                 value = target.checked;
249                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
250                             } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( target.nodeName ) > -1 ) {
251                                 value = target.value;
252                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
253                             } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( target.nodeName ) > -1 ) {
254                                 value = target.value;
255                                 dump('\t' + value + ' <== .' + attribute_list[j] + '\n');
256                             } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( target.nodeName ) > -1 ) {
257                                 value = window.windowState;
258                                 dump('\t' + value + ' <== window.windowState, @' + attribute_list[j] + '\n');
259                             } else if ( attribute_list[j] == 'height' && ['window'].indexOf( target.nodeName ) > -1 ) {
260                                 value = window.outerHeight;
261                                 dump('\t' + value + ' <== window.outerHeight, @' + attribute_list[j] + '\n');
262                             } else if ( attribute_list[j] == 'width' && ['window'].indexOf( target.nodeName ) > -1 ) {
263                                 value = window.outerWidth;
264                                 dump('\t' + value + ' <== window.outerWidth, @' + attribute_list[j] + '\n');
265                             } else {
266                                 dump('\t' + value + ' <== @' + attribute_list[j] + '\n');
267                             }
268                             prefs.setCharPref( key, value );
269                             // TODO: Need to add logic for splitter repositioning, grippy state, etc.
270                             // NOTE: oils_persist_peers and oils_persist="width" on those peers can help with the elements adjacent to a splitter
271                         }
272                         if (target.hasAttribute('oils_persist_peers') && ! ev.cancelable) { // We abuse the .cancelable field on the oils_persist event to prevent looping
273                             var peer_list = target.getAttribute('oils_persist_peers').split(' ');
274                             for (var j = 0; j < peer_list.length; j++) {
275                                 dump('on_oils_persist: dispatching oils_persist to peer ' + peer_list[j] + '\n');
276                                 oils_persist( document.getElementById( peer_list[j] ), true );
277                             } 
278                         }
279                     } catch(E) {
280                         alert('Error in persist_helper() event listener for ' + bk + ': ' + E);
281                     }
282                 };
283             }
284
285             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
286             var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
287             var nodes = document.getElementsByAttribute('oils_persist','*');
288             for (var i = 0; i < nodes.length; i++) {
289                 var filename = location.pathname.split('/')[ location.pathname.split('/').length - 1 ];
290                 var base_key = 'oils_persist_' + String(location.hostname + '_' + filename + '_' + nodes[i].getAttribute('id')).replace('/','_','g') + '_' + base_key_suffix;
291                 var attribute_list = nodes[i].getAttribute('oils_persist').split(' ');
292                 dump('persist_helper: >>> ' + nodes[i].nodeName + '.id = ' + nodes[i].id + '\t' + base_key + '\n');
293                 for (var j = 0; j < attribute_list.length; j++) {
294                     var key = base_key + attribute_list[j];
295                     var has_key = prefs.prefHasUserValue(key);
296                     var value;
297                     try {
298                         value = has_key ? decodeURI(prefs.getCharPref(key)) : null;
299                     } catch(E) {
300                         dump('Error in persist_helper with decodeURI: ' + E + '\n');
301                         value = has_key ? prefs.getCharPref(key) : null;
302                     }
303                     if (value == 'true') { value = true; }
304                     if (value == 'false') { value = false; }
305                     if (has_key) {
306                         if ( attribute_list[j] == 'checked' && ['checkbox','toolbarbutton'].indexOf( nodes[i].nodeName ) > -1 ) {
307                             nodes[i].checked = value; 
308                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
309                             if (!value) {
310                                 nodes[i].removeAttribute('checked');
311                                 dump('\tremoving @checked\n');
312                             }
313                         } else if ( attribute_list[j] == 'value' && ['textbox'].indexOf( nodes[i].nodeName ) > -1 ) {
314                             nodes[i].value = value;
315                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');
316                         } else if ( attribute_list[j] == 'value' && ['menulist'].indexOf( nodes[i].nodeName ) > -1 ) {
317                             nodes[i].value = value;
318                             dump('\t' + value + ' ==> .' + attribute_list[j] + '\n');       
319                         } else if ( attribute_list[j] == 'sizemode' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
320                             switch(value) {
321                                 case window.STATE_MAXIMIZED:
322                                     window.maximize();
323                                     break;
324                                 case window.STATE_MINIMIZED:
325                                     window.minimize();
326                                     break;
327                             };
328                             dump('\t' + value + ' ==> window.windowState, @' + attribute_list[j] + '\n');
329                         } else if ( attribute_list[j] == 'height' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
330                             window.outerHeight = value;
331                             dump('\t' + value + ' ==> window.outerHeight, @' + attribute_list[j] + '\n');
332                         } else if ( attribute_list[j] == 'width' && ['window'].indexOf( nodes[i].nodeName ) > -1 ) {
333                             window.outerWidth = value;
334                             dump('\t' + value + ' ==> window.outerWidth, @' + attribute_list[j] + '\n');
335                         } else {
336                             nodes[i].setAttribute( attribute_list[j], value);
337                             dump('\t' + value + ' ==> @' + attribute_list[j] + '\n');
338                         }
339                     }
340                 }
341                 var cmd = nodes[i].getAttribute('command');
342                 var cmd_el = document.getElementById(cmd);
343                 if (nodes[i].disabled == false && nodes[i].hidden == false) {
344                     var no_poke = nodes[i].getAttribute('oils_persist_no_poke');
345                     if (no_poke && no_poke == 'true') {
346                         // Timing issue for some checkboxes; don't poke them with an event
347                         dump('\tnot poking\n');
348                     } else {
349                         if (cmd_el) {
350                             dump('\tpoking @command\n');
351                             var evt = document.createEvent("Events");
352                             evt.initEvent( 'command', true, true );
353                             cmd_el.dispatchEvent(evt);
354                         } else {
355                             dump('\tpoking\n');
356                             var evt = document.createEvent("Events");
357                             evt.initEvent( 'command', true, true );
358                             nodes[i].dispatchEvent(evt);
359                         }
360                     }
361                 }
362                 if (cmd_el) {
363                     cmd_el.addEventListener(
364                         'command',
365                         gen_event_handler('command',cmd_el),
366                         false
367                     );
368                     cmd_el.addEventListener(
369                         'oils_persist',
370                         gen_oils_persist_handler( base_key, nodes[i] ),
371                         false
372                     );
373                 } else {
374                     var node = nodes[i];
375                     var event_types = [];
376                     if (node.hasAttribute('oils_persist_events')) {
377                         var event_type_list = node.getAttribute('oils_persist_events').split(' ');
378                         for (var j = 0; j < event_type_list.length; j++) {
379                             event_types.push( event_type_list[j] );
380                         }
381                     } else {
382                         if (node.nodeName == 'textbox') { 
383                             event_types.push('change');
384                         } else if (node.nodeName == 'menulist') { 
385                             event_types.push('select');  
386                         } else if (node.nodeName == 'window') {
387                             event_types.push('resize'); 
388                             node = window; // xul window is an element of window.document
389                         } else {
390                             event_types.push('command'); 
391                         }
392                     }
393                     for (var j = 0; j < event_types.length; j++) {
394                         node.addEventListener(
395                             event_types[j],
396                             gen_event_handler(event_types[j],node),
397                             false
398                         );
399                     }
400                     node.addEventListener(
401                         'oils_persist',
402                         gen_oils_persist_handler( base_key, node ),
403                         false
404                     );
405                 }
406             }
407         } catch(E) {
408             alert('Error in persist_helper(): ' + E);
409         }
410     }
411
412     function getKeys(o) {
413         var keys = [];
414         for (var k in o) keys.push(k);
415         return keys;
416     }
417
418     function get_contentWindow(frame) {
419         try {
420             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
421             if (frame && frame.contentWindow) {
422                 try {
423                     if (typeof frame.contentWindow.wrappedJSObject != 'undefined') {
424                                      return frame.contentWindow.wrappedJSObject;
425                           }
426                 } catch(E) {
427                     var Strings = $('offlineStrings') || $('commonStrings');
428                     alert(Strings.getFormattedString('openils.global_util.content_window_jsobject.error', [frame, E]));
429                 }
430                 return frame.contentWindow;
431             } else {
432                 return null;
433             }
434         } catch(E) {
435             var Strings = $('offlineStrings') || $('commonStrings');
436             alert(Strings.getFormattedString('openils.global_util.content_window.error', [frame, E]));
437         }
438     }
439
440     function update_modal_xulG(v) {
441         try {
442             if (typeof xulG != "undefined" && xulG.not_modal) {
443                 xulG = v;
444                 xulG.not_modal = true;
445                 return;
446             }
447
448             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
449             var key = location.pathname + location.search + location.hash;
450             if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
451                 data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ] = v;
452                 data.stash('modal_xulG_stack');
453             }
454         } catch(E) {
455             alert('FIXME: update_modal_xulG => ' + E);
456         }
457     }
458
459     function xul_param(param_name,_params) {
460         /* 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 */
461         try {
462             //dump('xul_param('+param_name+','+js2JSON(_params)+')\n');
463             var value = undefined; if (!_params) _params = {};
464             if (typeof _params.no_cgi == 'undefined') {
465                 var cgi = new CGI();
466                 if (cgi.param(param_name)) {
467                     var x = cgi.param(param_name);
468                     //dump('\tfound via location.href = ' + x + '\n');
469                     if (typeof _params.JSON2js_if_cgi != 'undefined') {
470                         x = JSON2js( x );
471                         //dump('\tJSON2js = ' + x + '\n');
472                     }
473                     if (typeof _params.concat == 'undefined') {
474                         //alert(param_name + ' x = ' + x);
475                         return x; // value
476                     } else {
477                         if (value) {
478                             if (value.constructor != Array) value = [ value ];
479                             value = value.concat(x);
480                         } else {
481                             value = x;
482                         }
483                     }
484                 }
485             }
486             if (typeof _params.no_xulG == 'undefined') {
487                 if (typeof _params.modal_xulG != 'undefined') {
488                     if (typeof xulG != 'undefined' && xulG.not_modal) {
489                         // for interfaces that used to be modal but aren't now, do nothing
490                     } else {
491                         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
492                         var key = location.pathname + location.search + location.hash;
493                         //dump('xul_param, considering modal key = ' + key + '\n');
494                         if (typeof data.modal_xulG_stack != 'undefined' && typeof data.modal_xulG_stack[key] != 'undefined') {
495                             xulG = data.modal_xulG_stack[key][ data.modal_xulG_stack[key].length - 1 ];
496                         }
497                     }
498                 }
499                 if (typeof xulG == 'object' && typeof xulG[ param_name ] != 'undefined') {
500                     var x = xulG[ param_name ];
501                     //dump('\tfound via xulG = ' + x + '\n');
502                     if (typeof _params.JSON2js_if_xulG != 'undefined') {
503                         x = JSON2js( x );
504                         //dump('\tJSON2js = ' + x + '\n');
505                     }
506                     if (typeof _params.concat == 'undefined') {
507                         //alert(param_name + ' x = ' + x);
508                         return x; // value
509                     } else {
510                         if (value) {
511                             if (value.constructor != Array) value = [ value ];
512                             value = value.concat(x);
513                         } else {
514                             value = x;
515                         }
516                     }
517                 }
518             }
519             if (typeof _params.no_xpcom == 'undefined') {
520                 /* 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 */
521                 if (typeof _params.stash_name != 'undefined') { 
522                     JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
523                     if (typeof data[ _params.stash_name ] != 'undefined') {
524                         var x = data[ _params.stash_name ];
525                         //dump('\tfound via xpcom = ' + x + '\n');
526                         if (typeof _params.JSON2js_if_xpcom != 'undefined') {
527                             x = JSON2js( x );
528                             //dump('\tJSON2js = ' + x + '\n');
529                         }
530                         if (_params.clear_xpcom) { 
531                             data[ _params.stash_name ] = undefined; data.stash( _params.stash_name ); 
532                         }
533                         if (typeof _params.concat == 'undefined') {
534                             //alert(param_name + ' x = ' + x);
535                             return x; // value
536                         } else {
537                             if (value) {
538                                 if (value.constructor != Array) value = [ value ];
539                                 value = value.concat(x);
540                             } else {
541                                 value = x;
542                             }
543                         }
544                     }
545                 }
546             }
547             //alert(param_name + ' value = ' + value);
548             return value;
549         } catch(E) {
550             dump('xul_param error: ' + E + '\n');
551         }
552     }
553
554     function get_bool(a) {
555         // Normal javascript interpretation except 'f' == false, per postgres, and 'F' == false, and '0' == false (newer JSON is returning '0' instead of 0 in cases)
556         // So false includes 'f', '', '0', 0, null, and undefined
557         if (a == 'f') return false;
558         if (a == 'F') return false;
559         if (a == '0') return false;
560         if (a) return true; else return false;
561     }
562
563     function get_localized_bool(a) {
564         var Strings = $('offlineStrings') || $('commonStrings');
565         return get_bool(a) ? Strings.getString('common.yes') : Strings.getString('common.no');
566     }
567
568     function get_db_true() {
569         return 't';
570     }
571
572     function get_db_false() {
573         return 'f';
574     }
575
576     function copy_to_clipboard(ev) {
577         try {
578             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
579             var text;
580             if (typeof ev == 'object') {
581                 if (typeof ev.target != 'undefined') {
582                     if (typeof ev.target.textContent != 'undefined') if (ev.target.textContent) text = ev.target.textContent;
583                     if (typeof ev.target.value != 'undefined') if (ev.target.value) text = ev.target.value;
584                 }
585             } else if (typeof ev == 'string') {
586                 text = ev;
587             }
588             const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
589                 .getService(Components.interfaces.nsIClipboardHelper);
590             gClipboardHelper.copyString(text);
591             var Strings = $('offlineStrings') || $('commonStrings');
592             alert(Strings.getFormattedString('openils.global_util.clipboard', [text]));
593         } catch(E) {
594             var Strings = $('offlineStrings') || $('commonStrings');
595             alert(Strings.getFormattedString('openils.global_util.clipboard.error', [E]));    
596         }
597     }
598
599     function clear_the_cache() {
600         try {
601             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
602             var cacheClass         = Components.classes["@mozilla.org/network/cache-service;1"];
603             var cacheService    = cacheClass.getService(Components.interfaces.nsICacheService);
604             cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
605             cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
606         } catch(E) {
607             var Strings = $('offlineStrings') || $('commonStrings');
608             alert(Strings.getFormattedString('openils.global_util.clear_cache.error', [E]));
609         }
610     }
611
612     function toOpenWindowByType(inType, uri) {
613         var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
614         window.open(uri, "_blank", winopts);
615     }
616
617     function url_prefix(url) {
618         if (url.match(/^\//)) url = urls.remote + url;
619         if (! url.match(/^(http|chrome):\/\//) && ! url.match(/^data:/) ) url = 'http://' + url;
620         dump('url_prefix = ' + url + '\n');
621         return url;
622     }
623
624     function widget_prompt(node,args) {
625         // args is an object that may contain the following keys: title, desc, ok_label, ok_accesskey, cancel_label, cancel_accesskey, access, method
626         // access may contain 'property' or 'attribute' or 'method' for retrieving the value from the node
627         // if 'method', then the method key will reference a function that returns the value
628         try {
629             if (!node) { return false; }
630             if (!args) { args = {}; }
631             args[ 'widget' ] = node;
632
633             var url = location.protocol == 'chrome'
634                 ? 'chrome://open_ils_staff_client/content/util/widget_prompt.xul'
635                 : '/xul/server/util/widget_prompt.xul';
636
637             JSAN.use('util.window'); var win = new util.window();
638             var my_xulG = win.open(
639                 url,
640                 args.title || 'widget_prompt',
641                 'chrome,modal',
642                 args
643             );
644
645             if (my_xulG.status == 'incomplete') {
646                 return false;
647             } else {
648                 return my_xulG.value;
649             }
650         } catch(E) {
651             alert('Error in global_utils.js, widget_prompt(): ' + E);
652         }
653     }