added much control over holds during placement and after the fact
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / holds.js
1
2 var currentHoldRecord;
3 var currentHoldRecordObj;
4 var holdsOrgSelectorBuilt = false;
5 var holdRecipient;
6 var holdRequestor
7 var holdEmail;
8 var holdPhone;
9
10
11 function holdsHandleStaff() {
12         swapCanvas($('xulholds_box'));
13         $('xul_recipient_barcode').focus();
14         $('xul_recipient_barcode').onkeypress = function(evt) 
15                 {if(userPressedEnter(evt)) { _holdsHandleStaff(); } };
16         $('xul_recipient_barcode_submit').onclick = _holdsHandleStaff;
17 }
18
19 function _holdsHandleStaff() {
20         var barcode = $('xul_recipient_barcode').value;
21         var user = grabUserByBarcode( G.user.session, barcode );
22         var code = checkILSEvent(user);
23         if(code || !user) {
24                 alertILSEvent(code, barcode);
25                 showCanvas();
26                 return;
27         }
28         holdRecipient = user;
29         holdsDrawWindow( currentHoldRecord, null );
30 }
31
32 function holdsDrawWindow(recid, type) {
33
34         if(recid == null) {
35                 recid = currentHoldRecord;
36                 if(recid == null) return;
37         }       
38         currentHoldRecord = recid;
39         
40         if(isXUL() && holdRecipient == null ) { 
41                 holdsHandleStaff();
42                 return;
43         }
44
45         if( holdRecipient == null ) holdRecipient = G.user;
46         if( holdRequestor == null ) holdRequestor = G.user;
47
48         if(!(holdRequestor && holdRequestor.session)) {
49
50                 detachAllEvt('common','locationChanged');
51                 attachEvt('common','loggedIn', holdsDrawWindow)
52                 initLogin();
53                 return;
54         }
55
56
57         swapCanvas($('check_holds_box'));
58         setTimeout( function() { holdsCheckPossibility(recid, type); }, 10 );
59 }
60
61 function _holdsDrawWindow(recid, type) {
62
63         swapCanvas($('holds_box'));
64
65         var rec = findRecord( recid, type );
66         currentHoldsRecordObj = rec;
67
68         if(!holdsOrgSelectorBuilt) {
69                 holdsBuildOrgSelector(null,0);
70                 holdsOrgSelectorBuilt = true;
71         }
72
73         appendClear($('holds_recipient'), text(
74                 holdRecipient.family_name() + ', ' +  
75                         holdRecipient.first_given_name()));
76         appendClear($('holds_title'), text(rec.title()));
77         appendClear($('holds_author'), text(rec.author()));
78
79         removeChildren($('holds_format'));
80         for( var i in rec.types_of_resource() ) {
81                 var res = rec.types_of_resource()[i];
82                 var img = elem("img");
83                 setResourcePic(img, res);
84                 $('holds_format').appendChild(text(' '+res+' '));
85                 $('holds_format').appendChild(img);
86                 $('holds_format').appendChild(text(' '));
87         }
88
89         $('holds_phone').value = holdRecipient.day_phone();
90         appendClear( $('holds_email'), text(holdRecipient.email()));
91         $('holds_cancel').onclick = showCanvas;
92         $('holds_submit').onclick = holdsPlaceHold; 
93
94         appendClear($('holds_physical_desc'), text(rec.physical_description()));
95         if(hold.hold_type() == 'M') hideMe($('hold_physical_desc_row'));
96 }
97
98
99 function holdsCheckPossibility(recid, type) {
100         var req = new Request(CHECK_HOLD_POSSIBLE, G.user.session, 
101                         { titleid : recid, patronid : G.user.id(), depth : 0 } );
102         req.send(true);
103         var res = req.result();
104
105         if(res) _holdsDrawWindow(recid, type);
106         else drawCanvas();
107 }
108
109
110 function holdsBuildOrgSelector(node) {
111
112         if(!node) node = globalOrgTree;
113
114         var selector = $('holds_org_selector');
115         var index = selector.options.length;
116
117         var type = findOrgType(node.ou_type());
118         var indent = type.depth() - 1;
119         var opt = setSelectorVal( selector, index, node.name(), node.id(), null, indent );
120         if(!type.can_have_vols()) opt.disabled = true;
121         
122         if( node.id() == holdRecipient.home_ou() ) {
123                 selector.selectedIndex = index;
124                 selector.options[index].selected = true;        
125         }
126
127         for( var i in node.children() ) {
128                 var child = node.children()[i];
129                 if(child) holdsBuildOrgSelector(child);
130         }
131 }
132
133 function holdsPlaceHold() {
134
135         var org = $('holds_org_selector').options[
136                 $('holds_org_selector').selectedIndex].value;
137
138         var hold = new ahr();
139
140
141         if( $('holds_enable_phone').checked ) {
142                 var phone = $('holds_phone').value;
143                 if( !phone.match(REGEX_PHONE) ) {
144                         alert($('holds_bad_phone').textContent);
145                         return;
146                 }
147                 hold.phone_notify(phone);
148
149         } else {
150                 hold.phone_notify(null);
151         }
152
153         if( $('holds_enable_email').checked ) 
154                 hold.email_notify(1);
155         else
156                 hold.email_notify(0);
157
158
159
160         hold.pickup_lib(org); 
161         hold.request_lib(org); 
162         hold.requestor(holdRequestor.id());
163         hold.usr(holdRecipient.id());
164         hold.hold_type('T');
165         hold.target(currentHoldRecord);
166         
167         var req = new Request( CREATE_HOLD, holdRequestor.session, hold );
168         req.send(true);
169         var res = req.result();
170
171         if( res == '1' ) alert($('holds_success').innerHTML);
172         else alert($('holds_failure').innerHTML);
173         
174         showCanvas();
175         holdRecipient = null;
176         holdRequestor = null;
177 }
178
179 function holdsCancel(holdid, user) {
180         if(!user) user = G.user;
181         var req = new Request(CANCEL_HOLD, user.session, holdid);
182         req.send(true);
183         return req.result();
184 }
185
186