
function setdollar(iamount) {
  //first round the number off so there will be only two decimal places
	iamount = iamount * 100;
  iamount = Math.round(iamount);
  iamount = iamount / 100;
	//
	//next determine where the decimal point lies
	var testtotal = new String(iamount);
	var result = testtotal.lastIndexOf(".");
	//
	//if there is no decimal place then tack it on
	if (result == -1) {
	  iamount = iamount + ".00";
	} else {
	  //
		//if there is only 1 digit after the decimal then add another zero
	  var ilen = testtotal.length - result - 1;
		
		if (ilen == 1) {
	    iamount = iamount + "0";
	  };
	};
	return iamount;
};


	 
function Calcnpv() {
	   var imonthly = 0, irate = 0, iinfl = 0, iyrs = 0, inpv = 0, inetrate = 0;
		 var iimsg = 0, ls_value;
     //
		 //imonthly -- monthly revenue required by spouse/family
     //irate    -- interest rate (enter as whole - will be divided by 100)
     //iinfl    -- inflation rate
     //iyrs     -- years income is required/ must be INTEGER!
     //inpv     -- net present value of cash required today
     //inetrate -- net investment rate 
  	 imonthly = parseFloat(document.calccoverage.txtmonthly.value);
		 iyrs = parseFloat(document.calccoverage.txtyrs.value);   
		 iinfl = parseFloat(document.calccoverage.txtinfl.value);
		 irate = parseFloat(document.calccoverage.txtinvest.value); 
     if (isNaN(document.calccoverage.txtmonthly.value)) {
	     imonthly = 0;
			 iimsg = 1;
			 ls_value = 'Monthly income';
		 } else if (imonthly < 0) {
		   iimsg = 2;
			 imonthly = 0;
		   ls_value = 'Monthly income';
		 } else if (document.calccoverage.txtmonthly.value == '' ) {
		  imonthly = 0;
			document.calccoverage.txtmonthly.value = 0;
		 };
     if (isNaN(document.calccoverage.txtyrs.value)) {
	     iyrs = 0;
			 iimsg = 1;
			 ls_value = 'Years needed';
		 } else if (iyrs < 0) {
		   iimsg = 2;
			 iyrs = 0;
		   ls_value = 'Years needed';
		 } else if (document.calccoverage.txtyrs.value == '' ) {
		   iyrs = 0;
			 document.calccoverage.txtyrs.value = 0;
		 };
		 if (isNaN(document.calccoverage.txtinfl.value)) {
	     iinfl = 0;
			 iimsg = 1;
			 ls_value = 'Inflation rate';
		 } else if (iinfl < 0) {
		   iimsg = 2;
			 iinfl = 0;
		   ls_value = 'Inflation rate';
		 } else if (document.calccoverage.txtinfl.value == '' ) {
		   iinfl = 0;
			 document.calccoverage.txtinfl.value = 0;
		 };
     if (isNaN(document.calccoverage.txtinvest.value)) {
	     irate = 0;
			 iimsg = 1;
			 ls_value = 'After tax';
		 } else if (irate < 0) {
		   iimsg = 2;
			 irate = 0;
		   ls_value = 'After tax';
		 } else if (document.calccoverage.txtinvest.value == '' ) {
		   irate = 0;
			 document.calccoverage.txtinvest.value = 0;
		 };		 
     inetrate = ( irate - iinfl) / 100; 
    
     inpv = ( imonthly * 12 );  
    
     for (var x = 1; x < iyrs; ++x) {
     
       inpv = inpv + ( ( imonthly * 12 ) / ( Math.pow( ( 1.0 + inetrate ), x ) ));			 
			 inpv = inpv * 100;
			 inpv = Math.round(inpv);
			 inpv = inpv / 100; 
    
		 } ;
		 inpv = Math.round(inpv);
		 return inpv;
     //document.calccoverage.txtnpv.value = inpv;		 
}
function Calcneed() {
	  var ifinalexp = 0, idebt = 0, imrtg = 0, iuniv = 0, iemer = 0, ittlneed = 0, imonthly = 0, iyrs = 0, inpv = 0, ittldth = 0;
    var iinfl = 0, iinvest = 0, icurrent = 0, iother = 0, iadditional = 0;
    var iimsg = 0, ls_value
    ifinalexp = parseFloat(document.calccoverage.txtfinal.value);
    idebt = parseFloat(document.calccoverage.txtdebt.value);
    imrtg = parseFloat(document.calccoverage.txtmrtg.value);
    iuniv = parseFloat(document.calccoverage.txtuniv.value);
    iemer = parseFloat(document.calccoverage.txtemer.value);
                
                if (isNaN(document.calccoverage.txtemer.value)) {
	    iuniv = 0;
			iimsg = 1;
			ls_value = 'Emergency Funds';
		} else if (iuniv < 0) {
		  iimsg = 2;
			iuniv = 0;
		  ls_value = 'Emergency Funds';
		} else if (document.calccoverage.txtemer.value == '' ) {
		  iuniv = 0;
			document.calccoverage.txtemer.value = 0;
		};

		if (isNaN(document.calccoverage.txtuniv.value)) {
	    iuniv = 0;
			iimsg = 1;
			ls_value = 'University funding';
		} else if (iuniv < 0) {
		  iimsg = 2;
			iuniv = 0;
		  ls_value = 'University funding';
		} else if (document.calccoverage.txtuniv.value == '' ) {
		  iuniv = 0;
			document.calccoverage.txtuniv.value = 0;
		};
		if (isNaN(document.calccoverage.txtfinal.value)) {
	    ifinalexp = 0;
			iimsg = 1;
			ls_value = 'Final Expenses';
		} else if (ifinalexp < 0) {
		  iimsg = 2;
			ifinalexp = 0;
		  ls_value = 'Final Expenses';
		} else if (document.calccoverage.txtfinal.value == '' ) {
		  ifinalexp = 0;
			document.calccoverage.txtfinal.value = 0;
		};
		if (isNaN(document.calccoverage.txtdebt.value)) {
	    idebt = 0;
			iimsg = 1;
			ls_value = 'Outstanding Debt';
		} else if (idebt < 0) {
		  iimsg = 2;
			ifinalexp = 0;
		  ls_value = 'Outstanding Debt';
		} else if (document.calccoverage.txtdebt.value == '' ) {
		  idebt = 0;
			document.calccoverage.txtdebt.value = 0;
		};
		if (isNaN(document.calccoverage.txtmrtg.value)) {
	    imrtg = 0;
			iimsg = 1;
			ls_value = 'Mortgage';
		} else if (imrtg < 0) {
		  iimsg = 2;
			ifinalexp = 0;
		  ls_value = 'Mortgage';
		} else if (document.calccoverage.txtmrtg.value == '' ) {
		  imrtg = 0;
			document.calccoverage.txtmrtg.value = 0;
		};
    ittlneed = ifinalexp + idebt + imrtg + iuniv + iemer; 
    document.calccoverage.txtneed.value = ittlneed;
				
		inpv = Calcnpv();
		if (inpv >= 0) {
		  document.calccoverage.txtnpv.value = inpv
		} else {
		  document.calccoverage.txtnpv.value = 0;
			iimsg = 3;
		};		
		icurrent = parseFloat(document.calccoverage.txtcurrent.value);
		iother = parseFloat(document.calccoverage.txtother.value);		
		if (isNaN(document.calccoverage.txtcurrent.value)) {
	    icurrent = 0;
			iimsg = 1;
			ls_value = 'Current investments';
		} else if (icurrent < 0) {
		  iimsg = 2;
			icurrent = 0;
		  ls_value = 'Current investments';
		} else if (document.calccoverage.txtcurrent.value == '' ) {
		  icurrent = 0;
			document.calccoverage.txtcurrent.value = 0;
		};		
		if (isNaN(document.calccoverage.txtother.value)) {
	    iother = 0;
			iimsg = 1;
			ls_value = 'Other life insurance';
		} else if (iother < 0) {
		  iimsg = 2;
			iother = 0;
		  ls_value = 'Other life insurance';
		} else if (document.calccoverage.txtother.value == '' ) {
		  iother = 0;
			document.calccoverage.txtother.value = 0;
		};
		
    if (iimsg == 1) {
	  	alert('All values entered into ' + ls_value + ' must be a number greater than zero.');
		} else if (iimsg == 2) {
		  alert('All values entered into ' + ls_value + ' must be greater than zero.');
    } ;
    //
		//if there was no error message calculate the cost, otherwise just give the user a 0 amount
		if (iimsg == 0) {
		  iadditional = (ittlneed + inpv) - (icurrent + iother);
		} else {
		    iadditional = 0;
		};

    
                document.getElementById('dat1').innerHTML="<div class='blockquote3'></div><center><hr style='width:60%;color:#fefefe'></center><div style='margin-bottom:20px'></div> ";

                 document.getElementById('dat2').innerHTML="<div class='divleft white' style= 'border-top:solid 1px #D2CAB1;'><strong>Insurance Needs Analysis</strong></div><div class= 'divright white'style='border-top:solid 1px #D2CAB1;'><span style= 'padding-left:45px'></span></div> ";
            
                 
                 document.getElementById('dat3').innerHTML="<div class='divleft blue greytext'><strong>Assets vs Liabilities</strong></div><div class='divright blue greytext'><span style='padding-left:45px;font-weight:bold'> Balance</span></div> ";

                 
                document.getElementById('dat4').innerHTML="<div class='divleft'>Total Immediate Expenses:</div><div class='divright'><span style='padding-left:45px'>$ " + ittlneed + " </span></div> ";

                

                document.getElementById('dat5').innerHTML="<div class='divleft alt'>Total Ongoing Income Requirements:</div><div class='divright alt'><span style='padding-left:35px'>+ $ " + inpv + " </span></div> ";


                document.getElementById('dat6').innerHTML="<div class='divleft'>TOTAL FUNDS NEEDED:</div><div class='divright'><span style='padding-left:45px'>$ " + (inpv + ittlneed) + " </span></div> ";

               
               document.getElementById('dat7').innerHTML="<div class='divleft alt'>Less Current Assets and Insurance:</div><div class='divright alt'><span style='padding-left:35px'>- $ " + (iother + icurrent) + " </span></div> ";


               document.getElementById('dat8').innerHTML="<div class='divleft redtext'><strong> ESTIMATED LIFE INSURANCE NEEDED:</strong></div><div class='divright bot'><span style='padding-left:0px;font-size:17px;color:#364086;font-weight:bold'>$ " + setdollar(iadditional) + " </span></div> ";

               




                if (iimsg == 0) {
		  iexpense = (icurrent + iother);
		} else {
		    iexpense = 0;
		};
                
                document.calccoverage.txtexpense.value = iexpense;

                if (iimsg == 0) {
		  iassets = (ittlneed + inpv);
		} else {
		    iassets = 0;
		};
             
                document.calccoverage.txtassets.value = iassets;
                                              
		document.calccoverage.txtadditional.value = setdollar(iadditional);				
  }
// avoid error of passing event object in older browsers
if (!document.layers && !document.all) {
		event = "nope"
}

function resetAll() {
        
                var div = document.getElementById('dat1');
	        div.innerHTML = "";
                var div = document.getElementById('dat2');
	        div.innerHTML = "";
                var div = document.getElementById('dat3');
	        div.innerHTML = "";
                var div = document.getElementById('dat4');
	        div.innerHTML = "";
                var div = document.getElementById('dat5');
	        div.innerHTML = "";
                var div = document.getElementById('dat6');
	        div.innerHTML = "";
                var div = document.getElementById('dat7');
	        div.innerHTML = "";
                var div = document.getElementById('dat8');
	        div.innerHTML = "";
                
                
                    

}       

//-->

