function formatDollarsCents(x)
{
    var d = Math.floor(x);
    var c = x - d;
    c = c * 100;
    c = Math.round(c);   
    var cs;
    if ( c < 10 )
        cs = "0" + c;
    else
        cs = "" + c;
    var ds = getDollarString(d);
    return ds + "." + cs;
}

function formatDollars(x)
{
    x = Math.round(x);
    return getDollarString(x);
}

function getDollarString(x)
{
	var p = "";
	if ( x < 0 )
	{
		x *= -1;
		p = "-";
	}
    var digits = 0;
    var ret = "";
    while ( x >= 10 )
    {  
        var y = x % 10;
        ret = y + ret;
        digits++;
        if ( digits % 3 == 0 )
           ret = "," + ret;
        x = (x-y) / 10;
    }
    ret = x + ret;
    ret = p + "$" + ret;
    return ret;
}

function fixNumber(x)
{
    x = x.replace(/\$/g, "");
    x = x.replace(/\%/g, "");
    x = x.replace(/,/g, "");
    return x;
}


function formatPercent(decimalAmount) {
	decimalAmount *= 100;
		
	// make sure we only get 2 decimals
	decimalAmount = decimalAmount.toFixed(2);
		
	// if the number is even, we don't need decimals
	var count = 0;
	while(decimalAmount.charAt(count) != '.') {
		count++;
	}
	if(decimalAmount.charAt(count+1) == '0' && decimalAmount.charAt(count+2) == '0')
		decimalAmount = Math.round(decimalAmount);
	return "" + decimalAmount + "%";
}


//***Dollar Date Class

function DollarDate()
{
}

function getMonthString(m)
{
   switch (m)
   {
       case 1:
	return "January";
       case 2:
	return "February";
       case 3:
	return "March";
       case 4:
	return "April";
       case 5:
	return "May";
       case 6:
	return "June";
       case 7:
	return "July";
       case 8:
	return "August";
       case 9:
	return "September";
       case 10:
	return "October";
       case 11:
	return "November";
       case 12:
	return "December";
   }
}


function getDaysInMonth(m, y)
{
   switch (m)
   {
       case 1:
	return 31;
       case 2:
		if( y % 4 == 0) {				
			if( y % 100 == 0) {	
				if ( y  % 400 == 0) 
					return 29;
				else							
					return 28;
			}
			else 
				return 29;
		}
		else return 28;
       case 3:
	return 31;
       case 4:
	return 30;
       case 5:
	return 31;
       case 6:
	return 30;
       case 7:
	return 31;
       case 8:
	return 31;
       case 9:
	return 30;
       case 10:
	return 31;
       case 11:
	return 30;
       case 12:
	return 31;
   }
}

// returns
//     1 -  d1 is later than d2
//     -1 - d2 is later than d1
//     0 - the dates are the same
function isLater(d1, d2)
{
    if ( d1.year > d2.year )
        return 1;
    if ( d1.year < d2.year )
        return -1;

    if ( d1.month > d2.month )
        return 1;
    if ( d1.month < d2.month )
        return -1;

    if ( d1.day > d2.day )
        return 1;
    if ( d1.day < d2.day )
        return -1;

    return 0;
}

function getDollarDateForToday()
{
	var date = new DollarDate();
	var today = new Date();
	date.year = today.getFullYear();
	date.month = today.getMonth() + 1;
	date.day = today.getDate();
	return date;
}


// returns a DollarDate parsed from a mm-dd-yyyy string
function getDollarDateFromString(s)
{
	var date = new DollarDate();

	date.correctFormat = false;

	var splits = s.split("-");
	if ( splits.length != 3 )
	{
		date.errorMessage = "Date must be in format : mm-dd-yyyy";
		return date;
	}

	var m = removeLeadingZeroes( splits[0] );
	m = parseInt(m);
	if ( isNaN(m) )
   	{
		date.errorMessage = "Please enter the month as a number.";
		return date;
   	}
	if ( m < 1 || m > 12 )
	{
		date.errorMessage = "Please enter a month between 1 and 12.";
		return date;
	}

	var y = removeLeadingZeroes( splits[2] );
	y = parseInt(y);
	if ( isNaN(y) )
   	{
		date.errorMessage = "Please enter the year as a number.";
		return date;
   	}
	if ( y < 1 || y > 9999 )
	{
		date.errorMessage = "Please enter a year between 1 and 9999.";
		return date;
	}

	var d = removeLeadingZeroes( splits[1] );
	d = parseInt(d);
	if ( isNaN(d) )
   	{
		date.errorMessage = "Please enter the day as a number."
		return date;
   	}
	var maxd = getDaysInMonth(m, y);
	if ( d < 1 || d > maxd )
	{
		date.errorMessage = getMonthString(m) + ", " + y + " has " + maxd + " days.  Enter a day between 1 and " + maxd + ".";
		return date;
	}

	date.day = d;
	date.month = m;
	date.year = y;
	date.correctFormat = true;
	return date;
}

// returns a string representation of x.  if the string is shorter than length, will pad zeroes
function padToLength(x, len)
{
	var s = x + "";
	while ( s.length < len )
		s = "0" + s;
	return s;
}

// gets a mm-dd-yyy formatted string for a date d. 
function getFormattedString(d)
{
	return padToLength(d.month,2) + "-" + padToLength(d.day,2) + "-" + padToLength(d.year, 4);
}

// removes any leading zeroes from string s
function removeLeadingZeroes(s)
{
	var i = 0;
	while ( s[i] == '0' && i < s.length ) i++;
	if ( i == s.length )
		return "0";
	return s.substring(i);
}


// calculates the approximate difference in days between two date objects
// this function assumes all months are 30.5 days.
function getDayDifference(date1, date2) {
	var days = 365 * (date2.year - date1.year);
	days += ((date2.month - date1.month) * 30.5) + (date2.day - date1.day);
	return Math.round(days);
}

/* isFloat() and TimeObj() added by Per
 * 
 */

function isFloat(n)
{
	return ( n - Math.floor(n) ) > 0;
}

// Time Object.
// Constructor:
//  converts seconds over 60 to minutes + rest seconds;
//	converts minutes over 60 to hours + rest minutes;
// 	converts fractions of hours to minutes.
function TimeObj(h, m, s)
{
	this.h = h > 0 ? h : 0;
	this.m = m > 0 ? m : 0;
	this.s = s > 0 ? s : 0;

	// add seconds over 60 to minutes
	if ( this.s > 59 )
	{
		this.m += Math.floor(this.s / 60);
		this.s = this.s % 60;
	}	
		
	// add minutes over 60 to hours
	if ( this.m > 59 )
	{
		this.h += Math.floor(this.m / 60);
		this.m = this.m % 60;
	}
	
	this.getHours = function()
	{
		return this.h;
	}
	
	this.getMinutes = function()
	{
		return this.m;
	}
	
	this.getSeconds = function()
	{
		return this.s;
	}
	
	this.getFormattedTime = function(format)
	{
		// format as strings.
		// mins, secs: add heading 0 if under 10
		var hStr = this.h.toString();
		var mStr = this.m < 10 ? '0' + this.m.toString() : this.m.toString();
		var sStr = this.s < 10 ? '0' + this.s.toString() : this.s.toString();
		var ret;
		
		if( format == 'table' )
		// return as html table row (without <tr></tr>)
		{
			ret = "<td>" + hStr + ":</td>\n";
			ret += "\t<td>"+ mStr +":</td>\n";
			ret += "\t<td>"+ sStr +"</td>\n";
		}
		// hh:mm:ss am/pm
		else if ( format == 'clock' )
			// hours : minutes
		{
			if( this.h > 12 ) // pm time
			{
				ret = (this.h - 12).toString() + ':' + mStr + ':' + sStr + ' pm';
			}
			else if( this.h == 12 ) // 12pm
			{
				ret = (this.h).toString() + ':' + mStr + ':' + sStr + ' pm';
			}
			else
			{
				ret = hStr + ':' + mStr + ':' + sStr  + ' am';
			}
		}
		else
			// hh:mm:ss
		{
			ret = hStr + ':' + mStr + ':' + sStr;
		}
		return ret;
	}
	
	/* Compares this TimeObj to another TimeObj.
	 * Treats hours and minutes as time of day.
	 * Uses 24-hour time.
	 * This time has to be earlier than time compared to (that).
	 * @return false if non-valid times, otherwise new TimeObj:difference in hours, minutes.*/
	this.compareClockTimes = function(that)
	{
		if ( this.getHours() + (this.getMinutes() / 60) + (this.getSeconds() / 3600) >= 
		 	 that.getHours() + (that.getMinutes() / 60) + (that.getSeconds() / 3600))
		{
			alert("Start-time ("+this.getFormattedTime('clock')+") is either the same, or later than end-time ("+that.getFormattedTime('clock')+"). Please enter a start-time that is earlier than the end-time.");
			return false;
		}		

	 	// adjust minutes if toSeconds < fromSeconds
		if ( that.getSeconds() < this.getSeconds() )
		{
			this.m += 1;
			that.s += ( 60 - this.getSeconds() )
			this.s = 0;		
		}

		// adjust hours if toMinutes < fromMinutes
		if ( that.getMinutes() < this.getMinutes() )
		{
			this.h += 1;
			that.m += ( 60 - this.getMinutes() )
			this.m = 0;		
		}
		
		return new TimeObj(that.h - this.h, that.m - this.m, that.s - this.s)
	}
}
