
// =====================================================================================================================
// Modifications : Some function has been modified for JATO naming format -- By BILLY 25July2002
// Note : all functions only used on Applicant Entry screen
// =====================================================================================================================

// ======= Function for the calculation of BorrowerTotalIncome (Calc 53)
//		-- Function to sum up the Employment Income Amounts (CURRENT) and all OtherIncome Amounts
//			then populate the result to "txTotalIncome"
//===========================================================================================================
function calcBorrowerTotalIncome(){
	var theTotalIncome = 0;
	var theTotalOtherIncome = 0;

// Handle multiple rows
	for(x = 1; x <= numberOfB1Employment; ++x)  {
		var currEmployStatusID = eval("document.forms[0].eh1_" + x + "_employmentHistoryStatusDd.options[document.forms[0].eh1_" + x + "_employmentHistoryStatusDd.selectedIndex].value");

		// Calc only if Status = 0 (CURRENT)
		if(currEmployStatusID == 0){
        	var currIncomePeriodID = eval("document.forms[0].ehi1_" + x + "_incomePeriodDd.selectedIndex");
			var currIncomeMultiplier = VALSIncomeMultiplier[currIncomePeriodID];
			var currIncomeAmount = eval("document.forms[0].ehi1_" + x + "_incomeAmount.value");

			// Add to total
			theTotalIncome = theTotalIncome + (currIncomeAmount * currIncomeMultiplier);
		}
	}

	for(x = 1; x <= numberOfB2Employment; ++x)  {
		var currEmployStatusID = eval("document.forms[0].eh2_" + x + "_employmentHistoryStatusDd.options[document.forms[0].eh2_" + x + "_employmentHistoryStatusDd.selectedIndex].value");

		// Calc only if Status = 0 (CURRENT)
		if(currEmployStatusID == 0){
        	var currIncomePeriodID = eval("document.forms[0].ehi2_" + x + "_incomePeriodDd.selectedIndex");
			var currIncomeMultiplier = VALSIncomeMultiplier[currIncomePeriodID];
			var currIncomeAmount = eval("document.forms[0].ehi2_" + x + "_incomeAmount.value");

			// Add to total
			theTotalIncome = theTotalIncome + (currIncomeAmount * currIncomeMultiplier);
		}
	}

	// Handle multiple rows
	for(x=1; x<=numOfOtherIncome ; ++x){
		if( eval("document.forms[0].oi_" + x + "_incomePeriodDd") == null ){
			continue;
		}
      	var currIncomePeriodID = eval("document.forms[0].oi_" + x + "_incomePeriodDd.selectedIndex");
		var currIncomeMultiplier = VALSIncomeMultiplier[currIncomePeriodID];
      	var currIncomeAmount = eval("document.forms[0].oi_" + x + "_incomeAmount.value");

		// Add to total
		theTotalIncome = theTotalIncome + (currIncomeAmount * currIncomeMultiplier);
		theTotalOtherIncome = theTotalOtherIncome + (currIncomeAmount * currIncomeMultiplier);
	}

	//Populate result to TotalIncome Display, since fields are disabled the values are not being sent back to the server.
	//format numeric numbers
	//document.forms[0].txtotalIncomeAmount.value = formatNumericValues(round_decimals(theTotalIncome, 2),",");
	// Populate hidden field for saving to DB.
	//document.forms[0].totalIncomeAmount.value = round_decimals(theTotalIncome, 2);
	//document.forms[0].txTotalOtherIncomeAmount.value = "$" + formatNumericValues(round_decimals(theTotalOtherIncome, 2),",");
	Format_UpdateContent("totalIncomeAmount", "" + theTotalIncome);
}

// ======= Function for the calculation of BorrowerTotalIncome (Calc 53)

// ======= Function for the calculation of LiabilityMonthlyPaymentAndTotal (Calc 72, Calc 55)
//		1) Calc 72 : if(LiabilityPaymentQualifier == "V") LiabilityMonthlyPayment = LiabilityAmount * TDSInPercent
//		2) Calc 55 : if(LiabilityTypeId != 14{Closing Costs}) TotalLiabilityMonthlyPayment += LiabilityMonthlyPayment
function calcLiabilityMonthlyPaymentAndTotal(){

	var TotalLiabilityLimitAmount = 0;
	var TotalLiabilityMonthlyPayment = 0;
	var TotalLiabilityAmount = 0;
   
	// Handle multiple rows
	for(x=1; x<=numOfLiability; ++x){

    var theIndex = eval("document.forms[0].liab_" + x + "_liabilityTypeDd.selectedIndex");

    var payOffIndex = eval("document.forms[0].hliab_" + x + "_liabilityPayoffTypeDd.selectedIndex");
    if(payOffIndex==-1 ){
       payOffIndex=0;
    }
    var currLiabilityPayoffType = eval("document.forms[0].hliab_" + x + "_liabilityPayoffTypeDd.options[" + payOffIndex + "].value");
    var currLiabilityType = eval("document.forms[0].liab_" + x + "_liabilityTypeDd.options[" + theIndex + "].value");
    var currLiabilityPaymentQualifier = VALSliabilityPaymentQualifier[theIndex];
                //var defaultLiabilityPercentageIncludeInTDS=VALStxLiabilityPercentageIncludeInTDS[theIndex];
    var defaultLiabilityMonthlyPaymentPercentage=VALStxLiabilityMonthlyPaymentPercentage[theIndex];
		var currLiabilityAmount = eval("document.forms[0].liab_" + x + "_liabilityAmount.value");
		if(currLiabilityAmount == ""){
			currLiabilityAmount = 0.00;
		}

    //For ticket #7588 i had removed the check for -1.  This still needs to be clarified so i have left it as is.
    TotalLiabilityAmount = TotalLiabilityAmount + (currLiabilityAmount * 1.00);

    var currLimitAmount = eval("document.forms[0].liab_" + x + "_creditLimit.value");
		if(currLimitAmount == ""){
			currLimitAmount = 0.00;
		}
		TotalLiabilityLimitAmount = TotalLiabilityLimitAmount + (currLimitAmount * 1.00);
		// Check for Calc 72
                if(currLiabilityPaymentQualifier == 1){
 			var currLiabilityMonthlyPaymentPercentage = defaultLiabilityMonthlyPaymentPercentage;
			var currLiabilityMonthlyPayment = currLiabilityAmount * currLiabilityMonthlyPaymentPercentage / 100.00;
			var theChbMonthPayOverCtl = eval("document.forms[0].checkbox_liab_" + x + "_monthlyPaymentOveride");

          if(!theChbMonthPayOverCtl.checked){
			    eval("document.forms[0].hliab_" + x + "_liabilityMonthlyPayment.value='" + round_decimals(currLiabilityMonthlyPayment, 2) + "'");
			    Format_UpdateContent("hliab_" + x + "_liabilityMonthlyPayment", round_decimals(currLiabilityMonthlyPayment, 2));
          Format_EnableControl("hliab_" + x + "_liabilityMonthlyPayment", false);


			}else {
				Format_EnableControl("hliab_" + x + "_liabilityMonthlyPayment", true);
			}
		}


		// Check for calc 55
  		//if(currLiabilityType != "14.0" && currLiabilityType != "14" && currLiabilityType != 14 ){
  		//Changed by James Zhou, (Mar 9, 2005), Ticket #3374, Doc Ref "\\tor-fs1\projects\filogix source\expert\release2\final_brd\expert Calculations.doc", Check for liability_type (0 and 12) instead of (14)
		if((currLiabilityType != "0.0" && currLiabilityType != "0" && currLiabilityType != 0) && (currLiabilityType != "12.0" && currLiabilityType != "12" && currLiabilityType != 12)){
      		//Check for Credit Card, Unsecured loan and secured Loan if not enable payment
          if((currLiabilityType != "2")&&(currLiabilityType != "10")&&(currLiabilityType != "13")){
          Format_EnableControl("hliab_" + x + "_liabilityMonthlyPayment", true);
          }
          var theLiabilityMonthlyPaymentCtl = eval("document.forms[0].hliab_" + x + "_liabilityMonthlyPayment");
          if(currLiabilityPayoffType == "-1"){
          TotalLiabilityMonthlyPayment = TotalLiabilityMonthlyPayment + (theLiabilityMonthlyPaymentCtl.value * 1.00);
          }

      }
	}

  // Handle multiple rows of Other Property Liabilities
	for(z=1; z<=numOfPropertyAssocList; z++){
    for(x=1; x<=numOfMortgagesPerProperty[z]; x++){
      var payOffIndex = eval("document.forms[0].malop_" + z + "_" + x + "_liabilityPayoffTypeDd.selectedIndex");
      if(payOffIndex==-1 ){
        payOffIndex=0;
      }
      var currLiabilityPayoffType = eval("document.forms[0].malop_" + z + "_" + x + "_liabilityPayoffTypeDd.options[" + payOffIndex + "].value");
      var currLiabilityAmount = eval("document.forms[0].malop_" + z + "_" + x + "_liabilityAmount.value");
      if(currLiabilityAmount == ""){
        currLiabilityAmount = 0.00;
      }
      	TotalLiabilityAmount = TotalLiabilityAmount + (currLiabilityAmount * 1.00);

      var theLiabilityMonthlyPayment = eval("document.forms[0].malop_" + z + "_" + x + "_liabilityMonthlyPayment");
      ////ticket 7617 - Do not add mortgage Types that are being paid off
      if(payOffIndex == 0){
        TotalLiabilityMonthlyPayment = TotalLiabilityMonthlyPayment + (theLiabilityMonthlyPayment.value * 1.00);
      }
    }
}

	//Populate result to TotalLiabilityMonthlyPayment field
	document.forms[0].totalLiabilityAmount.value = round_decimals(TotalLiabilityAmount, 2);
	Format_UpdateContent("txtotalLiabilityAmount", "" + TotalLiabilityAmount);
	//document.forms[0].txtotalLiabilityAmount.value = "$" + formatNumericValues(round_decimals(TotalLiabilityAmount, 2),",");
	Format_UpdateContent("txtotalLimitAmount", "" + TotalLiabilityLimitAmount);
  	//document.forms[0].txtotalLimitAmount.value = "$" + formatNumericValues(round_decimals(TotalLiabilityLimitAmount, 2),",");

	Format_UpdateContent("pgtotalLiabilityAmount", "" + TotalLiabilityAmount);
  	//if(document.forms[0].pgtotalLiabilityAmount){
    //	document.forms[0].pgtotalLiabilityAmount.value = "$" + formatNumericValues(round_decimals(TotalLiabilityAmount, 2),",");
  	//}

	document.forms[0].totalLiabilityPayments.value = round_decimals(TotalLiabilityMonthlyPayment, 2);
	Format_UpdateContent("txtotalLiabilityPayments", "" + TotalLiabilityMonthlyPayment);
	//document.forms[0].txtotalLiabilityPayments.value = "$" + formatNumericValues(round_decimals(TotalLiabilityMonthlyPayment, 2),",");

  	if (document.forms[0].totalLiability){
    	document.forms[0].totalLiability.value = TotalLiabilityAmount;
  	}

  	calcTotalNetworth();
}
// ======= Function for the calculation of LiabilityMonthlyPaymentAndTotal (Calc 72, Calc 55)

// ======= Function for the calculation of BorrowerTotalAsset (Calc 52)
function calcBorrowerTotalAsset(){
	var theTotalAsset = 0;


	// Handle multiple rows
	for(x=1; x<=numOfAsset; ++x){
		if( eval("document.forms[0].asset_" + x + "_assetValue") && eval("document.forms[0].asset_" + x + "_assetValue.value").length > 0){
      var currAssetValue = eval("document.forms[0].asset_" + x + "_assetValue.value");

      // Add to total
      theTotalAsset = theTotalAsset + (currAssetValue * 1.00);
    }
	}

  // Handle multiple rows of Other Property Liabilities
	for(z=1; z<=numOfPropertyAssocList; z++){
      if( eval("document.forms[0].assetValue_" + z) && eval("document.forms[0].assetValue_" + z + ".value").length > 0){
        var currAssetValue = eval("document.forms[0].assetValue_" + z + ".value");
        var currPropAssetType =  eval("document.forms[0].propertyAssetType_" + z + ".value");

        /*
        For ticket 7637 - The removal of this from the total was reverted
        Leaving in (but commented ot ) for now in case there is a discrepency
        //Check for type 5 and type 7 for subject property, index value is 5 and 7
        if((currPropAssetType != "1") && (currPropAssetType != "2"))
        {// Add to total

        theTotalAsset = theTotalAsset + (currAssetValue * 1.00);
        } --END For ticket 7637*/
        theTotalAsset = theTotalAsset + (currAssetValue * 1.00);
      }
  }
	//Populate result to TotalAsset


	Format_UpdateContent("txtotalAssetAmount", "" + theTotalAsset);
	//document.forms[0].txtotalAssetAmount.value = "$" + formatNumericValues(round_decimals(theTotalAsset, 2),",");
  //if(document.forms[0].pgtotalAssetAmount){
  	Format_UpdateContent('pgtotalAssetAmount', "" + theTotalAsset);
    //document.forms[0].pgtotalAssetAmount.value = "$" + formatNumericValues(round_decimals(theTotalAsset, 2),",");
  //}
	document.forms[0].totalAssetAmount.value = round_decimals(theTotalAsset, 2);
  if (document.forms[0].totalAsset){
    document.forms[0].totalAsset.value = theTotalAsset;
  }

  calcTotalNetworth();
}
// ======= Function for the calculation of BorrowerTotalAsset (Calc 52)

// ======= Adjusted to the JATO framework by VLAD 30Jul2002.
function calcTotalPropertyExpenses(){
	var theTotal = 0;

	var numOfExpense = getNumOfRow("pgPropertyEntry_RepeatedPropertyExpenses[0]_txExpenseAmount");
	var name1stPart = get1stPartName("pgPropertyEntry_RepeatedPropertyExpenses[0]_txExpenseAmount");

	////alert("name1stPart: " + name1stPart);

	// Handle multiple rows
	for(x=0; x<numOfExpense ; ++x)
	{
		inputRowNdx = x+"]_";

		////var currExpensePeriodID = eval("document.forms[0].cbExpensePeriod" + inputRowNdx + ".value");
		var currExpensePeriodID = eval("document.forms[0].elements['" + name1stPart + inputRowNdx + "cbExpensePeriod" + "']" + ".value");

		////alert("currExpensePeriodID: " + currExpensePeriodID);

		var theIndex = getDefaultNdx(currExpensePeriodID , "VALScbExpensePeriod");
		var currExpensePeriodMultiplier = VALSExpenseMultiplier[theIndex];

		////alert("theIndex: " + theIndex);

		////alert("The currExpensePeriodMultiplier = " + currExpensePeriodMultiplier + " RowNum = " + x);
		////var currExpenseAmount = eval("document.forms[0].txExpenseAmount" + inputRowNdx + ".value");
		var currExpenseAmount = eval("document.forms[0].elements['" + name1stPart + inputRowNdx + "txExpenseAmount" + "']" + ".value");

		////alert("The currExpenseAmount = " + currExpenseAmount + " RowNum = " + x);
		// Add to total
		theTotal = theTotal + (currExpenseAmount * currExpensePeriodMultiplier);
		//alert("The theTotal = " + theTotal + " RowNum = " + x);
	}

	//Populate result to TotalExpense
	document.forms[0].pgPropertyEntry_txTotalPropertyExp.value = round_decimals(theTotal, 2);
}

function calcTotalNetworth(){
  var totalAsset = 0.00;
  var totalLiability = 0.00;
  var totalNetworth = 0.00;

  if (document.forms[0].totalAsset){
    totalAsset = parseFloat(document.forms[0].totalAsset.value);
  }

  if (document.forms[0].totalLiability){
    totalLiability = parseFloat(document.forms[0].totalLiability.value);
  }

//  if(document.forms[0].pgtotalNetworthAmount){
    totalNetworth = totalAsset - totalLiability;
    Format_UpdateContent("pgtotalNetworthAmount", "" + totalNetworth);
//    document.forms[0].pgtotalNetworthAmount.value = "$" + formatNumericValues(round_decimals(totalNetworth, 2),",");
//  }

}



