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