//holds dollar times global functions
DT_F = function() {};

//holds global variables
DT_V = function() {};

String.prototype.DT_trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};

DT_F.getFormFloat = function(el, unsetErr, nanError)
{
	var sVal = document.getElementById(el).value;
	sVal = sVal.DT_trim();
	if ( sVal.length == 0 ) {
		throw unsetErr;
	}
	sVal = DT_F.fixNumber(sVal);
	fVal = parseFloat(sVal);
	if ( isNaN(fVal) ) {
		throw nanError;
	}
	return fVal;
};

DT_F.getFormInt = function(el, unsetErr, nanError, roundMessage)
{
	var sVal = document.getElementById(el).value;
	sVal = sVal.DT_trim();
	if ( sVal.length == 0 ) {
		throw unsetErr;
	}
	sVal = DT_F.fixNumber(sVal);
	fVal = parseFloat(sVal);
	if ( isNaN(fVal) ) {
		throw nanError;
	}
	iVal = Math.floor(fVal);
	if ( iVal != fVal ) {
		alert(roundMessage + " " + iVal);
	}
	return iVal;
};


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


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

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

DT_F.getDollarString = function(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;
};

DT_F.calcPayment = function(loan, rate, years)
{
	var mRate = rate / 1200;
	var factor = Math.pow(1 + mRate, years * 12 );
	payment = loan * ( mRate / (1 - 1/factor) );
	return payment;
};

DT_F.calcPaymentMonths = function(loan, months, rate)
{
	var mRate = rate / 1200;
	var payment;

	if ( rate == 0 )
	{
		payment = loan / months;
	}
	else
	{
		var factor = Math.pow(1 + mRate, months );
		payment = loan * ( mRate / (1 - 1/factor) );
	}
	return payment;
};

DT_F.setDateElements = function(id, d)
{
	document.getElementById("dt_day" + id).value = d.getDate();
	document.getElementById("dt_month" + id).value = d.getMonth();
	document.getElementById("dt_year" + id).value = d.getFullYear();
};

DT_F.isLeapYear = function(y)
{
	if( y % 4 == 0) {				
		if( y % 100 == 0) {	
			if ( y  % 400 == 0) {
				return true;
			}
			return false;
		}
		return true;
	}
	return false;
}

DT_F.getDaysInMonth = function(m, y) 
{
	switch (m)
	{
		case 0: return 31;
		case 1: 
			if ( DT_F.isLeapYear(y) ) {
				return 29;
			}
			return 28;
		case 2: return 31;
		case 3: return 30;
		case 4:	return 31;
		case 5: return 30;
		case 6:	return 31;
		case 7:	return 31;
		case 8:	return 30;
		case 9: return 31;
		case 10: return 30;
		case 11: return 31;
   }
   return -1;
}

//   1 = d1 later
//  -1 = d2 later
//   0 = same
DT_F.dateLater = function(d1, d2)
{
	if ( d1.getFullYear() > d2.getFullYear() ) {
		return 1;
	}
    if ( d1.getFullYear() < d2.getFullYear() ) {
		return -1;
	}
	if ( d1.getMonth() > d2.getMonth() ) {
		return 1;
	}
    if ( d1.getMonth() < d2.getMonth() ) {
		return -1;
	}
	if ( d1.getDate() > d2.getDate() ) {
		return 1;
	}
    if ( d1.getDate() < d2.getDate() ) {
		return -1;
	}
	return 0;
};

// Returns the difference in years between d1 and d2, where d2 is later than d1 
DT_F.getYearDifference = function(d1,d2)
{
	var y = d2.getFullYear();
	var m = d1.getMonth();
	var d = d1.getDate();

	if ( DT_F.dateLater(new Date(y,m,d), d2) == 1 ) {
		y--;
	}

	var yd = y - d1.getFullYear();
	var dd = 0;
	var ld = 0;

	while ( DT_F.dateLater(new Date(y,m,d), d2) == -1 )
	{
		if ( DT_F.isLeapYear(y) ) {
			ld++;
		}
		d++;
		dd++;
		if ( d > DT_F.getDaysInMonth(m,y) ) {
			d=1;
			m++;
			if ( m > 11 ) {
				m = 0;
				y++;
			}
		}

	}
	var diy = 365 + ld/366;
	return yd + dd/diy;
};

DT_F.getTotalSeconds = function(s, ap)
{
	var hours, minutes, seconds = 0;

	tabs = s.split(":")
	if ( tabs.length != 2 && tabs.length != 3) {
		throw("Time must be in format hh:mm or hh:mm:ss.");
	}

	hours = parseInt( tabs[0],10 );
	if ( isNaN(hours) ) {
		throw("Invalid hour: " + tabs[0] );
	}
	if ( hours < 1 || hours > 12 ) {
		throw("Invalid hours, must be between 1-12.");
	}
	if ( hours == 12 ) {
		hours = 0;
	}

	minutes = parseInt( tabs[1],10 );
	if ( isNaN(minutes) ) {
		throw("Invalid minutes: " + tabs[1] );
	}
	if ( minutes < 0 || minutes > 59 ) {
		throw("Invalid minutes, must be between 0-59.");
	}
	if  ( tabs.length == 3 )
	{
		seconds = parseInt( tabs[2],10 );
		if ( isNaN(seconds) ) {
			throw("Invalid seconds: " + tabs[2] );
		}
		if ( seconds < 0 || seconds > 59 ) {
			throw("Invalid seconds, must be between 0-59.");
		}
	}

	if ( ap != "AM" && ap != "PM" ) {
		throw("Invalid AM/PM value.")
	}

	if ( ap == "PM" ) {
		hours += 12;
	}


	var total = hours * 3600 + minutes * 60 + seconds;
	return total;
};

DT_F.getTimeString = function(t, useAp)
{
	var hours = Math.floor(t/3600);
	t -= hours * 3600;
	var minutes = Math.floor(t/60);
	t -= minutes * 60;
	seconds = t;

	var postFix = "";
	if ( useAp )
	{
		if ( hours >= 12 ) {
			hours -= 12;
			postFix = "pm";
		}
		else {
			postFix = "am";
		}
		if ( hours == 0 ) {
			hours = 12;
		}
	}

	var x = hours + ":";
	if ( minutes < 10 ) {
		x += "0";
	}
	x += minutes + ":";
	if ( seconds < 10 ) {
		x += "0";
	}
	x += seconds;
	x += postFix;

	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






//***Dollar Date Class

function DollarDate()
{
}





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;
}*/
