var TotalPaymentFactored;



function EREmoneyFormat(mnt) {

    mnt -= 0;

    mnt = (Math.round(mnt*100))/100;

	if(mnt == Math.floor(mnt)){

		mnt = mnt + '.00';

	} else {

		if(mnt*10 == Math.floor(mnt*10)){

			mnt = mnt + '0';

		}

	}

	return '$' + mnt;	

}



function moneyFormat(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);

}





function fmtMoney(number) {

	floatPoint = 2;

	decimalSep = '.';

	thousandsSep = ',';

	( number+'' ).match( /(\d+)(?:\.(\d+)|)/ );

	with( RegExp ) {

 		var frac = floatPoint ? decimalSep + Number( '.'+ $2 ).toFixed( floatPoint ).substr( 2 ) : '';

		alert('$ ' + ( ( x = $1.length % 3 ) ? $1.substr( 0, x )+thousandsSep :'' ) + $1.substr( x ).replace( /(\d{3})(?=\d)/g, '$1'+thousandsSep ) + frac);

 		return '$ ' + ( ( x = $1.length % 3 ) ? $1.substr( 0, x )+thousandsSep :'' ) + $1.substr( x ).replace( /(\d{3})(?=\d)/g, '$1'+thousandsSep ) + frac;

	}

}



function SetHeight(big,small,element,info){

	imageHeight = Math.round((small/big)*100);

	growImage(element,imageHeight);

	$(element).title = info;

	$(element).alt = info;

}



function growImage(element,goal){

		if(parseInt($(element).style.height) < goal && parseInt($(element).style.height) < 100) {

			$(element).style.height = Number(parseInt($(element).style.height) + 1) + 'px';

			my_timeout = setTimeout('growImage(\''+element+'\',\''+goal+'\');', 25);

		}

		if(parseInt($(element).style.height) > goal){

			$(element).style.height = Number(parseInt($(element).style.height) - 1) + 'px';

			my_timeout = setTimeout('growImage(\''+element+'\',\''+goal+'\');',25);

		}

}



function CalcCycle(debt,interest,min){

	TotalPaymentFactored = 0;

	TotalCycles = 0;

	currentDebt = debt;

	monthInterest = interest/12;

	while(currentDebt > 0){

		payment = currentDebt*min;

		if(payment < 25){

			payment = 25;

		}

		TotalPaymentFactored += payment;

		waste = currentDebt * monthInterest;

		currentDebt = currentDebt - (payment - waste);

		TotalCycles++;

		

	}

	return TotalCycles;

}



function calcForm(){
	

    
	TotalUnsecuredDebt = $('#userTotalUnsecuredDebt').val().replace('$','');

    
	TotalUnsecuredDebt = TotalUnsecuredDebt.replace(',','');
	
	

	AvgInterestRate = $('#userAvgInterestRate').val().replace('%','') / 100;

	
  
	 

	if(TotalUnsecuredDebt < 5000){
         
		show_calc_debt_warning();

		return;

	}

	
 
   
	if($('#creditorDuration').val() == 1){

		if(TotalUnsecuredDebt <= 20000){

			ProgramPeriod = 30;

		} else {

			ProgramPeriod = 32;

		}

	} else {

		if(TotalUnsecuredDebt <= 8000){

			ProgramPeriod = 21;

		}

		if(TotalUnsecuredDebt <= 9000 && TotalUnsecuredDebt > 8000){

			ProgramPeriod = 23;

		}

		if(TotalUnsecuredDebt <= 10000 && TotalUnsecuredDebt > 9000){

			ProgramPeriod = 26;

		}

		if(TotalUnsecuredDebt <= 11999 && TotalUnsecuredDebt > 10000){

			ProgramPeriod = 28;

		}

		if(TotalUnsecuredDebt <= 20999 && TotalUnsecuredDebt >= 12000){

			ProgramPeriod = 30;

		}

		if(TotalUnsecuredDebt <= 30999 && TotalUnsecuredDebt >=21000){

			ProgramPeriod = 32;

		}

		if(TotalUnsecuredDebt <= 40999 && TotalUnsecuredDebt >= 31000){

			ProgramPeriod = 34;

		}

		if(TotalUnsecuredDebt >= 41000){

			ProgramPeriod = 36;

		}

	}



	// Prep the standard fields

	$('#afsTotalUnsecuredDebt').html(moneyFormat(TotalUnsecuredDebt));
	 
	 

	$('#dcTotalUnsecuredDebt').html(moneyFormat(TotalUnsecuredDebt));

	$('#ccTotalUnsecuredDebt').html(moneyFormat(TotalUnsecuredDebt));

	$('#dnTotalUnsecuredDebt').html(moneyFormat(TotalUnsecuredDebt));

	$('#afsMonthsToPayOff').html(ProgramPeriod);
   


	if(AvgInterestRate*100 == Math.floor(AvgInterestRate*100)){

		displayInterestRate = AvgInterestRate*100 + '.0';

	} else {

		displayInterestRate = AvgInterestRate * 100;

	}

	$('#dnInterestRate').html(Math.round(displayInterestRate*100)/100 + '%');



	nothingDuration = CalcCycle(TotalUnsecuredDebt,AvgInterestRate,0.025);

	//alert(nothingDuration + ' cycles and ' + TotalPaymentFactored);



	// Elimadebt Fee Calculations

	afs_fee = TotalUnsecuredDebt * 0.15;

	TotalCost = (TotalUnsecuredDebt * 0.4) + afs_fee;

	TotalMonthlyPayment = (TotalCost/ProgramPeriod) + 48.30;

	

	$('#afsYourTotalCost').html(moneyFormat(TotalCost));

	$('#afsMonthlyPayment').html(moneyFormat(TotalMonthlyPayment));



	

	MonthlyPayment = (TotalUnsecuredDebt * (AvgInterestRate/12)) / (1 - Math.pow(1 + (AvgInterestRate/12),-1 * ProgramPeriod));

	IntrestPaid = (MonthlyPayment * ProgramPeriod) - TotalUnsecuredDebt;


  


//

// Debt Consolodation

//

	dccInterestRate = 0.11;

		

	dccMonthlyPayment = (TotalUnsecuredDebt * (dccInterestRate/12))/(1 - Math.pow(1 + (dccInterestRate/12),-1 * 60));

	$('#dcMonthlyPayment').html( moneyFormat(dccMonthlyPayment));

	

	dccExtraInterestPaid = (dccMonthlyPayment * 60) - TotalUnsecuredDebt;

	$('#dcExtraInterestPaid').html(moneyFormat(dccExtraInterestPaid));

	

	dccTotalCost = (dccMonthlyPayment * 60);

	$('#dcYourTotalCost').html(moneyFormat(dccTotalCost));

	

	//dccTotalSavings = dccTotalCost - TotalCost;

	//$('dcTotalSavings').innerHTML = moneyFormat(dccTotalSavings);





//

// Do Nothing

//



	dnnMonthlyPayment = TotalUnsecuredDebt*0.03;

	$('#dnMonthlyPayment').html(moneyFormat(dnnMonthlyPayment));



	dnnExtraInterestPaid = TotalPaymentFactored - TotalUnsecuredDebt;

	//dnnExtraInterestPaid = (dnnMonthlyPayment * 270) - TotalUnsecuredDebt;

	$('#dnExtraInterestPaid').html(moneyFormat(dnnExtraInterestPaid));

	

	dnnTotalCost = TotalPaymentFactored;

	//dnnTotalCost = (dnnMonthlyPayment * 270);

	$('#dnYourTotalCost').html(moneyFormat(dnnTotalCost));

	

	$('#dnMonthsToPayOff').html(nothingDuration);

	//dnnTotalSavings = dnnTotalCost - TotalCost;

	//$('dnTotalSavings').innerHTML = moneyFormat(dnnTotalSavings);



////

////	CC Fields

////

	cccRate = Math.round((AvgInterestRate * 0.95)*100)/100;



	//$('ccInterestRate').innerHTML = cccRate*100 + '.0%';

	

	//cccMonthlyPayment = (TotalUnsecuredDebt * (cccRate/12))/(1- Math.pow(1 + (0.09/12),-60));

	cccMonthlyPayment = dnnMonthlyPayment * 0.95;

	$('#ccMonthlyPayment').html(moneyFormat(cccMonthlyPayment));

	

	cccExtraInterestPaid = (cccMonthlyPayment * 60) - TotalUnsecuredDebt;

	$('#ccExtraInterestPaid').html( moneyFormat(cccExtraInterestPaid));

	

	cccTotalCost = (cccMonthlyPayment * 60);

	$('#ccYourTotalCost').html( moneyFormat(cccTotalCost));

	

	//cccTotalSavings = cccTotalCost - TotalCost;

	//$('ccTotalSavings').innerHTML = moneyFormat(cccTotalSavings);

////////////////////////////////////////////////////////////

	//DrawChart('totalinterestpaid','Total Interest Paid','1',dccExtraInterestPaid,cccExtraInterestPaid,dnnExtraInterestPaid,'0')

	//DrawChart('monthlypayments','Monthly Payments','1',dccMonthlyPayment,cccMonthlyPayment,dnnMonthlyPayment,TotalMonthlyPayment);

	//DrawChart('monthstopayoff','Total Months to Pay Off','0','60','60','270',ProgramPeriod)

	//DrawChart('totalpaid','Total Cost','1',dccTotalCost,cccTotalCost,dnnTotalCost,TotalCost);

	

	/*

	if($('results_table').style.display == 'none'){

		new Effect.SlideDown('results_table');

	}

	*/

	

	TotalMonthlyPayment = Math.round(TotalMonthlyPayment*100)/100;

	dccMonthlyPayment = Math.round(dccMonthlyPayment*100)/100;

	dnnMonthlyPayment = Math.round(dnnMonthlyPayment*100)/100;

	cccMonthlyPayment = Math.round(cccMonthlyPayment*100)/100;

var graph = new BAR_GRAPH("vBar");

graph.values = dccMonthlyPayment + "," + cccMonthlyPayment + "," + dnnMonthlyPayment + "," + TotalMonthlyPayment;

graph.labels = "Consolidation<br>Loan,Credit<br>Counseling,Do<br>Nothing,Premier<br>Debt<br>Solvers";

graph.showValues = 2;

graph.barWidth = 65;

graph.barLength = 1;

graph.labelSize = 10;

graph.absValuesSize = 12;

graph.percValuesSize = 12;

graph.graphPadding = 2;

graph.graphBGColor = "#ffffff";

graph.graphBorder = "1px solid #425b79";

graph.barColors = "#f2f2f2";

graph.barBGColor = "#ffffff";

graph.barBorder = "1px outset white";

graph.labelColor = "#ffffff";

graph.labelBGColor = "#425b79";

graph.labelBorder = "0px groove white";

graph.absValuesColor = "#000000";

graph.absValuesBGColor = "#e3a72f";

graph.absValuesBorder = "0px groove white";



$('#graph1').html("<b>Your Monthly Payments</b><br><br>" + graph.create());



	TotalCost = Math.round(TotalCost*100)/100;

	dccTotalCost = Math.round(dccTotalCost*100)/100;

	dnnTotalCost = Math.round(dnnTotalCost*100)/100;

	cccTotalCost = Math.round(cccTotalCost*100)/100;

var graph2 = new BAR_GRAPH("vBar");

graph.values = dccTotalCost + "," + cccTotalCost + "," + dnnTotalCost + "," + TotalCost;

graph.labels = "Consolidation<br>Loan,Credit<br>Counseling,Do<br>Nothing,Premier<br>Debt<br>Solvers";

graph.showValues = 2;

graph.barWidth = 65;

graph.barLength = 1;

graph.labelSize = 10;

graph.absValuesSize = 12;

graph.percValuesSize = 12;

graph.graphPadding = 2;

graph.graphBGColor = "#ffffff";

graph.graphBorder = "1px solid #425b79";

graph.barColors = "#f2f2f2";

graph.barBGColor = "#ffffff";

graph.barBorder = "1px outset white";

graph.labelColor = "#ffffff";

graph.labelBGColor = "#425b79";

graph.labelBorder = "0px groove white";

graph.absValuesColor = "#000000";

graph.absValuesBGColor = "#e3a72f";

graph.absValuesBorder = "0px groove white";



$('#graphCost').html("<b>Your Total Cost</b><br><br>" + graph.create());


}



function show_calc_debt_warning(){

	//AlertError('userTotalUnsecuredDebt');
    
	obj = $('#userTotalUnsecuredDebt');

	$('#debt_warning').style.left = (findPosX(obj)) + 'px';

	$('#debt_warning').style.top =  (findPosY(obj) - 60) + 'px';

	$('#debt_warning').style.visibility = 'visible';

}



function live_format_debt(n){

	n = String(n).replace(/[^0-9]+/g,'');

	return  moneyFormat(n);

}



function  live_format_percentage(n){

	n = String(n).replace(/[^0-9]/g,'');

	n = n.replace('.','');

	if(n.substring(0,1) == '0'){

		n = n.substring(1);

	}

	if(n.length >= 3){

		n = n.substring(0,2) + '.' + n.substring(2,3);

	}

	if(n.length == 2){

		n = n.substring(0,1) + '.' + n.substring(1);

	}

	if(n.length == 1){

		n = '0.' + n;

	}





	return n + '%';

}