sAgent = navigator.userAgent;
bIsMac = sAgent.indexOf("Mac") > -1;
bIsIE = sAgent.indexOf("MSIE") > -1;
bIsIE3 = sAgent.indexOf("IE 3") > -1;
bIsIE4 = sAgent.indexOf("IE 4") > -1;
bIsIE5 = sAgent.indexOf("IE 5")  > -1;
bIsNav = sAgent.indexOf("Mozilla") > -1 && !bIsIE;
bDoesAll = (bIsIE4 || bIsIE5) && !bIsMac;

var totaldebt = 0;
var totalrepayements = 0;
var totalmonthlyrepayements = 0;
    
function mycalc() {
    totaldebt = 0;
    totalrepayements = 0;
    totalmonthlyrepayements = 0;
    
    bondcalc(document.getElementById('home_Pv').value, document.getElementById('home_interestrate').value, document.getElementById('home_loanterm').value, 'home_pmt',  'home_total');
    bondcalc(document.getElementById('vehicle_Pv').value, document.getElementById('vehicle_interestrate').value, document.getElementById('vehicle_loanterm').value, 'vehicle_pmt',  'vehicle_total');
    bondcalc(document.getElementById('cc_Pv').value, document.getElementById('cc_interestrate').value, document.getElementById('cc_loanterm').value, 'cc_pmt',  'cc_total');
    bondcalc(document.getElementById('personal_Pv').value, document.getElementById('personal_interestrate').value, document.getElementById('personal_loanterm').value, 'personal_pmt',  'personal_total'); 
    bondcalc(document.getElementById('hp_Pv').value, document.getElementById('hp_interestrate').value, document.getElementById('hp_loanterm').value, 'hp_pmt',  'hp_total'); 
    bondcalc(document.getElementById('cash_Pv').value, document.getElementById('cash_interestrate').value, document.getElementById('cash_loanterm').value, 'cash_pmt',  'cash_total'); 
                
    document.getElementById('total_pV').firstChild.nodeValue = "R " + formatCalc(totaldebt);
    document.getElementById('total_pmt').firstChild.nodeValue =  "R " +  formatCalc(totalmonthlyrepayements);
    document.getElementById('total_total').firstChild.nodeValue =  "R " + formatCalc(totalrepayements);
    
    
    preconsolidatedtotaldebt = totaldebt;
    preconsolidatedmontlyrepayments = totalmonthlyrepayements;
    preconsolidatedrepayments = totalrepayements;
    
    totaldebt = 0;
    totalrepayements = 0;
    totalmonthlyrepayements = 0
    

    bondcalc(preconsolidatedtotaldebt, document.getElementById('bond_interestrate').value, document.getElementById('bond_loanterm').value, 'bond_pmt',  'bond_total'); 
    

    document.getElementById('bond_loan').firstChild.nodeValue = "R " + formatCalc(preconsolidatedtotaldebt);
    document.getElementById('total_monthly_savings').firstChild.nodeValue = "R " + formatCalc(preconsolidatedmontlyrepayments-totalmonthlyrepayements);
    document.getElementById('total_savings').firstChild.nodeValue = "R " + formatCalc(preconsolidatedrepayments-totalrepayements);
}

function bondcalc(Pv, iY, tY, Pmt_field ,total_field) {
    
    i = iY/1200;
    n = tY*12;
    
    if ( isNaN(Pv) || isNaN(i) || isNaN(n) ) {
        document.getElementById(Pmt_field).firstChild.nodeValue = "ERROR";
        document.getElementById(total_field).firstChild.nodeValue = "ERROR";
    } else {
        Pmt = parseFloat(i * Pv / (1-Math.pow(1+i,-n)));
        Total = parseFloat(Pmt*n);
       
        document.getElementById(Pmt_field).firstChild.nodeValue= "R " +formatCalc(Pmt);
        document.getElementById(total_field).firstChild.nodeValue= "R " +formatCalc(Total);
        
        totaldebt += parseFloat(Pv);
        totalrepayements += Total;
        totalmonthlyrepayements += Pmt;     
    }
}

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