class JavaScriptUtils {

  public static BigDecimal jsToBigNumber( Object value, String classType ) {
    if ( classType.equalsIgnoreCase( "org.mozilla.javascript.Undefined" ) ) {
      return null;
    } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeNumber" ) ) {
      Number nb = Context.toNumber( value );
      return new BigDecimal( nb.doubleValue() );
    } else if ( classType.equalsIgnoreCase( "org.mozilla.javascript.NativeJavaObject" ) ) {
      // Is it a BigDecimal class ?
      try {
        return (BigDecimal) Context.jsToJava( value, BigDecimal.class );
      } catch ( Exception e ) {
        String string = (String) Context.jsToJava( value, String.class );
        return new BigDecimal( string );
      }
    } else if ( classType.equalsIgnoreCase( "java.lang.Byte" ) ) {
      return new BigDecimal( ( (Byte) value ).longValue() );
    } else if ( classType.equalsIgnoreCase( "java.lang.Short" ) ) {
      return new BigDecimal( ( (Short) value ).longValue() );
    } else if ( classType.equalsIgnoreCase( "java.lang.Integer" ) ) {
      return new BigDecimal( ( (Integer) value ).longValue() );
    } else if ( classType.equalsIgnoreCase( "java.lang.Long" ) ) {
      return new BigDecimal( ( (Long) value ).longValue() );
    } else if ( classType.equalsIgnoreCase( "java.lang.Double" ) ) {
      return new BigDecimal( ( (Double) value ).doubleValue() );
    } else if ( classType.equalsIgnoreCase( "java.lang.String" ) ) {
      return new BigDecimal( ( new Long( (String) value ) ).longValue() );
    } else {
      throw new RuntimeException( "JavaScript conversion to BigNumber not implemented for " + classType );
    }
  }

}