///////// Math rounding function ///////////////////////////
// Permission to use/change this code is granted for all  //
// puposes as long as this header remains in tact.        //
//                                                        //
// Parameters --                                          //
//   input  -- pass in any number (int,float)             //
//   output -- returns a string representing the input    //
//             rounded off to 100ths place                //
//                                                        //
//  (C) 1996 Andy Augustine for Frequency Graphics 3.1.96 //
//                                                        //
// Modified calculator fields, but functions and concepts //
// intact.                              --LK. 13.7.2000.  //
//                                                        //
// Mods debugged, unnecessary legacy code removed, slight //
// reformatting ofcode for readibility. --JD. 18.7.2000.  //
//                                                        //
// Full revision of implementation-specific functions and //
// processes for the Budgeting sub-section of the Credit  //
// Care tier on Providian.com.  All fields modified and   //
// renamed, HTML formatting and UI design, CSS classes    //
// applied.                             --JD 21.08.2000   //
////////////////////////////////////////////////////////////

function isFieldBlank(theField) {
  if (theField.value.length == 0) {  // length has no properties error
    alert("All fields must be completed.");
    theField.focus();
    return true;
  } else {
   return false;
  }
}

function moneyFormat(input) {  // reformatted
  var dollars = Math.floor(input);
  var cents  = "" + Math.round(input * 100);
  cents = cents.substring(cents.length-2, cents.length);
  if (cents == "0") input = dollars + ".00";
  else input = dollars + "." + cents;
  return input;
}

function balanceFormat(input) {  // reformatted
  var dollars = 0;
  var cents = 0;
  if (input < 0) dollars = Math.ceil(input);
  else dollars = Math.floor(input);
  cents  = "" + Math.round(input * 100);
  cents = cents.substring(cents.length-2, cents.length);
  if ((input < 0) && (input > -1) && (cents == "0")) input = "-" + dollars + ".00";
  else if ((input < 0) && (input > -1) && (cents != "0")) input = "-" + dollars + "." + cents;
  else if ((input >=0) || (input < -1)) {
    if (cents == "0") input = dollars + ".00";
    else input = dollars + "." + cents;
  }
  return input;
}

function checkNumber(input, msg) {  // reformatted
  amsg = msg + " field has invalid data: " + input.value;  // input.value is not an object error
  var str = input.value;
  for (var i = 0; i < str.length; i++) {
    var ch = str.substring(i, i + 1);
    if ((ch < "0" || "9" < ch) && ch != '.') {
      alert(amsg);
      input.focus();
      return false;
    }
  }
  input.value = str;
  return true;
}

function processCalc(form) {
// Validate Input
 if ((isFieldBlank(form.monthpay) == true) ||
 (isFieldBlank(form.weekpay) == true) ||
 (isFieldBlank(form.biwkpay) == true) ||
 (isFieldBlank(form.bimopay) == true) ||
 (isFieldBlank(form.irreginc) == true) ||
 (isFieldBlank(form.ssinc) == true) ||
 (isFieldBlank(form.investinc) == true) ||
 (isFieldBlank(form.pubasstinc) == true) ||
 (isFieldBlank(form.alimonyinc) == true) ||
 (isFieldBlank(form.otherinc) == true) ||
 (isFieldBlank(form.rent) == true) ||
 (isFieldBlank(form.proptax) == true) ||
 (isFieldBlank(form.maint) == true) ||
 (isFieldBlank(form.hutils) == true) ||
 (isFieldBlank(form.water) == true) ||
 (isFieldBlank(form.otherhexp) == true) ||
 (isFieldBlank(form.groceries) == true) ||
 (isFieldBlank(form.daycare) == true) ||
 (isFieldBlank(form.carloan) == true) ||
 (isFieldBlank(form.carins) == true) ||
 (isFieldBlank(form.gas) == true) ||
 (isFieldBlank(form.carrepair) == true) ||
 (isFieldBlank(form.health) == true) ||
 (isFieldBlank(form.cc01) == true) ||
 (isFieldBlank(form.cc02) == true) ||
 (isFieldBlank(form.cc03) == true) ||
 (isFieldBlank(form.studloans) == true) ||
 (isFieldBlank(form.otherloan) == true) ||
 (isFieldBlank(form.alimonyexp) == true) ||
 (isFieldBlank(form.phone) == true) ||
 (isFieldBlank(form.cable) == true) ||
 (isFieldBlank(form.insurance) == true) ||
 (isFieldBlank(form.emerg) == true) ||
 (isFieldBlank(form.gensave) == true) ||
 (isFieldBlank(form.othersav) == true) ||
 (isFieldBlank(form.othertrans) == true) ||
 (isFieldBlank(form.otherexp) == true)  ||
 (isFieldBlank(form.computer) == true)  ||
 (isFieldBlank(form.magazines) == true)  ||
 (isFieldBlank(form.pets) == true))
 return false;

 if ((!checkNumber(form.monthpay, "Monthly Paycheck")) ||
 (!checkNumber(form.weekpay, "Weekly Paycheck")) ||
 (!checkNumber(form.biwkpay, "Bi-Weekly Paycheck")) ||
 (!checkNumber(form.bimopay, "Bi-Monthly Paycheck")) ||
 (!checkNumber(form.irreginc, "Irregular Income, Yearly Total")) ||
 (!checkNumber(form.ssinc, "Pension, Social Security Benefits")) ||
 (!checkNumber(form.investinc, "Investment Earnings")) ||
 (!checkNumber(form.pubasstinc, "Public Assistance")) ||
 (!checkNumber(form.alimonyinc, "Income from Alimony/Child Support")) ||
 (!checkNumber(form.otherinc, "Other Income")) ||
 (!checkNumber(form.rent, "Rent/Mortgage Payments")) ||
 (!checkNumber(form.proptax, "Property Tax/Insurance")) ||
 (!checkNumber(form.maint, "Home Maintenance")) ||
 (!checkNumber(form.hutils, "Gas/Oil")) ||
 (!checkNumber(form.water, "Water")) ||
 (!checkNumber(form.otherhexp, "Other Housing Expenses")) ||
 (!checkNumber(form.groceries, "Groceries")) ||
 (!checkNumber(form.daycare, "Day Care/Tuition")) ||
 (!checkNumber(form.carloan, "Car Loan(s)")) ||
 (!checkNumber(form.carins, "Car Insurance")) ||
 (!checkNumber(form.gas, "Gas and Oil")) ||
 (!checkNumber(form.carrepair, "Car Repairs")) ||
 (!checkNumber(form.health, "Health Care")) ||
 (!checkNumber(form.cc01, "Credit Card #1")) ||
 (!checkNumber(form.cc02, "Credit Card #2")) ||
 (!checkNumber(form.cc03, "Credit Card #3")) ||
 (!checkNumber(form.studloans, "Installments/Student Loans")) ||
 (!checkNumber(form.otherloan, "Other Loan Payments")) ||
 (!checkNumber(form.alimonyexp, "Alimony/Child Support Payments")) ||
 (!checkNumber(form.phone, "Telephone")) ||
 (!checkNumber(form.cable, "Cable")) ||
 (!checkNumber(form.insurance, "Insurance (other than car)")) ||
 (!checkNumber(form.emerg, "Emergency Savings")) ||
 (!checkNumber(form.gensave, "General Savings")) ||
 (!checkNumber(form.othersav, "Other Monthly Savings")) ||
 (!checkNumber(form.othertrans, "Other Transportation Expenses")) ||
 (!checkNumber(form.computer, "Computer/Internet")) ||
 (!checkNumber(form.magazines, "Magazines/Newspapers")) ||
 (!checkNumber(form.pets, "Pets")) ||
 (!checkNumber(form.otherexp, "Other Nonhousing Expenses")))
 return false;

// Get Input values
 gmonthpay = parseFloat(form.monthpay.value)
 gweekpay = parseFloat(form.weekpay.value)
 gbiwkpay = parseFloat(form.biwkpay.value)
 gbimopay = parseFloat(form.bimopay.value)
 girreginc = parseFloat(form.irreginc.value)
 gssinc = parseFloat(form.ssinc.value)
 ginvestinc = parseFloat(form.investinc.value)
 gpubasstinc = parseFloat(form.pubasstinc.value)
 galimonyinc = parseFloat(form.alimonyinc.value)
 gotherinc = parseFloat(form.otherinc.value)
 grent = parseFloat(form.rent.value)
 gproptax = parseFloat(form.proptax.value)
 gmaint = parseFloat(form.maint.value)
 ghutils = parseFloat(form.hutils.value)
 gwater = parseFloat(form.water.value)
 gotherhexp = parseFloat(form.otherhexp.value)
 ggroceries = parseFloat(form.groceries.value)
 gdaycare = parseFloat(form.daycare.value)
 gcarloan = parseFloat(form.carloan.value)
 gcarins = parseFloat(form.carins.value)
 ggas = parseFloat(form.gas.value)
 gcarrepair = parseFloat(form.carrepair.value)
 ghealth = parseFloat(form.health.value)
 gcc01 = parseFloat(form.cc01.value)
 gcc02 = parseFloat(form.cc02.value)
 gcc03 = parseFloat(form.cc03.value)
 gstudloans = parseFloat(form.studloans.value)
 gotherloan = parseFloat(form.otherloan.value)
 galimonyexp = parseFloat(form.alimonyexp.value)
 gphone = parseFloat(form.phone.value)
 gcable = parseFloat(form.cable.value)
 ginsurance = parseFloat(form.insurance.value)
 gemerg = parseFloat(form.emerg.value)
 ggensave = parseFloat(form.gensave.value)
 gotherexp = parseFloat(form.otherexp.value)
 gothersav = parseFloat(form.othersav.value)
 gothertrans = parseFloat(form.othertrans.value)
 gcomputer = parseFloat(form.computer.value)
 gmagazines = parseFloat(form.magazines.value)
 gpets = parseFloat(form.pets.value)

// Total monthly expenses 
 dtotexp  = grent + gproptax + gmaint  + ghutils + gwater + gotherhexp + ggroceries  + gdaycare + gcarloan + gcarins + ggas + gcarrepair  + ghealth + gcc01 + gcc02 + gcc03 + gstudloans + gotherloan + galimonyexp + gphone + gcable + ginsurance  + gothertrans  + gcomputer + gmagazines + gpets + gemerg + gothersav + ggensave + gotherexp

// Total Monthly Wages
 dnetpay =  gmonthpay + (gweekpay * 4.333) + (gbiwkpay * 2.167) + (gbimopay * 2) + (girreginc/12)

// Total Monthly Income
 dnetinc2 = dnetpay + gssinc + ginvestinc + gpubasstinc + galimonyinc + gotherinc
 
// Total Supplemental Income 
 dnetsupinc = gssinc + ginvestinc + gpubasstinc + galimonyinc + gotherinc
 
//Total Housing Expenses + gelect
 dnethouse = grent + gproptax + gmaint  + ghutils + gwater + gotherhexp
 
//Total General Family Expenses
dnetgen = ggroceries + gdaycare + ghealth + galimonyexp + ginsurance + gotherexp
 
//Total Transportation Expenses
dnettrans = gcarloan + gcarins + ggas + gcarrepair + gothertrans 

//Total Credit-related expenses
 dnetcredit = gcc01 + gcc02 + gcc03 + gstudloans + gotherloan 

//Total Entertainment expenses
 dnetentertainment =  gphone + gcable  + gcomputer + gmagazines + gpets
 
//total Savings expenses
 dnetsavings = gemerg + gothersav + ggensave
 
//Display formats
 gmonthpay = moneyFormat(gmonthpay)
 gweekpay = moneyFormat(gweekpay)
 gbiwkpay = moneyFormat(gbiwkpay)
 gbimopay = moneyFormat(gbimopay)
 girreginc = moneyFormat(girreginc)
 gssinc = moneyFormat(gssinc)
 ginvestinc = moneyFormat(ginvestinc)
 gpubasstinc = moneyFormat(gpubasstinc)
 galimonyinc = moneyFormat(galimonyinc)
 gotherinc = moneyFormat(gotherinc)
 grent = moneyFormat(grent)
 gproptax = moneyFormat(gproptax)
 gmaint = moneyFormat(gmaint)
 ghutils = moneyFormat(ghutils)
 gwater = moneyFormat(gwater)
 gotherhexp = moneyFormat(gotherhexp)
 ggroceries = moneyFormat(ggroceries)
 gdaycare = moneyFormat(gdaycare)
 gcarloan = moneyFormat(gcarloan)
 gcarins = moneyFormat(gcarins)
 ggas = moneyFormat(ggas)
 gcarrepair = moneyFormat(gcarrepair)
 ghealth = moneyFormat(ghealth)
 gcc01 = moneyFormat(gcc01)
 gcc02 = moneyFormat(gcc02)
 gcc03 = moneyFormat(gcc03)
 gstudloans = moneyFormat(gstudloans)
 gotherloan = moneyFormat(gotherloan)
 galimonyexp = moneyFormat(galimonyexp)
 gphone = moneyFormat(gphone)
 gcable = moneyFormat(gcable)
 ginsurance = moneyFormat(ginsurance)
 gemerg = moneyFormat(gemerg)
 gotherexp = moneyFormat(gotherexp)
 gothersav = moneyFormat(gothersav)
 ggensave = moneyFormat(ggensave)
 gothertrans = moneyFormat(gothertrans)
 gcomputer = moneyFormat(gcomputer)
 gmagazines = moneyFormat(gmagazines)
 gpets = moneyFormat(gpets)
 dtotexp = moneyFormat(dtotexp)
 dnetpay = moneyFormat(dnetpay)
 dnetsupinc = moneyFormat(dnetsupinc)
 dnetinc2 = moneyFormat(dnetinc2)
 dnethouse = moneyFormat(dnethouse)
 dnetgen = moneyFormat(dnetgen)
 dnettrans = moneyFormat(dnettrans)
 dnetcredit = moneyFormat(dnetcredit)
 dnetentertainment = moneyFormat(dnetentertainment)
 dnetsavings = moneyFormat(dnetsavings)
 dbalance = dnetinc2 - dtotexp 
 dbalance = balanceFormat(dbalance)

 form.netinc2.value = dnetinc2
 form.netpay.value = dnetpay
 form.netsupinc.value = dnetsupinc
 form.totexp.value = dtotexp
 form.nethouse.value = dnethouse
 form.netgen.value = dnetgen
 form.nettrans.value = dnettrans
 form.netcredit.value = dnetcredit
 form.netentertainment.value = dnetentertainment
 form.netsavings.value = dnetsavings
 form.monthpay.value = gmonthpay
 form.weekpay.value = gweekpay
 form.biwkpay.value = gbiwkpay
 form.bimopay.value = gbimopay
 form.irreginc.value = girreginc
 form.ssinc.value = gssinc
 form.investinc.value = ginvestinc
 form.pubasstinc.value = gpubasstinc
 form.alimonyinc.value = galimonyinc
 form.otherinc.value = gotherinc
 form.rent.value = grent
 form.proptax.value = gproptax
 form.maint.value = gmaint
 form.hutils.value = ghutils
 form.water.value = gwater
 form.otherhexp.value = gotherhexp
 form.groceries.value = ggroceries
 form.daycare.value = gdaycare
 form.carloan.value = gcarloan
 form.carins.value = gcarins
 form.gas.value = ggas
 form.carrepair.value = gcarrepair
 form.health.value = ghealth
 form.cc01.value = gcc01
 form.cc02.value = gcc02
 form.cc03.value = gcc03
 form.studloans.value = gstudloans
 form.otherloan.value = gotherloan
 form.alimonyexp.value = galimonyexp
 form.phone.value = gphone
 form.cable.value = gcable
 form.insurance.value = ginsurance
 form.emerg.value = gemerg
 form.otherexp.value = gotherexp
 form.othersav.value = gothersav
 form.gensave.value = ggensave
 form.othertrans.value = gothertrans
 form.computer.value = gcomputer
 form.magazines.value = gmagazines
 form.pets.value = gpets
 form.balance.value = dbalance
 form.balance.focus()
 return true
}

function clearForm(form) {
 form.netpay.value = ""
 form.netsupinc.value = ""
 form.netinc2.value = ""
 form.totexp.value = ""
 form.nethouse.value = ""
 form.netgen.value = ""
 form.nettrans.value = ""
 form.netcredit.value = ""
 form.netentertainment.value = ""
 form.netsavings.value = ""
 form.monthpay.value = "0.00"
 form.weekpay.value = "0.00"
 form.biwkpay.value = "0.00"
 form.bimopay.value = "0.00"
 form.irreginc.value = "0.00"
 form.ssinc.value = "0.00"
 form.investinc.value = "0.00"
 form.pubasstinc.value = "0.00"
 form.alimonyinc.value = "0.00"
 form.otherinc.value = "0.00"
 form.rent.value = "0.00"
 form.proptax.value = "0.00"
 form.maint.value = "0.00"
 form.hutils.value = "0.00"
 form.water.value = "0.00"
 form.otherhexp.value = "0.00"
 form.groceries.value = "0.00"
 form.daycare.value = "0.00"
 form.carloan.value = "0.00"
 form.carins.value = "0.00"
 form.gas.value = "0.00"
 form.carrepair.value = "0.00"
 form.health.value = "0.00"
 form.cc01.value = "0.00"
 form.cc02.value = "0.00"
 form.cc03.value = "0.00"
 form.studloans.value = "0.00"
 form.otherloan.value = "0.00"
 form.alimonyexp.value = "0.00"
 form.phone.value = "0.00"
 form.cable.value = "0.00"
 form.insurance.value = "0.00"
 form.emerg.value = "0.00"
 form.gensave.value = "0.00"
 form.otherexp.value = "0.00"
 form.othersav.value = "0.00"
 form.computer.value = "0.00"
 form.magazines.value = "0.00"
 form.othertrans.value = "0.00"
 form.balance.value = ""
}
