var totalIn, totalEx, endtotal;
function calcIncome() {
	var income1, income2, income3;
	if (document.propform.pincome1.value == "") {
		income1 = 0;
	} else {
		income1 = parseInt(document.propform.pincome1.value.replace(/,/g,""));
	}
	
	if (document.propform.pincome2.value == "") {
		income2 = 0;
	} else {
		income2 = parseInt(document.propform.pincome2.value.replace(/,/g,""));
	}
	
	if (document.propform.pincome3.value == "") {
		income3 = 0;
	} else {
		income3 = parseInt(document.propform.pincome3.value.replace(/,/g,""));
	}
	
	totalIn = income1 + income2 + income3;
	document.propform.ptotal.value = totalIn
	format_num(document.propform.ptotal);
}

function calcExpenses() {
	var interest, taxes, insurance, repairs, other;
	if (document.propform.einterest.value == "") {
		interest = 0;
	} else {
		interest = parseInt(document.propform.einterest.value.replace(/,/g,""));
	}
	////////////
	if (document.propform.etaxes.value == "") {
		taxes = 0;
	} else {
		taxes = parseInt(document.propform.etaxes.value.replace(/,/g,""));
	}
	///////////
	if (document.propform.einsurance.value == "") {
		insurance = 0;
	} else {
		insurance = parseInt(document.propform.einsurance.value.replace(/,/g,""));
	}
	///////////
	if (document.propform.erepairs.value == "") {
		repairs = 0;
	} else {
		repairs = parseInt(document.propform.erepairs.value.replace(/,/g,""));
	}
	///////////
	if (document.propform.eother.value == "") {
		other = 0;
	} else {
		other = parseInt(document.propform.eother.value.replace(/,/g,""));
	}
	
	
	totalEx = interest + taxes + insurance + repairs + other;
	document.propform.etotal.value = totalEx
	format_num(document.propform.etotal);
}

function calcTotal() {
	if (document.propform.etotal.value == "")
		totalEx = 0;
	if (document.propform.ptotal.value == "")
		totalIn = 0;	
		
	endtotal = totalIn - totalEx;
	
	if (endtotal < 0) {
	  document.propform.endtotal.className = "red";
	} else {
	  document.propform.endtotal.className = "normal";	
	}
	document.propform.endtotal.value = endtotal;	
	format_num(document.propform.endtotal);	
}

function printForm() {
	print();
}

function Calcrepayments() {
	var amount = parseInt(document.monthly.LoanAmount.value.replace(/,/g,""));
	var deposit = parseInt(document.monthly.Deposit.value.replace(/,/g,""));
	var term = ((parseInt(document.monthly.Term.value.replace(/,/g,"")))*12)*-1;
	var rate = ((document.monthly.Rate.value.replace(/,/g,"")) / 100) / 12;
	var bond = amount - deposit;
	var monthly = 0;
	
	monthly = bond * (rate / (1 - (Math.pow((1 + rate), term))));
	document.monthly.Monthly.value = Math.round(monthly);
	format_num(document.monthly.Monthly);	
	
}

function rounding(n) {
        pennies = n * 100 ;
        pennies = Math.round(pennies) ;
        strPennies = "" + pennies ;
        len = strPennies.length ;
        return strPennies.substring(0, len - 2) + "." + strPennies.substring((len - 2), len);
}

function nper() {
        fv = 0;
        origterm = parseFloat(document.monthly.Term.value.replace(/,/g,""));
        origpay = parseFloat(document.monthly.Monthly.value.replace(/,/g,""));
        extpay = parseFloat(document.monthly.Extra.value.replace(/,/g,""));
        var month = parseFloat(document.monthly.LoanAmount.value.replace(/,/g,""));
		var deposit = parseFloat(document.monthly.Deposit.value.replace(/,/g,""));
		pv = month - deposit;
        per = 12;
        pv = -pv;
        pay = origpay + extpay;
        rate = document.monthly.Rate.value;

   if (document.monthly.Extra.value == "") {

        alert("Please enter how much extra you would like to pay off your loan!");
   }
   else {
   rate = eval((rate)/(per * 100));
   if ( rate == 0 )    // Interest rate is 0
   {
         nper_value = - (fv + pv)/pay; 
   }
   else 
   {
         nper_value = Math.log((-fv * rate + pay)/(pay+ rate * pv))/ Math.log(1 + rate);
   }
        new_nper =  rounding(nper_value);
        orig_total_amt = origpay * origterm * 12;
        new_total_amt = pay * new_nper;
        save = orig_total_amt - new_total_amt;
        document.monthly.NewTerm.value = rounding(nper_value/12);
        document.monthly.Saving.value = Math.round(save);
        
        format_num(document.monthly.NewTerm);	
        format_num(document.monthly.Saving);	
   }
}

function doSaveAs(){
	if (document.execCommand){
		document.execCommand("SaveAs")
	}
	else {
		alert("Save-feature available only in Internet Exlorer 5.x.")
	}
}
