//Table object
function TableObject(sId, oRef){
	this.ID = sId;
	this.Color = 0;
	this.Reference = oRef;
			
	this.InsertRow = function(index){
		var oRow = this.Reference.insertRow(-1);
		if(this.Color == 0)
		{
			oRow.style.backgroundColor = '#bfcfd8'; // E6E6E6 
			this.Color = 1;
		}
		else
		{
			oRow.style.backgroundColor = '#FFFFFF'; 
			this.Color = 0;
		}
		return oRow;
		
	};
	
	this.InvertColor = function(index)
	{
		
		if(this.Color == 0)
		{
			this.Color = 1;
		}
		else
		{
			this.Color = 0;
		}
	};
	return this;
	
};

//Adding the double TableIndex array.
//Used for storing table information, such as current row color.
var MaxTables = 10;
var TableIndexArray = new Array(MaxTables);
	

//Adds a div container with a table, and returns the table reference
function AddDivTableContainer(sId)
{
	document.getElementById('mainTableArea').innerHTML = document.getElementById('mainTableArea').innerHTML + 
														 '<div id="' + sId + '" name="' + sId + '"><table id="' + 
														 sId + '_table" name="' + sId + '_table"' + 
														 'border=0 cellpadding=0 cellspacing=0  style="width:100%;padding:4px 4px 4px 4px;border:1px solid #cfdbe3;"></table></div>';
	//Add to table index array
	for(j=0; j < MaxTables; j++)
	{
		if(TableIndexArray[j] != '[object Object]')	
		{
			TableIndexArray[j] = new TableObject(sId ,document.getElementById(sId + '_table'));
			j=MaxTables;
		}
	}

	return GetTable(sId).Reference;
}

function GetTable(sId)
{
	for(z=0; z < MaxTables; z++)
	{
		if(TableIndexArray[z] == '[object Object]')	
		{
			if(TableIndexArray[z].ID == sId)
			{
				return TableIndexArray[z];
				z=MaxTables;
			}
		}
	}
	return false;	
}


if(1==1)
{

var tstTableX = AddDivTableContainer('mytab');
	
var periodCounter = 0;	//How many periods actually added?
var periodCounter2 = 0;	//How many period-cells encountered
var periodValues = '';  //Concat string of all encountered period values, speperated by [@ZEP-DEL@]

var numericCounter = 0;	//To make sure no more Numeric columns are added than the numebr of periods found
var headerCounter = 0;		//To make sure no more Header columns are added than the numebr of periods found
var subHeaderCounter = 0;	//To make sure no more SubHeader columns are added than the number of periods found
var totalCounter = 0;		//To make sure no more Total columns are added than the number of periods found
var subTotal1Counter = 0;	//To make sure no more SubTotal1 columns are added than the number of periods found
var subTotal2Counter = 0;	//To make sure no more SubTotal2 columns are added than the number of periods found
var standardCounter = 0;	//To make sure no more Standard columns are added than the number of periods found
var standard2Counter = 0;	//To make sure no more Standard columns are added than the number of periods found
var notFoundCounter = 0;	//To make sure no more not-found columns are added than the number of periods found

var oRow;
var oCell;

var j;
var k;

var passedSubHeader = 0;  //To determien if text should be spaced to the right


//Test if versions match
var renderOrNot = true;
if(TableRenderVersion != globSheet.EDXVersion)
{
	if(globSheet.EDXVersion == '')
	{
		renderOrNot = confirm('Javascript exports and table render versions does not match!\nAs a consequence, rendering may fail to yield the expected results.\nDo you want to continue?');
	}
	else
	{
		renderOrNot = confirm('Javascript exports and table render versions does not match!\nAs a consequence, rendering may fail to yield the expected results.\nDo you want to continue?');
	} 
}

if(renderOrNot == true)
{
	for(j=0; j < globSheet.Height; j++)
{
	//Only do stuff if row excists
	if(globSheet.DataGrid[j][0] == '[object Object]')
	{
	oRow = GetTable('mytab').InsertRow(j);
				
	for(k=0; k < globSheet.Width; k++)
	{
		if(globSheet.DataGrid[j][k] == '[object Object]')
		{
			var foundType = false;
					
			//PAGENAME
			if(InSemiColonSepString('PageName',globSheet.DataGrid[j][k].Type) == true && foundType == false)
			{
				oCell = oRow.insertCell(j); //Insert a cell to bet periods to align
				document.getElementById('pageName').innerHTML = document.getElementById('pageName').innerHTML + globSheet.DataGrid[j][k].Text;
				foundType = true;
			}
			
			//PAGETEXT
			if(InSemiColonSepString('PageText',globSheet.DataGrid[j][k].Type) == true && foundType == false)
			{
				document.getElementById('pageText').innerHTML = document.getElementById('pageText').innerHTML + globSheet.DataGrid[j][k].Text;
				foundType = true;
			}
			
			//PERIOD
			if(InSemiColonSepString('Period',globSheet.DataGrid[j][k].Type) == true && foundType == false)
			{
				periodCounter2 = periodCounter2 + 1
				periodValues = periodValues + globSheet.DataGrid[j][k].Text + '[@ZEP-DEL@]';
				if(periodCounter2 <= CONST_MAX_PERIOD_ROWS)
				{
					oCell = oRow.insertCell(-1);
					oCell.id = 'period_cell_' + periodCounter2;
					
					CSSFormatObject(oCell,'Period');
					if (globSheet.DataGrid[j][k].Text != '')
					{
						oCell.innerHTML = 'Period ' + periodCounter2;
						periodCounter = periodCounter + 1
					}
					else
					{
						oCell.innerHTML = '&nbsp;';
					}
					
					//oCell.innerHTML = 'Period ' + periodCounter;
				}
				else
				{
					//CSSFormatObject(oCell,'Empty'); 
					//oCell.innerHTML = '&nbsp;';
				}
				
				//Store period value
				//periodValues = periodValues + globSheet.DataGrid[j][k].Text + '[@ZEP-DEL@]';
									
				//Force grey header
				oRow.style.backgroundColor = '#efefef';
				GetTable('mytab').Color = 0;
									
				foundType = true;
			}
			//HEADER
			if(InSemiColonSepString('Header',globSheet.DataGrid[j][k].Type) == true && foundType == false)
			{
				passedSubHeader = 0;
				
				if(headerCounter <= periodCounter && headerCounter <= CONST_MAX_PERIOD_ROWS)
				{
					oCell = oRow.insertCell(-1);
					CSSFormatObject(oCell,'header');
					
					if (globSheet.DataGrid[j][k].Text != '')
						oCell.innerHTML = globSheet.DataGrid[j][k].Text;
					else
						oCell.innerHTML = '&nbsp;';
					
					//oCell.innerHTML = globSheet.DataGrid[j][k].Text;
						
					//Force white
					oRow.style.backgroundColor = '#FFFFFF';
					GetTable('mytab').Color = 0;
				}
				headerCounter+=1;
				foundType = true;
			}
			
			//SUBHEADER
			if(InSemiColonSepString('SubHeader',globSheet.DataGrid[j][k].Type) == true && foundType == false)
			{
				passedSubHeader = 1;
				
				if(subHeaderCounter <= periodCounter && subHeaderCounter <= CONST_MAX_PERIOD_ROWS)
				{
					oCell = oRow.insertCell(-1);
					CSSFormatObject(oCell,'subheader');
					if (globSheet.DataGrid[j][k].Text != '')
						oCell.innerHTML = globSheet.DataGrid[j][k].Text;
					else
						oCell.innerHTML = '&nbsp;';
						
					//Force white
					oRow.style.backgroundColor = '#FFFFFF';
					GetTable('mytab').Color = 0;
				}
				subHeaderCounter+=1;
				foundType = true;
			}
			
			//NUMERIC
			if(InSemiColonSepString('Numeric',globSheet.DataGrid[j][k].Type) == true && foundType == false)
			{
				//Add no more columns than the peroidCount
				if(numericCounter < periodCounter && numericCounter < CONST_MAX_PERIOD_ROWS)
				{
					oCell = oRow.insertCell(-1);
					CSSFormatObject(oCell,'Numeric'); 
					if (globSheet.DataGrid[j][k].Text != '')
						oCell.innerHTML = globSheet.DataGrid[j][k].Text;
					else
						oCell.innerHTML = '&nbsp;';
					oCell.id = 'c' + j + '_' + k;
					foundType = true;
					if(InSemiColonSepString('SubTotal2',globSheet.DataGrid[j][k].Type) == true || InSemiColonSepString('Total',globSheet.DataGrid[j][k].Type) || InSemiColonSepString('SubTotal1',globSheet.DataGrid[j][k].Type))
					{
						oCell.style.fontWeight = 'bold';
					}
				}
				else
				{
					//oCell = oRow.insertCell();
					//CSSFormatObject(oCell,'Empty'); 
					//oCell.innerHTML = '&nbsp;';
				}
				numericCounter+=1;
				foundType = true;
			}

			//TOTAL
			if(InSemiColonSepString('Total',globSheet.DataGrid[j][k].Type) == true && foundType == false )
			{
				passedSubHeader = 0;
				if(totalCounter < periodCounter && totalCounter < CONST_MAX_PERIOD_ROWS)
				{
					oCell = oRow.insertCell(-1);
					oCell.id = 'c' + j + '_' + k;
					
					if(oCell.attachEvent){

					oCell.attachEvent('onmouseover',ColorRed);
					oCell.attachEvent('onmouseout',ColorBlue);

					oCell.attachEvent('onclick',DisplayGfx);
					}
					CSSFormatObject(oCell,'Total'); 
					if (globSheet.DataGrid[j][k].Text != '')
						oCell.innerHTML = globSheet.DataGrid[j][k].Text;
						//'<img align=center src="db_arrow.gif"> ' + 
					else
						oCell.innerHTML = '&nbsp;';
				}
				totalCounter+=1
				foundType = true;
			}
			
			//SUBTOTAL1
			if(InSemiColonSepString('SubTotal1',globSheet.DataGrid[j][k].Type) == true && foundType == false )
			{
				passedSubHeader = 0;
				if(subTotal1Counter < periodCounter && subTotal1Counter < CONST_MAX_PERIOD_ROWS)
				{
					oCell = oRow.insertCell(-1);
					oCell.id = 'c' + j + '_' + k;
					
					if(oCell.attachEvent){
					oCell.attachEvent('onmouseover',ColorRed);
					oCell.attachEvent('onmouseout',ColorBlue);
					oCell.attachEvent('onclick',DisplayGfx);
					}
					
					CSSFormatObject(oCell,'SubTotal1'); 
					if (globSheet.DataGrid[j][k].Text != '')
						oCell.innerHTML = globSheet.DataGrid[j][k].Text;
					else
						oCell.innerHTML = '&nbsp;';
				}
				subTotal1Counter = subTotal1Counter + 1
				foundType = true;
			}
			
			//SUBTOTAL2
			if(InSemiColonSepString('SubTotal2',globSheet.DataGrid[j][k].Type) == true && foundType == false )
			{
				passedSubHeader = 1;
				if(subTotal2Counter < periodCounter && subTotal2Counter < CONST_MAX_PERIOD_ROWS)
				{
					oCell = oRow.insertCell(-1);
					oCell.id = 'c' + j + '_' + k;
					
					if(oCell.attachEvent){
					oCell.attachEvent('onmouseover',ColorRed);
					oCell.attachEvent('onmouseout',ColorBlue);
					oCell.attachEvent('onclick',DisplayGfx);
					}

					CSSFormatObject(oCell,'SubTotal2'); 
					if (globSheet.DataGrid[j][k].Text != '')
						oCell.innerHTML = Spaces(passedSubHeader*5) + globSheet.DataGrid[j][k].Text;
					else
						oCell.innerHTML = '&nbsp;';

				}
				subTotal2Counter = subTotal2Counter + 1
				foundType = true;
			}
			//STANDARD
			if(InSemiColonSepString('Standard',globSheet.DataGrid[j][k].Type) == true && foundType == false)
			{
				if(standardCounter < periodCounter && standardCounter < CONST_MAX_PERIOD_ROWS+1)
				{	
					oCell = oRow.insertCell(-1);
					oCell.id = 'c' + j + '_' + k;
					tempId = 'c' + j + '_' + k;
										
					if(oCell.attachEvent){
					oCell.attachEvent('onmouseover',ColorRed);
					oCell.attachEvent('onmouseout',ColorBlue);
					oCell.style.textDecoration = 'underline';
					oCell.attachEvent('onclick',DisplayGfx);
					}
										
					CSSFormatObject(oCell,'Standard'); 
					if (globSheet.DataGrid[j][k].Text != ''){
						if(document.all) oCell.innerHTML = Spaces(passedSubHeader*5) + globSheet.DataGrid[j][k].Text;
						else oCell.innerHTML = '<a class="Link" href=javascript:DisplayGfx('+j+')>'+Spaces(passedSubHeader*5) + globSheet.DataGrid[j][k].Text+'</a>';
					}
					else
						oCell.innerHTML = '&nbsp;';
					
				}
				standardCounter+=1
				foundType = true;
			}		
			//STANDARD2
			if(InSemiColonSepString('Standard2',globSheet.DataGrid[j][k].Type) == true && foundType == false)
			{
				if(standard2Counter < periodCounter && standard2Counter < CONST_MAX_PERIOD_ROWS+1)
				{	
					oCell = oRow.insertCell(-1);
					oCell.id = 'c' + j + '_' + k;
					tempId = 'c' + j + '_' + k;
										
					if(oCell.attachEvent){
					oCell.attachEvent('onmouseover',ColorRed);
					oCell.attachEvent('onmouseout',ColorBlue);
					oCell.style.textDecoration = 'underline';
					oCell.attachEvent('onclick',DisplayGfx);
					}
										
					CSSFormatObject(oCell,'Standard2'); 
					if (globSheet.DataGrid[j][k].Text != ''){
						if(document.all) oCell.innerHTML = Spaces(passedSubHeader*5) + globSheet.DataGrid[j][k].Text;
						else oCell.innerHTML = '<a class="Link" href=javascript:DisplayGfx('+j+')>'+Spaces(passedSubHeader*5) + globSheet.DataGrid[j][k].Text+'</a>';
					}
					else
						oCell.innerHTML = '&nbsp;';
					
				}
				standard2Counter+=1
				foundType = true;
			}	
			//DEFAULT
			if(foundType == false)
			{
				//oCell = oRow.insertCell();
				//oCell.innerHTML = 'o';//globSheet.DataGrid[j][k].Text;
				if(notFoundCounter < periodCounter && notFoundCounter < CONST_MAX_PERIOD_ROWS)
				{
				oCell = oRow.insertCell(-1);
				oCell.innerHTML = '&nbsp;';
				}
				notFoundCounter+=1
				foundType = true;
			}
			
		}
		else
		{
			//Cells with no data
			/*
			if(notFoundCounter < periodCounter && notFoundCounter < CONST_MAX_PERIOD_ROWS)
			{
				oCell = oRow.insertCell(-1);
				oCell.innerHTML = 'o&nbsp;';
			}
			notFoundCounter+=1
			*/
		}
	}
	
	
	//Remove row - if empty
	
	var foundData = false;
	for(u=0; u < oRow.cells.length; u++)
	{	if(Trim(oRow.cells.item(u).innerHTML) != "&nbsp;")
		{
			foundData = true;
		}
	}
	if(foundData == false && !callOnce)
	{	var callOnce = "slut";
		oRow.style.backgroundColor = '#FFFFFF';
		//GetTable('mytab').InvertColor();
		GetTable('mytab').Reference.deleteRow(oRow.rowIndex);
	}
	
	//End remove row
	
	k=0;
	numericCounter=0;
	headerCounter = 0;
	subheaderCounter = 0;
	totalCounter = 0;
	subTotal1Counter = 0;
	subTotal2Counter = 0;
	standardCounter = 0;
	standard2Counter = 0;
	notFoundCounter = 0;
}
}
} //End - tableRenderOrNot

}


function AddPeriods(strPeriodString, objSelectBox, selIndex)
{
	var PeriodsArray = strPeriodString.split('[@ZEP-DEL@]');
	for(y=0; y < PeriodsArray.length; y++)
	{
		if(Trim(PeriodsArray[y]).length != 0)
		{
			var oOption = document.createElement("OPTION");
			
			if(objSelectBox.options && objSelectBox.options.add) objSelectBox.options.add(oOption);
			else objSelectBox.appendChild(oOption);

			oOption.innerHTML = Trim(PeriodsArray[y]);
			oOption.Value = y;
			if(y == (selIndex-1) || (PeriodsArray.length == y && PeriodsArray.length < (selIndex-1)) )
			{
				oOption.selected = true;
			}
		}
	}
}

//Changes between periods
//Assumes that Numeric colums start in [1]
//If this is altered, add [new numeric start index] to the DataGrid second array index
function ChangePeriod(lngPeriodBox)
{
	var objSelBox = document.getElementById('period_' + lngPeriodBox);
	for(t=0; t < objSelBox.options.length; t++)
	{
		if(objSelBox.options.item(t).selected == true)
		{
			//globSheet.Height
			for(s=0; s<globSheet.Height; s++)
			{ 
				try{
					if(document.getElementById('c' + s + '_' + (lngPeriodBox)) != null)
					{
						
						document.getElementById('c' + s + '_' + (lngPeriodBox)).innerHTML = globSheet.DataGrid[s][t+1].Text;
						
					}
				}
				catch(e){}
			}
		}
	}
}

//Event function on the Standard-cells
function ColorRed()
{
	//event.srcElement.style.color = '#AB2525';
	event.srcElement.style.cursor = 'hand';

}
function ColorBlue()
{
	//event.srcElement.style.color = '#003366';
	event.srcElement.style.cursor = 'auto';
}

//Function to return non-breaking spaces if line belongs to a SubHeader
function Spaces(varNumber)
{
	var SpaceString = '';
	for(i=0; i<varNumber; i++)
	{
		SpaceString = SpaceString + '&nbsp;';
	}
	return SpaceString;
}

//Popup the JScript graphics, based on parameters
//Assumes that Numeric colums start in [1]
//Assumes that Text resides in [0]
function DisplayGfx(ids)
{
	//Determine no of period boxes to scan
	var periodDropDowns = 0;
	if(periodCounter > CONST_MAX_PERIOD_ROWS)
	{
		periodDropDowns = CONST_MAX_PERIOD_ROWS;
	}
	else
	{
		periodDropDowns = periodCounter;	
	}
	
	//Determine clicked column from ID
	
	if(document.all && event.srcElement && event.srcElement.id){
	var columnIdArray = event.srcElement.id.split('_');
	var re;
	var rowNo;
	re = /c/g;
	rowNo = columnIdArray[0].replace(re,'');
	}
	else rowNo = ids;

	var paramString = '';
	paramString = document.getElementById('pageName').childNodes[0].nodeValue + '[@ZEP-DEL@]';
	paramString = paramString + TableRenderVersion + '[@ZEP-DEL@]';
	paramString = paramString + globSheet.DataGrid[rowNo][0].Text;

	var goodForDisplay = true;
	for(a=1; a <= periodDropDowns; a++)
	{
		objSelBox = document.getElementById('period_' + a)
		
		for(b=0; b < objSelBox.options.length; b++)
		{
			if(objSelBox.options.item(b).selected == true)
			{
				if (globSheet.DataGrid[rowNo][objSelBox.options.item(b).Value+1].Value == '')
					goodForDisplay = false;	
				//paramString = document.getElementById('pageName').innerHTML + '[@ZEP-DEL@]' + paramString + '[@ZEP-DEL@]' + objSelBox.options.item(b).innerText + '[@VAL_PAIR_ZEP@]' + globSheet.DataGrid[rowNo][objSelBox.options.item(b).Value+1].Text + '[@VAL_PAIR_ZEP@]' + globSheet.DataGrid[rowNo][objSelBox.options.item(b).Value+1].Value;
				paramString = paramString + '[@ZEP-DEL@]' + objSelBox.options.item(b).childNodes[0].nodeValue + '[@VAL_PAIR_ZEP@]' + globSheet.DataGrid[rowNo][objSelBox.options.item(b).Value+1].Text + '[@VAL_PAIR_ZEP@]' + globSheet.DataGrid[rowNo][objSelBox.options.item(b).Value+1].Value;

			}
		}
	}
	if(goodForDisplay){
		if(window.showModalDialog) window.showModalDialog('/documents/ir/graphRender.html?OpenDocument&Data=' + paramString,paramString,'dialogHeight: 395px; dialogWidth: 550px; dialogTop: 200px; dialogLeft: 200px; center: Yes; help: Yes; resizable: No; status: No; Scroll:No');
		else{
			var bredde = (screen.width - 550) / 2;
			var hoejde = (screen.height - 395) / 2;
			var escapedParamString = escape(paramString);
			escapedParamString = escapedParamString.replace('%E6','ae'); //æ
			escapedParamString = escapedParamString.replace('%F8','oe'); //ø
			escapedParamString = escapedParamString.replace('%E5','aa'); //å
									
			
			printWindow = window.open('/documents/ir/graphRender.html?OpenDocument&Data='+escapedParamString,'','toolbar=no,scrollbars=no,status=no,width=550,height=395,left='+bredde+',top='+hoejde);
		}
	}
	else
		alert('The selected row could not be rendered graphicly.');
}

//Initialize Period drop downs
for(i=1; i <= periodCounter; i++){
	if(i <= CONST_MAX_PERIOD_ROWS){
		if(document.getElementById('period_cell_' + i)){
		document.getElementById('period_cell_' + i).innerHTML = '<select id="period_' + i + '" class="PeriodDropDown" onchange="ChangePeriod(' + i + ')"></select>';
		AddPeriods(periodValues, document.getElementById('period_' + i), i);
		}
	}
}
