cctime_calculate = function()
{
	var p = document.getElementById("cctime_balance").value;
	if ( p.length == 0 )
	{
		alert("Please enter your current credit card balance");
		return;
	}
	p = fixNumber(p);
	p = parseFloat(p);
	if ( isNaN(p) )
	{
	alert("Please enter the current balance as a number");
		return;
	}
	if ( p < 0 )
	{
		alert("Please enter a balance >= 0");
		return;
	}
	
	var rate = document.getElementById("cctime_rate").value;
	if ( rate.length == 0 )
	{
		alert("Please enter an interest rate");
		return;
	}
	rate = fixNumber(rate);
	rate = parseFloat(rate);
	if ( isNaN(rate) )
	{
		alert("Please enter the interest rate as a number");
		return;
	}
	if ( rate <= 0 || rate >= 35 )
	{
		alert("Please enter a interest rate greater than 0 and less than 35");
		return;
	}

	var spending = document.getElementById("cctime_spending").value;
	if ( spending.length > 0 )
	{
		spending = fixNumber(spending);
		spending = parseFloat(spending);
		if ( isNaN(spending) )
		{
			alert("Please enter the monthly spending as a number");
			return;
		}
	} 
	else spending = 0;
	
	if ( spending < 0 )
	{
		alert("Please enter a positive monthly spending");
		return;
	}

	var months = document.getElementById("cctime_months").value;
	if ( months.length == 0 )
	{
		alert("Please enter number of months");
		return;
	}
	months = fixNumber(months);
	months = parseFloat(months);
	if ( isNaN(months) )
	{
		alert("Please enter the number of months as  a number");
		return;
	}
	if ( months < 0 )
	{
		alert("Please enter a positive number of months");
		return;
	}
	
	// monthly rate:
	var m = rate / 1200;
	
	// calculate
	var nom = p * m ;
	var denom = 1 - Math.pow(1 + m, -1 * months);
	var x = spending + ( nom / denom );
	var interest = (months * x) - p - ( months * spending );

	var resel = document.getElementById("cctime_dt_results");

	var results = "<p>To pay off your credit card balance in " + months + " months, you will have to make a monthly payment of <b>" + formatDollars(x) + "</b>.</p>";

	results += "<p>You will pay a total of <b>" + formatDollars(interest) + "</b> in interest.</p>";
	
	resel.innerHTML = results;
	resel.style.display = "block";
}// modified css: http://www.dollartimes.com/calculators/on-your-site/calc-css.php
addJavascript = function(fileName) {
	var th = document.getElementsByTagName('head')[0];
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',fileName);
	th.appendChild(s);
}
addStylesheet = function(fileName) {
	var th = document.getElementsByTagName('head')[0];
	var s = document.createElement('link');
	s.setAttribute('type','text/css');
	s.setAttribute('title', 'dt-calc-style');
	s.setAttribute('rel','stylesheet');
	s.setAttribute('href',fileName);
	th.appendChild(s);
}


addJavascript('http://www.dollartimes.com/script/calcutil.js');
addStylesheet('http://www.dollartimes.com/calculators/on-your-site/calc-css.php');

var out = '\
<div id="cctime_dt_calculator" class="dt_calculator">\n\
	<h2>Credit Card Payment Calculator</h2>\n\
	<p class="small">Provided by DollarTimes.com</p>\n\
	<p class="instructions">This will calculate how much you have to pay every month to eliminate your credit card balance in a certain amount of time. It will also calculate the amount of interest you will have paid during that time.</p>\n\
	<div class="a">Current Balance</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="cctime_balance" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Amount Charged Per Month</div>\n\
	<div class="b">$</div>\n\
	<div class="c"><input id="cctime_spending" type="text" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Interest Rate (APR)</div>\n\
	<div class="b">%</div>\n\
	<div class="c"><input id="cctime_rate" type="text" value="14" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Months to Pay Off Balance</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="cctime_months" type="text" value="18"/></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="d"><button onclick="cctime_calculate()">Calculate</button></div>\n\
	<div id="cctime_dt_results" class"dt_results">&nbsp;</div>\n\
</div>\n\
';



var calcUrl = "http://www.dollartimes.com/calculators/on-your-site/calculator.php?id=12";

var scriptEls = document.getElementsByTagName('script');
var scriptEl=false;


for(i=0; i<scriptEls.length; i++)
{
	var t = scriptEls[i];
	var src = (t.getAttribute('src'));
	if(src == calcUrl) 
	{
		scriptEl = t;
		break;
	}
}
if(!scriptEl)
{
	document.write('\n<p>Failed loading calculator. The code might have been updated. Please get the correct code at <a href="http://www.dollartimes.com/calculators/on-your-site/'+calcUrl+'">http://dollartimes.com/calculators/on-your-site/'+calcUrl+'</a></p>');
}
else
{
	var par = scriptEl.parentNode;  
	var link = par.getElementsByTagName('a')[0];
	var el = document.createElement('div');
	el.innerHTML = out;
	par.appendChild(el);

	if (link.toString().match("/calculators/")) {

		link.style.fontSize="80%";
		var calcDiv = document.getElementById('cctime_dt_calculator');
		if(calcDiv)	{
			// Put link on bottom of calculator box
			par.removeChild(link)			
			calcDiv.appendChild(link);	
		}
	}
}
