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 + "%";
}

// Added by Per 2008-09-30
calcPayment = function(loanamount, rate, years)
{
	var mrate = rate / 1200;
	var factor = Math.pow(1 + mrate, years * 12 );
	payment = loanamount * ( mrate / (1 - 1/factor) );
	return payment;
}



//***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;
}
