Bug 7180: (follow-up) fix various issues
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / js / acq.js
blob3bac18bc8a1d30d5a1a464ca005e8472cdcd9bfc
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;
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;
31 function isNull(f,noalert) {
32     if (f.value.length > 0) {
33         return false;
34     }
35     return true;
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=='') {
47     return false;
48     }
49     return true;
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);
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;
87 function calcTotalRow(cell) {
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);
98     budgetTotal   =  budgetTotal.replace(/\,/, "");
100 //percent strip and convert
101     if ( val1.match(/\%/) )   {
102         val1 = val1.replace(/\%/, "");
103         cell.value =    (val1 / 100) *  Math.abs(budgetTotal ) ;
104     }
106     for ( var i=0, len=arr.length; i<len; ++i ){
107         remainingNew   +=   Math.abs(arr[i].value);
108     }
110     var cc = new Number(cell.value);
111     cell.value =  cc.toFixed(2); // TIDYME...
112     remainingNew    =    Math.abs( budgetTotal  ) -  remainingNew   ;
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     }
123     remainingTotal.textContent  = remainingNew.toFixed(2) ;
126 function autoFillRow(bud_id) {
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);
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 );
140         if ( arr[i].value == 0 ) {
141             novalueArr[qty] = arr[i];
142             qty += 1;
143         }
144     }
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)));
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     }
158     remainingTotal.textContent = '0.00' ;
159     remainingTotal.style.color = 'black';
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>");
174 //=======================================================================
176 //  NEXT BLOCK IS USED BY NEWORDERBEMPTY
178 function updateCosts(){
179     var quantity = new Number($("#quantity").val());
180     var discount = new Number($("#discount").val());
181     var applygst = new Number ($("#applygst").val());
182     var listprice   =  new Number($("#listprice").val());
183     var currcode = new String($("#currency").val());
184     var exchangerate =  new Number($("#currency_rate_"+currcode).val());
185     var gst_on=false;
187     var rrp   = new Number(listprice*exchangerate);
188     var ecost = rrp;
189     if ( 100-discount != 100 ) { //Prevent rounding issues if no discount
190         ecost = new Number(Math.floor(rrp * (100 - discount )) / 100);
191     }
192     var total =  new Number( ecost * quantity);
193     $("#rrp").val(rrp.toFixed(2));
194     $("#ecost").val(ecost.toFixed(2));
195     $("#total").val(total.toFixed(2));
196     $("listprice").val(listprice.toFixed(2));
198     return true;
201 // Calculates total amount in a suggestion
203 function calcNewsuggTotal(){
204     //collect values
205     var quantity = Number(document.getElementById('quantity').value);
206     var currcode = String(document.getElementById('currency').value);
207     var price   =  Number(document.getElementById('price').value);
208     var exchangerate =  Number(document.getElementById('currency_rate_'+currcode).value);
210     var total =  Number(quantity*price*exchangerate);
212     document.getElementById('total').value = total.toFixed(2);
213     document.getElementById('price').value =  price.toFixed(2);
214     return true;
217 function getAuthValueDropbox( name, cat, destination, selected ) {
218     if ( typeof(selected) == 'undefined' ) {
219         selected = "";
220     }
221     if (cat == null || cat == "") {
222         $(destination).replaceWith(' <input type="text" name="' + name + '" value="' + selected + '" />' );
223         return;
224     }
225     $.ajax({
226         url: "/cgi-bin/koha/acqui/ajax-getauthvaluedropbox.pl",
227         data: {
228             name: name,
229             category: cat,
230             default: selected
231         },
232         async: false,
233         success: function(data){
234             if(data === "0"){
235                 $(destination).replaceWith(' <input type="text" name="' + name + '" value="' + selected + '" />' );
236             }else{
237                 $(destination).replaceWith(data);
238             }
239         }
240     });
243 //USED BY NEWORDEREMPTY.PL
244 function totalExceedsBudget(budgetId, total) {
246     var xmlhttp = null;
247     xmlhttp = new XMLHttpRequest();
248     if ( typeof xmlhttp.overrideMimeType != 'undefined') {
249         xmlhttp.overrideMimeType('text/xml');
250     }
252     var url = '../acqui/check_budget_total.pl?budget_id=' + budgetId + "&total=" + total;
253     xmlhttp.open('GET', url, false);
254     xmlhttp.send(null);
256     xmlhttp.onreadystatechange = function() {
257         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
259             actTotal = eval ( xmlhttp.responseText );
261             if (  Math.abs(actTotal) < Math.abs(total)  ) {
262             // if budget is to low :(
263                 return true ;
264             } else {
265                 return false;
266             }
267         }
268     }
272 //USED BY AQBUDGETS.TMPL
273 function budgetExceedsParent(budgetTotal, budgetId, newBudgetParent, periodID) {
276     var xmlhttp = null;
277     xmlhttp = new XMLHttpRequest();
278     if ( typeof xmlhttp.overrideMimeType != 'undefined') {
279         xmlhttp.overrideMimeType('text/xml');
280     }
282 // make the call... yawn
283 //    var url = '../admin/check_parent_total.pl?budget_id=' + budgetId +   '&parent_id=' + newBudgetParent  + "&total=" + budgetTotal + "&period_id="+ periodID   ;
286     var url = '../admin/check_parent_total.pl?total=' + budgetTotal + "&period_id="+ periodID   ;
288 if (budgetId ) { url +=  '&budget_id=' + budgetId };
289 if ( newBudgetParent  ) { url +=  '&parent_id=' + newBudgetParent};
292     xmlhttp.open('GET', url, false);
293     xmlhttp.send(null);
295     xmlhttp.onreadystatechange = function() {
296         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
297     // stupid JS...
298         } else {
299     // wait for the call to complete
300         }
301     };
303     var result = eval ( xmlhttp.responseText );
305     if (result == '1') {
306             return MSG_BUDGET_PARENT_ALLOCATION;
307     } else if (result == '2') {
308             return MSG_BUDGET_PERIOD_ALLOCATION;
309     } else  {
310             return false;
311     }
317 //USED BY AQBUDGETS.TMPL
318 function checkBudgetParent(budgetId, newBudgetParent) {
319     var xmlhttp = null;
320     xmlhttp = new XMLHttpRequest();
321     if ( typeof xmlhttp.overrideMimeType != 'undefined') {
322         xmlhttp.overrideMimeType('text/xml');
323     }
325     var url = '../admin/check_budget_parent.pl?budget_id=' + budgetId + '&new_parent=' + newBudgetParent;
326     xmlhttp.open('GET', url, false);
327     xmlhttp.send(null);
329     xmlhttp.onreadystatechange = function() {
330         if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
331     // do something with the results
332         } else {
333     // wait for the call to complete
334         }
335     };
337     var result = eval ( xmlhttp.responseText );
339     if (result == '1') {
340             return MSG_PARENT_BENEATH_BUDGET;
341 //     } else if (result == '2') {
342 //            return "- New budget-parent has insufficent funds\n";
343 //     } else  {
344 //              return false;
345     }
348 function hideColumn(num) {
349     $("#hideall,#showall").removeAttr("checked").parent().removeClass("selected");
350     $("#"+num).parent().removeClass("selected");
351     var hide = Number(num.replace("col","")) + 2;
352     // hide header and cells matching the index
353     $("#plan td:nth-child("+hide+"),#plan th:nth-child("+hide+")").toggle();
356 function showColumn(num){
357     $("#hideall").removeAttr("checked").parent().removeClass("selected");
358     $("#"+num).parent().addClass("selected");
359     // set the index of the table column to hide
360     show = Number(num.replace("col","")) + 2;
361     // hide header and cells matching the index
362     $("#plan td:nth-child("+show+"),#plan th:nth-child("+show+")").toggle();
365 function showAllColumns(){
366     $("#selections").checkCheckboxes();
367     $("#selections span").addClass("selected");
368     $("#plan td:nth-child(2),#plan tr th:nth-child(2)").nextAll().show();
369     $("#hideall").removeAttr("checked").parent().removeClass("selected");
371 function hideAllColumns(){
372     var allCols = $("#plan th").length;
373     $("#selections").unCheckCheckboxes();
374     $("#selections span").removeClass("selected");
375     $("#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
376     $("#hideall").attr("checked","checked").parent().addClass("selected");