var assets;
var income;
var state;
var shortfall;
var savings;
var factors = new Array();
factors["AK"] = 1;
factors["AL"] = 5000;
factors["AR"] = 4657;
factors["AZ01"] = 5778;
factors["AZ02"] = 5135;
factors["CA"] = 6840;
factors["CO"] = 6394;
factors["CT"] = 10586;
factors["DC"] = 7149;
factors["DE"] = 6455;
factors["FL"] = 5000;
factors["GA"] = 4917;
factors["HI"] = 8850;
factors["IA"] = 4853;
factors["ID"] = 6494;
factors["IL"] = 1;
factors["IN"] = 5139;
factors["KS"] = 4423;
factors["KY"] = 5820;
factors["LA"] = 4000;
factors["MA"] = 8334;
factors["MD"] = 7000;
factors["ME"] = 7258;
factors["MI"] = 6816;
factors["MN"] = 5340;
factors["MO"] = 3960;
factors["MS"] = 4600;
factors["MT"] = 5550;
factors["NC"] = 5500;
factors["ND"] = 6577;
factors["NE"] = 1;
factors["NH"] = 8421;
factors["NJ"] = 7282;
factors["NM"] = 5774;
factors["NV"] = 6858;
factors["NY01"] = 7688;
factors["NY02"] = 11445;
factors["NY03"] = 10105;
factors["NY04"] = 8323;
factors["NY05"] = 10579;
factors["NY06"] = 8942;
factors["NY07"] = 7863;
factors["OH"] = 6023;
factors["OK"] = 4041;
factors["OR"] = 7663;
factors["PA"] = 7901;
factors["RI"] = 7777;
factors["SC"] = 5481;
factors["SD"] = 5204;
factors["TN"] = 4567;
factors["TX"] = 4347;
factors["UT"] = 4526;
factors["VA01"] = 7734;
factors["VA02"] = 5933;
factors["VT"] = 7477;
factors["WA"] = 7219;
factors["WI"] = 6554;
factors["WV"] = 5751;
factors["WY"] = 6032;
var t;
var c=0;
function calc_on(){
  expenses = document.mapp.expenses.value;
  alert01  = document.mapp.alert1.value;
  if((expenses < 3500) && (alert01 == 0)){
    document.mapp.alert1.value = "1";
    alert("It appears your value for Nursing Home Expenses are low. Please confirm this is the actual cost for Nursing Home Care and try again.");
  }else{
    if(c == 0){document.getElementById('calc_button').src = "/logo/calc_savings2.png";}
    c=c+1;
    if(c == 5){
      c=0;
      document.getElementById('calc_button').src = "/logo/calc_savings.png";
      calculate();
    }else{
      document.getElementById('results').innerHTML = "<span style=\"color:#990000;\">CALCULATING<br>please wait...</span>";
      t=setTimeout("calc_off()",500);
    }
  }
}
function calc_off(){
  document.getElementById('results').innerHTML = "<span style=\"color:#990000;\">&nbsp;<br>please wait...</span>";
  t=setTimeout("calc_on()",500);
}

function clean_values(myvalue){
  var nostr    = /[^\d.]+/g;
  myvalue = myvalue.replace(nostr, "");
  myvalue = Math.round(myvalue);
  return myvalue;
}

function calculate(){
  var state    = document.mapp.state.value;
  var assets   = document.mapp.assets.value;
  var income   = document.mapp.income.value;
  var expenses = document.mapp.expenses.value;
  assets   = clean_values(assets);
  income   = clean_values(income);
  expenses = clean_values(expenses);
  document.mapp.assets.value   = addCommas(assets);
  document.mapp.income.value   = addCommas(income);
  document.mapp.expenses.value = addCommas(expenses);
  shortfall = expenses - income;
  help = "<span style=\"color:#990000;\">Call For Help<br>1-866-334-2243</span>";
  if ((shortfall > 0) && (expenses > 0) && (assets > 0)){
    factor = ((state == 'AK') || (state == 'IL') || (state == 'ME')) ? expenses : factors[state];
    if (state == 'CA'){
      months = (assets/200000 == parseInt(assets/200000)) ? parseInt(assets/200000) :  1 + parseInt(assets/200000);
      savings = assets - (months * shortfall);
    }else if(state == 'IA'){
      savings = assets - (shortfall * 60);
    }else if((state == 'AL') || (state == 'CT') || (state == 'NJ')){
      months = assets/factor;
      savings1 = assets - (months * shortfall);
      savings2 = assets - (shortfall * 60);
      savings =  (savings1 > savings2) ?  savings1 : savings2;
    }else{
      savings1 = assets - (shortfall * (assets / (factor + shortfall)));
      savings2 = assets - (shortfall * 60);
      savings =  (savings1 > savings2) ?  savings1 : savings2;
    }
    if (savings > assets){savings = assets;}
    savings = parseInt(savings);
    savingsout = addCommas(savings);
    dsply = "<span style=\"font-size:11px;color:#990000;\">You should be able to</span><br><span style=\"font-size:12px;color:#990000;\">save at least</span><span style=\"font-size:14px;color:#990000;\">&nbsp;$" + savingsout + "</span>";
    document.getElementById('results').innerHTML = ((savings > (.2 * assets)) && (savings > 10000)) ? dsply : help;
  }else{
    document.getElementById('results').innerHTML = help;
  }
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = (x.length > 1) ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

