function init_cpi_table(t) {
t[1] = new Array();
t[1]['name'] = 'United States of America';
t[1]['firstYearAvailable'] = 2004;

t[1][20041]=1;
t[1][20042]=1.02;
t[1][20043]=1.08;
t[1][20044]=1.08;
t[1][20045]=1.17;
t[1][20046]=1.13;
t[1][20047]=1.15;
t[1][20048]=1.21;
t[1][20049]=1.20;
t[1][200410]=1.27;
t[1][200411]=1.20;
t[1][200412]=1.15;

t[1][20051]=1.21;
t[1][20052]=1.25;
t[1][20053]=1.38;
t[1][20054]=1.36;
t[1][20055]=1.32;
t[1][20056]=1.41;
t[1][20057]=1.45;
t[1][20058]=1.53;
t[1][20059]=1.53;
t[1][200510]=1.48;
t[1][200511]=1.41;
t[1][200512]=1.44;

t[1][20061]=1.55;
t[1][20062]=1.52;
t[1][20063]=1.54;
t[1][20064]=1.65;
t[1][20065]=1.69;
t[1][20066]=1.69;
t[1][20067]=1.76;
t[1][20068]=1.74;
t[1][20069]=1.58;
t[1][200610]=1.54;
t[1][200611]=1.55;
t[1][200612]=1.60;

t[1][20071]=1.48;
t[1][20072]=1.57;
t[1][20073]=1.61;
t[1][20074]=1.68;
t[1][20075]=1.69;
t[1][20076]=1.77;
t[1][20077]=1.85;
t[1][20078]=1.80;
t[1][20079]=1.93;
t[1][200710]=2.02;
t[1][200711]=2.17;
t[1][200712]=2.17;

t[1][20081]=2.22;
t[1][20082]=2.34;
t[1][20083]=2.51;
t[1][20084]=2.68;
t[1][20085]=2.92;
t[1][20086]=3.09;
t[1][20087]=3.09;
t[1][20088]=2.74;
t[1][20089]=2.45;
t[1][200810]=1.89;
t[1][200811]=1.54;
t[1][200812]=1.31;

t[1][20091]=1.39;
t[1][20092]=1.34;
t[1][20093]=1.43;
t[1][20094]=1.49;
t[1][20095]=1.64;
t[1][20096]=1.82;
t[1][20097]=1.72;
t[1][20098]=1.83;
t[1][20099]=1.78;
t[1][200910]=1.84;
t[1][200911]=1.92;


}

function fixStartYear() {
   t = new Array();
   init_cpi_table(t);
   cpilocation = document.getElementById("cpiform").cpilocation.value;
   firstAvailableYear = t[cpilocation]['firstYearAvailable'];
   currentSelectedYear = document.getElementById("cpiform").startYear.value;
   if( (firstAvailableYear*1) > (currentSelectedYear*1) ) {
      id = "startYear" + firstAvailableYear;
      document.getElementById(id).selected = true;
   }
   return false;
}

// Displays the calculated CPI value.
function displayResult(startYear, endYear, startAmount, endAmount, overallIncrease, averageIncrease, location) {
   resultHTML = "An initial investment amount of $" + startAmount + " is equal to the inflation adjusted value of $" + endAmount + " based on ";
   if((endAmount*1)>=(startAmount*1)) {
      resultHTML = resultHTML + "an increase of ";
   } else {
      resultHTML = resultHTML + "a decrease of ";
   }
   resultHTML = resultHTML + overallIncrease + "% "; 
   document.getElementById("cpiresult").innerHTML = resultHTML;
}

//  Vaildate that year is within bounds, cpilocation exists, etc...
function checkFormValues(t,cpilocation, startYear, endYear, startAmount) {
   // Make sure we have a valid location.
   if(typeof t[cpilocation]=='undefined') {
      document.getElementById("cpiresult").innerHTML = "You must pick a location from the list.";
      return false;
   }
   if(typeof startAmount != 'number' || !isFinite(startAmount)) {
      document.getElementById("cpiresult").innerHTML = "The starting amount should be must numbers -- no dollar signs or commas, for example.";
      return false;
   }
   if(startAmount == 0) {
      document.getElementById("cpiresult").innerHTML = "";
      return false;
   }
   if(startYear<t[cpilocation]['firstYearAvailable']) {
      document.getElementById("cpiresult").innerHTML = t[cpilocation]['name'] + " information is only available from " + t[cpilocation]['firstYearAvailable'] + " onwards.";
      return false;
   }
   return true;
}

function twoDegreesPrecision(finalAmount) {
   p = Math.round(finalAmount * 100);
   if(p>=10||p<0) {
      s = p.toString();
   } else {
      s = "0" + p;
   }
   l = s.length;
   return s.substring(0,l-2)+"."+s.substring(l-2,l);
}

function calculate() {
   f = document.getElementById("cpiform");
   cpilocation = f.cpilocation.value;
   startYear = f.startYear.value *1;
   endYear = f.endYear.value * 1;
   startAmount = f.startAmount.value;
   startAmount.replace("$","");
   startAmount.replace(",","");
   startAmount = startAmount * 1;

   t = new Array();
   init_cpi_table(t);
   if(checkFormValues(t,cpilocation, startYear, endYear, startAmount)) {

      cf = t[cpilocation][startYear];
      ct = t[cpilocation][endYear];
      overallIncrease = ct/cf;
      yearsElapsed = endYear - startYear;
      endAmount = startAmount* overallIncrease;
      if(overallIncrease==1) {
         averageIncrease = 0;
      } else {
         averageIncrease = Math.pow((endAmount/startAmount),(1/yearsElapsed)) - 1;
         averageIncrease = twoDegreesPrecision(averageIncrease*100);
      }
      startAmount = twoDegreesPrecision(startAmount);
      endAmount = twoDegreesPrecision(startAmount*overallIncrease);
      overallIncrease = twoDegreesPrecision((overallIncrease-1)*100);
      displayResult(startYear, endYear, startAmount, endAmount, overallIncrease, averageIncrease, t[cpilocation]['name']);
   }
   return true;
}


