//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Debt Ratio Calculator
//2002 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 06/12/2002
//Last Modified: 06/12/2002
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//For commercial use rates, contact:
//Dan Peterson:
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//Commercial User Licence #:5782-1237-39-1198
//Commercial Licence Date:2008-09-11
//*******************************************



function stripNum(num) {

   num=num.toString();


   var len = num.length;
   var rnum = "";
   var test = "";
   var j = 0;

   var b = num.substring(0,1);
   if(b == "-") {
      rnum = "-";
   }

   for(i = 0; i <= len; i++) {

      b = num.substring(i,i+1);

      if(b == "0" || b == "1" || b == "2" || b == "3" || b == "4" || b == "5" || b == "6" || b == "7" || b == "8" || b == "9" || b == ".") {
         rnum = rnum + "" + b;

      }

   }

   if(rnum == "" || rnum == "-") {
      rnum = 0;
   }

   rnum = Number(rnum);

   return rnum;

}



function formatNumber(num) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}
		
	finNum=tmpinteger+"."+decimal;

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}


function computeForm(form)

{

var VmonthlyIncome = stripNum(form.monthlyIncome.value);
var VspouseIncome = stripNum(form.spouseIncome.value);
var VotherIncome = stripNum(form.otherIncome.value);
var VrentMortgagePmt = stripNum(form.rentMortgagePmt.value);
var VsecondMortgagePmt = stripNum(form.secondMortgagePmt.value);
var VvehiclePmts = stripNum(form.vehiclePmts.value);
var VcreditUnionPmts = stripNum(form.creditUnionPmts.value);
var VotherLoanPmts = stripNum(form.otherLoanPmts.value);
var VchargePmts = stripNum(form.chargePmts.value);
var VotherPmts = stripNum(form.otherPmts.value);
var VpendingLoanPmts = stripNum(form.pendingLoanPmts.value);

var VtotalIncome = eval(VmonthlyIncome) + eval(VspouseIncome) + eval(VotherIncome);

if(VtotalIncome == 0) {
   form.totalIncome.value = "";
   form.totalPmts.value = "";
   form.debtRatio.value = "";
   alert("Please enter a monthly income figure.");
} else {

form.totalIncome.value= "$" + formatNumber(VtotalIncome);

var VtotalPmts = eval(VrentMortgagePmt) + eval(VsecondMortgagePmt) + eval(VvehiclePmts) + eval(VcreditUnionPmts) + eval(VotherLoanPmts) + eval(VchargePmts) + eval(VotherPmts) + eval(VpendingLoanPmts);
form.totalPmts.value= "$" + formatNumber(VtotalPmts);

var VdebtRatio = VtotalPmts / VtotalIncome * 100;
form.debtRatio.value = formatNumber(VdebtRatio) + "%";
}
}
