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('rel','stylesheet');
	s.setAttribute('href',fileName);
	th.appendChild(s);
}
cc_calculate = function()
{
	var p = document.getElementById("cc_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("cc_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("cc_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 x = document.getElementById("cc_payment").value;
	if ( x.length == 0 )
	{
		alert("Please enter a down payment");
		return;
	}
	x = fixNumber(x);
	x = parseFloat(x);
	if ( isNaN(x) )
	{
		alert("Please enter the monthly payment as a number");
		return;
	}
	if ( x < 0 )
	{
		alert("Please enter a monthly payment >= 0");
		return;
	}
		
	// monthly rate:
	var m = rate / 1200;
	
	// you don't pay back what you spend
	x -= spending;
	if(x < 0) {
		alert("You are charging more on your card than you are paying back. With this spending pattern, you won't pay back your credit card debt.");
		return;
	}
	
	// make sure payment is not too small i.e. smaller than the balance is growing
	if(x <= (m * p)) 
	{
		alert("Your monthly payment of " + formatDollars(x+spending) + " is smaller than your monthly charges and financing cost of " + formatDollars(m * p + spending) + ". You will never pay off your debt!");
		return;
	}
	
	// calculate
	var nom = ( (-1 * p * m) / x ) + 1 ;
	var denom = 1 + m;
	var months = -1 * ( Math.log ( nom ) / Math.log ( denom )	);
	var interest = (months * x)- p;
	
	var resel = document.getElementById("cc_dt_results");

	var results = "<p>With the information you have given, it will take <b>" + Math.ceil(months) + "</b> months to pay off your credit card balance.</p>";

	results += "<p>You will pay a total of <b>" + formatDollars(interest) + "</b> in interest.</p>";
	
	resel.innerHTML = results;
	resel.style.display = "block";

}


// RUN SCRIPT
addJavascript('calcutil.js');
addStylesheet('calc.css');

var out = '\
<div id="cc_dt_calculator" class="dt_calculator">\n\
	<p class="instructions">This will calculate how many months it will take to pay off your credit card balance, and 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="cc_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="cc_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="cc_rate" type="text" value="14" /></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="a">Monthly Payment</div>\n\
	<div class="b"></div>\n\
	<div class="c"><input id="cc_payment" type="text"/></div>\n\
	<div class="clear"></div>\n\
	\n\
	<div class="d"><button onclick="cc_calculate()">Calculate</button></div>\n\
	<div id="cc_dt_results" class="dt_results">&nbsp;</div>\n\
</div>\n\
';
var url = 'credit-card-payment.htm';

//var baseUrl = 'http://localhost/';
//var scriptUrl = 'on-your-site/' + url.split('.')[0] + '.js';
var baseUrl = '';
var scriptUrl = url.split('.')[0] + '.js';

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 == (baseUrl + scriptUrl))
	{
		scriptEl = t;
		break;
	}
}
if(!scriptEl)
{
	//document.write('\n<p>Failed loading calculator. </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 && link.href==baseUrl + url) {
		link.style.fontSize="80%";
		var calcDiv = document.getElementById('cc_dt_calculator');
		if(calcDiv)	{
			// Put link on bottom of calculator box
			par.removeChild(link)			
			calcDiv.appendChild(link);	
		}
	}
}
