var RATE = 0.00632;

// Determine whether or not the methods used by replaceContent() are supported by this browser.
var domSupport = isDOMSupport();
function isDOMSupport() {
	return (document.getElementById && document.replaceChild && document.createTextNode && document.firstChild);
}

// Only execute the following statements when DOM-standard mainipulation supported.
if (domSupport) {
	// Define the list of content block ids.
	var idList = 'info,calc';
	
	// Define the CSS to initially hide the content.
	document.write('<style type=\"text\/css\">');
	document.write('#'+ idList.replace(/,/g,', #'));
	document.write(', #lblresult {display:none;}');
	document.write('<\/style>');
	
	function replaceContent(id,txt) {
		document.getElementById(id).replaceChild(document.createTextNode(txt), document.getElementById(id).firstChild);
	}
	
	// Toggle the display of the content when the links are clicked.
	function toggleDisplay(id) {
		list = idList.split(','); // Split the list into an array.
		for (i=0; i<list.length; i++) {
			// Toggle the display of the main content block.
			document.getElementById(list[i]).style.display = (list[i]==id) ? 'block' : 'none';
			
			// Toggle the display of the Calculator results section.
			document.getElementById('lblresult').style.display = (id=='calc') ? 'block' : 'none';
		}
	}
}

// Define the default form values.
var defaultMort1payment  = 0, defaultMort1balance  = 0, defaultMort2payment  = 0, defaultMort2balance  = 0;
var defaultCc1payment    = 0, defaultCc1balance    = 0, defaultCc2payment    = 0, defaultCc2balance    = 0;
var defaultCc3payment    = 0, defaultCc3balance    = 0, defaultCc4payment    = 0, defaultCc4balance    = 0;
var defaultAuto1payment  = 0, defaultAuto1balance  = 0, defaultAuto2payment  = 0, defaultAuto2balance  = 0;
var defaultOther1payment = 0, defaultOther1balance = 0, defaultOther2payment = 0, defaultOther2balance = 0;

function cleanCurrency(n) { return parseFloat(n.toString().replace(/\$|\,/gi,'')); }
function cleanPercent(n)  { return parseFloat(n.toString().replace(/\%/gi,"")); }

function compute(form) {
	// Retrieve the form values.
	var mort1payment  = form.mort1payment.value;
	var mort1balance  = form.mort1balance.value;
	var mort2payment  = form.mort2payment.value;
	var mort2balance  = form.mort2balance.value;
	var cc1payment    = form.cc1payment.value;
	var cc1balance    = form.cc1balance.value;
	var cc2payment    = form.cc2payment.value;
	var cc2balance    = form.cc2balance.value;
	var cc3payment    = form.cc3payment.value;
	var cc3balance    = form.cc3balance.value;
	var cc4payment    = form.cc4payment.value;
	var cc4balance    = form.cc4balance.value;
	var auto1payment  = form.auto1payment.value;
	var auto1balance  = form.auto1balance.value;
	var auto2payment  = form.auto2payment.value;
	var auto2balance  = form.auto2balance.value;
	var other1payment = form.other1payment.value;
	var other1balance = form.other1balance.value;
	var other2payment = form.other2payment.value;
	var other2balance = form.other2balance.value;
	
	var totalpayments = 0;
	var totalbalance  = 0;
	var newpayment    = 0;
	var savings       = 0;
	
	// Clean the numbers.
	mort1payment  = cleanCurrency(mort1payment);
	mort1balance  = cleanCurrency(mort1balance);
	mort2payment  = cleanCurrency(mort2payment);
	mort2balance  = cleanCurrency(mort2balance);
	cc1payment    = cleanCurrency(cc1payment);
	cc1balance    = cleanCurrency(cc1balance);
	cc2payment    = cleanCurrency(cc2payment);
	cc2balance    = cleanCurrency(cc2balance);
	cc3payment    = cleanCurrency(cc3payment);
	cc3balance    = cleanCurrency(cc3balance);
	cc4payment    = cleanCurrency(cc4payment);
	cc4balance    = cleanCurrency(cc4balance);
	auto1payment  = cleanCurrency(auto1payment);
	auto1balance  = cleanCurrency(auto1balance);
	auto2payment  = cleanCurrency(auto2payment);
	auto2balance  = cleanCurrency(auto2balance);
	other1payment = cleanCurrency(other1payment);
	other1balance = cleanCurrency(other1balance);
	other2payment = cleanCurrency(other2payment);
	other2balance = cleanCurrency(other2balance);
	
	// Set default values.
	if (isNaN(mort1payment))  mort1payment  = defaultMort1payment;
	if (isNaN(mort1balance))  mort1balance  = defaultMort1balance;
	if (isNaN(mort2payment))  mort2payment  = defaultMort2payment;
	if (isNaN(mort2balance))  mort2balance  = defaultMort2balance;
	if (isNaN(cc1payment))    cc1payment    = defaultCc1payment;
	if (isNaN(cc1balance))    cc1balance    = defaultCc1balance;
	if (isNaN(cc2payment))    cc2payment    = defaultCc2payment;
	if (isNaN(cc2balance))    cc2balance    = defaultCc2balance;
	if (isNaN(cc3payment))    cc3payment    = defaultCc3payment;
	if (isNaN(cc3balance))    cc3balance    = defaultCc3balance;
	if (isNaN(cc4payment))    cc4payment    = defaultCc4payment;
	if (isNaN(cc4balance))    cc4balance    = defaultCc4balance;
	if (isNaN(auto1payment))  auto1payment  = defaultAuto1payment;
	if (isNaN(auto1balance))  auto1balance  = defaultAuto1balance;
	if (isNaN(auto2payment))  auto2payment  = defaultAuto2payment;
	if (isNaN(auto2balance))  auto2balance  = defaultAuto2balance;
	if (isNaN(other1payment)) other1payment = defaultOther1payment;
	if (isNaN(other1balance)) other1balance = defaultOther1balance;
	if (isNaN(other2payment)) other2payment = defaultOther2payment;
	if (isNaN(other2balance)) other2balance = defaultOther2balance;
	
	// Calculate the monthly payment and savings.
	if (mort1payment > 0) {
		totalpayments=mort1payment+mort2payment+cc1payment+cc2payment+cc3payment+cc4payment+auto1payment+auto2payment+other1payment+other2payment;
		totalbalance=mort1balance+mort2balance+cc1balance+cc2balance+cc3balance+cc4balance+auto1balance+auto2balance+other1balance+other2balance;
		newpayment=(totalbalance + 3000 + (totalbalance * 0.01)) * RATE;
		savings=totalpayments - newpayment;
		
		// Assign the monthly payment and post savings.
		setValue(form, 'totalpayments', formatCurrency(totalpayments));
		setValue(form, 'totalbalance',  formatCurrency(totalbalance));
		setValue(form, 'payment', formatCurrency(newpayment));
		setValue(form, 'savings', formatCurrency(savings));
		
		// Display the "You could save $xxx each month." message.
		//if (savings > 0) {
			document.getElementById('lblresult2').innerHTML='You could save <b>'+formatCurrency(savings)+'<\/b> each month.';
			document.getElementById('lblresult3').innerHTML='You could save <b>'+formatCurrency(savings)+'<\/b> each month.';
		//} else {
		//	replaceContent('lblresult2', '');
		//}
  } else {
		resetResults(form);
	}
}

function resetResults(frm) {
	setValue(frm, 'totalpayments', '0');
	setValue(frm, 'totalbalance',  '0');
	setValue(frm, 'payment',       '0');
	setValue(frm, 'savings',       '0');
	
	replaceContent('lblresult2', '');
}

function setValue(frm,id,txt) {
	if (domSupport) {
		replaceContent(id,txt)
	}	else {
		eval('frm.' + id + '.value = txt');
	}
}

function formatPercent(n) { return cleanPercent(n) + '%'; }

/***********************************************************
 * Function:  formatCurrency()
 * Original:  Cyanide_7 (leo7278@hotmail.com)
 * Web Site:  http://www7.ewebcity.com/cyanide7
 *
 * This funciton and many more are available free online at
 * The JavaScript Source!! http://javascript.internet.com
 ***********************************************************/
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	//return (((sign)?'':'-') + '$' + num + '.' + cents);
	return (((sign)?'':'-') + '$' + num);
}