annualCost = new Array();
annualCost[30] = 471.84;
annualCost[31] = 483.72;
annualCost[32] = 495.60;
annualCost[33] = 507.96;
annualCost[34] = 520.68;
annualCost[35] = 533.52;
annualCost[36] = 543.00;
annualCost[37] = 552.60;
annualCost[38] = 562.32;
annualCost[39] = 572.40;
annualCost[40] = 582.60;
annualCost[41] = 592.80;
annualCost[42] = 603.36;
annualCost[43] = 614.04;
annualCost[44] = 624.96;
annualCost[45] = 636.00;
annualCost[46] = 645.84;
annualCost[47] = 655.92;
annualCost[48] = 666.00;
annualCost[49] = 676.44;
annualCost[50] = 686.88;
annualCost[51] = 697.44;
annualCost[52] = 708.24;
annualCost[53] = 719.28;
annualCost[54] = 730.44;
annualCost[55] = 741.72;
annualCost[56] = 775.08;
annualCost[57] = 810.00;
annualCost[58] = 846.60;
annualCost[59] = 884.88;
annualCost[60] = 924.72;
annualCost[61] = 966.36;
annualCost[62] = 1010.04;
annualCost[63] = 1083.96;
annualCost[64] = 1163.28;
annualCost[65] = 1248.60;
annualCost[66] = 1339.92;
annualCost[67] = 1438.20;
annualCost[68] = 1573.44;
annualCost[69] = 1721.40;
annualCost[70] = 1883.16;
annualCost[71] = 2060.40;
annualCost[72] = 2253.96;
annualCost[73] = 2489.16;
annualCost[74] = 2748.96;
annualCost[75] = 3035.76;
annualCost[76] = 3352.32;
annualCost[77] = 3702.12;
annualCost[78] = 4029.24;
annualCost[79] = 4385.40;
annualCost[80] = 4773.12;
annualCost[81] = 5194.92;
annualCost[82] = 5654.04;
annualCost[83] = 6226.80;
//repopulate years to wait based on age started
function updateWait() {
   var form = document.costofwaiting;
   var iAgeStart = parseInt(form.iAgeStart.value);
   if (iAgeStart < 51) {
      iAgeEnd = 82;
   } else if (iAgeStart > 50 && iAgeStart < 61) {
      iAgeEnd = 80;
   } else {
      iAgeEnd = 79;
   }
   var iAgeMax = iAgeEnd - iAgeStart;
   form.iYearsWait.options.length = 0;
   for (var i=0;i<iAgeMax;i++) {
      form.iYearsWait.options[i] = new Option(i+1,i+1);
   }
}
function calculate() {
   var form = document.costofwaiting;
   var iAgeStart = parseInt(form.iAgeStart.value);
   var iAgeEnd = parseInt(form.iAgeEnd.value);
   var iYearsWait = parseInt(form.iYearsWait.value);
   var iAgeWait = iAgeStart+iYearsWait;
   //make sure they arent over the limit
   if (iAgeWait>iAgeEnd) {
      alert("You cannot wait until you are 83 to purchase coverage. Please adjust your values.");
   } else {
      var iInflationBenefit = roundnum(100*Math.pow(1.05,iYearsWait));
      if (iAgeStart<30) {
         var startAnnualCost = roundnum(annualCost[30]);
      } else {
         var startAnnualCost = roundnum(annualCost[iAgeStart]);
      }
      if (iAgeStart+iYearsWait<30) {
         var endAnnualCost = roundnum(annualCost[30] * iInflationBenefit/100);
      } else {
         var endAnnualCost = roundnum(annualCost[iAgeWait] * iInflationBenefit/100);
      }
      var startYears = (iAgeEnd-iAgeStart);
      var endYears = (iAgeEnd-iAgeWait);
      var startValue = roundnum(startAnnualCost * startYears);
      var endValue = roundnum(endAnnualCost * endYears);
      var costofwaiting = "$" + CommaFormatted(roundnum(endValue - startValue));
      if (iAgeWait < 50) {
         sText = "When you delay purchasing coverage, your good health may change unexpectedly and prevent you from getting approved when you apply.";
      } else {
         sText = "The greatest risk of waiting <span class=\"result\">" + iYearsWait + "</span> years to apply for coverage when you are <span class=\"result\">" + iAgeWait + "</span> years old may be getting declined due to pre-existing medical conditions.";
      }
      document.getElementById("sAgeSpecific").innerHTML = sText;
      document.getElementById("iCostofWaiting").innerHTML = costofwaiting;
      document.getElementById("iCostofWaiting2").innerHTML = costofwaiting;
      document.getElementById("iCostofWaiting3").innerHTML = costofwaiting;
      document.getElementById("iYears2Wait").innerHTML = iYearsWait;
      document.getElementById("iStartPremium").innerHTML = "$" + CommaFormatted(startAnnualCost) + " per year";
      document.getElementById("iEndPremium").innerHTML = "$" + CommaFormatted(endAnnualCost) + " per year";
      //document.getElementById("iInflationBenefit_result").innerHTML = "$" + iInflationBenefit;
      document.getElementById("iStartYears").innerHTML = "for " + startYears + " years";
      if (endYears == 1) {
         document.getElementById("iEndYears").innerHTML = "for " + endYears + " year";
      } else {
         document.getElementById("iEndYears").innerHTML = "for " + endYears + " years";
      }
      document.getElementById("iStartTotal").innerHTML = "$" + CommaFormatted(startValue);
      document.getElementById("iEndTotal").innerHTML = "$" + CommaFormatted(endValue);
      var percentCost = Math.round(200 * ((endValue - startValue)/endValue));
      changeStyle("buynowcost","height",percentCost + "px");
      var padding = percentCost - 15;
      if (padding > 0) {
         padding = padding/2;
      } else {
         padding = 0;
      }
      changeStyle("buylatercost","paddingTop",padding + "px");
      changeStyle("buylatercost","height",percentCost-padding + "px");
      showElement('results');
      location.href = "#theresults";
   }
}
function roundnum(val) {
   return Math.round(val * 100)/100;
}
function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
   amount = amount + "";
   if (amount.indexOf(".")!= -1) {
   	var a = amount.split('.',2)
   	var d = a[1];
      if (d.length < 2) { d = d + '0'; } 
   	var i = parseInt(a[0]);
   } else {
   	var d = "00";
   	var i = parseInt(amount);
   }
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	amount = n + '.' + d;
	amount = minus + amount;
	return amount;
}
updateWait();
preload('submit','/images/shared/butt/butt_what_is_cost');
