// Money Object  v1.0
// http://www.dithered.com/javascript/money/index.html
// code by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)


// Money constructor
function Money(value, units) {
	
	// convert the initial value to a number 
	if (value) value = parseFloat(value);
 	if ( value == null || isNaN(value) == true ) value = 0;
	
	// separate the dollars and cents parts of the initial value
	if (units == Money.CENTS) {
		this.dollars = parseInt(value / 100);
		this.cents   = value % 100;
	}
	else {
		this.dollars = parseInt(value);
		this.cents   = (value - this.dollars) * 100;
	}
}

/******************************************************************************
	Instance methods
*******************************************************************************/

// Function to return cents value
Money.prototype.getCents = function() {
	return parseInt(this.cents);
};

// Function to return dollars value
Money.prototype.getDollars = function() {
	return this.dollars;
};

// Function to assign new cents value
Money.prototype.setCents = function(newCents) {
	var additionalDollars = 0;
	newCents = parseInt(newCents);
	
	// for values over 99, separate the value into dollars and cents portions
	if ( isNaN(newCents) == false && newCents > 100 ) {
		additionalDollars = parseInt(newCents / 100);
		newCents = newCents % 100;
	}
	
	// assign new cents value and calculate new dollars value
	if ( isNaN(newCents) == false && newCents >= 0 ) this.cents = newCents;
	if ( additionalDollars != 0 ) this.setDollars(this.getDollars() + additionalDollars);
};

// Function to assign new dollars value
Money.prototype.setDollars = function (newDollars) {
	newDollars = parseInt(newDollars);
	if ( isNaN(newDollars) == false ) this.dollars = newDollars;
};

// Function to assign new dollars and cents values
Money.prototype.setValue = function(newDollars, newCents) {
	this.setDollars(newDollars);
	this.setCents(newCents);
};

// Over-ride inherited toString() method to return value with 2 decimal places
Money.prototype.toString = function() { 
	
	// convert integer part of cents value to a 2 character string
	var centsString = this.getCents().toString();
	if (centsString.length == 1) centsString = '0' + centsString;

	// prepend the dollar value and a decimal to cents string
	return (this.getDollars() + '.' + centsString);
};

// Over-ride inherited toValue() method to return decimal dollar value
Money.prototype.toValue = function() {
	return (this.dollars + (this.cents * 0.01));
};

// Create and discard one object to prevent NS 3 bug
new Money(0, 0);


/******************************************************************************
	Class properties
*******************************************************************************/
Money.CENTS   = 100;
Money.DOLLARS = 1;


/******************************************************************************
	Money related functions
*******************************************************************************/

// Function to convert a float to a money value (alias for Money constructor)
function parseMoney(number, units) {
	return new Money(number, units);
}


