From f0c36e1cc5fdd71f823eb8721c9c2da65c93689d Mon Sep 17 00:00:00 2001
From: Maxime Pelletier <maxime.pelletier@libeo.com>
Date: Thu, 1 Sep 2011 15:35:20 -0400
Subject: [PATCH] Fix multiple bugs and refactor the code to fix the alert of the total exceeding the budget in neworderempty

---
 koha-tmpl/intranet-tmpl/prog/en/js/acq.js          |   36 +++++++------------
 .../prog/en/modules/acqui/neworderempty.tmpl       |    2 +-
 2 files changed, 14 insertions(+), 24 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/js/acq.js b/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
index 4b65f82..0d74b13 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
+++ b/koha-tmpl/intranet-tmpl/prog/en/js/acq.js
@@ -752,30 +752,20 @@ for (i=1;i<=2;i++) {
 
 //USED BY NEWORDEREMPTY.PL
 function totalExceedsBudget(budgetId, total) {
-
-    var xmlhttp = null;
-    xmlhttp = new XMLHttpRequest();
-    if ( typeof xmlhttp.overrideMimeType != 'undefined') {
-        xmlhttp.overrideMimeType('text/xml');
-    }
-
-    var url = '../acqui/check_budget_total.pl?budget_id=' + budgetId + "&total=" + total;
-    xmlhttp.open('GET', url, false);
-    xmlhttp.send(null);
-
-    xmlhttp.onreadystatechange = function() {
-        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
-
-            actTotal = eval ( xmlhttp.responseText );
-
-            if (  Math.abs(actTotal) < Math.abs(total)  ) {
-            // if budget is to low :(
-                return true ;
-            } else {
-                return false;
-            }
+    budgetTotal = 0;
+    $.ajax({
+        url: "/cgi-bin/koha/acqui/check_budget_total.pl",
+        async:false,
+        method: "post",
+        data: {budget_id : budgetId, total : total},
+        success: function(actTotal) {
+            actTotal = Math.abs(actTotal);
+            exceeds = (actTotal < Math.abs(total))
+            if (exceeds)
+                budgetTotal = actTotal;
         }
-    }
+    });
+    return budgetTotal;
 }
 
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tmpl
index a76cb5c..f365672 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tmpl
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tmpl
@@ -34,7 +34,7 @@ function Check(ff) {
                     _alertString += "\n- " + _("Total must be a number");
     }
 
-    if (totalExceedsBudget(ff.budget_id.value, ff.total.value  )  ) {
+    if (actTotal = totalExceedsBudget(ff.budget_id.value, ff.total.value  )  ) {
         ok=1;
         _alertString += "\n- " + _("Order total (") + ff.total.value +
             _(") exceeds budget available (") + actTotal+")";
-- 
1.5.6.5