var inflate = new Array(2); 
inflate[0] = new Array(1.19, 1.41, 1.68, 1.99, 2.36, 2.81, 3.33);
inflate[1] = new Array(5, 10, 15, 20, 25, 30, 35);
var inflation = 0.0;
var m_stat = 0;
var socsec = new Array(3);
socsec[0] = new Array(3); //age 55
socsec[1] = new Array(3); //age 45
socsec[2] = new Array(3); //age 35
socsec[0][0] = new Array(16020, 8010); //age 55, income 40k
socsec[0][1] = new Array(19284, 9642); //age 55, income 60k
socsec[0][2] = new Array(21768, 10884); //age 55, income 80k+
socsec[1][0] = new Array(16308, 8154); //age 45, income 40k
socsec[1][1] = new Array(19524, 9762); //age 45, income 60k
socsec[1][2] = new Array(22452, 11226); //age 45, income 80k+
socsec[2][0] = new Array(16620, 8310); //age 35, income 40k
socsec[2][1] = new Array(19740, 9870); //age 35, income 60k
socsec[2][2] = new Array(22788, 11394); //age 35, income 80k+


function getNumber(obj) {
// performs data validation and returns float value
// parameter must be a form element with a 'value' attribute
// containing a parseFloat()-able piece of data, eg: 1.5
  var numString1 = obj.value+" ";
  var num = parseFloat(obj.value);
  var comma = numString1.indexOf(",", 0);
  if (!num || comma > -1){
    if (comma > -1) {
      alert("A necessary value has been omitted or incorrectly entered.\n\nThis error is probably due to a comma within a numeric value.\n\nPLEASE NOTE: All values must be numeric characters or decimal points.\nNo commas are necessary.");
    }else {
      alert("A necessary value has been omitted or incorrectly entered.\n\nPLEASE NOTE: All values must be numeric characters or decimal points.\nNo commas are necessary.");
    }
    obj.focus();
    return 0;
  }else {
    return num;
  }
}

function setmstat(value, aForm){
// part of Step 3
// register user as married w/ non-working spouse, married w/ working spouse, or single
// if married w/ working spouse, asks user to repeat process of calculation

 var form = aForm;
 
 if (value == 0 ) {
  m_stat = value;
  getSSBenefit(form);
 } else if (value == 1) {
  form.benefit2.value=0;
  var agree = confirm("You have indicated a marital status of 'Married w/ working spouse'.\n\nPlease repeat Step 1 and Step 2 of this section to calculate your spouse's social security benefits, before going on to the next section.");
  if (agree==true){  
    m_stat = value;
    form.benefit2.value="?";
    form.benefit3.value="?";
  }
 } else if (value == 2){
   m_stat = value;
   getSSBenefit(form);
 }
 return false;
}

function getSSBenefit(aForm) {
// part of Step 3
// calculates Social Security benefits for a household
// based on age, income, marital employment status
  var index1 = aForm.age1.selectedIndex;
  var incomdex = aForm.income3.selectedIndex;
  var ego = socsec[index1][incomdex][0]*inflate[0][index1];
  var spouse = socsec[index1][incomdex][1];
  
  if (m_stat == 0) {
   aForm.benefit1.value = makeDollars(ego);
   aForm.benefit2.value = 0.00;
   aForm.benefit3.value = makeDollars(ego);
  }
  if(m_stat == 1) {
   var old_ego = getNumber(aForm.benefit1);
   aForm.benefit2.value = makeDollars(ego);
   aForm.benefit3.value = makeDollars(ego+old_ego);
  }
  if(m_stat == 2) {
   aForm.benefit1.value = makeDollars(ego);
   aForm.benefit2.value = makeDollars(spouse);
   aForm.benefit3.value = makeDollars(ego+spouse);
  }
  return false;
}

function makeDollars(number) {
// rounds number and sticks dollar-like
// decimal point and zeros to the hundredth place
  var rounded = Math.round(number);
  var dollars = rounded+".00";
  return dollars;
}

//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------


function futureValue()
{
	ir = document.all.txt_fv_ir.value;
	np = document.all.txt_fv_pp.value;
	pv = document.all.txt_fv_pv.value;
	
	temp1 = Math.pow((1 + ir), np);
	temp2 = temp1 * pv;
		
	document.all.fv.innerText = document.functions.formatDecimal(temp2);
	
}

function getLife()
{
	if(document.forms[0].rGender[1].checked == true)
	{
		var gender = 1;
		var strGender = "female";
	}
	else
	{
		var gender = 0;
		var strGender = "male";
	}
	
	var iAge = document.forms[0].txtAge.value;
	
	var lifeEx = document.functions.getLifeExp(iAge, gender);
	if(iAge > 100)
	{
		document.forms[0].txtLife.value = "Invalid Age";
	}
	else
	{	
		document.forms[0].txtLife.value = Math.round(lifeEx);		
	}
}

function getMorgage()
{
	var Loan = document.forms[0].txtLoan.value;
	var Rate = (document.forms[0].txtInt.value / 100);
	var Period = document.forms[0].txtPer.value;

	if( (Rate > .25) || (Rate < .01))
	{
		alert("Invalid Mortgage Rate");
	}
	else
	{
		var dPayment = document.functions.getMorgage(Loan,Rate,Period);

		document.forms[0].txtPay1.value = (Math.round(dPayment))			
		document.forms[0].txtPay2.value = (Math.round(dPayment*Period))			
		document.forms[0].txtPay3.value = (Math.round((dPayment*Period)-Loan))
		
	}
}
function getLoanAmount()
{
	var Pay = document.forms[0].txtPay.value;
	var Rate = (document.forms[0].txtInt.value / 100);
	var Period = document.forms[0].txtPer.value;
	var DPay = document.forms[0].txtDPay.value;

	if( (Rate > .25) || (Rate < .01))
	{
		alert("Invalid Mortgage Rate");
	}
	else
	{
		var dLoan = document.functions.getLoanAmount(Pay,Rate,Period,DPay);

		document.forms[0].txtLoan.value = (Math.round(dLoan))					
	}
}

function getInflation()
{
		
	var Loan = document.forms[0].txtLoan.value;
	var Rate = (document.forms[0].txtInt.value / 100);
	var Period = document.forms[0].txtPer.value;

	if( (Rate > .5) || (Rate < .01))
	{
		alert("Invalid Inflation Rate");
	}
	else
	{
		var dInflate = document.functions.getInflation(Loan,Rate,Period);

		document.forms[0].txtInflate.value = Math.round(dInflate)
		
	}

	
}

function getCollege()
{
	var App = document.functions
		
	var Loan = document.forms[0].txtLoan.value;
	var Rate = (document.forms[0].txtInt.value / 100);
	var Period = document.forms[0].txtPer.value;
	var YearsIn = document.forms[0].txtEn.value;

	if( (Rate > .5) || (Rate < .01))
	{
		alert("Invalid Inflation Rate");
	}
	else
	{
		document.forms[0].txtEducate.value = App.formatDollars(Math.round(document.functions.getCollege(Loan,Rate,Period,YearsIn)));
	}	
	return false;
}

function getPayCheck()
{
	
	var App = document.functions
	
	var MedCareRate = .0145;	
	var SSRate = .0620;
	var SSBase = 84900;	
		
	var Salary = document.forms[0].txtSalary.value;
	var Savings = document.forms[0].selSave.value;
	var Tax = document.forms[0].selTax.value;
	
	var Monthly = (Salary / 12);
	
	document.forms[0].txtPay1.value = App.formatDollars(Math.round(Monthly));
	document.forms[0].txtPay2.value = App.formatDollars(Math.round(Monthly));
		
	document.forms[0].txtPTS1.value = App.formatDollars(Math.round(Monthly * Savings));
	document.forms[0].txtPTS2.value = App.formatDollars(0);
	
	document.forms[0].txtTI1.value = App.formatDollars(Math.round(Monthly - (Monthly * Savings)));
	document.forms[0].txtTI2.value = App.formatDollars(Math.round(Monthly));
	
	var TaxIncome1 = (Monthly - (Monthly * Savings))* Tax;
	var TaxIncome2 = Monthly * Tax;
	
	document.forms[0].txtFT1.value = App.formatDollars(Math.round(TaxIncome1))
	document.forms[0].txtFT2.value = App.formatDollars(Math.round(TaxIncome2));
	
	Medicare = (Salary * MedCareRate)/12;
	document.forms[0].txtMC1.value = App.formatDollars(Math.round(Medicare));
	document.forms[0].txtMC2.value = App.formatDollars(Math.round(Medicare));
		
	if(Salary >= SSBase)
	{
		SocialSecurity = (SSBase * SSRate)/12;
	}
	else
	{
		SocialSecurity = (Salary * SSRate)/12;
	}
	
	document.forms[0].txtSS1.value = App.formatDollars(Math.round(SocialSecurity));
	document.forms[0].txtSS2.value = App.formatDollars(Math.round(SocialSecurity));
	
	document.forms[0].txtATS1.value = App.formatDollars(0);
	document.forms[0].txtATS2.value = App.formatDollars(Math.round(Monthly * Savings));
	
	document.forms[0].txtNMP1.value = App.formatDollars(Math.round((Monthly - (Monthly * Savings)) - TaxIncome1 - SocialSecurity - Medicare));
	document.forms[0].txtNMP2.value = App.formatDollars(Math.round(Monthly - TaxIncome2 - SocialSecurity - Medicare - (Monthly * Savings)));
	
	document.forms[0].txtAITH.value = App.formatDollars(Math.round(12*(((Monthly - (Monthly * Savings)) - TaxIncome1 - SocialSecurity - Medicare) - (Monthly - TaxIncome2 - SocialSecurity - Medicare - (Monthly * Savings)))));
	
	document.forms[0].txtAIS1.value = App.formatDollars(Math.round(12*Monthly * Savings));
	document.forms[0].txtAIS2.value = App.formatDollars(Math.round(12*Monthly * Savings));
	
	
}
