

// last year with data
lastYear = 2006;

/*
 * Singapore Consumer Price Index
 * (2004 = 100)
 * Source: http://www.singstat.gov.sg/keystats/hist/cpi.html
 */
CPI = new Array();
CPI[1996] = 94.3;
CPI[1997] = 96.2;
CPI[1998] = 95.9;
CPI[1999] = 96.0;
CPI[2000] = 97.2;
CPI[2001] = 98.2;
CPI[2002] = 97.8;
CPI[2003] = 98.3;
CPI[2004] = 100.0;
CPI[2005] = 100.4;
CPI[2006] = 101.4;

// normalize against the last year
var lastCPI = CPI[CPI.length-1];
for (i = 0; i < CPI.length; i++) {
	CPI[i] = CPI[i] * 100/lastCPI;
}

/*
 * USD to SGD Exchange Rate
 * Source: http://www.singstat.gov.sg/keystats/hist/exchange.html
 */
exchangeUSD_SGD = new Array();
exchangeUSD_SGD[1996] = 1.3998; // for end of year
exchangeUSD_SGD[1997] = 1.4848;
exchangeUSD_SGD[1998] = 1.6736;
exchangeUSD_SGD[1999] = 1.6949;
exchangeUSD_SGD[2000] = 1.7239;
exchangeUSD_SGD[2001] = 1.7917;
exchangeUSD_SGD[2002] = 1.7906;
exchangeUSD_SGD[2003] = 1.7422;
exchangeUSD_SGD[2004] = 1.6903;
exchangeUSD_SGD[2005] = 1.6646;
exchangeUSD_SGD[2006] = 1.5889;

/**
 * Convert original cost from original currency, to current value in SGD
 */
function getCurrentValue(amount, currency, origDate) {
	var y = origDate.getFullYear();
	
	if (y > lastYear) { y = lastYear; }
	
	var inflationRate = 100/CPI[y];
	
	var exchangeRate = 1;
	if (currency == 'USD') {
		exchangeRate = exchangeUSD_SGD[y];
	}
	
	var currentValue = amount * inflationRate * exchangeRate;
            		
	// round to nearest cent
	currentValue = Math.round(currentValue*100)/100;
	// round to 3 decimal places
	inflationRate = Math.round(inflationRate*1000)/1000;
	exchangeRate = Math.round(exchangeRate*1000)/1000;
	
	return [currentValue, inflationRate, exchangeRate];
}

/**
 * Convert from current value in SGD, to original in original currency
 */
function getOriginalCost(currentValue, currency, origDate) {
	var y = origDate.getFullYear();
	
	if (y > lastYear) { y = lastYear; }
	
	var inflationRate = 100/CPI[y];
	
	var exchangeRate = 1;
	if (currency == 'USD') {
		exchangeRate = exchangeUSD_SGD[y];
	}
	
	var originalValue = currentValue / inflationRate / exchangeRate;
	
	return [originalValue, inflationRate, exchangeRate];
}