+
+// The recalculateCost function recalculates cost based on the current selection
+// of predefined expense and quantity and also changes the comment accordingly.
+function recalculateCost() {
+ var quantity_control = document.getElementById("quantity");
+ // Set quantity to 1 if it is not set already.
+ if (!quantity_control.value) {
+ quantity_control.value = "1";
+ }
+
+ var comment_control = document.getElementById("item_name");
+ var cost_control = document.getElementById("cost");
+ var replaceDecimalMark = ("." != "{$user->decimal_mark}");
+
+ // Calculate cost.
+ var dropdown = document.getElementById("predefined_expense");
+ if (dropdown.selectedIndex == 0) {
+ quantity_control.value = "";
+ comment_control.value = "";
+ cost_control.value = "";
+ } else {
+ comment_control.value = defined_expenses[dropdown.selectedIndex - 1][1] + " - " + quantity_control.value;
+ var quantity = quantity_control.value;
+ if (isNaN(quantity))
+ cost_control.value = "";
+ else {
+ var expenseCost = defined_expenses[dropdown.selectedIndex - 1][2];
+ if (replaceDecimalMark)
+ expenseCost = expenseCost.replace("{$user->decimal_mark}", ".");
+ var newCost = (quantity_control.value * expenseCost).toFixed(2);
+ if (replaceDecimalMark)
+ newCost = newCost.replace(".", "{$user->decimal_mark}");
+ cost_control.value = newCost;
+ }
+ }
+}