// JavaScript Document
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;



function getObjectByID(Objid){	
	if(ns4){
	 return eval('document.' + Objid)
    }
    if(ns6){
     return document.getElementById(Objid);
    }
    if(ie4){
      return document.all(Objid);      
    }  
	return null
}

/* old drawGraph
function drawGraph(graphID,maxheight){
	
	if(ns4){
	 graphObj = eval('document.' + graphID)
    }
    if(ns6){
     graphObj = document.getElementById(graphID);
    }
    if(ie4){
      graphObj = document.all(graphID);      
    }  
	
	currHeight = graphObj.height	
	//alert( " Height " + currHeight	+ " maxheight: " + maxheight)
	if(typeof currHeight.replace != "undefined")
		currHeight = currHeight.replace(/[p,t,x,\s]/g,'')
	
	//currHeight = isNaN(currHeight) || currHeight==""?"":parseInt(currHeight)
	//alert(isNaN(currHeight) + " // " + (currHeight==""))
	
	
	if(isNaN(currHeight) || currHeight==""){		
		
		graphObj.height = 1		
		setTimeout('drawGraph("' + graphID + '","' + maxheight + '")',55)
	}
	else{
		//alert( " Height " + currHeight	+ " maxheight: " + maxheight)
		//alert(currHeight)
		if(parseInt(currHeight) < parseInt(maxheight)){						
			graphObj.height = parseInt(currHeight) + 1
			setTimeout('drawGraph("' + graphID + '","' + maxheight + '")',55)
		}
		else if(parseInt(currHeight) > parseInt(maxheight)){
			if(parseInt(currHeight) - 1 > 1){
				graphObj.height = parseInt(currHeight) - 1
				setTimeout('drawGraph("' + graphID + '","' + maxheight + '")',55)			
			}
			else
				graphObj.height = 1
			
			
		}
	}
	
}
*/

function drawGraph(graphID,maxheight){

var graphObj = $("#"+graphID);
var currHeight = graphObj.height();



if(typeof currHeight.replace != "undefined")
		currHeight = currHeight.replace(/[p,t,x,\s]/g,'')
	
	//currHeight = isNaN(currHeight) || currHeight==""?"":parseInt(currHeight)
	//alert(isNaN(currHeight) + " // " + (currHeight==""))
	
	
	if(isNaN(currHeight) || currHeight==""){		
		
		graphObj.height(1)		
		setTimeout('drawGraph("' + graphID + '","' + maxheight + '")',55)
	}
	else{
		//alert( " Height " + currHeight	+ " maxheight: " + maxheight)
		//alert(currHeight)
		if(parseInt(currHeight) < parseInt(maxheight)){						
			graphObj.height(parseInt(currHeight) + 1);
			setTimeout('drawGraph("' + graphID + '","' + maxheight + '")',55)
		}
		else if(parseInt(currHeight) > parseInt(maxheight)){
			if(parseInt(currHeight) - 1 > 1){
				graphObj.height(parseInt(currHeight) - 1)
				setTimeout('drawGraph("' + graphID + '","' + maxheight + '")',55)			
			}
			else
				graphObj.height()
			
			
		}
	}





}



function SetText(objid,msg, fg, bg){

    if(!fg) fg = "#ff0000";
    if(!bg) bg = "#FFFFFF";
	
    var content = msg;
    
	$("#"+objid).html(content);
	
}



var pay_off_amount = 0;
var GRAPH_MAX_HEIGHT = 140

function monthsToPay(base_amount,annual_interest,minimum_payment){
	
	
	pay_off_amount = 0;
	months_payoff = 0;
	remaining_debt = base_amount;
	monthly_interest = annual_interest/12;
	while(remaining_debt > 0){
		monthly_payment = remaining_debt * minimum_payment;
		if(monthly_payment < 25){
			monthly_payment = 25;
		}
		pay_off_amount += monthly_payment;
		interestamount = remaining_debt * monthly_interest;
		remaining_debt  = remaining_debt - (monthly_payment - interestamount);
		months_payoff++;		
	
	}
	
	return months_payoff;
}





function calculate(){
	MINIMUM_PAYMENT_PERCENT = 0.025
	dz_FEE_PERCENT = 0.15
	dz_FEE_MISC_PERCENT = 0.5
	loanamount = document.dataForm.loanamount.value
	
	count_creditors = document.dataForm.creditors.value
	
	interest = document.dataForm.interest.value 

	clearingDuration = document.dataForm.monthstopayoff.value

	//clearingDuration = 0
	
	loanamount = loanamount.replace(/\$/g,'');
	loanamount = loanamount.replace(/,/g,'');
	loanamount = isNaN(loanamount)?0:parseInt(loanamount)
	interest = interest.replace(/\%/g,'') / 100;
	interest = isNaN(interest)?0:interest
	
	
	
	SetText('tud_loan',formatCurrency(loanamount) )
	SetText('cc_loan',formatCurrency(loanamount))
	SetText('don_loan', formatCurrency(loanamount) )
	SetText('dz_loan',"<b class='highlight'>" + formatCurrency(loanamount) + "</b>")
	doNothingClearingDuration = monthsToPay(loanamount,interest,MINIMUM_PAYMENT_PERCENT)
	
	SetText('dz_dura',"<b class='highlight'>" +clearingDuration+ "</b>")
	

	dz_fee = loanamount * dz_FEE_PERCENT;
	dz_totcost = (loanamount * dz_FEE_MISC_PERCENT) + dz_fee;
	dz_TotalMonthlyPayment = dz_totcost/clearingDuration;
	SetText('dz_TotalCost',"<b class='highlight'>" +formatCurrency(dz_totcost)+ "</b>")
	SetText('dz_monthPay',"<b class='highlight'>" +formatCurrency(dz_TotalMonthlyPayment)+ "</b>")
	SetText('dz_inter',"<b class='highlight'>" +"None"+ "</b>")
	SetText('dz_exp_inter',"<b class='highlight'>" +"None"+ "</b>")
	monthlypayout = (loanamount * (interest/12)) / (1 - Math.pow(1 + (interest/12),-1 * clearingDuration));
	interestpayout = (monthlypayout * clearingDuration) - loanamount;
	
	
	//Consolidated Loan

	consolidatedLoanInterestRate = 0.12;
		
	consolidatedLoanMonthlyPayment = (loanamount * (consolidatedLoanInterestRate/12))/(1 - Math.pow(1 + (consolidatedLoanInterestRate/12),-1 * 60));	
	SetText('tud_monthPay',formatCurrency(consolidatedLoanMonthlyPayment))
	
	
	consolidatedLoanExtraInterestPaid = (consolidatedLoanMonthlyPayment * 60) - loanamount;	
	SetText('tud_exp_inter',formatCurrency(consolidatedLoanExtraInterestPaid))
	
	consolidatedLoanTotalCost = (consolidatedLoanMonthlyPayment * 60);	
	SetText('tud_TotalCost',formatCurrency(consolidatedLoanTotalCost))
	
	SetText('tud_inter',(consolidatedLoanInterestRate * 100) + " %")
	SetText('tud_dura',60)
	
	//Do Nothing
	
	doNothingMonthlyPayment = loanamount*0.03;
	
	SetText('don_monthPay',formatCurrency(doNothingMonthlyPayment))
	
	doNothingExtraInterestPaid = pay_off_amount - loanamount;	
	
	SetText('don_exp_inter',formatCurrency(doNothingExtraInterestPaid))	
	
	doNothingTotalCost = pay_off_amount;
	
	SetText('don_TotalCost',formatCurrency(doNothingTotalCost))
	
	SetText('don_dura',doNothingClearingDuration)
	don_disp_interest = (interest * 100)
	SetText('don_inter',don_disp_interest + ' %')

	//CreditCounselling

	CreditCounsellingRate = Math.round((interest * 0.95)*100)/100;
	CreditCounsellingMonthlyPayment = doNothingMonthlyPayment * 0.95;
	SetText('cc_monthPay',formatCurrency(CreditCounsellingMonthlyPayment))
	
	CreditCounsellingExtraInterestPaid = (CreditCounsellingMonthlyPayment * 60) - loanamount;
	SetText('cc_exp_inter',formatCurrency(CreditCounsellingExtraInterestPaid))
	
	CreditCounsellingTotalCost = (CreditCounsellingMonthlyPayment * 60);	
	SetText('cc_TotalCost',formatCurrency(CreditCounsellingTotalCost))
	SetText('cc_inter',"Variable")
	SetText('cc_dura',60)


	drawMonthlyPayment(consolidatedLoanMonthlyPayment,	CreditCounsellingMonthlyPayment,	doNothingMonthlyPayment,	dz_TotalMonthlyPayment)
	drawTimeInProgram(60,60,doNothingClearingDuration,clearingDuration)	
	drawTotProgramCost(consolidatedLoanTotalCost,CreditCounsellingTotalCost,doNothingTotalCost,dz_totcost)
	
}

function doCompare(var1,var2){
	if(var1 > var2) return -1
	if(var1 < var2) return 1
	return 0
}

function drawMonthlyPayment(conamnt,ccamnt,donamnt,dzamnt){
	arr = new Array()
	arr.push(conamnt)
	arr.push(ccamnt)
	arr.push(donamnt)
	arr.push(dzamnt)	
	arr.sort(doCompare)
	
	MaxVal = arr[0]
	
	SetText('max_monthly_mark',formatCurrency(MaxVal))
	SetText('mid_monthly_mark',formatCurrency(MaxVal/2))
	SetText('min_monthly_mark',formatCurrency(0))
	
	tud_monp_graph_payperiod = (((conamnt/MaxVal) * 100)  * GRAPH_MAX_HEIGHT) / 100
	cc_monp_graph_payperiod = (((ccamnt/MaxVal) * 100)  * GRAPH_MAX_HEIGHT) / 100
	don_monp_graph_payperiod = (((donamnt/MaxVal) * 100)  * GRAPH_MAX_HEIGHT) / 100
	dz_monp_graph_payperiod = (((dzamnt/MaxVal) * 100)  * GRAPH_MAX_HEIGHT) / 100
	
	drawGraph('tud_monp_graph',isNaN(tud_monp_graph_payperiod)?0:tud_monp_graph_payperiod)
	drawGraph('cc_monp_graph',isNaN(cc_monp_graph_payperiod)?0:cc_monp_graph_payperiod)
	drawGraph('don_monp_graph',isNaN(don_monp_graph_payperiod)?0:don_monp_graph_payperiod)
	drawGraph('dz_monp_graph',isNaN(dz_monp_graph_payperiod)?0:dz_monp_graph_payperiod)			
}

function drawTimeInProgram(conamnt,ccamnt,donamnt,dzamnt){
	arr = new Array()
	arr.push(conamnt)
	arr.push(ccamnt)
	arr.push(donamnt)
	arr.push(dzamnt)	
	arr.sort(doCompare)
	
	MaxVal = arr[0]
	
	SetText('max_months',MaxVal)
	SetText('mid_months',MaxVal/2)
	SetText('min_months',0)
	tud_months_graph_time = (((conamnt/MaxVal) * 100) * GRAPH_MAX_HEIGHT) / 100
	cc_months_graph_time = (((ccamnt/MaxVal) * 100) * GRAPH_MAX_HEIGHT) / 100
	don_months_graph_time = (((donamnt/MaxVal) * 100) * GRAPH_MAX_HEIGHT) / 100
	dz_months_graph_time = (((dzamnt/MaxVal) * 100) * GRAPH_MAX_HEIGHT) / 100
	drawGraph('tud_months_graph',isNaN(tud_months_graph_time)?0:tud_months_graph_time)
	drawGraph('cc_months_graph',isNaN(cc_months_graph_time)?0:cc_months_graph_time)
	drawGraph('don_months_graph',isNaN(don_months_graph_time)?0:don_months_graph_time)
	drawGraph('dz_months_graph',isNaN(dz_months_graph_time)?0:dz_months_graph_time)			
}

function drawTotProgramCost(conamnt,ccamnt,donamnt,dzamnt){
	arr = new Array()
	arr.push(conamnt)
	arr.push(ccamnt)
	arr.push(donamnt)
	arr.push(dzamnt)	
	arr.sort(doCompare)
	
	MaxVal = arr[0]
	
	SetText('max_totcost',formatCurrency(MaxVal))
	SetText('mid_totcost',formatCurrency(MaxVal/2))
	SetText('min_totcost',formatCurrency(0))
	
	tud_tcost_graph_ht = (((conamnt/MaxVal) * 100) * GRAPH_MAX_HEIGHT) / 100
	cc_tcost_graph_ht = (((ccamnt/MaxVal) * 100) * GRAPH_MAX_HEIGHT) / 100
	don_tcost_graph_ht = (((donamnt/MaxVal) * 100) * GRAPH_MAX_HEIGHT) / 100
	dz_tcost_graph_ht = (((dzamnt/MaxVal) * 100) * GRAPH_MAX_HEIGHT) / 100
	
	drawGraph('tud_tcost_graph',isNaN(tud_tcost_graph_ht)?0:tud_tcost_graph_ht)
	drawGraph('cc_tcost_graph',isNaN(cc_tcost_graph_ht)?0:cc_tcost_graph_ht)
	drawGraph('don_tcost_graph',isNaN(don_tcost_graph_ht)?0:don_tcost_graph_ht)
	drawGraph('dz_tcost_graph',isNaN(dz_tcost_graph_ht)?0:dz_tcost_graph_ht)			
}




<!-- Original:  Cyanide_7 (leo7278@hotmail.com) -->
<!-- Web Site:  http://www7.ewebcity.com/cyanide7 -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
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);
}
//  End -->