//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Inflation Calculator
//2004 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 01/06/2004
//Last Modified: 01/06/2004
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//For commercial use rates, contact:
//Dan Peterson:
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//Commercial User Licence #:5782-1237-118-1198
//Commercial Licence Date:2008-09-11
//*******************************************



function stripNum(num) {

   num=num.toString();


   var len = num.length;
   var rnum = "";
   var test = "";
   var j = 0;

   var b = num.substring(0,1);
   if(b == "-") {
      rnum = "-";
   }

   for(i = 0; i <= len; i++) {

      b = num.substring(i,i+1);

      if(b == "0" || b == "1" || b == "2" || b == "3" || b == "4" || b == "5" || b == "6" || b == "7" || b == "8" || b == "9" || b == ".") {
         rnum = rnum + "" + b;

      }

   }

   if(rnum == "" || rnum == "-") {
      rnum = 0;
   }

   rnum = Number(rnum);

   return rnum;

}



function formatNumberDec(num, places, comma) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    var myDecFact = 1;
    var myPlaces = 0;
    var myZeros = "";
    while(myPlaces < places) {
       myDecFact = myDecFact * 10;
       myPlaces = eval(myPlaces) + eval(1);
       myZeros = myZeros + "0";
    }
    
	onum=Math.round(num*myDecFact)/myDecFact;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal=myZeros;
	} else{
		decimal=Math.round((onum-integer)* myDecFact)
	}
	decimal=decimal.toString();
	if (decimal.length<places) {
        fillZeroes = places - decimal.length;
	   for (z=0;z<fillZeroes;z++) {
        decimal="0"+decimal;
        }
     }

   if(places > 0) {
      decimal = "." + decimal;
   }

   if(comma == 1) {
	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}


	finNum=tmpinteger+""+decimal;
   } else {
      finNum=integer+""+decimal;
   }

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}


function computeForm(form)  {

if(form.amount.value.length == 0) {
   alert("Please enter a dollar amount.");
   form.amount.focus();
} else
if(form.year1.selectedIndex == form.year2.selectedIndex) {
   alert("Please select two different years.");
   form.amount.focus();
} else {


var cpiArr = new Array();
cpiArr[0] = "9.88"
cpiArr[1] = "10.02"
cpiArr[2] = "10.11"
cpiArr[3] = "10.88"
cpiArr[4] = "12.83"
cpiArr[5] = "15.04"
cpiArr[6] = "17.33"
cpiArr[7] = "20.04"
cpiArr[8] = "17.85"
cpiArr[9] = "16.75"
cpiArr[10] = "17.05"
cpiArr[11] = "17.13"
cpiArr[12] = "17.54"
cpiArr[13] = "17.7"
cpiArr[14] = "17.36"
cpiArr[15] = "17.16"
cpiArr[16] = "17.16"
cpiArr[17] = "16.7"
cpiArr[18] = "15.21"
cpiArr[19] = "13.64"
cpiArr[20] = "12.93"
cpiArr[21] = "13.38"
cpiArr[22] = "13.73"
cpiArr[23] = "13.87"
cpiArr[24] = "14.38"
cpiArr[25] = "14.09"
cpiArr[26] = "13.91"
cpiArr[27] = "14.01"
cpiArr[28] = "14.73"
cpiArr[29] = "16.33"
cpiArr[30] = "17.31"
cpiArr[31] = "17.59"
cpiArr[32] = "17.99"
cpiArr[33] = "19.52"
cpiArr[34] = "22.33"
cpiArr[35] = "24.04"
cpiArr[36] = "23.81"
cpiArr[37] = "24.07"
cpiArr[38] = "25.96"
cpiArr[39] = "26.55"
cpiArr[40] = "26.77"
cpiArr[41] = "26.85"
cpiArr[42] = "26.78"
cpiArr[43] = "27.18"
cpiArr[44] = "28.09"
cpiArr[45] = "28.86"
cpiArr[46] = "29.15"
cpiArr[47] = "29.58"
cpiArr[48] = "29.89"
cpiArr[49] = "30.25"
cpiArr[50] = "30.63"
cpiArr[51] = "31.02"
cpiArr[52] = "31.51"
cpiArr[53] = "32.46"
cpiArr[54] = "33.36"
cpiArr[55] = "34.78"
cpiArr[56] = "36.68"
cpiArr[57] = "38.83"
cpiArr[58] = "40.49"
cpiArr[59] = "41.82"
cpiArr[60] = "44.4"
cpiArr[61] = "49.31"
cpiArr[62] = "53.82"
cpiArr[63] = "56.91"
cpiArr[64] = "60.61"
cpiArr[65] = "65.23"
cpiArr[66] = "72.58"
cpiArr[67] = "82.41"
cpiArr[68] = "90.93"
cpiArr[69] = "96.5"
cpiArr[70] = "99.6"
cpiArr[71] = "103.88"
cpiArr[72] = "107.57"
cpiArr[73] = "109.61"
cpiArr[74] = "113.63"
cpiArr[75] = "118.26"
cpiArr[76] = "123.97"
cpiArr[77] = "130.66"
cpiArr[78] = "136.19"
cpiArr[79] = "140.32"
cpiArr[80] = "144.46"
cpiArr[81] = "148.23"
cpiArr[82] = "152.38"
cpiArr[83] = "156.85"
cpiArr[84] = "160.52"
cpiArr[85] = "163.01"
cpiArr[86] = "166.58"
cpiArr[87] = "172.2"
cpiArr[88] = "177.07"
cpiArr[89] = "179.88"
cpiArr[90] = "183.88"

  
var Vamount = stripNum(form.amount.value);

var Vyear1 = form.year1.options[form.year1.selectedIndex].value;
var Vyear1Sel = form.year1.selectedIndex;
var Vyear1cpi = cpiArr[Vyear1Sel];

var Vyear2 = form.year2.options[form.year2.selectedIndex].value;
var Vyear2Sel = form.year2.selectedIndex;
var Vyear2cpi = cpiArr[Vyear2Sel];
var multiplier = Vyear2cpi / Vyear1cpi;;
var VresultValue = Vamount * multiplier;

form.displayAmt1.value = "$" + formatNumberDec(Vamount,2,1);
form.displayYear1.value = Vyear1;

//if(Vyear1 < Vyear2) {
//   form.isWas.value = ";
//} else {
//   multiplier = Vyear1cpi / Vyear2cpi;
//}
form.displayAmt2.value = "$" + formatNumberDec(VresultValue,2,1);
form.displayYear2.value = Vyear2;

//form.resultValue.value = "$" + formatNumberDec(VresultValue,2,1);
//form.resultValue.value = eval(cpiArr[Vyear1]);

//END NESTED IF TO CHECK FOR NON-NUMERIC ENTRIES
   }

}

function clearResults(form) {

form.displayAmt1.value = "";
form.displayAmt2.value = "";
form.displayYear1.value = "";
form.displayYear2.value = "";


}
