var AXANZ = {};
with( AXANZ ){

AXANZ.getNumber = function( s ){
    var e = document.getElementById( s );
    var m =  new Number( e.value.replace(/,|\$|%/g, "" ));
    if( isNaN( m )){
        m = 0;
    }
    e.value = decimal( m, 0 ).toCommaString();
    return m;
}

AXANZ.getMoney = function( s ){
    var e = document.getElementById( s );
    var v = e.value.replace( /,|\$|%/g, "" );
    if( isNaN( new Number( v ))){
        e.value = "0.00";
        return decimal( 0, 2 );
    }
    else{
        var d = decimal( v ).roundTo( 2 );
        e.value = d.toCommaString();
        return d;
    }
}

AXANZ.cancelEvent = function( e ){
   if (e.preventDefault) {
       e.preventDefault();
   }
   else {
       e.returnValue = false;
   }
}

AXANZ.eventTarget = function( e ){
   return window.event ? window.event.srcElement : e ? e.target : null;
}

AXANZ.selectTarget = function( e ){
    if( window.event ){
        window.event.srcElement.select();
    }
    else if( e && e.target ){
        e.target.select();
    }
}

AXANZ.cancelNonNumeric = function( e ){
    var regex = /[-0-9,\.]/;
    var key = window.event ? window.event.keyCode : e ? e.which : 0;
    if ( key >= 32 && key < 127  ) {
       if( ! regex.test( String.fromCharCode( key ))){
           cancelEvent( e );
       }
    }
    return key;
}

AXANZ.makeBlank = function( s ){
    if( document.getElementById( s )){
        document.getElementById( s ).innerHTML = "&nbsp;";
    }
}

AXANZ.isChecked = function( s ){
    return document.getElementById( s ).checked;
}

AXANZ.getCheckedValue = function( arr ){
    for( s in arr ){
        if( isChecked( arr[ s ] )){
            return new Number( document.getElementById( arr[ s ]).value );
        } 
    }
    return null;
}


AXANZ.decimal = function( val, dp ){
    var commaPosition = /(\d+)(\d{3})/ ; 
    var iMantissa;
    var iDecimals;
    
    if( typeof( dp ) != "undefined" ){
        iMantissa = Math.round( val );
        iDecimals = dp;
    }
    else{
        var s = new String( val );
        var d = s.indexOf( "." );
        if( d == -1 || d == s.length - 1 ){
            iMantissa = new Number( s );
            iDecimals = 0;
        }
        else{
            iMantissa = new Number( s.replace( /\./, "" ));
            iDecimals = s.length - d - 1; 
        }
    }

    return { 
        plus : plus, 
        minus : minus, 
        times : times,
        divideRound : divideRound,
        divideNumber : divideNumber,
        toNumber : toNumber,
        toString : toString,
        toCommaString : toCommaString,
        writeDollarsTo : writeDollarsTo,
        greaterThan : greaterThan,
        lessThan : lessThan,
        greaterThanEqual : greaterThanEqual,
        lessThanEqual : lessThanEqual,
        roundTo : roundTo,
        getMantissa : function() { return iMantissa; },
        getDecimals : function() { return iDecimals; }
    };    

    function plus( v ){
        if( !v.getDecimals || !v.getMantissa ){
            v = decimal( v );        
        }
        
        if( v.getDecimals() > iDecimals ){
            return decimal( iMantissa * Math.pow( 10, v.getDecimals() - iDecimals ) + v.getMantissa(), v.getDecimals() );
        }
        else if( v.getDecimals() == iDecimals ){
            return decimal( iMantissa + v.getMantissa(), iDecimals );
        }
        else{
            return decimal( iMantissa + v.getMantissa() * Math.pow( 10, iDecimals - v.getDecimals() ), iDecimals );
        }
    }        
        
    function minus( v ){
        if( !v.getDecimals || !v.getMantissa ){
            v = decimal( v );        
        }
        
        if( v.getDecimals() > iDecimals ){
            return decimal( iMantissa * Math.pow( 10, v.getDecimals() - iDecimals ) - v.getMantissa(), v.getDecimals() );
        }
        else if( v.getDecimals() == iDecimals ){
            return decimal( iMantissa - v.getMantissa(), iDecimals );
        }
        else{
            return decimal( iMantissa - v.getMantissa() * Math.pow( 10, iDecimals - v.getDecimals() ), iDecimals );
        }
    }
    
    function times( v, dp ){
        if( !v.getDecimals || !v.getMantissa ){
            if( typeof( dp ) == "undefined" || dp == iDecimals ){
                return decimal( iMantissa * new Number( v ), iDecimals );
            }
            else{
                return decimal( iMantissa * new Number( v ) * Math.pow( 10, dp - iDecimals ), dp );
            }
        }
        else if( typeof( dp ) == "undefined" || dp == iDecimals + v.getDecimals() ){
            return decimal( iMantissa * v.getMantissa(), iDecimals + v.getDecimals() );
        }
        else{
            return decimal( iMantissa * v.getMantissa() * Math.pow( 10, dp - iDecimals - v.getDecimals() ), dp );
        }
    
    }        
        
    function divideRound( v, dp ){
        if( !v.getDecimals || !v.getMantissa ){
            if( typeof( dp ) == "undefined" || dp == iDecimals ){
                return decimal( iMantissa / new Number( v ), iDecimals );
            }
            else{
                return decimal( iMantissa / new Number( v ) * Math.pow( 10, dp - iDecimals ), dp );
            }
        }
        else if( typeof( dp ) == "undefined" || dp == iDecimals + v.getDecimals() ){
            return decimal( iMantissa / v.getMantissa() * Math.pow( 100, v.getDecimals() ), iDecimals + v.getDecimals() );
        }
        else{
            return decimal( iMantissa / v.getMantissa() * Math.pow( 10, dp - iDecimals + v.getDecimals() ), dp );
        }
    }    
     
    function roundTo( dec ){
        return decimal( iMantissa * Math.pow( 10, dec - iDecimals ), dec );
    } 
        
    function divideNumber( v ){
        return new Number( iMantissa / v / Math.pow( 10, iDecimals ));
    }
    
    function toNumber(){
        return new Number( iMantissa / Math.pow( 10, iDecimals ));
    }
    
    function toString(){
    
        var withoutDecimals = iMantissa.toFixed( 0 );
        if( iDecimals == 0 ){
            return withoutDecimals;
        }
        else if( iDecimals < withoutDecimals.length ){
            return withoutDecimals.substring( 0, withoutDecimals.length - iDecimals ) + "." + withoutDecimals.substring( withoutDecimals.length - iDecimals );
        }
        else{
            return "0." + new String( "000000000000000" + withoutDecimals ).substring( 15 + withoutDecimals.length - iDecimals );
        }
    }
    
    function toCommaString(){
        var v = toString();
        while( commaPosition.test( v )){
            v = v.replace( commaPosition, "$1,$2");
        }   
        return v;
    }
    
    function writeDollarsTo( f ){
        if( iDecimals < 2 ){
            iMantissa = iMantissa * ( iDecimals == 0 ? 100 : 10 );
            iDecimals = 2;
        }
        document.getElementById( f ).innerHTML = "$" + toCommaString();
    }
    
    function greaterThan( v ){
        if( !v.getDecimals || !v.getMantissa ){
            v = decimal( v );        
        }
        return iMantissa * Math.pow( 10, v.getDecimals() ) > v.getMantissa() * Math.pow( 10, iDecimals ); 
    }

    function lessThan( v ){
        if( !v.getDecimals || !v.getMantissa ){
            v = decimal( v );        
        }
        return iMantissa * Math.pow( 10, v.getDecimals() ) < v.getMantissa() * Math.pow( 10, iDecimals ); 
    }

    function greaterThanEqual( v ){
        if( !v.getDecimals || !v.getMantissa ){
            v = decimal( v );        
        }
        return iMantissa * Math.pow( 10, v.getDecimals() ) >= v.getMantissa() * Math.pow( 10, iDecimals ); 
    }

    function lessThanEqual( v ){
        if( !v.getDecimals || !v.getMantissa ){
            v = decimal( v );        
        }
        return iMantissa * Math.pow( 10, v.getDecimals() ) <= v.getMantissa() * Math.pow( 10, iDecimals ); 
    }
};
}