]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/opac_utils.js
removed unprotected dump statement
[Evergreen.git] / Open-ILS / web / opac / common / js / opac_utils.js
1 /* - Request ------------------------------------------------------------- */
2
3 /* define it again here for pages that don't load RemoteRequest */
4 function isXUL() { try { if(IAMXUL) return true;}catch(e){return false;}; }
5
6
7 var cookieManager = new HTTP.Cookies();
8
9 var __ilsEvent; /* the last event the occurred */
10
11 function Request(type) {
12
13         var s = type.split(":");
14         if(s[2] == "1" && isXUL()) s[1] += ".staff";
15         this.request = new RemoteRequest(s[0], s[1]);
16         var p = [];
17
18         for( var x = 1; x!= arguments.length; x++ ) {
19                 p.push(arguments[x]);
20                 this.request.addParam(arguments[x]);
21         }
22
23         if( getDebug() ) {
24                 var str = "";
25                 for( var i = 0; i != p.length; i++ ) {
26                         if( i > 0 ) str += ", "
27                         str += js2JSON(p[i]);
28                 }
29                 _debug('request ' + s[0] + ' ' + s[1] + ' ' + str );
30         }
31 }
32
33 Request.prototype.callback = function(cal) {this.request.setCompleteCallback(cal);}
34 Request.prototype.send          = function(block){this.request.send(block);}
35 Request.prototype.result        = function(){return this.request.getResultObject();}
36
37 function showCanvas() {
38         for( var x in G.ui.altcanvas ) {
39                 hideMe(G.ui.altcanvas[x]);
40         }
41         hideMe(G.ui.common.loading);
42         unHideMe(G.ui.common.canvas_main);
43         try{G.ui.searchbar.text.focus();}catch(e){}
44 }
45
46 function swapCanvas(newNode) {
47         for( var x in G.ui.altcanvas ) 
48                 hideMe(G.ui.altcanvas[x]);
49
50         hideMe(G.ui.common.loading);
51         hideMe(G.ui.common.canvas_main);
52         unHideMe(newNode);
53 }
54
55 /* finds the name of the current page */
56 var currentPage = null;
57 function findCurrentPage() {
58         if(currentPage) return currentPage;
59
60         var pages = [];
61         for( var p in config.page ) pages.push(config.page[p]);
62         pages = pages.sort( function(a,b){ return - (a.length - b.length); } );
63
64         var path = location.pathname;
65         if(!path.match(/.*\.xml$/))
66                 path += "index.xml"; /* in case they go to  / */
67
68         var page = null;
69         for( var p in pages ) {
70                 if( path.indexOf(pages[p]) != -1)
71                         page = pages[p];
72         }
73
74         for( var p in config.page ) {
75                 if(config.page[p] == page) {
76                         currentPage = p;
77                         return p;
78                 }
79         }
80         return null;
81 }
82
83
84 /* sets all of the params values  ----------------------------- */
85 function initParams() {
86         var cgi = new CGI();    
87
88         TERM            = cgi.param(PARAM_TERM);
89         STYPE           = cgi.param(PARAM_STYPE);
90         FORM            = cgi.param(PARAM_FORM);
91         LOCATION        = parseInt(cgi.param(PARAM_LOCATION));
92         ORIGLOC = parseInt(cgi.param(PARAM_ORIGLOC));
93         DEPTH           = parseInt(cgi.param(PARAM_DEPTH));
94         OFFSET  = parseInt(cgi.param(PARAM_OFFSET));
95         COUNT           = parseInt(cgi.param(PARAM_COUNT));
96         HITCOUNT        = parseInt(cgi.param(PARAM_HITCOUNT));
97         MRID            = parseInt(cgi.param(PARAM_MRID));
98         RID             = parseInt(cgi.param(PARAM_RID));
99         AUTHTIME        = parseInt(cgi.param(PARAM_AUTHTIME));
100         ADVTERM = cgi.param(PARAM_ADVTERM);
101         ADVTYPE = cgi.param(PARAM_ADVTYPE);
102         RTYPE           = cgi.param(PARAM_RTYPE);
103         SORT            = cgi.param(PARAM_SORT);
104         SORT_DIR        = cgi.param(PARAM_SORT_DIR);
105         DEBUG           = cgi.param(PARAM_DEBUG);
106         CALLNUM = cgi.param(PARAM_CN);
107         LITFORM = cgi.param(PARAM_LITFORM);
108         ITEMFORM        = cgi.param(PARAM_ITEMFORM);
109         ITEMTYPE        = cgi.param(PARAM_ITEMTYPE);
110         AUDIENCE        = cgi.param(PARAM_AUDIENCE);
111         SEARCHES = cgi.param(PARAM_SEARCHES);
112         LANGUAGE        = cgi.param(PARAM_LANGUAGE);
113         TFORM           = cgi.param(PARAM_TFORM);
114         RDEPTH  = cgi.param(PARAM_RDEPTH);
115
116         /* set up some sane defaults */
117         if(isNaN(LOCATION))     LOCATION        = 1;
118         if(isNaN(DEPTH))                DEPTH           = 0;
119         if(isNaN(RDEPTH))               RDEPTH  = 0;
120         if(isNaN(OFFSET))               OFFSET  = 0;
121         if(isNaN(COUNT))                COUNT           = 10;
122         if(isNaN(HITCOUNT))     HITCOUNT        = 0;
123         if(isNaN(MRID))         MRID            = 0;
124         if(isNaN(RID))                  RID             = 0;
125         if(isNaN(ORIGLOC))      ORIGLOC = 1;
126         if(isNaN(AUTHTIME))     AUTHTIME        = 0;
127         if(ADVTERM==null)               ADVTERM = "";
128 }
129
130 function clearSearchParams() {
131         TERM            = null;
132         STYPE           = null;
133         FORM            = null;
134         OFFSET  = 0;
135         HITCOUNT = 0;  
136         ADVTERM  = null;
137         ADVTYPE  = null;
138         MRID            = null;
139         RID             = null;
140         RTYPE           = null;
141         SORT            = null;
142         SORT_DIR = null;
143         RLIST           = null;
144         CALLNUM = null;
145         LITFORM = null;
146         ITEMFORM = null;
147         ITEMTYPE = null;
148         AUDIENCE = null;
149         SEARCHES = null;
150         LANGUAGE = null;
151         RDEPTH  = null;
152 }
153
154
155 function initCookies() {
156         FONTSIZE = "regular";
157         var font = cookieManager.read(COOKIE_FONT);
158         scaleFonts(font);
159         if(font) FONTSIZE = font;
160         SKIN = cookieManager.read(COOKIE_SKIN);
161 }
162
163 /* URL param accessors */
164 function getTerm(){return TERM;}
165 function getStype(){return STYPE;}
166 function getLocation(){return LOCATION;}
167 function getDepth(){return DEPTH;}
168 function getForm(){return FORM;}
169 function getTform(){return TFORM;}
170 function getOffset(){return OFFSET;}
171 function getDisplayCount(){return COUNT;}
172 function getHitCount(){return HITCOUNT;}
173 function getMrid(){return MRID;};
174 function getRid(){return RID;};
175 function getOrigLocation(){return ORIGLOC;}
176 function getAuthtime() { return AUTHTIME; }
177 function getSearchBarExtras(){return SBEXTRAS;}
178 function getFontSize(){return FONTSIZE;};
179 function getSkin(){return SKIN;};
180 function getAdvTerm(){return ADVTERM;}
181 function getAdvType(){return ADVTYPE;}
182 function getRtype(){return RTYPE;}
183 function getSort(){return SORT;}
184 function getSortDir(){return SORT_DIR;}
185 function getDebug(){return DEBUG;}
186 function getCallnumber() { return CALLNUM; }
187 function getLitForm() { return LITFORM; }
188 function getItemForm() { return ITEMFORM; }
189 function getItemType() { return ITEMTYPE; }
190 function getAudience() { return AUDIENCE; }
191 function getSearches() { return SEARCHES; }
192 function getLanguage() { return LANGUAGE; }
193 function getRdepth() { return RDEPTH; }
194
195
196 function findBasePath() {
197         var path = location.pathname;
198         if(!path.match(/.*\.xml$/)) path += "index.xml"; 
199         var idx = path.indexOf(config.page[findCurrentPage()]);
200         return path.substring(0, idx);
201 }
202
203 function findBaseURL(ssl) {
204         var path = findBasePath();
205         var proto = (ssl) ? "https:" : "http:";
206         return proto + "//" + location.host + path;
207 }
208
209 /*
210 function buildISBNSrc(isbn) {
211         return "http://" + location.host + "/jackets/" + isbn;
212 }
213 */
214
215 function buildImageLink(name, ssl) {
216         return findBaseURL(ssl) + "../../../../images/" + name;
217 }
218
219 function buildExtrasLink(name, ssl) {
220         return findBaseURL(ssl) + "../../../../extras/" + name;
221 }
222
223 var consoleService;
224 function _debug(str) { 
225         try { dump('dbg: ' + str + '\n'); } catch(e) {} 
226
227         /* potentially useful, but usually just annoying */
228         /*
229         if(!IE) {
230                 if(!consoleService) {
231                         try {
232                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
233                                 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
234                                         .getService(Components.interfaces.nsIConsoleService);
235                         } catch(e) {}
236                 }
237         
238                 try {
239                         if(consoleService) {
240                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
241                                 consoleService.logStringMessage(str + '\n');
242                         }
243                 } catch(e){}
244         }
245         */
246 }
247
248 function  buildOPACLink(args, slim, ssl) {
249
250         if(!args) args = {};
251         var string = "";
252
253         if(!slim) {
254                 string = findBaseURL(ssl);
255                 if(args.page) string += config.page[args.page];
256                 else string += config.page[findCurrentPage()];
257         }
258
259         string += "?";
260
261         for( var x in args ) {
262                 var v = args[x];
263                 if(x == "page" || v == null || v == undefined || v+'' == 'NaN' ) continue;
264                 if(x == PARAM_OFFSET && v == 0) continue;
265                 /*if(x == PARAM_LOCATION && v == 1) continue;*/
266                 if(x == PARAM_DEPTH && v == 0) continue;
267                 if(x == PARAM_COUNT && v == 10) continue;
268                 if(x == PARAM_FORM && v == 'all' ) continue;
269                 if( instanceOf(v, Array) && v.length ) {
270                         for( var i = 0; i < v.length; i++ ) {
271                                 string += "&" + x + "=" + encodeURIComponent(v[i]);
272                         }
273                 } else {
274                         string += "&" + x + "=" + encodeURIComponent(v);
275                 }
276         }
277
278         if(getDebug())
279                 string += _appendParam(DEBUG,   PARAM_DEBUG, args, getDebug, string);
280         if(getOrigLocation() != 1) 
281                 string += _appendParam(ORIGLOC, PARAM_ORIGLOC, args, getOrigLocation, string);
282         if(getTerm()) 
283                 string += _appendParam(TERM,            PARAM_TERM, args, getTerm, string);
284         if(getStype()) 
285                 string += _appendParam(STYPE,           PARAM_STYPE, args, getStype, string);
286         if(getLocation() != 1) 
287                 string += _appendParam(LOCATION, PARAM_LOCATION, args, getLocation, string);
288         if(getDepth() != 0) 
289                 string += _appendParam(DEPTH,           PARAM_DEPTH, args, getDepth, string);
290         if(getForm() && (getForm() != 'all') ) 
291                 string += _appendParam(FORM,            PARAM_FORM, args, getForm, string);
292         if(getTform() && (getTform() != 'all') ) 
293                 string += _appendParam(TFORM,           PARAM_TFORM, args, getTform, string);
294         if(getOffset() != 0) 
295                 string += _appendParam(OFFSET,  PARAM_OFFSET, args, getOffset, string);
296         if(getDisplayCount() != 10) 
297                 string += _appendParam(COUNT,           PARAM_COUNT, args, getDisplayCount, string);
298         if(getHitCount()) 
299                 string += _appendParam(HITCOUNT, PARAM_HITCOUNT, args, getHitCount, string);
300         if(getMrid())
301                 string += _appendParam(MRID,            PARAM_MRID, args, getMrid, string);
302         if(getRid())
303                 string += _appendParam(RID,             PARAM_RID, args, getRid, string);
304         if(getAuthtime())
305                 string += _appendParam(AUTHTIME,        PARAM_AUTHTIME, args, getAuthtime, string);
306         if(getAdvTerm())
307                 string += _appendParam(ADVTERM, PARAM_ADVTERM, args, getAdvTerm, string);
308         if(getAdvType())
309                 string += _appendParam(ADVTYPE, PARAM_ADVTYPE, args, getAdvType, string);
310         if(getRtype())
311                 string += _appendParam(RTYPE,           PARAM_RTYPE, args, getRtype, string);
312         if(getItemForm())
313                 string += _appendParam(ITEMFORM,        PARAM_ITEMFORM, args, getItemForm, string);
314         if(getItemType())
315                 string += _appendParam(ITEMTYPE,        PARAM_ITEMTYPE, args, getItemType, string);
316         if(getLitForm())
317                 string += _appendParam(LITFORM, PARAM_LITFORM, args, getLitForm, string);
318         if(getAudience())
319                 string += _appendParam(AUDIENCE,        PARAM_AUDIENCE, args, getAudience, string);
320         if(getSearches())
321                 string += _appendParam(SEARCHES,        PARAM_SEARCHES, args, getSearches, string);
322         if(getLanguage())
323                 string += _appendParam(LANGUAGE,        PARAM_LANGUAGE, args, getLanguage, string);
324         if(getRdepth() != null)
325                 string += _appendParam(RDEPTH,  PARAM_RDEPTH, args, getRdepth, string);
326
327         return string.replace(/\&$/,'').replace(/\?\&/,"?");    
328 }
329
330 var xx = 1;
331 function _appendParam( fieldVar, fieldName, overrideArgs, getFunc, string ) {
332
333         var ret = "";
334
335
336         if(     fieldVar != null && 
337                         (fieldVar +'' != 'NaN') && 
338                         overrideArgs[fieldName] == null &&
339                         getFunc() != null &&
340                         getFunc() != '' ) {
341
342                 ret = "&" + fieldName + "=" + encodeURIComponent(getFunc());
343         }
344
345         return ret;
346 }
347
348 /* ----------------------------------------------------------------------- */
349 function cleanISBN(isbn) {
350    if(isbn) {
351       isbn = isbn.toString().replace(/^\s+/,"");
352       var idx = isbn.indexOf(" ");
353       if(idx > -1) { isbn = isbn.substring(0, idx); }
354    } else isbn = "";
355    return isbn;
356 }       
357
358
359 /* builds a link that goes to the title listings for a metarecord */
360 function buildTitleLink(rec, link) {
361         if(!rec) return;
362         link.appendChild(text(normalize(truncate(rec.title(), 65))));
363         var args = {};
364         args.page = RRESULT;
365         args[PARAM_OFFSET] = 0;
366         args[PARAM_MRID] = rec.doc_id();
367         args[PARAM_RTYPE] = RTYPE_MRID;
368         link.setAttribute("href", buildOPACLink(args));
369 }
370
371 function buildTitleDetailLink(rec, link) {
372         if(!rec) return;
373         link.appendChild(text(normalize(truncate(rec.title(), 65))));
374         var args = {};
375         args.page = RDETAIL;
376         args[PARAM_OFFSET] = 0;
377         args[PARAM_RID] = rec.doc_id();
378         link.setAttribute("href", buildOPACLink(args));
379 }
380
381 /* 'type' is one of STYPE_AUTHOR, STYPE_SUBJECT, ... found in config.js 
382         'trunc' is the number of characters to show in the string, defaults to 65 */
383 function buildSearchLink(type, string, linknode, trunc) {
384         if(!trunc) trunc = 65;
385         var args = {};
386         args.page = MRESULT;
387         args[PARAM_OFFSET] = 0;
388         args[PARAM_TERM] = string;
389         args[PARAM_STYPE] = type;
390         linknode.appendChild(text(normalize(truncate(string, trunc))));
391         linknode.setAttribute("href", buildOPACLink(args));
392 }
393
394
395 /* ----------------------------------------------------------------------- */
396 /* user session handling */
397 /* ----------------------------------------------------------------------- */
398 /* session is the login session.  If no session is provided, we attempt
399         to find one in the cookies.  If 'force' is true we retrieve the 
400         user from the server even if there is already a global user present.
401         if ses != G.user.session, we also force a grab */
402 function grabUser(ses, force) {
403
404         if(!ses && isXUL()) ses = xulG['authtoken'];
405
406         if(!ses) {
407                 ses = cookieManager.read(COOKIE_SES);
408                 /* https cookies don't show up in http servers.. */
409         }
410
411         if(!ses) return false;
412
413         if(!force) 
414                 if(G.user && G.user.session == ses)
415                         return G.user;
416
417         /* first make sure the session is valid */
418         var request = new Request(FETCH_SESSION, ses, 1 );
419         request.request.alertEvent = false;
420         request.send(true);
421         var user = request.result();
422
423         if(!user) {
424                 doLogout();
425                 return false; /* unable to grab the session */
426         }
427
428         if( !(typeof user == 'object' && user._isfieldmapper) ) {
429                 doLogout();
430                 return false;
431         }
432
433         G.user = user;
434         G.user.fleshed = false;
435         G.user.session = ses;
436         cookieManager.write(COOKIE_SES, ses, '+1d');
437
438
439         grabUserPrefs();
440         if(G.user.prefs['opac.hits_per_page'])
441                 COUNT = parseInt(G.user.prefs['opac.hits_per_page']);
442
443         if(G.user.prefs[PREF_DEF_FONT]) 
444                 setFontSize(G.user.prefs[PREF_DEF_FONT]);
445
446         var at = getAuthtime();
447         //if(isXUL()) at = xulG['authtime'];
448
449         if(at && !isXUL()) new AuthTimer(at).run(); 
450         return G.user;
451 }
452
453
454 /* sets the 'prefs' field of the user object to their preferences 
455         and returns the preferences */
456 function grabUserPrefs(user, force) {
457         if(user == null) user = G.user;
458         if(!force && user.prefs) return user.prefs;     
459         var req = new Request(FETCH_USER_PREFS, user.session, user.id());
460         req.send(true);
461         user.prefs = req.result();
462         return user.prefs;
463 }
464
465 function grabFleshedUser() {
466
467         if(!G.user || !G.user.session) {
468                 grabUser();     
469                 if(!G.user || !G.user.session) return null;
470         }
471
472         if(G.user.fleshed) return G.user;
473
474    var req = new Request(FETCH_FLESHED_USER, G.user.session);
475         req.send(true);
476
477         G.user = req.result();
478
479         if(!G.user || G.user.length == 0) { 
480                 G.user = null; return false; 
481                 cookieManager.write(COOKIE_SES,"");
482         }
483
484         G.user.session = ses;
485         G.user.fleshed = true;
486
487         cookieManager.write(COOKIE_SES, ses, '+1y'); /*  update the cookie */
488         return G.user;
489 }
490
491 function checkUserSkin(new_skin) {
492
493         return; /* XXX do some debugging with this... */
494
495         var user_skin = getSkin();
496         var cur_skin = grabSkinFromURL();
497
498         if(new_skin) user_skin = new_skin;
499
500         if(!user_skin) {
501
502                 if(grabUser()) {
503                         if(grabUserPrefs()) {
504                                 user_skin = G.user.prefs["opac.skin"];
505                                 cookieManager.write( COOKIE_SKIN, user_skin, '+1y' );
506                         }
507                 }
508         }
509
510         if(!user_skin) return;
511
512         if( cur_skin != user_skin ) {
513                 var url = buildOPACLink();
514                 goTo(url.replace(cur_skin, user_skin));
515         }
516 }
517
518 function updateUserSetting(setting, value, user) {
519         if(user == null) user = G.user;
520         var a = {};
521         a[setting] = value;
522         var req = new Request( UPDATE_USER_PREFS, user.session, a );
523         req.send(true);
524         return req.result();
525 }
526
527 function commitUserPrefs() {
528         var req = new Request( 
529                 UPDATE_USER_PREFS, G.user.session, null, G.user.prefs );
530         req.send(true);
531         return req.result();
532 }
533
534 function grabSkinFromURL() {
535         var path = findBasePath();
536         path = path.replace("/xml/", "");
537         var skin = "";
538         for( var i = path.length - 1; i >= 0; i-- ) {
539                 var ch = path.charAt(i);
540                 if(ch == "/") break;
541                 skin += ch;
542         }
543
544         var skin2 = "";
545         for( i = skin.length - 1; i >= 0; i--)
546                 skin2 += skin.charAt(i);
547
548         return skin2;
549 }
550
551
552 /* returns a fleshed G.user on success, false on failure */
553 function doLogin(suppressEvents) {
554
555         abortAllRequests();
556
557         var uname = G.ui.login.username.value;
558         var passwd = G.ui.login.password.value; 
559
560         var init_request = new Request( LOGIN_INIT, uname );
561    init_request.send(true);
562    var seed = init_request.result();
563
564    if( ! seed || seed == '0') {
565       alert( "Error Communicating with Authentication Server" );
566       return null;
567    }
568
569         var args = {
570                 password : hex_md5(seed + hex_md5(passwd)), 
571                 type            : "opac", 
572                 org             : getOrigLocation()
573         };
574
575         if( uname.match(REGEX_BARCODE) ) args.barcode = uname; /* make this better */
576         else args.username = uname;
577
578    var auth_request = new Request( LOGIN_COMPLETE, args );
579
580    auth_request.send(true);
581    var auth_result = auth_request.result();
582         if(!auth_result) return null;
583
584         AUTHTIME = parseInt(auth_result.payload.authtime);
585         var u = grabUser(auth_result.payload.authtoken, true);
586         if(u && ! suppressEvents) 
587                 runEvt( "common", "locationChanged", u.ws_ou(), findOrgDepth(u.ws_ou()) );
588
589         checkUserSkin();
590
591         return u;
592 }
593
594 function doLogout(noredirect) {
595
596         /* cancel everything else */
597         abortAllRequests();
598
599         /* be nice and delete the session from the server */
600         if(G.user && G.user.session) { 
601                 var req = new Request(LOGIN_DELETE, G.user.session);
602       req.send(true);
603                 try { req.result(); } catch(E){}
604     }
605
606         G.user = null;
607
608         /* remove any cached data */
609         cookieManager.remove(COOKIE_IDS);
610         cookieManager.remove(COOKIE_SRIDS);
611         cookieManager.remove(COOKIE_RIDS);
612         cookieManager.remove(COOKIE_SES);
613         cookieManager.remove(COOKIE_SKIN);
614
615         checkUserSkin("default");
616         COUNT = 10;
617
618
619         var args = {};
620         args[PARAM_TERM] = "";
621         args[PARAM_LOCATION] = globalOrgTree.id();
622         args[PARAM_DEPTH] = findOrgDepth(globalOrgTree);
623         args.page = "home";
624
625         
626         var nored = false;
627         try{ if(isFrontPage) nored = true; } catch(e){nored = false;}
628         if(!nored) goTo(buildOPACLink(args));
629 }
630
631
632 function hideMe(obj) { addCSSClass(obj, config.css.hide_me); } 
633 function unHideMe(obj) { removeCSSClass(obj, config.css.hide_me); }
634
635
636 /* ----------------------------------------------------------------------- */
637 /* build the org tree */
638 /* ----------------------------------------------------------------------- */
639 function drawOrgTree() {
640         //setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 10 );
641         setTimeout( 'buildOrgSelector(G.ui.common.org_tree, orgTreeSelector);', 1 );
642 }
643         
644 var orgTreeSelector;
645 function buildOrgSelector(node) {
646         var tree = new SlimTree(node,'orgTreeSelector');
647         orgTreeSelector = tree;
648         for( var i in orgArraySearcher ) { 
649                 var node = orgArraySearcher[i];
650                 if( node == null ) continue;
651                 if(node.parent_ou() == null)
652                         tree.addNode(node.id(), -1, node.name(), 
653                                 "javascript:orgSelect(" + node.id() + ");", node.name());
654                 else {
655                         tree.addNode(node.id(), findOrgUnit(node.parent_ou()).id(), node.name(), 
656                                 "javascript:orgSelect(" + node.id() + ");", node.name());
657                 }
658         }
659         hideMe($('org_loading_div'));
660         unHideMe($('org_selector_tip'));
661         return tree;
662 }
663
664 function orgSelect(id) {
665         showCanvas();
666         runEvt("common", "locationChanged", id, findOrgDepth(id) );
667
668         removeChildren(G.ui.common.now_searching);
669         G.ui.common.now_searching.appendChild(text(findOrgUnit(id).name()));
670 }
671
672 function setFontSize(size) {
673         scaleFonts(size);
674         cookieManager.write(COOKIE_FONT, size, '+1y');
675 }
676
677
678 var resourceFormats = [
679    "text",
680    "moving image",
681    "sound recording", "software, multimedia",
682    "still images",
683    "cartographic",
684    "mixed material",
685    "notated music",
686    "three dimensional object" ];
687
688
689 function modsFormatToMARC(format) {
690    switch(format) {
691       case "text":
692          return "at";
693       case "moving image":
694          return "g";
695       case "sound recording":
696          return "ij";
697       case "sound recording-nonmusical":
698          return "i";
699       case "sound recording-musical":
700          return "j";
701       case "software, multimedia":
702          return "m";
703       case "still images":
704          return "k";
705       case "cartographic":
706          return "ef";
707       case "mixed material":
708          return "op";
709       case "notated music":
710          return "cd";
711       case "three dimensional object":
712          return "r";
713    }
714    return "at";
715 }
716
717
718 function MARCFormatToMods(format) {
719    switch(format) {
720       case "a":
721       case "t":
722          return "text";
723       case "g":
724          return "moving image";
725       case "i":
726          return "sound recording-nonmusical";
727       case "j":
728          return "sound recording-musical";
729       case "m":
730          return "software, multimedia";
731       case "k":
732          return "still images";
733       case "e":
734       case "f":
735          return "cartographic";
736       case "o":
737       case "p":
738          return "mixed material";
739       case "c":
740       case "d":
741          return "notated music";
742       case "r":
743          return "three dimensional object";
744    }
745    return "text";
746 }
747
748 function setResourcePic( img, resource ) {
749         img.setAttribute( "src", "../../../../images/tor/" + resource + ".jpg");
750         img.title = resource;
751 }
752
753
754
755 function msg( text ) {
756         try { alert( text ); } catch(e) {}
757 }
758
759 function findRecord(id,type) {
760         try {
761                 for( var i = 0; i != recordsCache.length; i++ ) {
762                         var rec = recordsCache[i];
763                         if( rec && rec.doc_id() == id ) return rec;
764                 }
765         } catch(E){}
766         var meth = FETCH_RMODS
767         if(type == 'M') meth = FETCH_MRMODS;
768         var req = new Request(meth, id);
769         req.send(true);
770         return req.result();
771 }
772
773 function Timer(name, node){
774         this.name = name;
775         this.count = 1;
776         this.node = node;
777 }
778 Timer.prototype.start = 
779         function(){_timerRun(this.name);}
780 Timer.prototype.stop = 
781         function(){this.done = true;}
782 function _timerRun(tname) {
783         var _t;
784         eval('_t='+tname);
785         if(_t.done) return;
786         if(_t.count > 100) return;
787         var str = ' . ';
788         if( (_t.count % 5) == 0 ) 
789                 str = _t.count / 5;
790         _t.node.appendChild(text(str));
791         setTimeout("_timerRun('"+tname+"');", 200);
792         _t.count++;
793 }
794
795 function checkILSEvent(obj) {
796         if( obj && obj.ilsevent != null && obj.ilsevent != 0 )
797                 return parseInt(obj.ilsevent);
798         return null;
799 }
800
801 /*
802 function alertILSEvent(code, msg) {
803    if(!msg) msg = "";
804         alert( msg + '\n' + $('ilsevent.' + code).innerHTML );
805 }
806 */
807
808 function alertILSEvent(evt, msg) {
809    if(!msg) msg = "";
810         alert(evt.textcode + '\n' + evt.desc );
811 }
812
813
814 var __authTimer;
815 function AuthTimer(time) { 
816         this.time = (time - LOGOUT_WARNING_TIME) * 1000; 
817         __authTimer = this;
818 }
819
820 AuthTimer.prototype.run = function() {
821         setTimeout('_authTimerAlert()', this.time);
822 }
823
824 function _authTimerAlert() {
825         alert( $('auth_session_expiring').innerHTML );
826         if(!grabUser(null, true)) doLogout();
827 }
828
829
830 function grabUserByBarcode( authtoken, barcode ) {
831         var req = new Request( FETCH_USER_BYBARCODE, authtoken, barcode );
832         req.send(true);
833         return req.result();
834 }
835
836
837 function goHome() {
838         goTo(buildOPACLink({page:HOME}));
839 }
840
841
842 function buildOrgSel(selector, org, offset) {
843         insertSelectorVal( selector, -1, 
844                 org.name(), org.id(), null, findOrgDepth(org) - offset );
845         for( var c in org.children() )
846                 buildOrgSel( selector, org.children()[c], offset);
847 }
848
849
850 function parseForm(form) {
851         if(!form) return {};
852
853         var it = form.replace(/-\w+$/,"");
854         var itf = null;
855         var item_form;
856         var item_type;
857
858         if(form.match(/-/)) itf = form.replace(/^\w+-/,"");
859
860         if(it) {
861                 item_type = [];
862                 for( var i = 0; i < it.length; i++ ) 
863                         item_type.push( it.charAt(i) );
864         }
865
866         if(itf) {
867                 item_form = [];
868                 for( var i = 0; i < itf.length; i++ ) 
869                         item_form.push( itf.charAt(i) );
870         }
871
872         return {item_type: item_type, item_form:item_form};
873 }
874
875