/*********************************************************
This File Contain All Validate Function for Calculators

You can use following Generic function for common validation
	1.IsNumeric:Return True if string is numeric otherwise False.
	2.IsEmpty:Return True if string is empty.
	3.checkMailId/isEmail: check email id

Note: Before writing validate function mention following things
	1.function name alwasy start from 'validate'and then your File name
	2.Try to write generic function that can be use anywhere & minimize your coding.
	3.Write filename before function name.
*********************************************************/
function IsNumeric(strString)
{
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;

	if (strString.length == 0)
	{
		return false;
	}
	//  test strString consists of valid characters listed above
	for (var j = 0; j < strString.length && blnResult == true; j++)
	{
		strChar = strString.charAt(j);
		 if (strValidChars.indexOf(strChar) == -1)
		 {
			blnResult = false;
		 }
	}
		return blnResult;
}
	

function IsEmpty(aTextField) 
{
   if ((aTextField==null) || aTextField=="") 
   {
	  return true;
   }
   else { return false; }
}


//to validate dreamz calculator

function validationDreamz()
	{
		var goalName=document.meetDreamzCal.goalName.value;
		var l = IsEmpty(goalName);
		if(l == true)
		{
			alert ("Please name your goal");
			return false;
		}
		
		var goalCost=document.meetDreamzCal.goalCost.value;
		var a = IsNumeric(goalCost);
		if(a == false)
		{
			alert ("Please enter cost of the goal");
			return false;
		}
		
		var annualRate=document.meetDreamzCal.annualRate.value;
		var b = IsNumeric(annualRate);
		if(b == false)
		{
			alert ("Please enter annual rate");
			return false;
		}
		
		var time1=document.meetDreamzCal.time1.value;
		var c = IsNumeric(time1);
		if(c == false)
		{
			alert ("Please enter time when you wish to fulfill it");
			return false;
		}
		
		var expectedRate=document.meetDreamzCal.expectedRate.value;
		var d = IsNumeric(expectedRate);
		if(d == false)
		{
			alert ("Please enter expected rate of return");
			return false;
		}
		
		return true;
	}


//to show output of dreamz calculator

function showDreamz()
{
	var result = validationDreamz();
	if(result == true)
	{
		var goalName=document.meetDreamzCal.goalName.value;
		var goalCost=document.meetDreamzCal.goalCost.value;
		var annualRate=document.meetDreamzCal.annualRate.value;
		var time1=document.meetDreamzCal.time1.value;
		var expectedRate=document.meetDreamzCal.expectedRate.value;

		annualRate=(annualRate/100);
		expectedRate=(expectedRate/100);

		//var futureVal=(goalCost*[(1+annualRate)^time1]);
		//var investPerMonth=[(futureVal*expectedRate/12)/([(1+expectedRate/12)^12*time1]-1)];

		var dr1=(parseFloat(annualRate)+parseInt("1"));
		var dr2=Math.pow(dr1,time1);
		var futureVal=(goalCost*dr2);

		var dr3=(parseFloat(expectedRate)/12);
		var dr4=(futureVal*dr3);
		var dr5=(parseFloat(dr3)+parseInt("1"));
		var dr6=(time1*12);
		var dr7=Math.pow(dr5,dr6);
		//var dr6=Math.pow(dr5,12);
		//var dr7=(dr6*time1);
		var dr8=(dr7-parseInt("1"));
		var investPerMonth=(dr4/dr8);


		//alert (dr1);
		//alert ("Math.pow(dr1,time1)"+dr2);
		//alert ("(goalCost*dr2)"+futureVal);

		//alert ("(expectedRate/12)"+dr3);
		//alert ("(futureVal*dr3)"+dr4);
		//alert ("(1+dr3)"+dr5);
		//alert ("Math.pow(dr5,12)"+dr6);
		//alert ("(dr6*time1)"+dr7);
		//alert ("(dr7-1)"+dr8);
		//alert ("(dr4/dr8)"+investPerMonth);
		
		if(futureVal == 0)
		{
			var a1=0;
		}
		else
		{
		var a=Math.abs(Math.round(futureVal));
		var a1=currencyFormat(a);
		}
		
		if(investPerMonth == 0)
		{
			var b1=0;
		}
		else
		{
		var b=Math.abs(Math.round(investPerMonth));
		var b1=currencyFormat(b);
		}
		
		document.getElementById('r1').innerHTML = '<b>RESULT</b><hr>To meet your '+goalName+' goal you require Rs. '+a1+' in next '+time1+' years for which you would need to invest a sum of Rs. '+b1+' per month.';

		//alert("To meet your "+goalName+" goal you require Rs. "+a1+" in next "+time1+" years for which you would need to invest a sum of Rs. "+b1+" per month.");  

		
	}
}



	//to validate compounding calculator

	function validationCompounding()
	{
		var amt=document.compoundingCal.amt.value;
		var e = IsNumeric(amt);
		if(e == false)
		{
			alert ("Please enter amount to be invested");
			return false;
		}

		var time=document.compoundingCal.time.value;
		var f = IsNumeric(time);
		if(f == false)
		{
			alert ("Enter time horizon");
			return false;
		}

		var rateReturn=document.compoundingCal.rateReturn.value;
		var g = IsNumeric(rateReturn);
		if(g == false)
		{
			alert ("Enter expected rate of return");
			return false;
		}

		return true;
	}


//to show output of compounding calculator

function showCompounding()
{
	var result = validationCompounding();
	if(result == true)
	{
		//alert ("true");
		var amt=document.compoundingCal.amt.value;
		var period=document.compoundingCal.period.value;
		var time=document.compoundingCal.time.value;
		var rateReturn=document.compoundingCal.rateReturn.value;
		//var totalAmount=[amt*[(1+rateReturn/period)^(time*period)-1]/(rateReturn/period)];

		rateReturn=(rateReturn/100);
		var co1=(rateReturn/period);
		var co2=(parseFloat(co1)+parseFloat("1"));
		var co3=(time*period);
		var co4=Math.pow(co2,co3);
		var co5=(co4-parseInt("1"));
		var co6=(amt*co5);
		var co7=(rateReturn/period);
		var totalAmount=(co6/co7);

		//alert("(rateReturn/period)"+co1);
		//alert("(co1+1)"+co2);
		//alert("(time*period)"+co3);
		//alert("Math.pow(co2,co3)"+co4);
		//alert("(co4-1)"+co5);
		//alert("(amt*co5)"+co6);
		//alert("(rateReturn/period)"+co7);

		if(totalAmount == 0)
		{
			var b1=0;
		}
		else
		{
		var b=Math.abs(Math.round(totalAmount));
		var b1=currencyFormat(b);
		}
		//var b1=addCommas(b);

		//alert (b);

		

		document.getElementById('r2').innerHTML = '<b>RESULT</b><hr>Total Corpus at the end of '+time+' years is Rs '+b1;

		//alert("Total Corpus at the end of "+time+" years is Rs "+b1);
		
		//return totalAmount;
		
			//alert ("output >> "+totalAmount);
		//document.write ([amt*[(1+rateReturn/period)^(time*period)-1]/(reteReturn/period)]);
	}
	
}



//to validate insurance calculator

function validationInsurance()
{
	var livingExp=document.InsuranceCal.livingExp.value;
	var h = IsNumeric(livingExp);
	if(h == false)
	{
		alert ("Enter your annual living expenses");
		return false;
	}

	var existLoan=document.InsuranceCal.existLoan.value;
	var i = IsNumeric(existLoan);
	if(i == false)
	{
		existLoan=0;
		//alert ("Enter your existing loan amount");
		//return false;
	}

	var futGoals=document.InsuranceCal.futGoals.value;
	var j = IsNumeric(futGoals);
	if(j == false)
	{
		alert ("Enter total amount needed to meet future goals");
		return false;
	}

	var accCapital=document.InsuranceCal.accCapital.value;
	var k = IsNumeric(accCapital);
	if(k == false)
	{
		alert ("Enter your present accumulated capital");
		return false;
	}

	return true;
}


//to show output of insurance calculator

function showInsurance()
{
	var result = validationInsurance();
	if(result == true)
	{
		var livingExp=document.InsuranceCal.livingExp.value;
		var existLoan=document.InsuranceCal.existLoan.value;
		IsNumeric(existLoan);
		if(existLoan.length==0)
		{
			existLoan=0;
		}
		var futGoals=document.InsuranceCal.futGoals.value;
		var accCapital=document.InsuranceCal.accCapital.value;

		var capital=(livingExp/0.05);
		var total1=(capital+parseFloat(existLoan));
		var total2=(parseFloat(total1)+parseFloat(futGoals));
		var insuranceNeed=(total2-accCapital);

		//var total=(existLoan+futGoals-accCapital);
		//var insuranceNeed=(capital+total);

		//alert ("capital needed"+ capital);
		//alert (" and "+total1);
		//alert (total2);
		//alert (insuranceNeed);

		if(insuranceNeed == 0)
		{
			var b1=0;
		}
		else
		{
		var b=Math.abs(Math.round(insuranceNeed));
		var b1=currencyFormat(b);
		}

		document.getElementById('r3').innerHTML = '<b>RESULT</b><hr>Considering your present situation and future goals you need a life insurance cover of Rs. '+b1;

		//alert ("Considering your present situation and future goals you need a life insurance cover of Rs. "+b1);
	}
}



//to validate retirement calculator

function validationRetirement()
{
	var age=document.retirement.age.value;
	var p = IsNumeric(age);
	if(p == false)
	{
		alert ("Enter your age");
		return false;
	}

	var annIncome=document.retirement.annIncome.value;
	var q = IsNumeric(annIncome);
	if(q == false)
	{
		alert ("Enter Current Annual income");
		return false;
	}

	var neededR=document.retirement.neededR.value;
	var r = IsNumeric(neededR);
	if(r == false)
	{
		alert ("Enter Percentage of income needed during retirement");
		return false;
	}

	var ageR=document.retirement.ageR.value;
	var s = IsNumeric(ageR);
	if(s == false)
	{
		alert ("Enter Age when you wish to retire");
		return false;
	}

	var retBenefit=document.retirement.retBenefit.value;
	var t = IsNumeric(retBenefit);
	if(t == false)
	{
		alert ("Enter Till what age you expect to receive retirement benefit");
		return false;
	}

	var lumpsumR=document.retirement.lumpsumR.value;
	var u = IsNumeric(lumpsumR);
	if(u == false)
	{
		alert ("Enter Lumpsum expected to receive at retirement (include gratuity)");
		return false;
	}

	var accR=document.retirement.accR.value;
	var v = IsNumeric(accR);
	if(v == false)
	{
		alert ("Enter Present accumulated capital for retirement");
		return false;
	}

	var retSaving=document.retirement.retSaving.value;
	var w = IsNumeric(retSaving);
	if(w == false)
	{
		alert ("Enter Current monthly retirement savings");
		return false;
	}

	var retRS=document.retirement.retRS.value;
	var x = IsNumeric(retRS);
	if(x == false)
	{
		alert ("Enter Expected rate of return on retirement savings");
		return false;
	}

	if(retRS == 0)
	{
		alert ("Expected rate of return on retirement savings should be greater than 0");
		return false;
	}

	return true;
}


//to show output of retirement calculator

function showRetirement()
{
	var result=validationRetirement();
	if(result == true)
	{
		var age=document.retirement.age.value;
		var annIncome=document.retirement.annIncome.value;
		var neededR=document.retirement.neededR.value;
		var ageR=document.retirement.ageR.value;
		var retBenefit=document.retirement.retBenefit.value;
		var lumpsumR=document.retirement.lumpsumR.value;
		var accR=document.retirement.accR.value;
		var retSaving=document.retirement.retSaving.value;
		var retRS=document.retirement.retRS.value;

		//neededR=(neededR/100);
		retRS=(retRS/100);

		var rt1=(neededR/100);
		var rt2=(annIncome*rt1);
		var rt3=(retBenefit-ageR);
		var o1=(rt2*rt3);
			
		//value of T is written as ageD
		var ageD=(ageR-age);

		//changes on 25-mar-08
		var new1=(retRS+parseInt("1"));
		var new2=Math.pow(new1,ageD);
		var new3=(accR*new2);

		// m = 12
		//var m=12;

		var rt4=(retRS/12);
		var rt5=(rt4+parseInt("1"));
		var rt6=(ageD*12);
		var rt7=Math.pow(rt5,rt6);
		var rt8=(rt7-parseInt("1"));
		var rt9=(rt8/rt4);
		var rt10=(retSaving*rt9);

		//changes on 25-mar-08
		//var rt11=(parseFloat(rt10)+parseInt(accR));
		var rt11=(parseFloat(rt10)+parseFloat(new3));
		
		var o2=(parseFloat(lumpsumR)+parseFloat(rt11));

		//o3=T
		var o3=ageD;

		//c1=o1-o2
		var oDiff=(o1-o2);

		//changed on date:24-10-07

		var rm1=(retRS/12);
		var rm2=(rm1*oDiff);
		var rm3=(parseFloat(rm1)+parseInt("1"));
		var rm4=(12*ageD);
		var rm5=Math.pow(rm3,rm4);
		var rm6=(parseFloat(rm5)-parseInt("1"));
		var o4=(rm2/rm6);

		//alert ("rt1"+rt1);
		//alert ("rt2"+rt2);
		//alert ("o1"+o1);
		//alert ("rt4"+rt4);
		//alert ("rt6"+rt6);
		//alert ("rt7"+rt7);
		//alert ("rt9"+rt9);
		//alert ("rt10"+rt10);
		//alert ("o2"+o2);
		//alert ("rm2"+rm2);
		//alert ("rm4"+rm4);
		//alert ("rm5"+rm5);
		//alert ("o4"+o4);

		//alert ("retRS/12"+rt4);
		//alert (rt5);
		//alert (rt6);
		//alert (rt7);
		//alert (rt8);
		//alert (rt9);
		//alert (rt10);
		//alert (rt11);
		//alert (o2);

		if(o1 == 0)
		{
			var a1=0;
		}
		else
		{
		var a=Math.abs(Math.round(o1));
		var a1=currencyFormat(a);
		}
		//var a1=new NumberFormat(a).toFormatted();
		//alert(a1);
		
		if(o2 == 0)
		{
			var b1=0;
		}
		else
		{
		var b=Math.abs(Math.round(o2));
		var b1=currencyFormat(b);
		}

		if(o4 <= 0)
		{
			var c1=0;
		}
		else
		{
		var c=Math.abs(Math.round(o4));
		//alert (c);
		var c1=currencyFormat(c);
		}

		document.getElementById('r4').innerHTML = '<b>RESULT</b><hr>For your retirement goal you would require Rs '+a1+' By the time of your retirement you would have accumulated (including gratuity) Rs. '+b1+' Thus, to provide for the deficit in the next '+o3+' years, you need to invest a sum of Rs. '+c1+' per month.';

		//alert ("For your retirement goal you would require Rs "+a1+" By the time of your retirement you would have accumulated (including gratuity) Rs. "+b1+" Thus, to provide for the deficit in the next "+o3+" years, you need to invest a sum of Rs. "+c1+" per month.");

	}
}



//to validate networth calc

function validationNetworth()
{
	//modified on 26-oct-07
/*	var loan=document.netWorthCal.loan.value;
	var a = IsNumeric(loan);
	if(a == false)
	{
		alert ("Enter loans  if not applicable please enter 0");
		return false;
	}
*/
	var cash=document.netWorthCal.cash.value;
	var b = IsNumeric(cash);
	if(b == false)
	{
		alert ("Enter cash in hand   if not applicable please enter 0");
		return false;
	}

	var home_ln=document.netWorthCal.home_ln.value;
	var c = IsNumeric(home_ln);
	if(c == false)
	{
		alert ("Enter home loan   if not applicable please enter 0");
		return false;
	}

	var bank_b=document.netWorthCal.bank_b.value;
	var d = IsNumeric(bank_b);
	if(d == false)
	{
		alert ("Enter bank balance  if not applicable please enter 0");
		return false;
	}

	var educn=document.netWorthCal.educn.value;
	var e = IsNumeric(educn);
	if(e == false)
	{
		alert ("Enter education loan  if not applicable please enter 0");
		return false;
	}

	var house=document.netWorthCal.house.value;
	var f = IsNumeric(house);
	if(f == false)
	{
		alert ("Enter house amt  if not applicable please enter 0");
		return false;
	}

	var vehicle=document.netWorthCal.vehicle.value;
	var g = IsNumeric(vehicle);
	if(g == false)
	{
		alert ("Enter vehicle loan   if not applicable please enter 0");
		return false;
	}

	var jewelry=document.netWorthCal.jewelry.value;
	var h = IsNumeric(jewelry);
	if(h == false)
	{
		alert ("Enter jewelry   if not applicable please enter 0");
		return false;
	}

	var personal_ln=document.netWorthCal.personal_ln.value;
	var i = IsNumeric(personal_ln);
	if(i == false)
	{
		alert ("Enter Personal loan   if not applicable please enter 0");
		return false;
	}

	var estate=document.netWorthCal.estate.value;
	var j = IsNumeric(estate);
	if(j == false)
	{
		alert ("Enter Land/Real Estate   if not applicable please enter 0");
		return false;
	}

	var mortgage=document.netWorthCal.mortgage.value;
	var k = IsNumeric(mortgage);
	if(k == false)
	{
		alert ("Enter Loan against mortgage   if not applicable please enter 0");
		return false;
	}

	var fixed_dp=document.netWorthCal.fixed_dp.value;
	var l = IsNumeric(fixed_dp);
	if(l == false)
	{
		alert ("Enter Fixed Deposits   if not applicable please enter 0");
		return false;
	}

	var medical=document.netWorthCal.medical.value;
	var m = IsNumeric(medical);
	if(m == false)
	{
		alert ("Enter Medical Loan   if not applicable please enter 0");
		return false;
	}

	var securities=document.netWorthCal.securities.value;
	var n = IsNumeric(securities);
	if(n == false)
	{
		alert ("Enter Fixed income Securities   if not applicable please enter 0");
		return false;
	}

	var credit_crd=document.netWorthCal.credit_crd.value;
	var o = IsNumeric(credit_crd);
	if(o == false)
	{
		alert ("Enter Credit Card Bills    if not applicable please enter 0");
		return false;
	}

	var saving_schemes=document.netWorthCal.saving_schemes.value;
	var p = IsNumeric(saving_schemes);
	if(p == false)
	{
		alert ("Enter Small Savings Schemes   if not applicable please enter 0");
		return false;
	}

	var liability=document.netWorthCal.liability.value;
	var q = IsNumeric(liability);
	if(q == false)
	{
		alert ("Enter Any other liability   if not applicable please enter 0");
		return false;
	}

	var equity_shr=document.netWorthCal.equity_shr.value;
	var r = IsNumeric(equity_shr);
	if(r == false)
	{
		alert ("Enter Equity Shares   if not applicable please enter 0");
		return false;
	}

	var mf=document.netWorthCal.mf.value;
	var s = IsNumeric(mf);
	if(s == false)
	{
		alert ("Enter Mutual Funds   if not applicable please enter 0");
		return false;
	}

	return true;

}




//to show output of networth calc

function showNetworth()
{
	var result=validationNetworth();

	if(result == true)
	{
		//alert ("hi");

		//var loan=document.netWorthCal.loan.value;
		var home_ln=document.netWorthCal.home_ln.value;
		var educn=document.netWorthCal.educn.value;
		var vehicle=document.netWorthCal.vehicle.value;
		var personal_ln=document.netWorthCal.personal_ln.value;
		var mortgage=document.netWorthCal.mortgage.value;
		var medical=document.netWorthCal.medical.value;
		var credit_crd=document.netWorthCal.credit_crd.value;
		var liability=document.netWorthCal.liability.value;

		//var lb1=(parseFloat(loan)+parseFloat(home_ln));
		var lb1=(parseFloat(home_ln));
		var lb2=(parseFloat(lb1)+parseFloat(educn));
		var lb3=(parseFloat(lb2)+parseFloat(vehicle));
		var lb4=(parseFloat(lb3)+parseFloat(personal_ln));
		var lb5=(parseFloat(lb4)+parseFloat(mortgage));
		var lb6=(parseFloat(lb5)+parseFloat(medical));
		var lb7=(parseFloat(lb6)+parseFloat(credit_crd));
		var total_liabilities=(parseFloat(lb7)+parseFloat(liability));
		
		//alert (total_liabilities);

		var cash=document.netWorthCal.cash.value;
		var bank_b=document.netWorthCal.bank_b.value;
		var house=document.netWorthCal.house.value;
		var jewelry=document.netWorthCal.jewelry.value;
		var estate=document.netWorthCal.estate.value;
		var fixed_dp=document.netWorthCal.fixed_dp.value;
		var securities=document.netWorthCal.securities.value;
		var saving_schemes=document.netWorthCal.saving_schemes.value;
		var equity_shr=document.netWorthCal.equity_shr.value;
		var mf=document.netWorthCal.mf.value;

		var ast1=(parseFloat(cash)+parseFloat(bank_b));
		var ast2=(parseFloat(ast1)+parseFloat(house));
		var ast3=(parseFloat(ast2)+parseFloat(jewelry));
		var ast4=(parseFloat(ast3)+parseFloat(estate));
		var ast5=(parseFloat(ast4)+parseFloat(fixed_dp));
		var ast6=(parseFloat(ast5)+parseFloat(securities));
		var ast7=(parseFloat(ast6)+parseFloat(saving_schemes));
		var ast8=(parseFloat(ast7)+parseFloat(equity_shr));
		var total_assets=(parseFloat(ast8)+parseFloat(mf));

		//alert (total_assets);
		var output=(parseFloat(total_assets)-parseFloat(total_liabilities));

		if(output == 0)
		{
			var net_worth1=0;
		}
		else
		{
			var net_worth=Math.abs(output);

			var net_worth1=currencyFormat(net_worth);
		}

		if (parseFloat(total_liabilities)>parseFloat(total_assets))
		{
			document.getElementById('r5').innerHTML = '<b>RESULT</b><hr>Your actual Net Worth is Rs -'+net_worth1+' as your liabilities are greater than your assets.';
		}
		else
		{
			if(parseFloat(total_assets)>parseFloat(total_liabilities))
			{
				document.getElementById('r5').innerHTML = '<b>RESULT</b><hr>Your actual Net Worth is Rs '+net_worth1;
			}
			else
			{
				document.getElementById('r5').innerHTML = '<b>RESULT</b><hr>Your actual Net Worth is Rs '+net_worth1+' as your liabilities are equal to your assets.';
			}

		}

	}

}





/*

//to validate krorepati cal

function validationKrorepati()
{

	var age=document.krorepatiCal.age.value;
	var a = IsNumeric(age);
	if(a == false)
	{
		alert ("Enter your age");
		return false;
	}

	var annual_inc=document.krorepatiCal.annual_inc.value;
	var b = IsNumeric(annual_inc);
	if(b == false)
	{
		alert ("Enter Current Annual income");
		return false;
	}

	var save=document.krorepatiCal.save.value;
	var c = IsNumeric(save);
	if(c == false)
	{
		alert ("Enter What % of annual income you are able to save/ invest");
		return false;
	}

	var growth_r=document.krorepatiCal.growth_r.value;
	var d = IsNumeric(growth_r);
	if(d == false)
	{
		alert ("Enter Annual growth rate of savings");
		return false;
	}

	var r_return=document.krorepatiCal.r_return.value;
	var e = IsNumeric(r_return);
	if(e == false)
	{
		alert ("Enter Expected rate of return on investments");
		return false;
	}

	var acc_cap=document.krorepatiCal.acc_cap.value;
	var f = IsNumeric(acc_cap);
	if(f == false)
	{
		alert ("Enter Your present accumulated capital");
		return false;
	}


	return true;

}





//to show output of krorepati cal

function showKrorepati()
{
	var result=validationKrorepati();

	if(result == true)
	{
		//alert("hi");

	}

}





*/




//to validate budgeting cal

function validationBudgeting()
{
	var c_educn=document.budgetingCal.c_educn.value;
	var a = IsNumeric(c_educn);
	if(a == false)
	{
		alert ("Enter Expenses on your children`s education  if not applicable please enter 0");
		return false;
	}

	var medical_exp=document.budgetingCal.medical_exp.value;
	var b = IsNumeric(medical_exp);
	if(b == false)
	{
		alert ("Enter Medical Expenses  if not applicable please enter 0");
		return false;
	}

	var self=document.budgetingCal.self.value;
	var c = IsNumeric(self);
	if(c == false)
	{
		alert ("Enter Self(salary)  if not applicable please enter 0");
		return false;
	}

	var food_exp=document.budgetingCal.food_exp.value;
	var d = IsNumeric(food_exp);
	if(d == false)
	{
		alert ("Enter Grocery and food expenses  if not applicable please enter 0");
		return false;
	}

	var spouse=document.budgetingCal.spouse.value;
	var e = IsNumeric(spouse);
	if(e == false)
	{
		alert ("Enter Spouse(salary)  if not applicable please enter 0");
		return false;
	}

	var utilities=document.budgetingCal.utilities.value;
	var f = IsNumeric(utilities);
	if(f == false)
	{
		alert ("Enter Utilities (gas, electricity, phone)  if not applicable please enter 0");
		return false;
	}

	var profession=document.budgetingCal.profession.value;
	var g = IsNumeric(profession);
	if(g == false)
	{
		alert ("Enter Income from Business and Profession  if not applicable please enter 0");
		return false;
	}

	var maintenance=document.budgetingCal.maintenance.value;
	var h = IsNumeric(maintenance);
	if(h == false)
	{
		alert ("Enter Home maintenance (society, servants)  if not applicable please enter 0");
		return false;
	}

	var household_exp=document.budgetingCal.household_exp.value;
	var i = IsNumeric(household_exp);
	if(i == false)
	{
		alert ("Enter Other household expenses  if not applicable please enter 0");
		return false;
	}

	var rent=document.budgetingCal.rent.value;
	var j = IsNumeric(rent);
	if(j == false)
	{
		alert ("Enter Rent  if not applicable please enter 0");
		return false;
	}

	var communication_exp=document.budgetingCal.communication_exp.value;
	var k = IsNumeric(communication_exp);
	if(k == false)
	{
		alert ("Enter Communication Expenses  if not applicable please enter 0");
		return false;
	}

	var others=document.budgetingCal.others.value;
	var l = IsNumeric(others);
	if(l == false)
	{
		alert ("Enter Others  if not applicable please enter 0");
		return false;
	}

	var clothes=document.budgetingCal.clothes.value;
	var m = IsNumeric(clothes);
	if(m == false)
	{
		alert ("Enter Expenses on clothes  if not applicable please enter 0");
		return false;
	}

	var other_sources=document.budgetingCal.other_sources.value;
	var n = IsNumeric(other_sources);
	if(n == false)
	{
		alert ("Enter Other sources of Income  if not applicable please enter 0");
		return false;
	}

	var entertainment_exp=document.budgetingCal.entertainment_exp.value;
	var o = IsNumeric(entertainment_exp);
	if(o == false)
	{
		alert ("Enter Entertainment expenses  if not applicable please enter 0");
		return false;
	}

	var bonuses=document.budgetingCal.bonuses.value;
	var p = IsNumeric(bonuses);
	if(p == false)
	{
		alert ("Enter Bonuses or Commissions  if not applicable please enter 0");
		return false;
	}

	var restaurants=document.budgetingCal.restaurants.value;
	var q = IsNumeric(restaurants);
	if(q == false)
	{
		alert ("Enter restaurants  if not applicable please enter 0");
		return false;
	}

	var gifts=document.budgetingCal.gifts.value;
	var r = IsNumeric(gifts);
	if(r == false)
	{
		alert ("Enter gifts  if not applicable please enter 0");
		return false;
	}

	var policy=document.budgetingCal.policy.value;
	var s = IsNumeric(policy);
	if(s == false)
	{
		alert ("Enter Premium on Insurance policy and pension plan  if not applicable please enter 0");
		return false;
	}

	//var loan=document.budgetingCal.loan.value;
	//var t = IsNumeric(loan);
	//if(t == false)
	//{
	//	alert ("Enter Loan Repayments  if not applicable please enter 0");
	//	return false;
	//}

	var emi_1=document.budgetingCal.emi_1.value;
	var u = IsNumeric(emi_1);
	if(u == false)
	{
		alert ("Enter EMI 1  if not applicable please enter 0");
		return false;
	}

	var emi_2=document.budgetingCal.emi_2.value;
	var v = IsNumeric(emi_2);
	if(v == false)
	{
		alert ("Enter EMI 2  if not applicable please enter 0");
		return false;
	}

	var emi_3=document.budgetingCal.emi_3.value;
	var w = IsNumeric(emi_3);
	if(w == false)
	{
		alert ("Enter EMI 3  if not applicable please enter 0");
		return false;
	}

	var misc=document.budgetingCal.misc.value;
	var x = IsNumeric(misc);
	if(x == false)
	{
		alert ("Enter Misc.  if not applicable please enter 0");
		return false;
	}


	return true;

}






//to show output of budgeting cal

function showBudgeting()
{

	var result=validationBudgeting();

	if(result == true)
	{
		//alert ("hi");

		var duration=document.budgetingCal.duration.value;

		var self=document.budgetingCal.self.value;
		var spouse=document.budgetingCal.spouse.value;
		var profession=document.budgetingCal.profession.value;
		var rent=document.budgetingCal.rent.value;
		var others=document.budgetingCal.others.value;
		var other_sources=document.budgetingCal.other_sources.value;
		var bonuses=document.budgetingCal.bonuses.value;

		var incm1=(parseFloat(self)+parseFloat(spouse));
		var incm2=(parseFloat(incm1)+parseFloat(profession));
		var incm3=(parseFloat(incm2)+parseFloat(rent));
		var incm4=(parseFloat(incm3)+parseFloat(others));
		var incm5=(parseFloat(incm4)+parseFloat(other_sources));
		var total_income=(parseFloat(incm5)+parseFloat(bonuses));

		//alert (total_income);

		var c_educn=document.budgetingCal.c_educn.value;
		var medical_exp=document.budgetingCal.medical_exp.value;
		var food_exp=document.budgetingCal.food_exp.value;
		var utilities=document.budgetingCal.utilities.value;
		var maintenance=document.budgetingCal.maintenance.value;
		var household_exp=document.budgetingCal.household_exp.value;
		var communication_exp=document.budgetingCal.communication_exp.value;
		var clothes=document.budgetingCal.clothes.value;
		var entertainment_exp=document.budgetingCal.entertainment_exp.value;
		var restaurants=document.budgetingCal.restaurants.value;
		var gifts=document.budgetingCal.gifts.value;
		var policy=document.budgetingCal.policy.value;
		//var loan=document.budgetingCal.loan.value;
		var emi_1=document.budgetingCal.emi_1.value;
		var emi_2=document.budgetingCal.emi_2.value;
		var emi_3=document.budgetingCal.emi_3.value;
		var misc=document.budgetingCal.misc.value;

		var expd1=(parseFloat(c_educn)+parseFloat(medical_exp));
		var expd2=(parseFloat(expd1)+parseFloat(food_exp));
		var expd3=(parseFloat(expd2)+parseFloat(utilities));
		var expd4=(parseFloat(expd3)+parseFloat(maintenance));
		var expd5=(parseFloat(expd4)+parseFloat(household_exp));
		var expd6=(parseFloat(expd5)+parseFloat(communication_exp));
		var expd7=(parseFloat(expd6)+parseFloat(clothes));
		var expd8=(parseFloat(expd7)+parseFloat(entertainment_exp));
		var expd9=(parseFloat(expd8)+parseFloat(restaurants));
		var expd10=(parseFloat(expd9)+parseFloat(gifts));
		var expd11=(parseFloat(expd10)+parseFloat(policy));
		var expd12=(parseFloat(expd11)+parseFloat(emi_1));
		var expd13=(parseFloat(expd12)+parseFloat(emi_2));
		var expd14=(parseFloat(expd13)+parseFloat(emi_3));
		var total_expenditure=(parseFloat(expd14)+parseFloat(misc));

		//var total_expenditure=(parseFloat(expd15)+parseFloat(misc));

		//alert (total_expenditure);



		var surplus=(parseFloat(total_income)-parseFloat(total_expenditure));

		if(surplus == 0)
		{
			var surplus1=0;
		}
		else
		{
			var surplus2=Math.abs(surplus);
			var surplus1=currencyFormat(surplus2);
		}

		//alert (surplus);

		if(parseFloat(total_income)>parseFloat(total_expenditure))
		{
			document.getElementById('r6').innerHTML = '<b>RESULT</b><hr>You have '+duration+' Surplus of Rs '+surplus1;
		}
		else
		{
			if(parseFloat(total_expenditure)>parseFloat(total_income))
			{
				document.getElementById('r6').innerHTML = '<b>RESULT</b><hr>You have '+duration+' Deficit of Rs '+surplus1;
			}
			else
			{
				document.getElementById('r6').innerHTML = '<b>RESULT</b><hr>You have '+duration+' income and expenditure equal.';
			}
		}
	}

}









//to validate am_i_saving_enough_cal

function validationAmISavingEnough()
{
	var age=document.AmISavingEnoughCal.age.value;
	var a=IsNumeric(age);
	if(a==false)
	{
		alert ("Enter your age.");
		return false;
	}

	var age_r=document.AmISavingEnoughCal.age_r.value;
	var b=IsNumeric(age_r);
	if(b==false)
	{
		alert ("Enter age you plan to retire.");
		return false;
	}

	if(age_r<age)
	{
		alert ("Age you plan to retire should be greater than your age.");
		return false;
	}

	var amount=document.AmISavingEnoughCal.amount.value;
	var c=IsNumeric(amount);
	if(c==false)
	{
		alert ("Enter Amount you want to retire with (As today's price)");
		return false;
	}

	var total_saving=document.AmISavingEnoughCal.total_saving.value;
	var d=IsNumeric(total_saving);
	if(d==false)
	{
		alert ("Enter Your total saving for retirement till date.");
		return false;
	}

	var monthly_saving=document.AmISavingEnoughCal.monthly_saving.value;
	var e=IsNumeric(monthly_saving);
	if(e==false)
	{
		alert ("Enter Your monthly saving for retirement");
		return false;
	}

	var rate_r=document.AmISavingEnoughCal.rate_r.value;
	var f=IsNumeric(rate_r);
	if(f==false)
	{
		alert ("Enter Expected rate of return on your savings");
		return false;
	}

	var rate_i=document.AmISavingEnoughCal.rate_i.value;
	var g=IsNumeric(rate_i);
	if(g==false)
	{
		alert ("Enter Expected rate of inflation");
		return false;
	}

	return true;
}






//to show output of Am I Saving Enough cal

function showAmISavingEnough()
{
	var result=validationAmISavingEnough();
	if(result==true)
	{
		var age=document.AmISavingEnoughCal.age.value;
		var age_r=document.AmISavingEnoughCal.age_r.value;
		var amount=document.AmISavingEnoughCal.amount.value;
		var total_saving=document.AmISavingEnoughCal.total_saving.value;
		var monthly_saving=document.AmISavingEnoughCal.monthly_saving.value;
		var rate_r=document.AmISavingEnoughCal.rate_r.value;
		var rate_i=document.AmISavingEnoughCal.rate_i.value;

		var age_diff=(parseFloat(age_r)-parseFloat(age));
		var enf1=(age_diff*12);
		var enf2=(rate_r/100);
		var enf=(enf2/12);
		var enf3=(parseFloat(enf)+parseFloat("1"));

		var enf4=Math.pow(enf3,enf1);
		var enf5=(parseFloat(enf4)-parseFloat("1"));
		var enf6=(enf5*monthly_saving);
		var o1=(enf6/enf);

		var enf12=(parseFloat(enf2)+parseFloat("1"));
		var enf7=Math.pow(enf12,age_diff);
		var o2=(total_saving*enf7);

		var o3=(parseFloat(o1)+parseFloat(o2));

		var enf8=(rate_i/100);
		var enf9=(parseFloat(enf8)+parseFloat("1"));
		var enf10=Math.pow(enf9,age_diff);
		var o4=(amount*enf10);

		var o5=(parseFloat(o4)-parseFloat(o3));

		var enf11=(enf*o5);
		var o6=(enf11/enf5);

		//alert (o1);
		//alert (o2);
		//alert (o3);
		//alert (o4);
		//alert (o5);
		//alert (o6);
		
		if(o3==0)
		{
			var output=0;
		}
		else
		{
			var output1=Math.round(Math.abs(o3));
			var output=currencyFormat(output1);
		}

		if(parseFloat(o4)<=parseFloat(o3))
		{
			if(o4==0)
			{
				var output3=0;
			}
			else
			{
				var output2=Math.round(Math.abs(o4));
				var output3=currencyFormat(output2);
			}

			document.getElementById('r7').innerHTML = '<b>RESULT</b><hr>Your savings should be worth Rs. '+output+' on retirement.<br />You need a retirement corpus of Rs. '+output3+' (adjusted to inflation)<br />CONGRATULATIONS! You are saving adequately for your retirement.';
		}
		else
		{
			if(o6==0)
			{
				var more_rs=0;
			}
			else
			{
				var more_r=Math.round(Math.abs(o6));
				var more_rs=currencyFormat(more_r);
			}

			if(o4==0)
			{
				var output3=0;
			}
			else
			{
				var output2=Math.round(Math.abs(o4));
				var output3=currencyFormat(output2);
			}

			document.getElementById('r7').innerHTML = '<b>RESULT</b><hr>Your savings should be worth Rs. '+output+' on retirement.<br />You need a retirement corpus of Rs. '+output3+' (adjusted to inflation)<br />You need to increase your monthly savings by Rs. '+more_rs+' to meet your target amount on retirement.';
		}

	}
}









function calc_emi()
{
    var loan;
    var rate;
    var period;
    var roi;
    var basis;

	//alert(document.frmEmi.loan_amt.value);
    loan = parseFloat(document.frmEmi.loan_amt.value,10);
    rate = parseFloat(document.frmEmi.rate.value,10);
    period = parseFloat(document.frmEmi.period.value,10);
    basis = parseFloat(document.frmEmi.redbal.options[document.frmEmi.redbal.selectedIndex].value,10);
	//alert(loan);
	//alert(rate);
	//alert(period);
	//alert(basis);
    
    rate = rate / 100; 		
    roi = rate / 12; 
    roi = parseFloat(roi,10);
    period = period * 12;

	var output=(loan *rate*(Math.pow( (roi*basis+1),period/basis))) / ((Math.pow((1+roi*basis),period/basis) - 1)*12);

	var output2=Math.round(output*100);

    var EMI=(output2/100);

    //document.frmEmi.emi.value = EMI;    
	//return false;

	document.getElementById('r').innerHTML = '<b>EMI RESULT</b> &nbsp;&nbsp;Rs.&nbsp;&nbsp; '+EMI;
}












//to convert ans into Rs format

function currencyFormat(amt)
{
	if(amt!=0)
	{
		var num=String(amt);
		var arr= new Array();
		var num1=String(num);
		//alert (num1.length);
		if(num.length>3)
		{
				for(var i=0; i<num.length; i++)
				{
					arr[i]=num1%10;
					var mnum=arr[i];
					num1=(num1-mnum);
					num1=(num1/10);
					//alert (arr[i]);
				}
				var remlen=(num.length-3);
				var mrem=(remlen%2);
				//alert (remlen);
				//alert (mrem);

				if(mrem==0)
				{
					var finalnum=' ';
					for(var j=(num.length-1) ; j >=3 ; j=(j-2) )
					{
						finalnum=finalnum+arr[j]+arr[j-1]+',';
					}
					//alert (finalnum);
					finalnum=finalnum+arr[2]+arr[1]+arr[0];
					//alert (finalnum);
					return finalnum;
				}

				if(mrem==1)
				{
					var finalnum=' ';
					var telem=(num.length-1);
					finalnum=finalnum+arr[telem]+',';
					for(var j=(num.length-2) ; j >=3 ; j=(j-2) )
					{
						finalnum=finalnum+arr[j]+arr[j-1]+',';
					}
					//alert (finalnum);
					finalnum=finalnum+arr[2]+arr[1]+arr[0];
					//alert (finalnum);
					return finalnum;
				}
		}
		else
		{
			return amt;
		}
	}
	else
	{
		return amt;
	}
}



/*

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{2})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

*/
