// Define global variables
	var g_strPreviousColor = "";


function showMessage()
{
	var strMessage = "";

	strMessage += "The date period you have selected has not been pre-processed by IMS. ";
	strMessage += "For your convenience, IMS has pre-processed 29 date period combinations to assist you. ";
	strMessage += "Here are the examples:";
	strMessage += "\n";
	strMessage += "\n1) Year to Date (This vs. Last)";
	strMessage += "\n2) Any 12 months  (This vs. Last)";
	strMessage += "\n3) Any Quarter of year  (This vs. Last)";
	strMessage += "\n4) Semi-annual  (This vs. Last)";
	strMessage += "\n5) Annual  (This vs. Last)";
	strMessage += "\n6) First Three Quarters  (This vs. Last)";
	strMessage += "\n";
	
	
	alert(strMessage);
}

function ConvertToDate(text)
{
	var arDate = text.split(',');
	if (arDate.length != 2)
		return null;

	var intMonth = 0;
	var monthArray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	for (intMonth = 0; intMonth < monthArray.length; intMonth++)
	{
		if (monthArray[intMonth] == arDate[0])
			break;
	}
	return new Date(parseInt(arDate[1]), intMonth, 1);
}

function IsValidRange(first, last)
{
	return ConvertToDate(first) < ConvertToDate(last);
}

function checkPreProcessRange(in_strThisStart, in_strThisEnd, in_strLastStart, in_strLastEnd, in_strLength)
{
	var thisStart = document.getElementById(in_strThisStart);
	var thisEnd = document.getElementById(in_strThisEnd);
	var lastStart = document.getElementById(in_strLastStart);
	var lastEnd = document.getElementById(in_strLastEnd);
	
	if (!IsValidRange(thisStart.value, thisEnd.value) || !IsValidRange(lastStart.value, lastEnd.value))
	{
		showMessage();
		return false;
	}
	/*
	if (!IsValidRange(lastEnd.value, thisStart.value))
	{
		showMessage();
		return false;	
	}*/

	return true;
}

function InvidHighlight(in_objSender, in_blnHighlight)
{
	var objRow = in_objSender.parentElement;
	var objTable = objRow.parentElement;
	var intTotalColumns = objRow.children.length;
	var intTotalGroups = 3;
	var intGroupSpan = intTotalColumns / intTotalGroups;
	var strHighlightColor;
	
	if (in_blnHighlight)
	{
		g_strPreviousColor = in_objSender.style.backgroundColor;
		strHighlightColor = "lightyellow";
	}
	else
	{
		strHighlightColor = g_strPreviousColor;
	}
	
	var intIndex = in_objSender.cellIndex;
	
	if (intIndex >= intGroupSpan)
	{
		while (intIndex >= intGroupSpan)
		{
			intIndex -= intGroupSpan;
		}
	}
	
	var intTotalRows = objTable.childNodes.length;
	var intStartRow = objRow.rowIndex;
	
	if (intStartRow == 1 && intIndex == 1)
	{
		// Highlight the current cell and it's invid siblings.
		for (var i = 0; i < intTotalGroups; i++)
		{
			objRow.childNodes[intIndex + (i * intGroupSpan)].style.backgroundColor = strHighlightColor;
		}

		for (var j = intStartRow + 2; j < intTotalRows - 1; j++)
		{
			objTable.childNodes[j].childNodes[intIndex + (0 * 7) + 4].style.backgroundColor = strHighlightColor;
			objTable.childNodes[j].childNodes[intIndex + (1 * 7) + 5].style.backgroundColor = strHighlightColor;
			objTable.childNodes[j].childNodes[intIndex + (2 * 7) + 6].style.backgroundColor = strHighlightColor;
		}


	}
	else if (intStartRow == 2)
	{
		// Highlight the current cell and it's invid siblings.
		for (var i = 0; i < intTotalGroups; i++)
		{
			objRow.childNodes[intIndex + (i * intGroupSpan)].style.backgroundColor = strHighlightColor;
		}

		if (in_objSender.cellIndex < 4)
		{
			for (var j = intStartRow + 1; j < intTotalRows - 1; j++)
			{
				objTable.childNodes[j].childNodes[intIndex + (0 * intGroupSpan) + 1].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (1 * intGroupSpan) + 2].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (2 * intGroupSpan) + 3].style.backgroundColor = strHighlightColor;
			}
		}
		else if (in_objSender.cellIndex > 3 && in_objSender.cellIndex < 7)
		{
			for (var j = intStartRow + 1; j < intTotalRows - 1; j++)
			{
				objTable.childNodes[j].childNodes[intIndex + (0 * intGroupSpan) + 2].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (1 * intGroupSpan) + 3].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (2 * intGroupSpan) + 4].style.backgroundColor = strHighlightColor;
			}
		}
		else if (in_objSender.cellIndex > 6 && in_objSender.cellIndex < 11)
		{
			for (var j = intStartRow + 1; j < intTotalRows - 1; j++)
			{
				objTable.childNodes[j].childNodes[intIndex + (0 * intGroupSpan) + 1].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (1 * intGroupSpan) + 2].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (2 * intGroupSpan) + 3].style.backgroundColor = strHighlightColor;
			}
		}
		else if (in_objSender.cellIndex > 10 && in_objSender.cellIndex < 14)
		{
			for (var j = intStartRow + 1; j < intTotalRows - 1; j++)
			{
				objTable.childNodes[j].childNodes[intIndex + (0 * intGroupSpan) + 2].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (1 * intGroupSpan) + 3].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (2 * intGroupSpan) + 4].style.backgroundColor = strHighlightColor;
			}
		}
		else if (in_objSender.cellIndex > 13 && in_objSender.cellIndex < 18)
		{
			for (var j = intStartRow + 1; j < intTotalRows - 1; j++)
			{
				objTable.childNodes[j].childNodes[intIndex + (0 * intGroupSpan) + 1].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (1 * intGroupSpan) + 2].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (2 * intGroupSpan) + 3].style.backgroundColor = strHighlightColor;
			}
		}
		else if (in_objSender.cellIndex > 17 && in_objSender.cellIndex < 21)
		{
			for (var j = intStartRow + 1; j < intTotalRows - 1; j++)
			{
				objTable.childNodes[j].childNodes[intIndex + (0 * intGroupSpan) + 2].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (1 * intGroupSpan) + 3].style.backgroundColor = strHighlightColor;
				objTable.childNodes[j].childNodes[intIndex + (2 * intGroupSpan) + 4].style.backgroundColor = strHighlightColor;
			}
		}
	}
}

function openExcelDocument(in_strTarget)
{
	var strURL = window.location.href;
	if (strURL.indexOf('?') >= 0 )
		strURL += "&excel=yes";
	else
		strURL += "?excel=yes";
		
	var strWindowOptions = "fullscreen=no,location=no,menubar=yes,resizable=yes,status=yes,toolbar=no";
	
	window.open(strURL, in_strTarget, strWindowOptions);
	return false;
}