var oDSP_ViewData = null;

function dspTable_GetTableHtml(iMode, oSetting, xRs, sTableId) {
	var oParam = new Object;
	sFirstColHeader = new String(oSetting.FirstColumnHeader);
	if (iMode!=null) {
		var a = new dspData_FillHeaderArrays(xRs, 2);
		oParam.ColHeaders = dspTable_HeaderPlaceHolder(a.ColHeader, sFirstColHeader, xRs);
		oParam.RowHeaders = dspTable_HeaderPlaceHolder(a.RowHeader, "", xRs);
		oParam.Values = dspData_FillResultArray(xRs, 2);
	}
	else {
		with (oSetting) {
			oParam.ColHeaders = dspTable_HeaderPlaceHolder(ColHeader, sFirstColHeader, xRs);
			oParam.RowHeaders = dspTable_HeaderPlaceHolder(RowHeader, "", xRs);
			oParam.Values = Values;
		}
	}
	with (oSetting) {
		oParam.BorderColor = BorderColor;
		oParam.Pivot = TablePivot;
	}
	oParam.Action = new dspTable_InitAction(oSetting, iMode);
	oParam.ColumnWidth = oSetting.TableColumnWidth;
	oParam.Rules = new dspTable_Rules(oSetting);
	oParam.TableId = sTableId;
	oParam.ResourcePath = oSetting.ResourcePath;
	oParam.RowIndentation = oSetting.RowIndentation;
	return dspTable_BuildTable(oParam);
}

function dspTable_AddBorderColorStyle(oSetting, sSelector) {
	if (dspTable_GetRuntimeStyleSheet() == null) {
		var style = document.createElement("style");
		style.type = "text/css"; 
		style.id = "oDSP_RuntimeStyle";
		document.getElementsByTagName('head').item(0).appendChild(style);
		var styleSheet = document.styleSheets("oDSP_RuntimeStyle");
		styleSheet.addRule(sSelector, "border-color:" + oSetting.BorderColor); 
	}
}

function dspTable_GetRuntimeStyleSheet() {
	var styleSheet = null;
	try {
		styleSheet = document.styleSheets("oDSP_RuntimeStyle");
	}
	catch(e) {
		styleSheet = null;
	}
	return styleSheet;
}

function dspTable_Rules(oSetting) {
	var id = "dsp-" + (oSetting.TableStyleSheet!=""?oSetting.TableStyleSheet.split("_")[1]:"00") + "-";

	dspTable_AddBorderColorStyle(oSetting, "TD.dsp-bordercolor");
	
	this.ColHeaderBase = "ms-SPLink dsp-bordercolor " + id + "columnheader ";
	this.ColHeaderLeft = id + "columnheader-left ";
	this.ColHeaderRight = id + "columnheader-right";

	this.RowHeaderBase = "ms-SPLink dsp-bordercolor " + id + "rowheader ";
	this.RowHeaderLevel = id + "rowheader-";
	this.RowHeaderTop = id + "rowheader-top ";
	this.RowHeaderBottom = id + "rowheader-bottom ";

	this.CellBase = "dsp-bordercolor " + id + "cell ";
	this.CellLevel =  id + "cell-";
	this.CellLeft = id + "cell-left ";
	this.CellRight= id + "cell-right ";
	this.CellTop = id + "cell-top ";
	this.CellBottom = id + "cell-bottom ";

	this.Table = id + "table";
}

function dspTable_HeaderPlaceHolder(a, sFirstColHeader, xRs) {
	var oDims = xRs.selectNodes("/DSPWebPart/Dimensions/Dimension[@Axis='5']");
	for(var i=0;i<oDims.length;i++) {
		var oMembers = xRs.selectNodes("/DSPWebPart/Members/Member[@Dim='" + oDims[i].getAttribute("Id") + "']");
		if (oMembers.length > 0) {
			var sToken = "(" + oDims[i].getAttribute("Caption") + ")";
			for(var j=0;j<a.length;j++) {
				if (a[j].indexOf(sToken) > -1)
					a[j] = a[j].replace(sToken, oMembers[0].getAttribute("Caption"));
				if (sFirstColHeader.indexOf(sToken) > -1)
					sFirstColHeader = sFirstColHeader.replace(sToken, oMembers[0].getAttribute("Caption"));
			}
		}
	}
	a.FirstColHeader = sFirstColHeader;
	return a;
}

function dspTable_InitAction(oParam, iMode) {
	if (iMode == null) {
		var sEvent = " onmouseover=\"this.style.cursor='hand';this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\"";
		this.CellAction = (oParam.CellActionName!="")?sEvent + " cellid='_MEMBERID_' title='" + oParam.CellActionDescription + "'":"";	
		this.ColumnAction = (oParam.ColumnActionName!="")?sEvent + " memberid='_MEMBERID_' title='_TOOLTIP_" + oParam.ColumnActionDescription + "'":"";	
		this.RowAction = (oParam.RowActionName!="")?sEvent + " memberid='_MEMBERID_' title='_TOOLTIP_" + oParam.RowActionDescription + "'":"";	
	}
	else {
		this.CellAction = "";
		this.ColumnAction = "";
		this.RowAction = "";	
	}
}

function dspTable_ColumnWidth(iColIndex, aColWidth) {
	var w = aColWidth[iColIndex];
	if (isNaN(w) || w == "")
		return " nowrap ";
	else
		return " WIDTH='" + w + "px' ";
}

function dspTable_AddColHeaders(oParam, aCols, bRowHeader, iColCount, aColWidth) {
	var sTD = "<TD class='_CLASS_'", r = oParam.Rules, sTitleBase = oParam.Action.ColumnAction;
	var sRow = (bRowHeader)?sTD.replace("_CLASS_", r.ColHeaderBase + r.ColHeaderLeft) + dspTable_ColumnWidth(0, aColWidth) + ">" + aCols.FirstColHeader + "</TD>":"";
	for (var c=0;c<iColCount;c++) {
		var t = sTitleBase.replace("_MEMBERID_", aCols[c].MemberId);
		if (t == "" && aCols[c].Tooltip != "")
		{
			t = " title='" + aCols[c].Tooltip + "'";
		}
		else
		{
			t = t.replace("_TOOLTIP_", aCols[c].Tooltip);
		}
		sRow += sTD.replace("_CLASS_", r.ColHeaderBase + (((c+1)==iColCount)?r.ColHeaderRight:""));
		sRow += dspTable_ColumnWidth(c+1, aColWidth) + t + " >" + aCols[c] + "</TD>";
	}
	return "<TR>" + sRow + "</TR>";
}

function dspTable_AddRowHeader(oParam, aRows, aColWidth, i, iRowCount) {
	var r = oParam.Rules;
	var sTitle = oParam.Action.RowAction;
	if (sTitle == "" && aRows[i].Tooltip != "")
	{
		sTitle = " title='" + aRows[i].Tooltip + "'";
	}
	else
	{
		sTitle = sTitle.replace("_MEMBERID_", aRows[i].MemberId);
		sTitle = sTitle.replace("_TOOLTIP_", aRows[i].Tooltip);
	}
	var sTD = "<TD class='" + r.RowHeaderBase + ((i==0)?r.RowHeaderTop:"") + ((i+1==iRowCount)?r.RowHeaderBottom:"") + r.RowHeaderLevel + ((oParam.RowIndentation==1)?aRows[i].LevelDepth:0) + "'";
	sTD += dspTable_ColumnWidth(0, aColWidth) + sTitle + ">";
	sTD += aRows[i] + "</TD>";
	return sTD;
}

function dspTable_AddCell(oParam, r, c, iRowCount, iColCount, iLevel) {
	var oValue = null;
	if (iColCount==0)
		oValue = oParam.Values[r];
	else
		oValue = (oParam.Pivot)?oParam.Values[r][c]:oParam.Values[c][r];
	var sTD = "<TD nowrap class='" + oParam.Rules.CellBase + ((c==0)?oParam.Rules.CellLeft:"") + ((c+1==iColCount)?oParam.Rules.CellRight:"");
	sTD += ((r==0)?oParam.Rules.CellTop:"") + ((r+1==iRowCount)?oParam.Rules.CellBottom:"") + oParam.Rules.CellLevel + iLevel + "'";
	sTD += " style='" + dspTable_CellStyle(oValue) + "' " + oParam.Action.CellAction.replace("_MEMBERID_", oValue.CellId) + ">";
	sTD += ((oValue.Symbol=="IMG")?"<IMG src='" + oParam.ResourcePath + "/symbol/" + oValue.Formatted + "'>":oValue.Formatted) + "</TD>";
	return sTD;
}

function dspTable_BuildTable(oParam) {
	var sRow="", sRows="";
	var aColWidth = oParam.ColumnWidth.toString().split(";");
	var sTableHeader = "<TABLE id='" + oParam.TableId + "' border='0' cellpadding='0' cellspacing='0' class='" + oParam.Rules.Table + "'>";

	var aRows = (oParam.Pivot)?oParam.ColHeaders:oParam.RowHeaders;
	var aCols = (oParam.Pivot)?oParam.RowHeaders:oParam.ColHeaders;
	
	var iRowCount = aRows.length; 
	var bRowHeader = !(iRowCount==1 && aRows[0]=="");
	var iColCount = aCols.length; 
	var bColHeader = !(iColCount==1 && aCols[0]=="");
	
	if (bColHeader)
		sRows = dspTable_AddColHeaders(oParam, aCols, bRowHeader, iColCount, aColWidth);
	
	for (var r=0;r<iRowCount;r++) {
		sRow = (bRowHeader)?dspTable_AddRowHeader(oParam, aRows, aColWidth, r, iRowCount):"";
		for (var c=0;c<iColCount;c++)
			sRow += dspTable_AddCell(oParam, r, c, iRowCount, iColCount, aRows[r].LevelDepth);
		sRows += "<TR>" + sRow + "</TR>";
	}
	return sTableHeader + sRows + "<TABLE>";
}

function dspTable_CellStyle(oValue) {
	var s = (oValue.BackColor!=null)?"background-color:" + oValue.BackColor + ";":"";
	s += (oValue.ForeColor!=null)?"color:" + oValue.ForeColor  + ";":"";
	s += (oValue.FontName!=null)?"font-family:" + oValue.FontName  + ";":"";
	s += (oValue.FontSize!=null)?"font-size:" + oValue.FontSize  + "pt;":"";
	if (oValue.FontStyle!=null) {
		if (oValue.FontStyle & 1)
			s += "font-weight:bold;";
		if (oValue.FontStyle & 2)
			s += "font-style:italic;";
		if (oValue.FontStyle & 4)
			s += (oValue.FontStyle & 8)?"text-decoration:underline line-through;":"text-decoration:underline;";
		if ((oValue.FontStyle & 8) && !(oValue.FontStyle & 4))
			s += "text-decoration:line-through;";
	}
	if (oValue.Symbol != "")
		s += "text-align:center;"
	return s;
}

function dspTable_ViewData(sHTML) { 
	var sLinks = "";
	for (var i=0;i<document.styleSheets.length;i++) {
		sLinks += "<link rel='stylesheet' type='text/css' href='" + document.styleSheets[i].href + "'>";
	}
	var sRuntimeStyle = "<style>TD.dsp-bordercolor{border-color:" + dspTable_GetRuntimeStyleSheet().rules("TD.dsp-bordercolor").style.borderColor + "}</style>";
	var sContent = "<html><head>" + sLinks + "</head>" + sRuntimeStyle + "<body><center><div class='ms-WPBody' style='padding:10px'>" + sHTML + "</div></center></body></html>";
	if (oDSP_ViewData != null) {
		if (!oDSP_ViewData.closed)
			oDSP_ViewData.close();
	}
	var oSize = new dspTable_CalcZoomWinSize();
	try {
		oDSP_ViewData = window.open("", "ViewData", "directories=no,top=" + oSize.Top + ",left=" + oSize.Left + ",height=" + oSize.Height + ",width=" + oSize.Width + ",location=no,titlebar=yes,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
		try {
			oDSP_ViewData.document.write(sContent);
		}
		catch(e){
			oDSP_ViewData.close();
			alert("Could not open Data View window. Reason: " + e);
		}
	}
	catch(e){}
}

function dspTable_CalcZoomWinSize() {
	var iHeight = window.screen.availHeight / 2;
	var iWidth = window.screen.availWidth / 2;
	this.Width = iWidth;
	this.Height = iHeight;
	this.Left = iWidth/2;
	this.Top = iHeight/2;
}


