
//declare global variables
var cashsubtotal = 0;
//var investmentsSubtotal = 0;
//var useSubtotal = 0;
var assetTotal = 0;
var ltLiaSubtotal = 0;
//var stLiaSubtotal = 0;
var liabilitiesTotal = 0;
var totNetworth = 0;
var valid;

//Check for non-numeric info and alert user
function CheckNumber(TheNumber, theField) {
	var valid = 1;
	var GoodChars = "0123456789,.";
	var i = 0;
	if (TheNumber=="") {
		// Return false if number is empty
		valid = 0;
	}
	for (i =0; i <= TheNumber.length -1; i++) {
		if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
alert(TheNumber.charAt(i) + " is not a valid number.  Please enter numbers only.");
			valid = 0;
			theField.focus()
			theField.select();
			break;
		} // End if statement
		else{calcAll()}
	} // End for loop
	return valid
}

//Remove Commas from numbers entered
function remove(number){
	var number = number.replace(",", "");
	var number = number.replace(",", "");
	var number = number.replace(",", "");
	return number;
}

//Calculate Asset Subtotal
function CalcCash(){
	var Cash = remove(document.formCash.inputCash.value);
	var Savings = remove(document.formCash.inputSavings.value);
	var Checking = remove(document.formCash.inputChecking.value);
	var Moneymarket = remove(document.formCash.inputMoneymarket.value);
	var Cd = remove(document.formCash.inputCd.value);
	var Metal = remove(document.formCash.inputMetal.value);
	var Stock = remove(document.formCash.inputStock.value);
	var Funds = remove(document.formCash.inputFunds.value);
	var Bonds = remove(document.formCash.inputBonds.value);
	var R401k = remove(document.formCash.inputR401k.value);
	var Securities = remove(document.formCash.inputSecurities.value);
	var Lifeins = remove(document.formCash.inputLifeins.value);
	var Auto = remove(document.formCash.inputAuto.value);
	var Jewelry = remove(document.formCash.inputJewelry.value);
	var Art = remove(document.formCash.inputArt.value);
	var Antiques = remove(document.formCash.inputAntiques.value);
	var Computers = remove(document.formCash.inputComputers.value);
	var Tools = remove(document.formCash.inputTools.value);
	var Other = remove(document.formCash.inputOther.value);
	var Homes = remove(document.formCash.inputHomes.value);
	var Condo = remove(document.formCash.inputCondo.value);
	var Mobilehomes = remove(document.formCash.inputMobilehomes.value);
	var Land = remove(document.formCash.inputLand.value);
	var Partnerships = remove(document.formCash.inputPartnerships.value);
	var Interests = remove(document.formCash.inputInterests.value);
	var Copy = remove(document.formCash.inputCopy.value);
	var Patent = remove(document.formCash.inputPatent.value);
	var Trademark = remove(document.formCash.inputTrademark.value);
	var Stockopt = remove(document.formCash.inputStockopt.value);
	
	cashsubtotal = eval(Cash) + eval(Savings)  + eval(Checking) + eval(Moneymarket)
	+ eval(Cd) + eval(Metal) + eval(Stock)+ eval(Funds) + eval(Bonds) + eval(R401k)
	+ eval(Securities)+ eval(Lifeins) + eval(Auto) + eval(Jewelry) + eval(Art) + eval(Antiques)
	+ eval(Computers)+ eval(Tools) + eval(Other) + eval(Homes) + eval(Condo) + eval(Mobilehomes)+ eval(Land)
	+ eval(Partnerships) + eval(Interests) + eval(Copy) + eval(Patent) + eval(Trademark) + eval(Stockopt);
	cashsubtotalcur = currency(cashsubtotal);
	document.formCash.inputcashsubtotal.value = cashsubtotalcur;
	}
//
//	 
//	 
//	 
	//
//	 

//Calculate Investment Subtotal
//function calcInvestment(){
	//var CD = remove(document.formInvestments.inputCD.value);
	//var Stock = remove(document.formInvestments.inputStock.value);
	//var BondsTF = remove(document.formInvestments.inputBondsTF.value);
	//var BondsTA = remove(document.formInvestments.inputBondsTa.value);
	//var MutFundF = remove(document.formInvestments.inputMutFundF.value);
	//var MutFundE = remove(document.formInvestments.inputMutFundE.value);
	//var ProMon = remove(document.formInvestments.inputProMon.value);
	//var Pen = remove(document.formInvestments.inputPen.value);
	//var IRA = remove(document.formInvestments.inputIRA.value);
	//var Ann = remove(document.formInvestments.inputAnn.value);
	//var Notes = remove(document.formInvestments.inputNotes.value);
	//var Real = remove(document.formInvestments.inputReal.value);
	//var OtherInv = remove(document.formInvestments.inputOtherInv.value);
	//investmentsSubtotal = eval(CD) + eval(Stock) + eval(BondsTF) +
//eval(BondsTA) + eval(MutFundF) + eval(MutFundE) + eval(ProMon) +
//eval(Pen) + eval(IRA) + eval(Ann) + eval(Notes) + eval(Real) + eval(OtherInv);
//investmentsSubtotalCur = currency(investmentsSubtotal);
	//document.formInvestments.inputInvestmentsSubtotal.value = investmentsSubtotalCur;
//}

//Calculate Use Subtotal
//function calcUse(){
	//var Home = remove(document.formUse.inputHome.value);
	//var Pers = remove(document.formUse.inputPers.value);
	//var Auto = remove(document.formUse.inputAuto.value);
	//var Coll = remove(document.formUse.inputColl.value);
	//var OtherUse1 = remove(document.formUse.inputOtherUse1.value);
	//var OtherUse2 = remove(document.formUse.inputOtherUse2.value);
	//var OtherUse3 = remove(document.formUse.inputOtherUse3.value);
	//var OtherUse4 = remove(document.formUse.inputOtherUse4.value);
	//useSubtotal = eval(Home) + eval(Pers) + eval(Auto) + eval(Coll) + eval(OtherUse1) + eval(OtherUse2) + eval(OtherUse3) + eval(OtherUse4);
	//useSubtotalCur = currency(useSubtotal);
	//document.formUse.inputUseSubtotal.value = useSubtotalCur;
//}
//Calculate Assets Total
function calcAssetTotals(){
	assetTotal = eval(cashsubtotal) ;
	assetTotalCur = currency(assetTotal);
	document.formUse.inputTotalAssets.value = assetTotalCur;
}
//+ eval(investmentsSubtotal) + eval(useSubtotal)

//Calculate Long Term Liabilities Subtotal
function calcLTLiabilitiesSubtotal(){
	var Ploans = remove(document.formLT.inputPloans.value);
	var Mortgage = remove(document.formLT.inputMortgage.value);
	var Life = remove(document.formLT.inputLife.value);
	var Tax = remove(document.formLT.inputTax.value);
	var Depts = remove(document.formLT.inputDepts.value);
	ltLiaSubtotal = eval(Ploans) + eval(Mortgage) + eval(Life) + eval(Tax) + eval(Depts);
	ltLiaSubtotalCur = currency(ltLiaSubtotal);
	document.formLT.inputLongTermSubtotal.value = ltLiaSubtotalCur;
}

//Calculate Short Term Liabilities Subtotal
//function calcSTLiabilitiesSubTotal(){
	//var CC = remove(document.formST.inputCC.value);
	//var OSTD1 = remove(document.formST.inputOSTD1.value);
	//var OSTD2 = remove(document.formST.inputOSTD2.value);
	//var OSTD3 = remove(document.formST.inputOSTD3.value);
	//var OSTD4 = remove(document.formST.inputOSTD4.value);
	//	stLiaSubtotal = eval(CC) + eval(OSTD1) + eval(OSTD2) + eval(OSTD3) + eval(OSTD4);
	//stLiaSubtotalCur = currency(stLiaSubtotal);
	//document.formST.inputShortTermSubtotal.value = stLiaSubtotalCur;
//}

//Calculate Total Liabilities
function calcTotLiabilities(){
	liabilitiesTotal = eval(ltLiaSubtotal) ;
	liabilitiesTotalCur = currency(liabilitiesTotal);
	document.formST.inputTotalLiabilities.value = liabilitiesTotalCur;
}
//+ eval(stLiaSubtotal)

//Copy Insurance to Totals Insurance
function copyInsurancy(){
	document.formTotalNetWorth.inputTotalNetWorthIns.value = document.formIns.inputLI.value;
}

//Calculate Death Estate
function calcDeathEstate(){
	var LI = remove(document.formIns.inputLI.value)
	totDeathEstate = eval(totNetworth) + eval(LI);
	totDeathEstateCur = currency(totDeathEstate);
	document.formTotalNetWorth.inputdeathNetWorth.value = totDeathEstateCur;
}

//Calculate Total Net Worth (Living Benefit)
function calcTotNetWorth(){
	totNetworth = eval(cashsubtotal) - eval(ltLiaSubtotal);
	totNetWorthCur = currency(totNetworth);
	document.formTotalNetWorth.inputTotalNetWorth.value = totNetWorthCur;
	//calcDeathEstate();
}

//Convert Numbers to Currency format
function currency(anynum) {
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))
   while (pStr.length<3){pStr+="0"}

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   retval = dStr + pStr 
   //-- Put negative sign infront of negative numbers.
   if (anynum<0) {retval="-"+retval}
   return "$"+retval
}


//Execute all functions
function calcAll(){
			CalcCash();
			//calcInvestment();
			//calcUse();
			//calcAssetTotals();
			calcLTLiabilitiesSubtotal();
			//calcSTLiabilitiesSubTotal();
			//calcTotLiabilities();
			//calcDeathEstate();
			calcTotNetWorth();
				
}


//Reset all values to zero
function resetAll(){
	document.formCash.inputCash.value = 0;
	document.formCash.inputSavings.value = 0;
	document.formCash.inputChecking.value = 0;
	document.formCash.inputMoneymarket.value = 0;
	document.formCash.inputCd.value = 0;
	document.formCash.inputMetal.value = 0;
	document.formCash.inputStock.value = 0;
	document.formCash.inputFunds.value = 0;
	document.formCash.inputBonds.value = 0;
	document.formCash.inputR401k.value = 0;
	document.formCash.inputSecurities.value = 0;
	document.formCash.inputLifeins.value = 0;
	document.formCash.inputAuto.value = 0;
	document.formCash.inputJewelry.value = 0;
	document.formCash.inputArt.value = 0;
	document.formCash.inputAntiques.value = 0;
	document.formCash.inputComputers.value = 0;
	document.formCash.inputTools.value = 0;
	document.formCash.inputOther.value = 0;
	document.formCash.inputHomes.value = 0;
	document.formCash.inputCondo.value = 0;
	document.formCash.inputMobilehomes.value = 0;
	document.formCash.inputLand.value = 0;
	document.formCash.inputPartnerships.value = 0;
	document.formCash.inputInterests.value = 0;
	document.formCash.inputCopy.value = 0;
	document.formCash.inputPatent.value = 0;
	document.formCash.inputTrademark.value = 0;
	document.formCash.inputStockopt.value = 0;
	document.formCash.inputcashsubtotal.value = 0;
	document.formLT.inputPloans.value = 0;
	document.formLT.inputMortgage.value = 0;
	document.formLT.inputLife.value = 0;
	document.formLT.inputTax.value = 0;
	document.formLT.inputDepts.value = 0;
	document.formLT.inputLongTermSubtotal.value = 0;
	document.formTotalNetWorth.inputTotalNetWorth.value = 0;
	//document.formTotalNetWorth.inputTotalNetWorthIns.value = 0;
	calcAll();
}
//-->
