

//////////////////////////////////////////////////////////////////////////////////////

// NUMBERS

/////////////


function number_format(elem) {

var field = document.forms[elem.form.name].elements[elem.name]; 
var text = field.value;

// Strip off anything to the right of the DP
var rightOfDp = '';
var dpPos = text.indexOf('.');
if (dpPos != -1) {
    rightOfDp = text.substr(dpPos);
    text = text.substr(0, dpPos);
}

var leftOfDp = '';
var counter = 0;
// Format the remainder into 3 char blocks, starting from the right
for (var loop=text.length-1; loop>-1; loop--) {
    var char = text.charAt(loop);

    // Ignore existing spaces
    if (char == ' ') continue;

    leftOfDp = char + leftOfDp;
    counter++;
    if (counter % 3 == 0) leftOfDp = ' ' + leftOfDp;
}

// Strip leading space if present
if (leftOfDp.charAt(0) == ' ') leftOfDp = leftOfDp.substr(1);

var output = leftOfDp + rightOfDp;

output = output.replace(' ,',',');

field.value = output;

}