var G_ulCellNoLimit = 15000;
var G_ulExcelColumnsLimit = 255;
var G_ulExcelRowsLimit = 16384;
var G_strTableView = "tableView.aspx";

// ----------------------------------------------------------------------------
function DispPage(i_unFirstRow, i_unFirstCol) {
	ObjWdsForm.action = G_strTableView;
	ObjWdsForm.sWD_FirstRow.value = i_unFirstRow;
	ObjWdsForm.sWD_FirstCol.value = i_unFirstCol;
	executeWait(ObjWdsForm);
}

// ----------------------------------------------------------------------------
// i_strItemIdentifier is composed of 2 lists separated by character \036.
// The first list is composed of the column dimension names, separated by character \035.
// The second list is composed of the item codes, one for each dimension, separated by
// character \035.
function onColSortData(i_strItemIdentifier, i_nAscending)
{  
	if (typeof(ObjWdsForm.sWD_viewsorttype) != "undefined" &&
		typeof(ObjWdsForm.sWD_viewsortcode) != "undefined")
	{
		if (parseInt(ObjWdsForm.sWD_viewsorttype.value) == i_nAscending &&
			ObjWdsForm.sWD_viewsortcode.value == i_strItemIdentifier)
		{
			// Reset the sort
			ObjWdsForm.sWD_viewsorttype.value = i_nAscending;
			ObjWdsForm.sWD_viewsortcode.value = "";
		}
		else {
			// Perform the sort
			ObjWdsForm.sWD_viewsorttype.value = i_nAscending;
			ObjWdsForm.sWD_viewsortcode.value = i_strItemIdentifier;
		}
	}
	DispPage(ObjWdsForm.sWD_FirstRow.value, ObjWdsForm.sWD_FirstCol.value);
}

// ----------------------------------------------------------------------------    
function OnPreparePrint(i_object)
{
	var ulCellCount;
	var ulRowCount;
	var ulColCount;
	
    // Check the download limit
    // If the download limit is zero, we use to the default value
    if (ObjWdsForm.WD_DownloadLimit.value != "0") {
   	    G_ulCellNoLimit = ObjWdsForm.WD_DownloadLimit.value;	
    }
	
	ulRowCount = parseInt(ObjWdsForm.sWD_RowsItemsCount.value);
	ulColCount = parseInt(ObjWdsForm.sWD_ColsItemsCount.value);
	ulCellCount = ulRowCount * ulColCount;
	if (ulCellCount > G_ulCellNoLimit) {
		alert(ulCellCount.toString() + resCellNoLimitReached + 
		G_ulCellNoLimit.toString() + resCellNoLimitTryAgain);
	}
	else {
    	ObjWdsForm.WD_Printable.value = "1";
	    i_object.action = G_strTableView;
	    i_object.IF_ReportType.value = REPORT_TYPE_TABLE;
	    executeWait(i_object);
	}
}

// ----------------------------------------------------------------------------    
function OnSelectMembers(i_nDim, i_szGroupCode, i_nPos)
{
	var szProfileValue;
	var szEval;

	// If a profile is activated for this dimesnion then it needs to be deactivated.
	szEval = "if (ObjWdsForm.oWD_DimProfile" + i_nDim + "!=null) szProfileValue=ObjWdsForm.oWD_DimProfile" + i_nDim + ".value;";
	eval(szEval);
	if (szProfileValue != null) {
		if (szProfileValue != -1 ) {
			if (!confirm(resProfileDeAct)) {
			return;
			}
			ObjWdsForm.PR_DeactProfile.value = "True"; 
		}
	}

	ObjWdsForm.WD_SelectMembers.value = i_szGroupCode;
	ObjWdsForm.WD_SelectMembersDim.value = i_nDim;
	// reset the pagination
	if (i_nPos == 0) {
		ObjWdsForm.sWD_FirstRow.value = 0;
	}
	else {
		ObjWdsForm.sWD_FirstCol.value = 0;
	}
	ObjWdsForm.action = G_strTableView;
	executeWait(ObjWdsForm);
}

// ----------------------------------------------------------------------------    
function onCalcTtlPrct(i_nType)
{
	if (ObjWdsForm.oWD_TotalPercent.value == i_nType) {
		ObjWdsForm.oWD_TotalPercent.value = 0;
	}
	else {
		ObjWdsForm.oWD_TotalPercent.value = i_nType;
	}    
	ObjWdsForm.action = G_strTableView;
	executeWait(ObjWdsForm);
}

// ----------------------------------------------------------------------------    
function onPageLoad()
{
	var unRowCount;
	var unColCount;

	onPageLoadGlobal();
	if (ObjWdsForm.CS_InHelp.value == "True") {
		onLayoutResize(true);
		return;
	}
	
	ObjWdsForm.WD_PrevPageReport.value = true;
	
	var clJsdnd = new GJsdnd();
	clJsdnd.Initialize(this, false);

	unRowCount = parseInt(ObjWdsForm.sWD_RowsItemsCount.value);
	unColCount = parseInt(ObjWdsForm.sWD_ColsItemsCount.value);
	newTable(unRowCount, unColCount);

	if (ObjWdsForm.WD_SourceInfo != null) {
		window.status = unescape(ObjWdsForm.WD_SourceInfo.value);
	}
	onLayoutResize(true);
}

// ----------------------------------------------------------------------------    
function CheckExcelLimits()
{
    var astrColsDims;
	var astrOthersDims;
	var astrRowDims;
	var bIsLimitsChecked = true;
	var ulColDimsCount = 0;
	var ulColItemsCount = 0;
	var ulOtherDim = 0;
	var ulRowDimsCount = 0;
	var ulRowItemsCount = 0;

    if (ObjWdsForm.sWD_Others.value != "") {
	    astrOthersDims = ObjWdsForm.sWD_Others.value.split(",");
	    // Other dims are placed on one line.
	    if (astrOthersDims.length > 0) {
            ulOtherDim = 1;
        }
	}
    if (ObjWdsForm.sWD_Rows.value != "") {
        astrRowDims = ObjWdsForm.sWD_Rows.value.split(",");
        ulRowDimsCount = astrRowDims.length;
    }
    if (ObjWdsForm.sWD_Cols.value != "") {
	    astrColDims = ObjWdsForm.sWD_Cols.value.split(",");
	    ulColDimsCount = astrColDims.length;
	}
	ulRowItemsCount = parseInt(ObjWdsForm.sWD_RowsItemsCount.value);
	ulColItemsCount = parseInt(ObjWdsForm.sWD_ColsItemsCount.value);
	
	if ((ulColItemsCount + ulRowDimsCount) > G_ulExcelColumnsLimit) {
		alert(resExcelColumnsLimit);
		bIsLimitsChecked = false;
	}
	// + 3: the report title is placed on one row + followed by a blank row + row dims are placed on one line.
	if ((ulColDimsCount + ulOtherDim + ulRowItemsCount + 3) > G_ulExcelRowsLimit) {
		alert(resExcelRowsLimit);
		bIsLimitsChecked = false;
	}
	
	return bIsLimitsChecked;
}

// ----------------------------------------------------------------------------    
function onDownload(i_nType)
{
// TBD: Download will not work properly because the calculation of how many
// cells are selected is not used properly yet.  This code used to be on the
// server and the number of selected cells was calculated with each trip to the
// server.  But now we will be limiting the number of requests to the server
// and should keep track on the client of how many cells are selected.

	var szTypes = new Array('IVT', 'XLS', 'CSV', 'SSV', 'PDF');
	var szType;
	var ulCellCount;
	var ulRowItemsCount;
	var ulColItemsCount;
	var ulSelectedItemCount;

	szType = szTypes[i_nType];
	if (typeof(ObjWdsForm.oWD_TotalPercent) != "undefined") 
	{	
		if (ObjWdsForm.oWD_TotalPercent.value != 0)
		{
			if (!confirm(resPromptTotalPercent))
			{
				return false;
			}
		}
	}
	if (ObjWdsForm.WD_DownloadLimit != null) {
		G_ulCellNoLimit = ObjWdsForm.WD_DownloadLimit.value;	
	}
	
	if (szType == "IVT") {
	   ulCellCount = 1;
	   for( var i = 0; i < ObjWdsForm.sWD_MaxDim.value; i++){
	      eval("ulSelectedItemCount = ObjWdsForm.oWD_DimSelectedItemsCount" + i + ".value;");
	      ulCellCount = ulCellCount * ulSelectedItemCount;
	   }
	}
	else {
	   ulRowItemsCount = parseInt(ObjWdsForm.sWD_RowsItemsCount.value);
	   ulColItemsCount = parseInt(ObjWdsForm.sWD_ColsItemsCount.value);
	   ulCellCount = ulRowItemsCount * ulColItemsCount;
	}
	
	if (ulCellCount > G_ulCellNoLimit) {
		alert(ulCellCount.toString() + resCellNoLimitReached + 
		G_ulCellNoLimit.toString() + resCellNoLimitTryAgain);
	}
	else {
	    //currently we limit the number of cells to 15000, if we choose to increase this number,
	    //we have to consider the limit on maximum number of rows in Excel 95 which is 16384.
	    var bIsLimitsChecked = true;
	    if (szType == "XLS") {
	        bIsLimitsChecked = CheckExcelLimits();
	    }
		if (bIsLimitsChecked) {
		   var strPreviousPage = ObjWdsForm.action;
		   
			ObjWdsForm.WD_format.value = szType;
			var suffix = "";
			if( !PWdsapp_bIsIE ) // See below
			{
				if( PWdsapp_eBrowser == GJSSYS_BROWSER_N70 )
					suffix += "/";
				suffix += "?x=";
			}
			ObjWdsForm.action = "download.aspx" + suffix;
			ObjWdsForm.submit();
			ObjWdsForm.action = strPreviousPage;
		}
	}
}
// The /?x= in the action line above is required to make Netscape dowload the files properly.
// Netscape 7.1 requires the ?x=, while Netscape 7.0 also requires the slash.
// Without that, Netscape will try to add a .asp extension to whatever filename is suggested,
// resulting in things like download.xls.asp, which doesn't work at all.
// This solution was found on BugZilla: http://bugzilla.mozilla.org/show_bug.cgi?id=65827#c81

// ----------------------------------------------------------------------------
function GetDefaultView()
{	
   ObjWdsForm.WD_GetDefaultView.value = "True"
   ObjWdsForm.oWD_TotalPercent.value = "0";
   ObjWdsForm.action = G_strTableView;
   executeWait(ObjWdsForm);
}

// ----------------------------------------------------------------------------    
function OnPreviousItem(i_nDim)
{
	ObjWdsForm.WD_PreviousItemForDim.value = GetViewDim(i_nDim);
	ObjWdsForm.action = G_strTableView;
	executeWait(ObjWdsForm);
}

// ----------------------------------------------------------------------------    
function OnNextItem(i_nDim)
{	
	ObjWdsForm.WD_NextItemForDim.value = GetViewDim(i_nDim);
	ObjWdsForm.action = G_strTableView;
	executeWait(ObjWdsForm);
}