var fs = require('fs')
  , path = require('path')
  , couchapp = require('couchapp')
  ;

var ddoc = module.exports = {};

ddoc._id = '_design/bigdecimal';

ddoc.validate_doc_update = function(newDoc, oldDoc, userCtx, secObj) {
  throw {forbidden:"No changes allowed in the BigDecimal demo"};
}

ddoc.shows = {};
ddoc.shows.ui = function(doc, req) {
  var ddoc = this
    , bd = require("bigdecimal")
    ;

  log(req.query);

  var result;
  var response = {'headers': {}};

  provides('html', function() {
    var match = req.query.e && req.query.e.match(/(-?\d+\.?\d*) *([\+\-\*\/]) *(-?\d+\.?\d*)/);
    if(!match)
      response.body = "Usage: ('e' parameter) must be: A &lt;operand&gt; B" +
                      "<br><br>" +
                      "where A, B are real numbers (initial 0 required), and operand is *, /, -, or +." +
                      "<br><br>" +
                      "(Also, division tends to throw exceptions because I hard-coded unlimited precision for this demo.)";
    else {
      result = run({'a':match[1], 'b':match[3], 'op':match[2]});
      response.body = req.query.e + '<br><br>';
      response.body += '= ' + result;

      if(match[2] == '/')
        response.body += ' (Precision set to 300)';

      response.body += "<br><br><a href='https://github.com/iriscouch/bigdecimal.js'>bigdecimal.js - BigDecimal and BigInteger for Javascript</a>";
    }

    response.body += '\n';
    return response;
  })

  function run(opts) {
    var result;
    var ops = {'*': 'multiply', '/': 'divide', '+': 'add', '-': 'subtract'};

    var a = new bd.BigDecimal("" + opts.a);
    var b = new bd.BigDecimal("" + opts.b);
    var op = ops[ opts.op ];

    if(op == 'divide')
      return a.divide(b, 300, bd.RoundingMode.HALF_UP());
    else
      return a[op].call(a, b);
  }
};

ddoc.bigdecimal = fs.readFileSync(path.join(__dirname, '..', 'lib', 'bigdecimal.js'), 'utf8');

if(require.main === module)
  console.log('ok');

@@ -1066,16 +1066,17 @@
 h%5B2%5D%7D);%0A
+%0A
       re
@@ -1093,21 +1093,188 @@
 y = 
-req.query.e +
+%5B '%3Chtml%3E'%0A                      , '%3Chead%3E%3Ctitle%3EBigDecimal for CouchDB%3C/title%3E%3C/head%3E'%0A                      , '%3Cbody%3E'%0A                      , req.query.e%0A                      ,
  '%3Cb
@@ -1284,57 +1284,71 @@
 br%3E'
-;
 %0A      
-response.body += '= ' + result;%0A%0A
+                , '= ' + result%0A                
       
-if
+, 
 (mat
@@ -1364,38 +1364,15 @@
 '/')
-%0A        response.body +=
+ ?
  ' (
-P
+p
 reci
@@ -1392,51 +1392,82 @@
 00)'
-;%0A%0A      response.body += %22%3Cbr%3E%3Cbr%3E
+ : ''%0A                      , '%3Cbr%3E%3Cbr%3E'%0A                      , '
 %3Ca href=
 'htt
@@ -1462,17 +1462,17 @@
 %3Ca href=
-'
+%22
 https://
@@ -1505,17 +1505,17 @@
 cimal.js
-'
+%22
 %3Ebigdeci
@@ -1567,18 +1567,187 @@
 ript%3C/a%3E
-%22;
+'%0A                      , '%3C/body%3E'%0A                      , '%3Cscript src=%22../bigdecimal.js%22%3E%3C/script%3E'%0A                      , '%3C/html%3E'%0A                      %5D.join('');%0A
 %0A    %7D%0A%0A
@@ -2260,16 +2260,84 @@
 tf8');%0A%0A
+couchapp.loadAttachments(ddoc, path.join(__dirname, '..', 'lib'));%0A%0A
 if(requi

Load the script into the browser too