var config = require('./config')
var client = require('mongodb').MongoClient

var instance = false

module.exports = {
	newISOdate: function(d) {
		function pad(n) {return n<10 ? '0'+n : n}
    	return d.getUTCFullYear()+'-'
        + pad(d.getUTCMonth()+1)+'-'
        + pad(d.getUTCDate())+'T'
        + pad(d.getUTCHours())+':'
        + pad(d.getUTCMinutes())+':'
        + pad(d.getUTCSeconds())+'Z'
	},
	getISOdate: function() {
		var d = new Date()
		function pad(n) {return n<10 ? '0'+n : n}
    	return d.getUTCFullYear()+'-'
        + pad(d.getUTCMonth()+1)+'-'
        + pad(d.getUTCDate())+'T'
        + pad(d.getUTCHours())+':'
        + pad(d.getUTCMinutes())+':'
        + pad(d.getUTCSeconds())+'Z'
	},
	getPrettyDate: function(date) {
		var yyyy = date.getUTCFullYear().toString()
		var mm = (date.getUTCMonth()()+1).toString()
		var dd  = date.getUTCDate().toString()
		return yyyy + '-' + (mm[1]?mm:"0"+mm[0]) + '-' + (dd[1]?dd:"0"+dd[0])
	},
  getPrettyDateTime: function(date) {
    year = "" + date.getUTCFullYear();
    month = "" + (date.getUTCMonth() + 1); if (month.length == 1) { month = "0" + month; }
    day = "" + date.getUTCDate(); if (day.length == 1) { day = "0" + day; }
    hour = "" + date.getHours(); if (hour.length == 1) { hour = "0" + hour; }
    minute = "" + date.getUTCMinutes(); if (minute.length == 1) { minute = "0" + minute; }
    second = "" + date.getUTCSeconds(); if (second.length == 1) { second = "0" + second; }
    return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
	},
  formatHtmlEntities: function (str) {
    return str.replace(/[^]/g, function(str) {
        return "&#" + str.charCodeAt(0) + ";"
    })
  },
  returnUrl: function(req) {
    var url = '/'
    previousUrl = req.headers.referer
    host = req.headers.host

    if(typeof(previousUrl) !== 'undefined') {
      if(previousUrl.indexOf(host) >= 0) {
        var page = ( previousUrl.substr(previousUrl.indexOf(host) + host.length) )
        url = page

        if ( req.session.returnTo !== 'undefined' ) {
          if( req.originalUrl.indexOf('.') === -1 ) { //Fix for not redirecting to an image or something
            url = req.originalUrl
          }
        }
      } else {
        url = req.originalUrl
      }
    } else {
      url = req.originalUrl
    }

    return url
  }
}
@@ -741,24 +741,96 @@
 ion(date) %7B%0A
+    if( ! (date instanceof Date) ) %7B%0A      date = new Date(date)%0A    %7D%0A%0A
 %09%09var yyyy =
@@ -1061,24 +1061,96 @@
 ion(date) %7B%0A
+    if( ! (date instanceof Date) ) %7B%0A      date = new Date(date)%0A    %7D%0A%0A
     year = %22

Make sure that the argument is a Date object