Bug 12839: aqbooksellers.gstreg is never used
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / acq.js
1 //=======================================================================
2 //input validation:
3 // acqui/uncertainprice.tmpl uses this
4 function uncheckbox(form, field) {
5     var price = new Number(form.elements['price' + field].value);
6     var tmpprice = "";
7     var errmsg = MSG_INVALIDPRICE;
8     if (isNaN(price)) {
9         alert(errmsg);
10         for(var i=0; i<form.elements['price' + field].value.length; ++i) {
11             price = new Number(form.elements['price' + field].value[i]);
12             if(! isNaN(price) || form.elements['price' + field].value[i] == ".") {
13                 tmpprice += form.elements['price' + field].value[i];
14             }
15         }
16         form.elements['price' + field].value = tmpprice;
17         return false;
18     }
19     form.elements['uncertainprice' + field].checked = false;
20     return true;
21 }
22
23 // returns false if value is empty
24 function isNotNull(f,noalert) {
25     if (f.value.length ==0) {
26         return false;
27     }
28     return true;
29 }
30
31 function isNull(f,noalert) {
32     if (f.value.length > 0) {
33         return false;
34     }
35     return true;
36 }
37
38 //Function returns false if v is not a number (if maybenull is 0, it also returns an error if the number is 0)
39 function isNum(v,maybenull) {
40     var n = new Number(v.value);
41     if (isNaN(n)) {
42         return false;
43     }
44     if (maybenull==0 && v.value=='') {
45
46
47     return false;
48     }
49     return true;
50 }
51
52
53 //a logging function (a bit buggy, might open millions of log pages when initializing, but works fine after...
54 function log(message) {
55     if (!log.window_ || log.window_.closed) {
56         var win = window.open("", null, "width=400,height=200," +
57                             "scrollbars=yes,resizable=yes,status=no," +
58                             "location=no,menubar=no,toolbar=no");
59         if (!win) return;
60         var doc = win.document;
61         doc.write("<html><head><title>Debug Log</title></head>" +
62                 "<body></body></html>");
63         doc.close();
64         log.window_ = win;
65     }
66     var logLine = log.window_.document.createElement("div");
67     logLine.appendChild(log.window_.document.createTextNode(message));
68     log.window_.document.body.appendChild(logLine);
69 }
70
71 //=======================================================================
72 function getElementsByClass( searchClass, domNode, tagName) {
73     if (domNode == null) domNode = document;
74     if (tagName == null) tagName = '*';
75     var el = new Array();
76     var tags = domNode.getElementsByTagName(tagName);
77     var tcl = " "+searchClass+" ";
78     for(i=0,j=0; i<tags.length; i++) {
79         var test = " " + tags[i].className + " ";
80         if (test.indexOf(tcl) != -1)
81             el[j++] = tags[i];
82     }
83     return el;
84 }
85
86
87 function calcTotalRow(cell) {
88
89     var string = cell.name;
90     var pos = string.indexOf(",", 0);
91     var bud_id = string.substring(0, pos);
92     var val1 =    cell.value;
93     var remainingTotal =   document.getElementById("budget_est_"+bud_id);
94     var remainingNew =0;
95     var budgetTotal  =  document.getElementById("budget_tot_"+bud_id ).textContent;
96     var arr =  getElementsByClass(cell.className);
97
98     budgetTotal   =  budgetTotal.replace(/\,/, "");
99
100 //percent strip and convert
101     if ( val1.match(/\%/) )   {
102         val1 = val1.replace(/\%/, "");
103         cell.value =    (val1 / 100) *  Math.abs(budgetTotal ) ;
104     }
105
106     for ( var i=0, len=arr.length; i<len; ++i ){
107         remainingNew   +=   Math.abs(arr[i].value);
108     }
109
110     var cc = new Number(cell.value);
111     cell.value =  cc.toFixed(2); // TIDYME...
112     remainingNew    =    Math.abs( budgetTotal  ) -  remainingNew   ;
113
114     if ( remainingNew  == 0)  {
115         remainingTotal.style.color = 'black';
116     }
117     else if ( remainingNew   > 0   )       {
118         remainingTotal.style.color = 'green';
119     } else  {    // if its negative, make it red..
120         remainingTotal.style.color = 'red';
121     }
122
123     remainingTotal.textContent  = remainingNew.toFixed(2) ;
124 }
125
126 function autoFillRow(bud_id) {
127
128     var remainingTotal =   document.getElementById("budget_est_"+bud_id);
129     var remainingNew = new Number;
130     var budgetTotal  =  document.getElementById("budget_tot_"+bud_id ).textContent;
131     var arr =  getElementsByClass("plan_entry_" + bud_id);
132
133     budgetTotal   =  budgetTotal.replace(/\,/, "");
134     var qty = new Number;
135 // get the totals
136     var novalueArr = new Array();
137     for ( var i=0, len=arr.length; i<len; ++i ) {
138         remainingNew   +=   Math.abs (arr[i].value );
139
140         if ( arr[i].value == 0 ) {
141             novalueArr[qty] = arr[i];
142             qty += 1;
143         }
144     }
145
146     remainingNew    =    Math.abs( budgetTotal) -  remainingNew   ;
147     var newCell = new Number (remainingNew / qty);
148     var rest = new Number (remainingNew - (newCell.toFixed(2) * (novalueArr.length - 1)));
149
150     for (var i = 0; i<novalueArr.length; ++i) {
151          if (i == novalueArr.length - 1) {
152              novalueArr[i].value = rest.toFixed(2);
153          }else {
154              novalueArr[i].value = newCell.toFixed(2);
155         }
156     }
157
158     remainingTotal.textContent = '0.00' ;
159     remainingTotal.style.color = 'black';
160 }
161
162
163 function messenger(X,Y,etc){    // FIXME: unused?
164     win=window.open("","mess","height="+X+",width="+Y+",screenX=150,screenY=0");
165     win.focus();
166     win.document.close();
167     win.document.write("<body link='#333333' bgcolor='#ffffff' text='#000000'><font size='2'><p><br />");
168     win.document.write(etc);
169     win.document.write("<center><form><input type=button onclick='self.close()' value='Close'></form></center>");
170     win.document.write("</font></body></html>");
171 }
172
173
174 //=======================================================================
175
176 //  NEXT BLOCK IS USED BY NEWORDERBEMPTY
177
178 function updateCosts(){
179     var quantity = new Number($("#quantity").val());
180     var discount = new Number($("#discount").val());
181     var listprice   =  new Number($("#listprice").val());
182     var currcode = new String($("#currency").val());
183     var exchangerate =  new Number($("#currency_rate_"+currcode).val());
184     var gst_on=false;
185
186     var rrp   = new Number(listprice*exchangerate);
187     var ecost = rrp;
188     if ( 100-discount != 100 ) { //Prevent rounding issues if no discount
189         ecost = new Number(Math.floor(rrp * (100 - discount )) / 100);
190     }
191     var total =  new Number( ecost * quantity);
192     $("#rrp").val(rrp.toFixed(2));
193     $("#ecost").val(ecost.toFixed(2));
194     $("#total").val(total.toFixed(2));
195     $("listprice").val(listprice.toFixed(2));
196
197     return true;
198 }
199
200 // Calculates total amount in a suggestion
201
202 function calcNewsuggTotal(){
203     //collect values
204     var quantity = Number(document.getElementById('quantity').value);
205     var currcode = String(document.getElementById('currency').value);
206     var price   =  Number(document.getElementById('price').value);
207     var exchangerate =  Number(document.getElementById('currency_rate_'+currcode).value);
208
209     var total =  Number(quantity*price*exchangerate);
210
211     document.getElementById('total').value = total.toFixed(2);
212     document.getElementById('price').value =  price.toFixed(2);
213     return true;
214 }
215
216 function getAuthValueDropbox( name, cat, destination, selected ) {
217     if ( typeof(selected) == 'undefined' ) {
218         selected = "";
219     }
220     if (cat == null || cat == "") {
221         $(destination).replaceWith(' <input type="text" name="' + name + '" value="' + selected + '" />' );
222         return;
223     }
224     $.ajax({
225         url: "/cgi-bin/koha/acqui/ajax-getauthvaluedropbox.pl",
226         data: {
227             name: name,
228             category: cat,
229             default: selected
230         },
231         async: false,
232         success: function(data){
233             if(data === "0"){
234                 $(destination).replaceWith(' <input type="text" name="' + name + '" value="' + selected + '" />' );
235             }else{
236                 $(destination).replaceWith(data);
237             }
238         }
239     });
240 }
241
242 //USED BY NEWORDEREMPTY.PL
243 function totalExceedsBudget(budgetId, total) {
244
245     var xmlhttp = null;
246     xmlhttp = new XMLHttpRequest();
247     if ( typeof xmlhttp.overrideMimeType != 'undefined') {
248         xmlhttp.overrideMimeType('text/xml');
249     }
250
251     var url = '../acqui/check_budget_total.pl?budget_id=' + budgetId + "&total=" + total;
252     xmlhttp.open('GET', url, false);
253     xmlhttp.send(null);
254
255     xmlhttp.onreadystatechange = function() {
256         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
257
258             actTotal = eval ( xmlhttp.responseText );
259
260             if (  Math.abs(actTotal) < Math.abs(total)  ) {
261             // if budget is to low :(
262                 return true ;
263             } else {
264                 return false;
265             }
266         }
267     }
268 }
269
270
271 //USED BY AQBUDGETS.TMPL
272 function budgetExceedsParent(budgetTotal, budgetId, newBudgetParent, periodID) {
273
274
275     var xmlhttp = null;
276     xmlhttp = new XMLHttpRequest();
277     if ( typeof xmlhttp.overrideMimeType != 'undefined') {
278         xmlhttp.overrideMimeType('text/xml');
279     }
280
281 // make the call... yawn
282 //    var url = '../admin/check_parent_total.pl?budget_id=' + budgetId +   '&parent_id=' + newBudgetParent  + "&total=" + budgetTotal + "&period_id="+ periodID   ;
283
284
285     var url = '../admin/check_parent_total.pl?total=' + budgetTotal + "&period_id="+ periodID   ;
286
287 if (budgetId ) { url +=  '&budget_id=' + budgetId };
288 if ( newBudgetParent  ) { url +=  '&parent_id=' + newBudgetParent};
289
290
291     xmlhttp.open('GET', url, false);
292     xmlhttp.send(null);
293
294     xmlhttp.onreadystatechange = function() {
295         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
296     // stupid JS...
297         } else {
298     // wait for the call to complete
299         }
300     };
301
302     var result = eval ( xmlhttp.responseText );
303
304     if (result == '1') {
305             return MSG_BUDGET_PARENT_ALLOCATION;
306     } else if (result == '2') {
307             return MSG_BUDGET_PERIOD_ALLOCATION;
308     } else  {
309             return false;
310     }
311 }
312
313
314
315
316 //USED BY AQBUDGETS.TMPL
317 function checkBudgetParent(budgetId, newBudgetParent) {
318     var xmlhttp = null;
319     xmlhttp = new XMLHttpRequest();
320     if ( typeof xmlhttp.overrideMimeType != 'undefined') {
321         xmlhttp.overrideMimeType('text/xml');
322     }
323
324     var url = '../admin/check_budget_parent.pl?budget_id=' + budgetId + '&new_parent=' + newBudgetParent;
325     xmlhttp.open('GET', url, false);
326     xmlhttp.send(null);
327
328     xmlhttp.onreadystatechange = function() {
329         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
330     // do something with the results
331         } else {
332     // wait for the call to complete
333         }
334     };
335
336     var result = eval ( xmlhttp.responseText );
337
338     if (result == '1') {
339             return MSG_PARENT_BENEATH_BUDGET;
340 //     } else if (result == '2') {
341 //            return "- New budget-parent has insufficent funds\n";
342 //     } else  {
343 //              return false;
344     }
345 }
346
347 function hideColumn(num) {
348     $("#hideall,#showall").removeAttr("checked").parent().removeClass("selected");
349     $("#"+num).parent().removeClass("selected");
350     var hide = Number(num.replace("col","")) + 2;
351     // hide header and cells matching the index
352     $("#plan td:nth-child("+hide+"),#plan th:nth-child("+hide+")").toggle();
353 }
354
355 function showColumn(num){
356     $("#hideall").removeAttr("checked").parent().removeClass("selected");
357     $("#"+num).parent().addClass("selected");
358     // set the index of the table column to hide
359     show = Number(num.replace("col","")) + 2;
360     // hide header and cells matching the index
361     $("#plan td:nth-child("+show+"),#plan th:nth-child("+show+")").toggle();
362 }
363
364 function showAllColumns(){
365     $("#selections").checkCheckboxes();
366     $("#selections span").addClass("selected");
367     $("#plan td:nth-child(2),#plan tr th:nth-child(2)").nextAll().show();
368     $("#hideall").removeAttr("checked").parent().removeClass("selected");
369 }
370 function hideAllColumns(){
371     var allCols = $("#plan th").length;
372     $("#selections").unCheckCheckboxes();
373     $("#selections span").removeClass("selected");
374     $("#plan td:nth-child(2),#plan th:nth-child(2)").nextUntil("th:nth-child("+(allCols-1)+"),td:nth-child("+(allCols-1)+")").hide(); // hide all but the last two columns
375     $("#hideall").attr("checked","checked").parent().addClass("selected");
376 }