+//
+// Javascript conversions to/from user numeric format.
+//
+function add_js_user_num() {
+ global $thoseps, $decseps;
+ $ts = $thoseps[user_tho_sep()];
+ $ds = $decseps[user_dec_sep()];
+
+ $js =
+ "<script language=\"javascript\">
+function price_format(post, num, dec) {
+ //num = num.toString().replace(/\\$|\\,/g,'');
+ if(isNaN(num))
+ num = \"0\";
+ sign = (num == (num = Math.abs(num)));
+ if(dec<0) dec = 2;
+ decsize = Math.pow(10, dec);
+ num = Math.floor(num*decsize+0.50000000001);
+ cents = num%decsize;
+ num = Math.floor(num/decsize).toString();
+ for( i=cents.toString().length; i<dec; i++){
+ cents = \"0\" + cents;
+ }
+ for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
+ num = num.substring(0,num.length-(4*i+3))+'".$ts."'+
+ num.substring(num.length-(4*i+3));
+ num = ((sign)?'':'-') + num;
+ if(dec!=0) num = num + '".$ds."' + cents;
+ document.getElementsByName(post)[0].value = num;
+ }
+ function get_amount(doc) {
+ var val = document.getElementsByName(doc)[0].value;
+ val = val.replace(/\\".$ts."/g,'');
+ val = val.replace(/\\".$ds."/g,'.');
+ return 1*val;
+ }
+ </script>";
+ add_js_source($js);
+}
+
+function add_js_allocate() {
+ $source =
+ "<script>
+ function allocate_all(doc) {
+ var alloc = get_amount('left_to_allocate');
+ var amount = get_amount('amount'+doc);
+ var unallocated = get_amount('un_allocated'+doc);
+ if(unallocated<amount) amount = unallocated;
+ if((unallocated-amount)<=alloc){
+ alloc-=unallocated-amount;
+ amount=unallocated;
+ }else{
+ amount=amount+alloc;
+ alloc=0;
+ }
+ price_format('amount'+doc, amount, ".user_price_dec().");
+ price_format('left_to_allocate', alloc, ".user_price_dec().");
+ }
+ function allocate_none(doc) {
+ amount = get_amount('amount'+doc);
+ left = get_amount('left_to_allocate');
+ price_format('left_to_allocate',amount+left, ".user_price_dec().");
+ price_format('amount'+doc, 0, ".user_price_dec().");
+ }
+ </script>";
+ add_js_user_num();
+ add_js_source($source);
+}
+