/**
 * Project:     CaribMedia javascript library
 * File:        cm.date.js
 *
 * @link http://www.visitaruba.com/plus
 * @copyright 2008 CaribMedia
 * @author Michiel van der Blonk <michiel@caribmedia.com>
 * @package Types
 */

// some handy date additions
Date.prototype.format = function(mask, delimiter, addZeros) {
	function addZero(n)    {
		if (addZeros==false) 
			return n; 
		else 
			return n<10?'0'+n:n;
		}
	if (!delimiter)
		delimiter = '/';
	if (mask == 'month')
		return '' + addZero((this.getMonth()+1)) + delimiter + this.getFullYear();
	else
		return addZero(this.getMonth()+1) + delimiter + addZero(this.getDate()) + delimiter + this.getFullYear();
};

// add n number of days
Date.prototype.navigate = function(n) {
	this.setTime(this.getTime()+n*86400000);
	return this;
};

// calculate # of workdays from this to d2
Date.prototype.workdays = function(d2) {
	var start = new Date(this);
	var end = d2;
	var ret = 0;
	var diff = end.days() - start.days();
	if (diff > 0)
		while (start.days() < end.days() && ret < 3650)
		{
			start.navigate(1);
			if (start.getDay() != 0 && start.getDay() != 6)
				ret++;
		}
	return ret;
};

Date.yearsAgo = function(n) {
	return new Date().navigate(-n*365.25);
};

Date.prototype.days = function() { return parseInt(this.getTime() / (1000*24*60*60)); };