//Calculates the consumer-price-index for the period specified in the drop-down lists
function calculate(e)
{
	var startYear,startMonth,startDate,
		endYear,endMonth,endDate,
		lastYear,lastMonth,lastDate,
		currentDate,errorMessage,rows,rowsLength,
		i,currentDateArray,diff,result,row,months,
		percentage,tBody,resultsLabel,args;

	diff=0;
	months=0;

	args={IsValid:false};

	tBody=document.getElementById('IndexTableBody');
	result=document.getElementById('Percentage');
	errorMessage=document.getElementById(IDPREFIX+'DatesValidator');
	resultsTextBox=document.getElementById(IDPREFIX+'TotalMonthsTextBox');
	rows=tBody.rows;
	rowsLength=rows.length;

	currentDateArray=rows[0].cells[0].firstChild.nodeValue.split('/');

	//Start* - First two drop-downs, End* - Next two drop-downs, Last* - Last date for which data is available
	startYear=Number(document.getElementById(IDPREFIX+'StartYearDropDownList').value);
	startMonth=Number(document.getElementById(IDPREFIX+'StartMonthDropDownList').value);
	startDate=new Date(startYear,startMonth-1,1);
	endYear=Number(document.getElementById(IDPREFIX+'EndYearDropDownList').value);
	endMonth=Number(document.getElementById(IDPREFIX+'EndMonthDropDownList').value);
	endDate=new Date(endYear,endMonth-1,1);
	lastYear=Number(currentDateArray[1]);
	lastMonth=Number(currentDateArray[0]);
	lastDate=new Date(lastYear,lastMonth-1,1);

	validateDates(null,args);
	errorMessage.style.display='none';

	if(args.IsValid)
	{
		for(i=rowsLength-1;i>-1;i--)
		{
			row=rows[i];
			percentage=row.cells[2];
			currentDateArray=row.cells[0].firstChild.nodeValue.split('/');
			currentDateArray[0]=Number(currentDateArray[0]);
			currentDateArray[1]=Number(currentDateArray[1]);
			currentDate=new Date(currentDateArray[1],currentDateArray[0]-1,1);
			if((currentDate<=endDate)&&(currentDate>=startDate))
			{
				months++;
				addClass('Highlight',row,false);//Dates in range are highlighted
				currentDateString=currentDate.toDateString();
				if(0===(currentDate-startDate))
				{
					diff=Number(percentage.firstChild.nodeValue.replace(/,/g,''));//Save first value
					removeClass('Highlight',row,true);//First date in range is never highlighted
				}
				else if(0===(currentDate-endDate))
				{
					diff=Number(percentage.firstChild.nodeValue.replace(/,/g,''))/diff;//Divide by the value saved above
					row.scrollIntoView();//Last date in range should be visible.
				}
			}
			else
			{
				removeClass('Highlight',row,true);//Out of range dates revert back to their old Normal/Alternate colors
			}
		}
		result.value=((100*diff)-100).toFixed(2)+'%';//The result is the percentage gain(possibliy negative-gain) between the first and last dates
	}
	else
	{//Bad data - show notification and reset results.
		errorMessage.style.display='inline';
		months=1;
		result.value='';
	}
	resultsTextBox.value=months-1;
}
//Page init
function body_Load()
{
	var startYear,startMonth,endYear,endMonth,calculateButton,help,disclaimer;
	if('function'===typeof ieLoader)
	{//Only MSIE gets here, this function makes sure MSIE displays things correctly (scrollbars,borders,width/height...)
		ieLoader();
	}
	else
	{//Everybody else goes here, 
		disclaimer=document.getElementById('Disclaimer');
		setInterval(function()
		{//Continuously ensure that the tBody's hight takes every available pixel
			var indexTable,height;
			indexTable=document.getElementById('IndexTable');
			height=document.documentElement.clientHeight-disclaimer.offsetHeight-indexTable.offsetTop-indexTable.tHead.offsetHeight;
			if((height>50)&&(indexTable.tBodies[0].style.height!==(height+'px')))
			{
				indexTable.tBodies[0].style.height=height+'px';
			}
		},0);
		disclaimer.setAttribute('style','position:absolute;bottom:0px;');
	}
	startYear=document.getElementById(IDPREFIX+'StartYearDropDownList');
	startMonth=document.getElementById(IDPREFIX+'StartMonthDropDownList');
	endYear=document.getElementById(IDPREFIX+'EndYearDropDownList');
	endMonth=document.getElementById(IDPREFIX+'EndMonthDropDownList');
	calculateButton=document.getElementById(IDPREFIX+'CalculateButton');
	help=document.getElementById('Help');
	attachEventListener('change',startYear,calculate);
	attachEventListener('change',startMonth,calculate);
	attachEventListener('change',endYear,calculate);
	attachEventListener('change',endMonth,calculate);
	attachEventListener('submit',calculateButton.form,function(e)
	{//Gecko needs this, it allows us to prevent doing a form.submit() when doing async calls
		var evt=new CommonEvent(e);
		evt.cancel();
		return false;
	});
	attachEventListener('click',calculateButton,calculate);
	attachEventListener('click',help,showHelp);
}
function validateDates(source,args)
{
	var startYear,startMonth,startDate,
	endYear,endMonth,endDate,
	lastYear,lastMonth,lastDate,
	currentDate,rows;
	rows=document.getElementById('IndexTableBody').rows;
	startYear=Number(document.getElementById(IDPREFIX+'StartYearDropDownList').value);
	startMonth=Number(document.getElementById(IDPREFIX+'StartMonthDropDownList').value);
	startDate=new Date(startYear,startMonth-1,1);
	endYear=Number(document.getElementById(IDPREFIX+'EndYearDropDownList').value);
	endMonth=Number(document.getElementById(IDPREFIX+'EndMonthDropDownList').value);
	endDate=new Date(endYear,endMonth-1,1);	
	currentDateArray=rows[0].cells[0].firstChild.nodeValue.split('/');
	lastYear=Number(currentDateArray[1]);
	lastMonth=Number(currentDateArray[0]);
	lastDate=new Date(lastYear,lastMonth-1,1);
	args.IsValid=((endDate>startDate)&&(endDate<=lastDate));
}