var maxDecimalPlaces=2;

function inputNumericFormattedKeyUp(evt)
{
            if(document.all)
            {
                e=event;
            }
            else
            {
                e=evt.target;
                e.keyCode=evt.which;
            }

            if((String.fromCharCode(e.keyCode)=='.')&&(this.value.length>(this.value.indexOf('.')+parseInt(this.getAttribute('maxDecimalPlaces')))))
            {
                this.value='';
            }
}


function inputNumericFormattedKeyPress(evt)
{
            // unfortunately there is no logical xor operator in javascript so my IF block logic is going to be nasty 
            //if(((String.fromCharCode(event.keyCode)=='.')&&(this.value.indexOf('.')>-1))XOR(event.keyCode<48)||(event.keyCode>57))

            if(document.all)
            {
                e=event;
            }
            else
            {
                e=evt.target;
                e.keyCode=evt.which;
            }

            if(String.fromCharCode(e.keyCode)=='.')
            {
                if(this.value.indexOf('.')>-1)
                {
                    return(false);
                }
                else
                {
                    periodFlag=1;
                }
            }
            else if( (e.keyCode != 0) && (e.keyCode != 8) && ((e.keyCode<48)||(e.keyCode>57)) ) // if they didn't hit a period but hit a non-number
            {
                return(false);
            }
            else if((this.value.indexOf('.')>-1)&&(this.value.length>(this.value.indexOf('.')+parseInt(this.getAttribute('maxDecimalPlaces'))))) 
            {
                // they hit a number but there are too many decimal places
                if(periodFlag)
                {
                    return(false);
                }
            }
}

function inputNumericFormattedBlur()
{
            periodFlag=0;
            temp=this.value;
            // we need an anchor if it doesn't already exist
            if(temp.indexOf('.')==-1)
            {
                temp+='.';
            }

            re = /(\d{1})(\d{3}[\.,]{1})/;

            // not the most efficient I know
            while(temp!=temp.replace(re, "$1,$2"))
            {
                temp=temp.replace(re, "$1,$2");   
            }

            if(temp.length-(temp.indexOf('.')+1)>parseInt(this.getAttribute('maxDecimalPlaces')))
            {
                temp=temp.substr(0, (temp.indexOf('.')+1)+parseInt(this.getAttribute('maxDecimalPlaces')));
            }

            // remove anchor if it doesn't prefix decimal place digits
            if(temp.indexOf('.')+1==temp.length)
            {
                temp=temp.substr(0, temp.length-1);
            }

            this.value=temp;
}

function inputNumericFormattedFocus(evt)
{
            this.value=this.value.replace(/,/g,'');
            this.select();
}

function initialize_calc_decimal_format()
{
    var inputNumericFormattedCollection=document.getElementsByTagName('INPUT');
    for(i=0;i<inputNumericFormattedCollection.length;i++)
    {
        tempInput=inputNumericFormattedCollection[i];

        if(tempInput.getAttribute('subType')=='numericFormatted')
        {
            tempInput.onkeypress=inputNumericFormattedKeyPress
            tempInput.onkeyup=inputNumericFormattedKeyUp;
            tempInput.onblur=inputNumericFormattedBlur;
            tempInput.onfocus=inputNumericFormattedFocus;
            periodFlag=0;
        }
    } // end of for loop
}  // end of startup